File: head.js

package info (click to toggle)
firefox 147.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,532 kB
  • sloc: cpp: 7,607,356; javascript: 6,533,348; ansic: 3,775,236; python: 1,415,508; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,760; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (224 lines) | stat: -rw-r--r-- 6,391 bytes parent folder | download | duplicates (3)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
ChromeUtils.defineESModuleGetters(this, {
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
});

const TRANSITION_LINK = PlacesUtils.history.TRANSITION_LINK;
const TRANSITION_TYPED = PlacesUtils.history.TRANSITION_TYPED;
const TRANSITION_BOOKMARK = PlacesUtils.history.TRANSITION_BOOKMARK;
const TRANSITION_REDIRECT_PERMANENT =
  PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT;
const TRANSITION_REDIRECT_TEMPORARY =
  PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY;
const TRANSITION_EMBED = PlacesUtils.history.TRANSITION_EMBED;
const TRANSITION_FRAMED_LINK = PlacesUtils.history.TRANSITION_FRAMED_LINK;
const TRANSITION_DOWNLOAD = PlacesUtils.history.TRANSITION_DOWNLOAD;

function whenNewWindowLoaded(aOptions, aCallback) {
  BrowserTestUtils.waitForNewWindow().then(aCallback);
  OpenBrowserWindow(aOptions);
}

async function clearHistoryAndHistoryCache() {
  await PlacesUtils.history.clear();
  // Clear HistoryRestiction cache as well.
  Cc["@mozilla.org/browser/history;1"]
    .getService(Ci.mozIAsyncHistory)
    .clearCache();
}

async function synthesizeVisitByUser(browser, url) {
  let onNewTab = BrowserTestUtils.waitForNewTab(browser.ownerGlobal.gBrowser);
  // We intentionally turn off this a11y check, because the following click is
  // purposefully sent on an arbitrary web content that is not expected to be
  // tested by itself with the browser mochitests, therefore this rule check
  // shall be ignored by a11y_checks suite.
  AccessibilityUtils.setEnv({ mustHaveAccessibleRule: false });
  await ContentTask.spawn(browser, [url], async ([href]) => {
    EventUtils.synthesizeMouseAtCenter(
      content.document.querySelector(`a[href='${href}'`),
      {},
      content
    );
  });
  AccessibilityUtils.resetEnv();
  let tab = await onNewTab;
  BrowserTestUtils.removeTab(tab);
}

async function synthesizeVisitByScript(browser, url) {
  let onNewTab = BrowserTestUtils.waitForNewTab(browser.ownerGlobal.gBrowser);
  AccessibilityUtils.setEnv({ mustHaveAccessibleRule: false });
  await ContentTask.spawn(browser, [url], async ([href]) => {
    let a = content.document.querySelector(`a[href='${href}'`);
    a.click();
  });
  AccessibilityUtils.resetEnv();
  let tab = await onNewTab;
  BrowserTestUtils.removeTab(tab);
}

async function assertLinkVisitedStatus(
  browser,
  url,
  { visitCount: expectedVisitCount, isVisited: expectedVisited }
) {
  await BrowserTestUtils.waitForCondition(async () => {
    let visitCount =
      (await PlacesTestUtils.getDatabaseValue("moz_places", "visit_count", {
        url,
      })) ?? 0;

    if (visitCount != expectedVisitCount) {
      return false;
    }

    Assert.equal(visitCount, expectedVisitCount, "The visit count is correct");
    return true;
  });

  await ContentTask.spawn(
    browser,
    [url, expectedVisited],
    async ([href, visited]) => {
      // ElementState::VISITED
      const VISITED_STATE = 1 << 18;
      await ContentTaskUtils.waitForCondition(() => {
        let isVisited = !!(
          content.InspectorUtils.getContentState(
            content.document.querySelector(`a[href='${href}']`)
          ) & VISITED_STATE
        );
        return isVisited == visited;
      });
    }
  );
  Assert.ok(true, "The visited state is corerct");
}

async function checkFrecencyEqual(url1, url2) {
  let frecency1 = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "frecency",
    {
      url: url1,
    }
  );
  let frecency2 = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "frecency",
    {
      url: url2,
    }
  );
  Assert.equal(
    frecency1,
    frecency2,
    `Frecency of ${url1} is equal to frecency of ${url2}.`
  );
}

async function checkFrecencyGreater(url1, url2) {
  let frecency1 = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "frecency",
    {
      url: url1,
    }
  );
  let frecency2 = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "frecency",
    {
      url: url2,
    }
  );
  Assert.greater(
    frecency1,
    frecency2,
    `Frecency of ${url1} is greater than frecency of ${url2}.`
  );
}

async function checkFrecencyNonZero(url) {
  Assert.greater(
    await PlacesTestUtils.getDatabaseValue("moz_places", "frecency", {
      url,
    }),
    0,
    `Frecency of ${url} is greater than 0.`
  );
}

async function checkUrlHidden(url, expectedHidden) {
  let hiddenState = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "hidden",
    {
      url,
    }
  );
  Assert.equal(
    hiddenState,
    expectedHidden,
    `URL ${url} is ${expectedHidden ? "hidden" : "not hidden."}`
  );
}

async function checkRedirect(
  redirectUrl,
  targetUrl,
  intermediateUrls = [],
  isTyped = false
) {
  // First, check redirect and intermediate urls are hidden and the target URL
  // is not hidden.
  await checkUrlHidden(redirectUrl, 1);
  for (let intermediateUrl of intermediateUrls) {
    await checkUrlHidden(intermediateUrl, 1);
  }
  await checkUrlHidden(targetUrl, 0);

  // Check the frecency values of redirect URLs are expected.
  await checkFrecencyNonZero(redirectUrl);
  for (let intermediateUrl of intermediateUrls) {
    if (isTyped) {
      await checkFrecencyGreater(redirectUrl, intermediateUrl);
    } else {
      await checkFrecencyEqual(redirectUrl, intermediateUrl);
    }
  }

  // Check intermediate urls have the same frecency.
  let intermediateUrlObj = [];
  for (let intermediateUrl of intermediateUrls) {
    let frecency = await PlacesTestUtils.getDatabaseValue(
      "moz_places",
      "frecency",
      {
        url: intermediateUrl,
      }
    );
    intermediateUrlObj.push({ url: intermediateUrl, frecency });
  }
  if (intermediateUrlObj.length) {
    let expected = intermediateUrlObj[0];
    for (let i = 1; i < intermediateUrlObj.length; ++i) {
      Assert.equal(
        intermediateUrlObj[i].frecency,
        expected.frecency,
        `Frecency of intermediate url ${intermediateUrlObj[i].url} should match ${expected.url}`
      );
    }
  }

  // Check the target URL has a higher frecency than the intermediate URLs.
  for (let intermediateUrl of intermediateUrls) {
    await checkFrecencyGreater(targetUrl, intermediateUrl);
  }

  if (isTyped && intermediateUrls.length) {
    await checkFrecencyGreater(redirectUrl, targetUrl);
  } else {
    await checkFrecencyGreater(targetUrl, redirectUrl);
  }
}