summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser-tips/suppress-tips/browser_suppressTips.js
blob: 44fa912356d72c857a42061744e8a7c7f14b180b (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that the browser tips are suppressed correctly.

"use strict";

/* import-globals-from ../head.js */

ChromeUtils.defineESModuleGetters(this, {
  LaterRun: "resource:///modules/LaterRun.sys.mjs",
  UrlbarProviderSearchTips:
    "resource:///modules/UrlbarProviderSearchTips.sys.mjs",
});

const LAST_UPDATE_THRESHOLD_HOURS = 24;

add_setup(async function () {
  await PlacesUtils.history.clear();

  await SpecialPowers.pushPrefEnv({
    set: [
      [
        `browser.urlbar.tipShownCount.${UrlbarProviderSearchTips.TIP_TYPE.ONBOARD}`,
        0,
      ],
    ],
  });

  registerCleanupFunction(() => {
    resetSearchTipsProvider();
    Services.prefs.clearUserPref(
      "browser.laterrun.bookkeeping.profileCreationTime"
    );
    Services.prefs.clearUserPref(
      "browser.laterrun.bookkeeping.updateAppliedTime"
    );
  });
});

add_task(async function updateApplied() {
  // Check the update time.
  Assert.notEqual(
    Services.prefs.getIntPref(
      "browser.laterrun.bookkeeping.updateAppliedTime",
      0
    ),
    0,
    "updateAppliedTime pref should be updated when booting this test"
  );
  Assert.equal(
    LaterRun.hoursSinceUpdate,
    0,
    "LaterRun.hoursSinceUpdate is 0 since one hour should not have passed from starting this test"
  );

  // To not suppress the tip by profile creation.
  Services.prefs.setIntPref(
    "browser.laterrun.bookkeeping.profileCreationTime",
    secondsBasedOnNow(LAST_UPDATE_THRESHOLD_HOURS + 0.5)
  );

  // The test harness will use the current tab and remove the tab's history.
  // Since the page that is tested is opened prior to the test harness taking
  // over the current tab the active-update.xml specifies two pages to open by
  // having 'https://example.com/|https://example.com/' for the value of openURL
  // and then uses the first tab for the test.
  gBrowser.selectedTab = gBrowser.tabs[0];
  // The test harness also changes the page to about:blank so go back to the
  // page that was originally opened.
  gBrowser.goBack();
  // Wait for the page to go back to the original page.
  await TestUtils.waitForCondition(
    () => gBrowser.selectedBrowser?.currentURI?.spec == "https://example.com/",
    "Waiting for the expected page to reopen"
  );
  gBrowser.removeTab(gBrowser.selectedTab);

  // Check whether the tip is suppressed by update.
  await checkTab(window, "about:newtab");

  // Clean up.
  const alternatePath = Services.prefs.getCharPref(
    "app.update.altUpdateDirPath"
  );
  const testRoot = Services.prefs.getCharPref("mochitest.testRoot");
  let relativePath = alternatePath.substring("<test-root>".length);
  if (AppConstants.platform == "win") {
    relativePath = relativePath.replace(/\//g, "\\");
  }
  const updateDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  updateDir.initWithPath(testRoot + relativePath);
  const updatesFile = updateDir.clone();
  updatesFile.append("updates.xml");
  await TestUtils.waitForCondition(
    () => updatesFile.exists(),
    "Waiting until the updates.xml file exists"
  );
  updatesFile.remove(false);
});

add_task(async function profileAge() {
  // To not suppress the tip by profile creation and update.
  Services.prefs.setIntPref(
    "browser.laterrun.bookkeeping.profileCreationTime",
    secondsBasedOnNow(LAST_UPDATE_THRESHOLD_HOURS + 0.5)
  );
  Services.prefs.setIntPref(
    "browser.laterrun.bookkeeping.updateAppliedTime",
    secondsBasedOnNow(LAST_UPDATE_THRESHOLD_HOURS + 0.5)
  );
  await checkTab(
    window,
    "about:newtab",
    UrlbarProviderSearchTips.TIP_TYPE.ONBOARD
  );

  // To suppress the tip by profile creation.
  Services.prefs.setIntPref(
    "browser.laterrun.bookkeeping.profileCreationTime",
    secondsBasedOnNow()
  );
  await checkTab(window, "about:newtab");
});

function secondsBasedOnNow(howManyHoursAgo = 0) {
  return Math.floor(Date.now() / 1000 - howManyHoursAgo * 60 * 60);
}