fune/browser/base/content/test/general/browser_lastAccessedTab.js
Tom Ritter aa82f54ab6 Bug 1435296 Address test failures caused by bumping timer precision to 2 ms r=baku
There are a few different reasons why tests needed updating (not an exhaustive list):

- Tests assume that successive operations take place at different times.
- Tests assume that an operation took a minimum amount of time.
- Tests hardcodes a specific delay.

In most cases we hardcode the preference off. In some cases this is the best approach,
in others, we would like to improve. The bug for tracking those improvements is Bug 1429648

An improvement that is present in some tests is to hardcode a specific precision reduction
that is acceptable based on the confides of the test. (Obviously this needs to be a fix for
the test framework and not a requirement on the feature being tested.)

In a few places, the test itself can be fixed, for example to no longer require the end
time of an operation to be strictly greater than the start time, and allows it to be equal
to it.

MozReview-Commit-ID: J59c7xQtZZJ

--HG--
extra : rebase_source : df8a03e76eaf9cdc9524dbb3eb9035af237e534b
2018-02-12 11:39:41 -06:00

53 lines
1.7 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint-disable mozilla/no-arbitrary-setTimeout */
// gBrowser.selectedTab.lastAccessed and Date.now() called from this test can't
// run concurrently, and therefore don't always match exactly.
const CURRENT_TIME_TOLERANCE_MS = 15;
function isCurrent(tab, msg) {
const DIFF = Math.abs(Date.now() - tab.lastAccessed);
ok(DIFF <= CURRENT_TIME_TOLERANCE_MS, msg + " (difference: " + DIFF + ")");
}
function nextStep(fn) {
setTimeout(fn, CURRENT_TIME_TOLERANCE_MS + 10);
}
var originalTab;
var newTab;
function test() {
waitForExplicitFinish();
// This test assumes that time passes between operations. But if the precision
// is low enough, and the test fast enough, an operation, and a successive call
// to Date.now() will have the same time value.
SpecialPowers.pushPrefEnv({"set": [["privacy.reduceTimerPrecision", false]]},
function() {
originalTab = gBrowser.selectedTab;
nextStep(step2);
});
}
function step2() {
isCurrent(originalTab, "selected tab has the current timestamp");
newTab = BrowserTestUtils.addTab(gBrowser, "about:blank", {skipAnimation: true});
nextStep(step3);
}
function step3() {
ok(newTab.lastAccessed < Date.now(), "new tab hasn't been selected so far");
gBrowser.selectedTab = newTab;
isCurrent(newTab, "new tab has the current timestamp after being selected");
nextStep(step4);
}
function step4() {
ok(originalTab.lastAccessed < Date.now(),
"original tab has old timestamp after being deselected");
isCurrent(newTab, "new tab has the current timestamp since it's still selected");
gBrowser.removeTab(newTab);
finish();
}