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

/**
 * What this is aimed to test:
 *
 * Expiration can be manually triggered through a debug topic, but that should
 * only expire orphan entries, unless -1 is passed as limit.
 */

var gNow = getExpirablePRTime(60);

add_task(function* test_expire_orphans()
{
  // Add visits to 2 pages and force a orphan expiration. Visits should survive.
  yield PlacesTestUtils.addVisits({
    uri: uri("http://page1.mozilla.org/"),
    visitDate: gNow++
  });
  yield PlacesTestUtils.addVisits({
    uri: uri("http://page2.mozilla.org/"),
    visitDate: gNow++
  });
  // Create a orphan place.
  let bm = yield PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://page3.mozilla.org/",
    title: ""
  });
  yield PlacesUtils.bookmarks.remove(bm);

  // Expire now.
  yield promiseForceExpirationStep(0);

  // Check that visits survived.
  do_check_eq(visits_in_database("http://page1.mozilla.org/"), 1);
  do_check_eq(visits_in_database("http://page2.mozilla.org/"), 1);
  do_check_false(page_in_database("http://page3.mozilla.org/"));

  // Clean up.
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_expire_orphans_optionalarg()
{
  // Add visits to 2 pages and force a orphan expiration. Visits should survive.
  yield PlacesTestUtils.addVisits({
    uri: uri("http://page1.mozilla.org/"),
    visitDate: gNow++
  });
  yield PlacesTestUtils.addVisits({
    uri: uri("http://page2.mozilla.org/"),
    visitDate: gNow++
  });
  // Create a orphan place.
  let bm = yield PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://page3.mozilla.org/",
    title: ""
  });
  yield PlacesUtils.bookmarks.remove(bm);

  // Expire now.
  yield promiseForceExpirationStep();

  // Check that visits survived.
  do_check_eq(visits_in_database("http://page1.mozilla.org/"), 1);
  do_check_eq(visits_in_database("http://page2.mozilla.org/"), 1);
  do_check_false(page_in_database("http://page3.mozilla.org/"));

  // Clean up.
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_expire_limited()
{
  yield PlacesTestUtils.addVisits([
    { // Should be expired cause it's the oldest visit
      uri: "http://old.mozilla.org/",
      visitDate: gNow++
    },
    { // Should not be expired cause we limit 1
      uri: "http://new.mozilla.org/",
      visitDate: gNow++
    },
  ]);

  // Expire now.
  yield promiseForceExpirationStep(1);

  // Check that newer visit survived.
  do_check_eq(visits_in_database("http://new.mozilla.org/"), 1);
  // Other visits should have been expired.
  do_check_false(page_in_database("http://old.mozilla.org/"));

  // Clean up.
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_expire_limited_longurl()
{
  let longurl = "http://long.mozilla.org/" + "a".repeat(232);
  yield PlacesTestUtils.addVisits([
    { // Should be expired cause it's the oldest visit
      uri: "http://old.mozilla.org/",
      visitDate: gNow++
    },
    { // Should be expired cause it's a long url older than 60 days.
      uri: longurl,
      visitDate: gNow++
    },
    { // Should not be expired cause younger than 60 days.
      uri: longurl,
      visitDate: getExpirablePRTime(58)
    }
  ]);

  yield promiseForceExpirationStep(1);

  // Check that some visits survived.
  do_check_eq(visits_in_database(longurl), 1);
  // Other visits should have been expired.
  do_check_false(page_in_database("http://old.mozilla.org/"));

  // Clean up.
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_expire_limited_exoticurl()
{
  yield PlacesTestUtils.addVisits([
    { // Should be expired cause it's the oldest visit
      uri: "http://old.mozilla.org/",
      visitDate: gNow++
    },
    { // Should be expired cause it's a long url older than 60 days.
      uri: "http://download.mozilla.org",
      visitDate: gNow++,
      transition: 7
    },
    { // Should not be expired cause younger than 60 days.
      uri: "http://nonexpirable-download.mozilla.org",
      visitDate: getExpirablePRTime(58),
      transition: 7
    }
  ]);

  yield promiseForceExpirationStep(1);

  // Check that some visits survived.
  do_check_eq(visits_in_database("http://nonexpirable-download.mozilla.org/"), 1);
  // The visits are gone, the url is not yet, cause we limited the expiration
  // to one entry, and we already removed http://old.mozilla.org/.
  // The page normally would be expired by the next expiration run.
  do_check_eq(visits_in_database("http://download.mozilla.org/"), 0);
  // Other visits should have been expired.
  do_check_false(page_in_database("http://old.mozilla.org/"));

  // Clean up.
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_expire_unlimited()
{
  let longurl = "http://long.mozilla.org/" + "a".repeat(232);
  yield PlacesTestUtils.addVisits([
    {
      uri: "http://old.mozilla.org/",
      visitDate: gNow++
    },
    {
      uri: "http://new.mozilla.org/",
      visitDate: gNow++
    },
    // Add expirable visits.
    {
      uri: "http://download.mozilla.org/",
      visitDate: gNow++,
      transition: PlacesUtils.history.TRANSITION_DOWNLOAD
    },
    {
      uri: longurl,
      visitDate: gNow++
    },

    // Add non-expirable visits
    {
      uri: "http://nonexpirable.mozilla.org/",
      visitDate: getExpirablePRTime(5)
    },
    {
      uri: "http://nonexpirable-download.mozilla.org/",
      visitDate: getExpirablePRTime(5),
      transition: PlacesUtils.history.TRANSITION_DOWNLOAD
    },
    {
      uri: longurl,
      visitDate: getExpirablePRTime(5)
    }
  ]);

  yield promiseForceExpirationStep(-1);

  // Check that some visits survived.
  do_check_eq(visits_in_database("http://nonexpirable.mozilla.org/"), 1);
  do_check_eq(visits_in_database("http://nonexpirable-download.mozilla.org/"), 1);
  do_check_eq(visits_in_database(longurl), 1);
  // Other visits should have been expired.
  do_check_false(page_in_database("http://old.mozilla.org/"));
  do_check_false(page_in_database("http://download.mozilla.org/"));
  do_check_false(page_in_database("http://new.mozilla.org/"));

  // Clean up.
  yield PlacesTestUtils.clearHistory();
});

function run_test()
{
  // Set interval to a large value so we don't expire on it.
  setInterval(3600); // 1h
  // Set maxPages to a low value, so it's easy to go over it.
  setMaxPages(1);

  run_next_test();
}