summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/head.js
blob: da40a832c630690ee11b628713962d1e3af8df94 (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
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");
}