summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_visituri.js
blob: 6aafe084e76c7d8fc9fd8776fde0ad19304d5c51 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
  TestUtils: "resource://testing-common/TestUtils.sys.mjs",
});

add_task(async function test_basic() {
  // Make sure places visit chains are saved correctly with a redirect
  // transitions.

  // Part 1: observe history events that fire when a visit occurs.
  // Make sure visits appear in order, and that the visit chain is correct.
  const expectedUrls = [
    "http://example.com/tests/toolkit/components/places/tests/browser/begin.html",
    "http://example.com/tests/toolkit/components/places/tests/browser/redirect_twice.sjs",
    "http://example.com/tests/toolkit/components/places/tests/browser/redirect_once.sjs",
    "http://test1.example.com/tests/toolkit/components/places/tests/browser/final.html",
  ];

  let currentIndex = 0;
  let visitUriPromise = lazy.TestUtils.topicObserved(
    "uri-visit-saved",
    async subject => {
      let uri = subject.QueryInterface(Ci.nsIURI);
      let expected = expectedUrls[currentIndex];
      is(uri.spec, expected, "Saved URL visit " + uri.spec);

      let placeId = await lazy.PlacesTestUtils.getDatabaseValue(
        "moz_places",
        "id",
        {
          url: uri.spec,
        }
      );
      let fromVisitId = await lazy.PlacesTestUtils.getDatabaseValue(
        "moz_historyvisits",
        "from_visit",
        {
          place_id: placeId,
        }
      );

      if (currentIndex == 0) {
        is(fromVisitId, 0, "First visit has no from visit");
      } else {
        let lastVisitId = await lazy.PlacesTestUtils.getDatabaseValue(
          "moz_historyvisits",
          "place_id",
          {
            id: fromVisitId,
          }
        );
        let fromVisitUrl = await lazy.PlacesTestUtils.getDatabaseValue(
          "moz_places",
          "url",
          {
            id: lastVisitId,
          }
        );
        is(
          fromVisitUrl,
          expectedUrls[currentIndex - 1],
          "From visit was " + expectedUrls[currentIndex - 1]
        );
      }

      currentIndex++;
      return currentIndex >= expectedUrls.length;
    }
  );

  const testUrl =
    "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, testUrl);

  // Load begin page, click link on page to record visits.
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#clickme",
    {},
    gBrowser.selectedBrowser
  );
  await visitUriPromise;

  // Clean up.
  await PlacesUtils.history.clear();
  gBrowser.removeCurrentTab();
});

add_task(async function test_userpass() {
  // Avoid showing the auth prompt.
  await SpecialPowers.pushPrefEnv({
    set: [["network.auth.confirmAuth.enabled", false]],
  });

  // Open a html having test links.
  await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.org/tests/toolkit/components/places/tests/browser/userpass.html"
  );

  const clickedUrl =
    "https://user:pass@example.org/tests/toolkit/components/places/tests/browser/favicon.html";
  const exposablePageUrl =
    "https://example.org/tests/toolkit/components/places/tests/browser/favicon.html";

  let visitUriPromise = lazy.TestUtils.topicObserved(
    "uri-visit-saved",
    async subject => {
      let uri = subject.QueryInterface(Ci.nsIURI);
      if (uri.spec !== exposablePageUrl) {
        return false;
      }

      let placeForExposable = await lazy.PlacesTestUtils.getDatabaseValue(
        "moz_places",
        "id",
        {
          url: exposablePageUrl,
        }
      );
      Assert.ok(placeForExposable, "Found the place for exposable URL");

      let placeForOriginal = await lazy.PlacesTestUtils.getDatabaseValue(
        "moz_places",
        "id",
        {
          url: clickedUrl,
        }
      );
      Assert.ok(!placeForOriginal, "Not found the place for the original URL");

      return true;
    }
  );

  // Open the target link as background.
  await ContentTask.spawn(gBrowser.selectedBrowser, null, async () => {
    let link = content.document.getElementById("target-userpass");
    EventUtils.synthesizeMouseAtCenter(
      link,
      {
        ctrlKey: true,
        metaKey: true,
      },
      content
    );
    return link.href;
  });

  // Wait for fireing visited event.
  await visitUriPromise;

  // Check the title.
  await BrowserTestUtils.waitForCondition(async () => {
    let titleForExposable = await lazy.PlacesTestUtils.getDatabaseValue(
      "moz_places",
      "title",
      {
        url: exposablePageUrl,
      }
    );
    return titleForExposable == "favicon page";
  }, "Wait for the proper title is updated");

  // Check the link status.
  const expectedResults = {
    "target-userpass": true,
    "no-userpass": true,
    "another-userpass": false,
    "another-url": false,
  };

  for (const [key, value] of Object.entries(expectedResults)) {
    await ContentTask.spawn(
      gBrowser.selectedBrowser,
      [key, value],
      async ([k, v]) => {
        // ElementState::VISITED
        const VISITED_STATE = 1 << 18;
        await ContentTaskUtils.waitForCondition(() => {
          const isVisited = !!(
            content.InspectorUtils.getContentState(
              content.document.getElementById(k)
            ) & VISITED_STATE
          );
          return isVisited == v;
        });
      }
    );
    Assert.ok(true, `The status of ${key} is correct`);
  }

  // Clean up.
  await PlacesUtils.history.clear();
  // Remove the tab for userpass.html
  gBrowser.removeCurrentTab();
  // Remove the tab for favicon.html
  gBrowser.removeCurrentTab();
});