summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_redirect.js
blob: 912b817ad1946d7c51bc1f0ff1f187cf3e9eb32b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const ROOT_URI =
  "http://mochi.test:8888/tests/toolkit/components/places/tests/browser/";
const REDIRECT_URI = Services.io.newURI(ROOT_URI + "redirect.sjs");
const TARGET_URI = Services.io.newURI(ROOT_URI + "redirect-target.html");

const REDIRECT_SOURCE_VISIT_BONUS = Services.prefs.getIntPref(
  "places.frecency.redirectSourceVisitBonus"
);
const LINK_VISIT_BONUS = Services.prefs.getIntPref(
  "places.frecency.linkVisitBonus"
);
const TYPED_VISIT_BONUS = Services.prefs.getIntPref(
  "places.frecency.typedVisitBonus"
);

// Ensure that decay frecency doesn't kick in during tests (as a result
// of idle-daily).
Services.prefs.setCharPref("places.frecency.decayRate", "1.0");

registerCleanupFunction(async function () {
  Services.prefs.clearUserPref("places.frecency.decayRate");
  await PlacesUtils.history.clear();
});

let redirectSourceFrecency = 0;
let redirectTargetFrecency = 0;

async function check_uri(uri, frecency, hidden) {
  is(
    await PlacesTestUtils.getDatabaseValue("moz_places", "frecency", {
      url: uri,
    }),
    frecency,
    "Frecency of the page is the expected one"
  );
  is(
    await PlacesTestUtils.getDatabaseValue("moz_places", "hidden", {
      url: uri,
    }),
    hidden,
    "Hidden value of the page is the expected one"
  );
}

add_task(async function redirect_check_new_typed_visit() {
  // Used to verify the redirect bonus overrides the typed bonus.
  PlacesUtils.history.markPageAsTyped(REDIRECT_URI);

  redirectSourceFrecency += REDIRECT_SOURCE_VISIT_BONUS;
  redirectTargetFrecency += TYPED_VISIT_BONUS;
  let redirectNotified = false;

  let visitedPromise = PlacesTestUtils.waitForNotification(
    "page-visited",
    visits => {
      is(visits.length, 1, "Was notified for the right number of visits.");
      let { url } = visits[0];
      info("Received 'page-visited': " + url);
      if (url == REDIRECT_URI.spec) {
        redirectNotified = true;
      }
      return url == TARGET_URI.spec;
    }
  );

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    REDIRECT_URI.spec
  );
  info("Waiting for onVisits");
  await visitedPromise;
  ok(redirectNotified, "The redirect should have been notified");

  await check_uri(REDIRECT_URI, redirectSourceFrecency, 1);
  await check_uri(TARGET_URI, redirectTargetFrecency, 0);

  BrowserTestUtils.removeTab(tab);
});

add_task(async function redirect_check_second_typed_visit() {
  // A second visit with a typed url.
  PlacesUtils.history.markPageAsTyped(REDIRECT_URI);

  redirectSourceFrecency += REDIRECT_SOURCE_VISIT_BONUS;
  redirectTargetFrecency += TYPED_VISIT_BONUS;
  let redirectNotified = false;

  let visitedPromise = PlacesTestUtils.waitForNotification(
    "page-visited",
    visits => {
      is(visits.length, 1, "Was notified for the right number of visits.");
      let { url } = visits[0];
      info("Received 'page-visited': " + url);
      if (url == REDIRECT_URI.spec) {
        redirectNotified = true;
      }
      return url == TARGET_URI.spec;
    }
  );

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    REDIRECT_URI.spec
  );
  info("Waiting for onVisits");
  await visitedPromise;
  ok(redirectNotified, "The redirect should have been notified");

  await check_uri(REDIRECT_URI, redirectSourceFrecency, 1);
  await check_uri(TARGET_URI, redirectTargetFrecency, 0);

  BrowserTestUtils.removeTab(tab);
});

add_task(async function redirect_check_subsequent_link_visit() {
  // Another visit, but this time as a visited url.
  redirectSourceFrecency += REDIRECT_SOURCE_VISIT_BONUS;
  redirectTargetFrecency += LINK_VISIT_BONUS;
  let redirectNotified = false;

  let visitedPromise = PlacesTestUtils.waitForNotification(
    "page-visited",
    visits => {
      is(visits.length, 1, "Was notified for the right number of visits.");
      let { url } = visits[0];
      info("Received 'page-visited': " + url);
      if (url == REDIRECT_URI.spec) {
        redirectNotified = true;
      }
      return url == TARGET_URI.spec;
    }
  );

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    REDIRECT_URI.spec
  );
  info("Waiting for onVisits");
  await visitedPromise;
  ok(redirectNotified, "The redirect should have been notified");

  await check_uri(REDIRECT_URI, redirectSourceFrecency, 1);
  await check_uri(TARGET_URI, redirectTargetFrecency, 0);

  BrowserTestUtils.removeTab(tab);
});