summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_double_redirect.js
blob: d5b2fca1feeb94dad1bf7c05a373edeaa7f08056 (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
// Test for bug 411966.
// When a page redirects multiple times, from_visit should point to the
// previous visit in the chain, not to the first visit in the chain.

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

  const BASE_URL =
    "http://example.com/tests/toolkit/components/places/tests/browser/";
  const TEST_URI = NetUtil.newURI(BASE_URL + "begin.html");
  const FIRST_REDIRECTING_URI = NetUtil.newURI(BASE_URL + "redirect_twice.sjs");
  const FINAL_URI = NetUtil.newURI(
    "http://test1.example.com/tests/toolkit/components/places/tests/browser/final.html"
  );

  let promiseVisits = new Promise(resolve => {
    let observer = {
      _notified: [],
      onVisit(uri) {
        info("Received onVisit: " + uri);
        this._notified.push(uri);

        if (uri != FINAL_URI.spec) {
          return;
        }

        is(this._notified.length, 4);
        PlacesObservers.removeListener(["page-visited"], this.handleEvents);

        (async function () {
          // Get all pages visited from the original typed one
          let db = await PlacesUtils.promiseDBConnection();
          let rows = await db.execute(
            `SELECT url FROM moz_historyvisits
             JOIN moz_places h ON h.id = place_id
             WHERE from_visit IN
                (SELECT v.id FROM moz_historyvisits v
                 JOIN moz_places p ON p.id = v.place_id
                 WHERE p.url_hash = hash(:url) AND p.url = :url)
            `,
            { url: TEST_URI.spec }
          );

          is(rows.length, 1, "Found right number of visits");
          let visitedUrl = rows[0].getResultByName("url");
          // Check that redirect from_visit is not from the original typed one
          is(
            visitedUrl,
            FIRST_REDIRECTING_URI.spec,
            "Check referrer for " + visitedUrl
          );

          resolve();
        })();
      },
      handleEvents(events) {
        is(events.length, 1, "Right number of visits notified");
        is(events[0].type, "page-visited");
        let { url, visitId, visitTime, referringVisitId, transitionType } =
          events[0];
        this.onVisit(url, visitId, visitTime, referringVisitId, transitionType);
      },
    };
    observer.handleEvents = observer.handleEvents.bind(observer);
    PlacesObservers.addListener(["page-visited"], observer.handleEvents);
  });

  PlacesUtils.history.markPageAsTyped(TEST_URI);
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_URI.spec,
    },
    async function (browser) {
      // Load begin page, click link on page to record visits.
      await BrowserTestUtils.synthesizeMouseAtCenter("#clickme", {}, browser);

      await promiseVisits;
    }
  );

  await PlacesUtils.history.clear();
});