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

/* eslint-disable @microsoft/sdl/no-insecure-url */

"use strict";

// This test checks that when a upgrade is happening through HTTPS-Only,
// only a history entry for the https url of the site is added visibly for
// the user, while the http version gets added as a hidden entry.

async function assertIsPlaceHidden(url, expectHidden) {
  const hidden = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "hidden",
    { url }
  );
  Assert.ok(hidden !== undefined, `We should have saved a visit to ${url}`);
  Assert.equal(
    hidden,
    expectHidden ? 1 : 0,
    `Check if the visit to ${url} is hidden`
  );
}

async function assertVisitFromAndType(
  url,
  expectedFromVisitURL,
  expectedVisitType
) {
  const db = await PlacesUtils.promiseDBConnection();
  const rows = await db.execute(
    `SELECT v1.visit_type FROM moz_historyvisits v1
     JOIN moz_places p1 ON p1.id = v1.place_id
     WHERE p1.url = :url
     AND from_visit IN
       (SELECT v2.id FROM moz_historyvisits v2
        JOIN moz_places p2 ON p2.id = v2.place_id
        WHERE p2.url = :expectedFromVisitURL)
    `,
    { url, expectedFromVisitURL }
  );
  Assert.equal(
    rows.length,
    1,
    `There should be a single visit to ${url} with "from_visit" set to the visit id of ${expectedFromVisitURL}`
  );
  Assert.equal(
    rows[0].getResultByName("visit_type"),
    expectedVisitType,
    `The visit to ${url} should have a visit type of ${expectedVisitType}`
  );
}

function waitForVisitNotifications(urls) {
  return Promise.all(
    urls.map(url =>
      PlacesTestUtils.waitForNotification("page-visited", events =>
        events.some(e => e.url === url)
      )
    )
  );
}

add_task(async function test_upgrade() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_only_mode", true]],
  });

  await PlacesUtils.history.clear();

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
  });

  const visitPromise = waitForVisitNotifications([
    "http://example.com/",
    "https://example.com/",
  ]);

  info("Opening http://example.com/");
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "http://example.com/",
      waitForLoad: true,
    },
    async browser => {
      Assert.equal(
        browser.currentURI.scheme,
        "https",
        "We should have been upgraded"
      );
      info("Waiting for page visits to reach places database");
      await visitPromise;
      info("Checking places database");
      await assertIsPlaceHidden("http://example.com/", true);
      await assertIsPlaceHidden("https://example.com/", false);
      await assertVisitFromAndType(
        "https://example.com/",
        "http://example.com/",
        Ci.nsINavHistoryService.TRANSITION_REDIRECT_PERMANENT
      );
    }
  );
});