summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/history/test_fetch.js
blob: 899e45940363666b76b9849c04f254d5276f0b6e (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

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

  // Populate places and historyvisits.
  let uriString = `http://mozilla.com/test_browserhistory/test_fetch`;
  let uri = NetUtil.newURI(uriString);
  let title = `Test Visit ${Math.random()}`;
  let dates = [];
  let visits = [];
  let transitions = [
    PlacesUtils.history.TRANSITION_LINK,
    PlacesUtils.history.TRANSITION_TYPED,
    PlacesUtils.history.TRANSITION_BOOKMARK,
    PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY,
    PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT,
    PlacesUtils.history.TRANSITION_DOWNLOAD,
    PlacesUtils.history.TRANSITION_FRAMED_LINK,
    PlacesUtils.history.TRANSITION_RELOAD,
  ];
  let guid = "";
  for (let i = 0; i != transitions.length; i++) {
    dates.push(new Date(Date.now() - i * 10000000));
    visits.push({
      uri,
      title,
      transition: transitions[i],
      visitDate: dates[i],
    });
  }
  await PlacesTestUtils.addVisits(visits);
  Assert.ok(await PlacesTestUtils.isPageInDB(uri));
  Assert.equal(await PlacesTestUtils.visitsInDB(uri), visits.length);

  // Store guid for further use in testing.
  guid = await PlacesTestUtils.getDatabaseValue("moz_places", "guid", {
    url: uri,
  });
  Assert.ok(guid, guid);

  // Initialize the objects to compare against.
  let idealPageInfo = {
    url: new URL(uriString),
    guid,
    title,
  };
  let idealVisits = visits.map(v => {
    return {
      date: v.visitDate,
      transition: v.transition,
    };
  });

  // We should check these 4 cases:
  // 1, 2: visits not included, by URL and guid (same result expected).
  // 3, 4: visits included, by URL and guid (same result expected).
  for (let includeVisits of [true, false]) {
    for (let guidOrURL of [uri, guid]) {
      let pageInfo = await PlacesUtils.history.fetch(guidOrURL, {
        includeVisits,
      });
      if (includeVisits) {
        idealPageInfo.visits = idealVisits;
      } else {
        // We need to explicitly delete this property since deepEqual looks at
        // the list of properties as well (`visits in pageInfo` is true here).
        delete idealPageInfo.visits;
      }

      // Since idealPageInfo doesn't contain a frecency, check it and delete.
      Assert.ok(typeof pageInfo.frecency === "number");
      delete pageInfo.frecency;

      // Visits should be from newer to older.
      if (includeVisits) {
        for (let i = 0; i !== pageInfo.visits.length - 1; i++) {
          Assert.lessOrEqual(
            pageInfo.visits[i + 1].date.getTime(),
            pageInfo.visits[i].date.getTime()
          );
        }
      }
      Assert.deepEqual(idealPageInfo, pageInfo);
    }
  }
});

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

  let TEST_URI = NetUtil.newURI("http://mozilla.com/test_fetch_page_meta_info");
  await PlacesTestUtils.addVisits(TEST_URI);
  Assert.ok(page_in_database(TEST_URI));

  // Test fetching the null values
  let includeMeta = true;
  let pageInfo = await PlacesUtils.history.fetch(TEST_URI, { includeMeta });
  Assert.strictEqual(
    null,
    pageInfo.previewImageURL,
    "fetch should return a null previewImageURL"
  );
  Assert.strictEqual(
    "",
    pageInfo.siteName,
    "fetch should return a null siteName"
  );
  Assert.equal(
    "",
    pageInfo.description,
    "fetch should return a empty string description"
  );

  // Now set the pageMetaInfo for this page
  let description = "Test description";
  let siteName = "Mozilla";
  let previewImageURL = "http://mozilla.com/test_preview_image.png";
  await PlacesUtils.history.update({
    url: TEST_URI,
    description,
    previewImageURL,
    siteName,
  });

  includeMeta = true;
  pageInfo = await PlacesUtils.history.fetch(TEST_URI, { includeMeta });
  Assert.equal(
    previewImageURL,
    pageInfo.previewImageURL.href,
    "fetch should return a previewImageURL"
  );
  Assert.equal(siteName, pageInfo.siteName, "fetch should return a siteName");
  Assert.equal(
    description,
    pageInfo.description,
    "fetch should return a description"
  );

  includeMeta = false;
  pageInfo = await PlacesUtils.history.fetch(TEST_URI, { includeMeta });
  Assert.ok(
    !("description" in pageInfo),
    "fetch should not return a description if includeMeta is false"
  );
  Assert.ok(
    !("siteName" in pageInfo),
    "fetch should not return a siteName if includeMeta is false"
  );
  Assert.ok(
    !("previewImageURL" in pageInfo),
    "fetch should not return a previewImageURL if includeMeta is false"
  );
});

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

  const TEST_URI = "http://mozilla.com/test_fetch_page_meta_info";
  await PlacesTestUtils.addVisits(TEST_URI);
  Assert.ok(page_in_database(TEST_URI));

  let includeAnnotations = true;
  let pageInfo = await PlacesUtils.history.fetch(TEST_URI, {
    includeAnnotations,
  });
  Assert.equal(
    pageInfo.annotations.size,
    0,
    "fetch should return an empty annotation map"
  );

  await PlacesUtils.history.update({
    url: TEST_URI,
    annotations: new Map([["test/annotation", "testContent"]]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URI, { includeAnnotations });
  Assert.equal(
    pageInfo.annotations.size,
    1,
    "fetch should have only one annotation"
  );

  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    "testContent",
    "fetch should return the expected annotation"
  );

  await PlacesUtils.history.update({
    url: TEST_URI,
    annotations: new Map([["test/annotation2", 123]]),
  });

  pageInfo = await PlacesUtils.history.fetch(TEST_URI, { includeAnnotations });
  Assert.equal(
    pageInfo.annotations.size,
    2,
    "fetch should have returned two annotations"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation"),
    "testContent",
    "fetch should still have the first annotation"
  );
  Assert.equal(
    pageInfo.annotations.get("test/annotation2"),
    123,
    "fetch should have the second annotation"
  );

  includeAnnotations = false;
  pageInfo = await PlacesUtils.history.fetch(TEST_URI, { includeAnnotations });
  Assert.ok(
    !("annotations" in pageInfo),
    "fetch should not return annotations if includeAnnotations is false"
  );
});

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

  let uri = NetUtil.newURI("http://doesntexist.in.db");
  let pageInfo = await PlacesUtils.history.fetch(uri);
  Assert.equal(pageInfo, null);
});

add_task(async function test_error_cases() {
  Assert.throws(
    () => PlacesUtils.history.fetch("3"),
    /TypeError: URL constructor: 3 is not a valid /
  );
  Assert.throws(
    () => PlacesUtils.history.fetch({ not: "a valid string or guid" }),
    /TypeError: Invalid url or guid/
  );
  Assert.throws(
    () => PlacesUtils.history.fetch("http://valid.uri.com", "not an object"),
    /TypeError: options should be/
  );
  Assert.throws(
    () => PlacesUtils.history.fetch("http://valid.uri.com", null),
    /TypeError: options should be/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.fetch("http://valid.uri.come", {
        includeVisits: "not a boolean",
      }),
    /TypeError: includeVisits should be a/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.fetch("http://valid.uri.come", {
        includeMeta: "not a boolean",
      }),
    /TypeError: includeMeta should be a/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.fetch("http://valid.url.com", {
        includeAnnotations: "not a boolean",
      }),
    /TypeError: includeAnnotations should be a/
  );
});