tenfourfox/mobile/android/tests/browser/chrome/test_get_last_visited.html

107 lines
4.0 KiB
HTML
Raw Normal View History

2017-04-19 07:56:45 +00:00
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1214366
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1214366</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="head.js"></script>
<script type="application/javascript;version=1.7">
"use strict";
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Messaging.jsm");
Cu.import("resource://gre/modules/Task.jsm");
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
let BrowserApp = chromeWin.BrowserApp;
function get_last_visited(prePath) {
return Messaging.sendRequestForResult({
type: "History:GetPrePathLastVisitedTimeMilliseconds",
prePath: prePath,
});
};
var browser = BrowserApp.addTab("about:blank").browser;
// It's useful to see *all* "link-visited" events in the face of intermittent failures.
let observe = function(subject, topic, data) {
var uri = subject.QueryInterface(Ci.nsIURI);
info("Witnessed " + topic + " notification from Gecko with URI " + uri.spec);
}
Services.obs.addObserver(observe, "link-visited", false);
SimpleTest.registerCleanupFunction(function cleanup() {
BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
Services.obs.removeObserver(observe, "link-visited");
});
// N.b.: the write to the Fennec DB happens before the Gecko notification
// is fired. This is delicate.
function add_history_visit(url) {
browser.loadURI(url, null, null);
return promiseLinkVisit(url);
};
// Be aware that some paths under mochi.test and example.org redirect. The
// blank robocop pages appear to not. Redirects can impact this test, since
// they can write to the history database.
// The apparent mis-ordering here just uses simpler pages (01 and 03) for the
// real test, and a more complex page (02) for a final delay. See comment below.
const url1 = "http://example.org/tests/robocop/robocop_blank_01.html";
const url2 = "http://example.org/tests/robocop/robocop_blank_03.html";
const url3 = "http://example.org/tests/robocop/robocop_blank_02.html";
add_task(function* test_get_last_visited() {
var v = yield get_last_visited("https://random.com/");
is(v, 0, `Last visited timestamp is 0 for unknown prePath: ${v}`);
let prePath = Services.io.newURI(url1, null, null).prePath + "/";
is(prePath, Services.io.newURI(url2, null, null).prePath + "/", "url1 and url2 have the same prePath");
let t0 = Date.now();
yield add_history_visit(url1);
v = yield get_last_visited(prePath);
let t1 = Date.now();
ok(t0 <= v, `Last visited timestamp is after visit: ${t0} <= ${v}.`);
ok(v <= t1, `Last visited timestamp is before present ${v} <= ${t1}.`);
let t2 = Date.now();
yield add_history_visit(url1);
v = yield get_last_visited(prePath);
ok(t2 <= v, `Last visited timestamp is updated after visit: ${t2} <= ${v}`);
let t3 = Date.now();
yield add_history_visit(url2);
v = yield get_last_visited(prePath);
ok(t3 <= v, `Last visited timestamp is updated after visit to URL with same prePath: ${t3} <= ${v}`);
// This whole system is flaky, so we wait for an unrelated visit, so that we
// can witness "link-visited" events a little after the test completes
// while debugging.
yield add_history_visit(url3);
});
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1214366">Mozilla Bug 1214366</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</html>