summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_isURIVisited.js
blob: cf3a8c139fd5c0c4ea869b3463509a72d86f369c (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests functionality of the isURIVisited API.

const SCHEMES = {
  "http://": true,
  "https://": true,
  "ftp://": true,
  "file:///": true,
  "about:": false,
  // nsIIOService.newURI() can throw if e.g. the app knows about imap://
  // but the account is not set up and so the URL is invalid for it.
  //  "imap://": false,
  "news://": false,
  "mailbox:": false,
  "moz-anno:favicon:http://": false,
  "view-source:http://": false,
  "chrome://browser/content/browser.xhtml?": false,
  "resource://": false,
  "data:,": false,
  "javascript:": false,
};

add_task(async function test_isURIVisited() {
  let history = Cc["@mozilla.org/browser/history;1"].getService(
    Ci.mozIAsyncHistory
  );

  function visitsPromise(uri) {
    return new Promise(resolve => {
      history.isURIVisited(uri, (receivedURI, visited) => {
        resolve([receivedURI, visited]);
      });
    });
  }

  for (let scheme in SCHEMES) {
    info("Testing scheme " + scheme);
    for (let t in PlacesUtils.history.TRANSITIONS) {
      if (t == "EMBED") {
        continue;
      }
      info("With transition " + t);
      let aTransition = PlacesUtils.history.TRANSITIONS[t];

      let aURI = Services.io.newURI(scheme + "mozilla.org/");

      let [receivedURI1, visited1] = await visitsPromise(aURI);
      Assert.ok(aURI.equals(receivedURI1));
      Assert.ok(!visited1);

      if (PlacesUtils.history.canAddURI(aURI)) {
        await PlacesTestUtils.addVisits([
          {
            uri: aURI,
            transition: aTransition,
          },
        ]);
        info("Added visit for " + aURI.spec);
      }

      let [receivedURI2, visited2] = await visitsPromise(aURI);
      Assert.ok(aURI.equals(receivedURI2));
      Assert.equal(SCHEMES[scheme], visited2);

      await PlacesUtils.history.clear();
      let [receivedURI3, visited3] = await visitsPromise(aURI);
      Assert.ok(aURI.equals(receivedURI3));
      Assert.ok(!visited3);
    }
  }
});