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

// Tests for `History.insert` as implemented in History.jsm

"use strict";

add_task(async function test_insert_error_cases() {
  const TEST_URL = "http://mozilla.com";

  Assert.throws(
    () => PlacesUtils.history.insert(),
    /Error: PageInfo: Input should be /,
    "passing a null into History.insert should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.insert(1),
    /Error: PageInfo: Input should be/,
    "passing a non object into History.insert should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.insert({}),
    /Error: PageInfo: The following properties were expected/,
    "passing an object without a url to History.insert should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.insert({ url: 123 }),
    /Error: PageInfo: Invalid value for property/,
    "passing an object with an invalid url to History.insert should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.insert({ url: TEST_URL }),
    /Error: PageInfo: The following properties were expected/,
    "passing an object without a visits property to History.insert should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.insert({ url: TEST_URL, visits: 1 }),
    /Error: PageInfo: Invalid value for property/,
    "passing an object with a non-array visits property to History.insert should throw an Error"
  );
  Assert.throws(
    () => PlacesUtils.history.insert({ url: TEST_URL, visits: [] }),
    /Error: PageInfo: Invalid value for property/,
    "passing an object with an empty array as the visits property to History.insert should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.insert({
        url: TEST_URL,
        visits: [
          {
            transition: TRANSITION_LINK,
            date: "a",
          },
        ],
      }),
    /PageInfo: Invalid value for property/,
    "passing a visit object with an invalid date to History.insert should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.insert({
        url: TEST_URL,
        visits: [
          {
            transition: TRANSITION_LINK,
          },
          {
            transition: TRANSITION_LINK,
            date: "a",
          },
        ],
      }),
    /PageInfo: Invalid value for property/,
    "passing a second visit object with an invalid date to History.insert should throw an Error"
  );
  let futureDate = new Date();
  futureDate.setDate(futureDate.getDate() + 1000);
  Assert.throws(
    () =>
      PlacesUtils.history.insert({
        url: TEST_URL,
        visits: [
          {
            transition: TRANSITION_LINK,
            date: futureDate,
          },
        ],
      }),
    /PageInfo: Invalid value for property/,
    "passing a visit object with a future date to History.insert should throw an Error"
  );
  Assert.throws(
    () =>
      PlacesUtils.history.insert({
        url: TEST_URL,
        visits: [{ transition: "a" }],
      }),
    /PageInfo: Invalid value for property/,
    "passing a visit object with an invalid transition to History.insert should throw an Error"
  );
});

add_task(async function test_history_insert() {
  const TEST_URL = "http://mozilla.com/";

  let inserter = async function (name, filter, referrer, date, transition) {
    info(name);
    info(
      `filter: ${filter}, referrer: ${referrer}, date: ${date}, transition: ${transition}`
    );

    let uri = NetUtil.newURI(TEST_URL + Math.random());
    let title = "Visit " + Math.random();

    let pageInfo = {
      title,
      visits: [{ transition, referrer, date }],
    };

    pageInfo.url = await filter(uri);

    let result = await PlacesUtils.history.insert(pageInfo);

    Assert.ok(
      PlacesUtils.isValidGuid(result.guid),
      "guid for pageInfo object is valid"
    );
    Assert.equal(
      uri.spec,
      result.url.href,
      "url is correct for pageInfo object"
    );
    Assert.equal(title, result.title, "title is correct for pageInfo object");
    Assert.equal(
      TRANSITION_LINK,
      result.visits[0].transition,
      "transition is correct for pageInfo object"
    );
    if (referrer) {
      Assert.equal(
        referrer,
        result.visits[0].referrer.href,
        "url of referrer for visit is correct"
      );
    } else {
      Assert.equal(
        null,
        result.visits[0].referrer,
        "url of referrer for visit is correct"
      );
    }
    if (date) {
      Assert.equal(
        Number(date),
        Number(result.visits[0].date),
        "date of visit is correct"
      );
    }

    Assert.ok(await PlacesTestUtils.isPageInDB(uri), "Page was added");
    Assert.ok(await PlacesTestUtils.visitsInDB(uri), "Visit was added");
  };

  try {
    for (let referrer of [TEST_URL, null]) {
      for (let date of [new Date(), null]) {
        for (let transition of [TRANSITION_LINK, null]) {
          await inserter(
            "Testing History.insert() with an nsIURI",
            x => x,
            referrer,
            date,
            transition
          );
          await inserter(
            "Testing History.insert() with a string url",
            x => x.spec,
            referrer,
            date,
            transition
          );
          await inserter(
            "Testing History.insert() with a URL object",
            x => URL.fromURI(x),
            referrer,
            date,
            transition
          );
        }
      }
    }
  } finally {
    await PlacesUtils.history.clear();
  }
});