summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_get_last_visited.html
blob: da8a0fbfbc5e0c896a35a47a54ec52b7fea4036e (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
<!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>