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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "places_test_harness.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsString.h"
#include "mozilla/Attributes.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/StaticPrefs_layout.h"
#include "nsNetUtil.h"
#include "mock_Link.h"
using namespace mozilla;
using namespace mozilla::dom;
/**
* This file tests the IHistory interface.
*/
////////////////////////////////////////////////////////////////////////////////
//// Helper Methods
void expect_visit(Link::State aState) {
do_check_true(aState == Link::State::Visited);
}
void expect_no_visit(Link::State aState) {
do_check_true(aState == Link::State::Unvisited);
}
already_AddRefed<nsIURI> new_test_uri() {
// Create a unique spec.
static int32_t specNumber = 0;
nsCString spec = "http://mozilla.org/"_ns;
spec.AppendInt(specNumber++);
// Create the URI for the spec.
nsCOMPtr<nsIURI> testURI;
nsresult rv = NS_NewURI(getter_AddRefs(testURI), spec);
do_check_success(rv);
return testURI.forget();
}
class VisitURIObserver final : public nsIObserver {
~VisitURIObserver() = default;
public:
NS_DECL_ISUPPORTS
explicit VisitURIObserver(int aExpectedVisits = 1)
: mVisits(0), mExpectedVisits(aExpectedVisits) {
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
do_check_true(observerService);
(void)observerService->AddObserver(this, "uri-visit-saved", false);
}
void WaitForNotification() {
SpinEventLoopUntil("places:VisitURIObserver::WaitForNotification"_ns,
[&]() { return mVisits >= mExpectedVisits; });
}
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) override {
mVisits++;
if (mVisits == mExpectedVisits) {
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
(void)observerService->RemoveObserver(this, "uri-visit-saved");
}
return NS_OK;
}
private:
int mVisits;
int mExpectedVisits;
};
NS_IMPL_ISUPPORTS(VisitURIObserver, nsIObserver)
////////////////////////////////////////////////////////////////////////////////
//// Test Functions
void test_set_places_enabled() {
// Ensure places is enabled for everyone.
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefBranch =
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
do_check_success(rv);
rv = prefBranch->SetBoolPref("places.history.enabled", true);
do_check_success(rv);
// Run the next test.
run_next_test();
}
void test_wait_checkpoint() {
// This "fake" test is here to wait for the initial WAL checkpoint we force
// after creating the database schema, since that may happen at any time,
// and cause concurrent readers to access an older checkpoint.
nsCOMPtr<mozIStorageConnection> db = do_get_db();
nsCOMPtr<mozIStorageAsyncStatement> stmt;
db->CreateAsyncStatement("SELECT 1"_ns, getter_AddRefs(stmt));
RefPtr<PlacesAsyncStatementSpinner> spinner =
new PlacesAsyncStatementSpinner();
nsCOMPtr<mozIStoragePendingStatement> pending;
(void)stmt->ExecuteAsync(spinner, getter_AddRefs(pending));
spinner->SpinUntilCompleted();
// Run the next test.
run_next_test();
}
// These variables are shared between part 1 and part 2 of the test. Part 2
// sets the nsCOMPtr's to nullptr, freeing the reference.
namespace test_unvisited_does_not_notify {
nsCOMPtr<nsIURI> testURI;
RefPtr<mock_Link> testLink;
} // namespace test_unvisited_does_not_notify
void test_unvisited_does_not_notify_part1() {
using namespace test_unvisited_does_not_notify;
// This test is done in two parts. The first part registers for a URI that
// should not be visited. We then run another test that will also do a
// lookup and will be notified. Since requests are answered in the order they
// are requested (at least as long as the same URI isn't asked for later), we
// will know that the Link was not notified.
// First, we need a test URI.
testURI = new_test_uri();
// Create our test Link.
testLink = new mock_Link(expect_no_visit);
// Now, register our Link to be notified.
nsCOMPtr<IHistory> history = do_get_IHistory();
history->RegisterVisitedCallback(testURI, testLink);
// Run the next test.
run_next_test();
}
void test_visited_notifies() {
// First, we add our test URI to history.
nsCOMPtr<nsIURI> testURI = new_test_uri();
addURI(testURI);
// Create our test Link. The callback function will release the reference we
// have on the Link.
RefPtr<Link> link = new mock_Link(expect_visit);
// Now, register our Link to be notified.
nsCOMPtr<IHistory> history = do_get_IHistory();
history->RegisterVisitedCallback(testURI, link);
// Note: test will continue upon notification.
}
void test_unvisited_does_not_notify_part2() {
using namespace test_unvisited_does_not_notify;
SpinEventLoopUntil("places:test_unvisited_does_not_notify_part2"_ns,
[&]() { return testLink->GotNotified(); });
// We would have had a failure at this point had the content node been told it
// was visited. Therefore, now we change it so that it expects a visited
// notification, and unregisters itself after addURI.
testLink->AwaitNewNotification(expect_visit);
addURI(testURI);
// Clear the stored variables now.
testURI = nullptr;
testLink = nullptr;
}
void test_same_uri_notifies_both() {
// First, we add our test URI to history.
nsCOMPtr<nsIURI> testURI = new_test_uri();
addURI(testURI);
// Create our two test Links. The callback function will release the
// reference we have on the Links. Only the second Link should run the next
// test!
RefPtr<Link> link1 = new mock_Link(expect_visit, false);
RefPtr<Link> link2 = new mock_Link(expect_visit);
// Now, register our Link to be notified.
nsCOMPtr<IHistory> history = do_get_IHistory();
history->RegisterVisitedCallback(testURI, link1);
history->RegisterVisitedCallback(testURI, link2);
// Note: test will continue upon notification.
}
void test_unregistered_visited_does_not_notify() {
// This test must have a test that has a successful notification after it.
// The Link would have been notified by now if we were buggy and notified
// unregistered Links (due to request serialization).
nsCOMPtr<nsIURI> testURI = new_test_uri();
RefPtr<Link> link = new mock_Link(expect_no_visit, false);
nsCOMPtr<IHistory> history(do_get_IHistory());
history->RegisterVisitedCallback(testURI, link);
// Unregister the Link.
history->UnregisterVisitedCallback(testURI, link);
// And finally add a visit for the URI.
addURI(testURI);
// If history tries to notify us, we'll either crash because the Link will
// have been deleted (we are the only thing holding a reference to it), or our
// expect_no_visit call back will produce a failure. Either way, the test
// will be reported as a failure.
// Run the next test.
run_next_test();
}
void test_new_visit_notifies_waiting_Link() {
// Create our test Link. The callback function will release the reference we
// have on the link.
//
// Note that this will query the database and we'll get an _unvisited_
// notification, then (after we addURI) a _visited_ one.
RefPtr<mock_Link> link = new mock_Link(expect_no_visit);
// Now, register our content node to be notified.
nsCOMPtr<nsIURI> testURI = new_test_uri();
nsCOMPtr<IHistory> history = do_get_IHistory();
history->RegisterVisitedCallback(testURI, link);
SpinEventLoopUntil("places:test_new_visit_notifies_waiting_Link"_ns,
[&]() { return link->GotNotified(); });
link->AwaitNewNotification(expect_visit);
// Add ourselves to history.
addURI(testURI);
// Note: test will continue upon notification.
}
void test_RegisterVisitedCallback_returns_before_notifying() {
// Add a URI so that it's already in history.
nsCOMPtr<nsIURI> testURI = new_test_uri();
addURI(testURI);
// Create our test Link.
RefPtr<Link> link = new mock_Link(expect_no_visit, false);
// Now, register our content node to be notified. It should not be notified.
nsCOMPtr<IHistory> history = do_get_IHistory();
history->RegisterVisitedCallback(testURI, link);
// Remove ourselves as an observer. We would have failed if we had been
// notified.
history->UnregisterVisitedCallback(testURI, link);
run_next_test();
}
void test_visituri_inserts() {
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> lastURI = new_test_uri();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
history->VisitURI(nullptr, visitedURI, lastURI, mozilla::IHistory::TOP_LEVEL,
0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_true(place.id > 0);
do_check_false(place.hidden);
do_check_false(place.typed);
do_check_eq(place.visitCount, 1);
run_next_test();
}
void test_visituri_updates() {
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> lastURI = new_test_uri();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
RefPtr<VisitURIObserver> finisher;
history->VisitURI(nullptr, visitedURI, lastURI, mozilla::IHistory::TOP_LEVEL,
0);
finisher = new VisitURIObserver();
finisher->WaitForNotification();
history->VisitURI(nullptr, visitedURI, lastURI, mozilla::IHistory::TOP_LEVEL,
0);
finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_eq(place.visitCount, 2);
run_next_test();
}
void test_visituri_preserves_shown_and_typed() {
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> lastURI = new_test_uri();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
history->VisitURI(nullptr, visitedURI, lastURI, mozilla::IHistory::TOP_LEVEL,
0);
// this simulates the uri visit happening in a frame. Normally frame
// transitions would be hidden unless it was previously loaded top-level
history->VisitURI(nullptr, visitedURI, lastURI, 0, 0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver(2);
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_false(place.hidden);
run_next_test();
}
void test_visituri_creates_visit() {
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> lastURI = new_test_uri();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
history->VisitURI(nullptr, visitedURI, lastURI, mozilla::IHistory::TOP_LEVEL,
0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
VisitRecord visit;
do_get_place(visitedURI, place);
do_get_lastVisit(place.id, visit);
do_check_true(visit.id > 0);
do_check_eq(visit.lastVisitId, 0);
do_check_eq(visit.transitionType, nsINavHistoryService::TRANSITION_LINK);
run_next_test();
}
void test_visituri_frecency() {
// Adding a visit calculates frecency immediately.
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
RefPtr<WaitForNotificationSpinner> spinner =
new WaitForNotificationSpinner(PlacesEventType::Pages_rank_changed);
history->VisitURI(nullptr, visitedURI, nullptr, mozilla::IHistory::TOP_LEVEL,
0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
spinner->SpinUntilCompleted();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_true(place.frecency > 0);
run_next_test();
}
void test_visituri_hidden() {
nsCOMPtr<IHistory> history = do_get_IHistory();
{
// Insert a framed link visit.
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
nsCOMPtr<nsINavHistoryService> navHistory = do_get_NavHistory();
navHistory->MarkPageAsFollowedLink(visitedURI);
history->VisitURI(nullptr, visitedURI, nullptr, 0, 0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_true(place.hidden);
}
// Insert a redirect.
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
history->VisitURI(nullptr, visitedURI, nullptr,
mozilla::IHistory::TOP_LEVEL | IHistory::REDIRECT_SOURCE,
0);
{
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_true(place.hidden);
}
// Now add a non-hidden visit to the hidden page, check it gets unhidden.
history->VisitURI(nullptr, visitedURI, nullptr, mozilla::IHistory::TOP_LEVEL,
0);
{
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_false(place.hidden);
}
// Add another hidden visit, it should stay unhidden.
history->VisitURI(nullptr, visitedURI, nullptr,
mozilla::IHistory::TOP_LEVEL | IHistory::REDIRECT_SOURCE,
0);
{
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_false(place.hidden);
}
run_next_test();
}
void test_visituri_transition_typed() {
nsCOMPtr<nsINavHistoryService> navHistory = do_get_NavHistory();
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> lastURI = new_test_uri();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
navHistory->MarkPageAsTyped(visitedURI);
history->VisitURI(nullptr, visitedURI, lastURI, mozilla::IHistory::TOP_LEVEL,
0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
VisitRecord visit;
do_get_place(visitedURI, place);
do_get_lastVisit(place.id, visit);
do_check_true(visit.transitionType == nsINavHistoryService::TRANSITION_TYPED);
run_next_test();
}
void test_visituri_transition_embed() {
nsCOMPtr<IHistory> history = do_get_IHistory();
nsCOMPtr<nsIURI> lastURI = new_test_uri();
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
history->VisitURI(nullptr, visitedURI, lastURI, 0, 0);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
PlaceRecord place;
VisitRecord visit;
do_get_place(visitedURI, place);
do_get_lastVisit(place.id, visit);
do_check_eq(place.id, 0);
do_check_eq(visit.id, 0);
run_next_test();
}
void test_new_visit_adds_place_guid() {
// First, add a visit and wait. This will also add a place.
nsCOMPtr<nsIURI> visitedURI = new_test_uri();
nsCOMPtr<IHistory> history = do_get_IHistory();
nsresult rv = history->VisitURI(nullptr, visitedURI, nullptr,
mozilla::IHistory::TOP_LEVEL, 0);
do_check_success(rv);
RefPtr<VisitURIObserver> finisher = new VisitURIObserver();
finisher->WaitForNotification();
// Check that we have a guid for our visit.
PlaceRecord place;
do_get_place(visitedURI, place);
do_check_eq(place.visitCount, 1);
do_check_eq(place.guid.Length(), 12u);
run_next_test();
}
////////////////////////////////////////////////////////////////////////////////
//// Test Harness
/**
* Note: for tests marked "Order Important!", please see the test for details.
*/
Test gTests[] = {
PTEST(test_set_places_enabled), // Must come first!
PTEST(test_wait_checkpoint), // Must come second!
PTEST(test_unvisited_does_not_notify_part1), // Order Important!
PTEST(test_visited_notifies),
PTEST(test_unvisited_does_not_notify_part2), // Order Important!
PTEST(test_same_uri_notifies_both),
PTEST(test_unregistered_visited_does_not_notify), // Order Important!
PTEST(test_new_visit_adds_place_guid),
PTEST(test_new_visit_notifies_waiting_Link),
PTEST(test_RegisterVisitedCallback_returns_before_notifying),
PTEST(test_visituri_inserts),
PTEST(test_visituri_updates),
PTEST(test_visituri_preserves_shown_and_typed),
PTEST(test_visituri_creates_visit),
PTEST(test_visituri_frecency),
PTEST(test_visituri_hidden),
PTEST(test_visituri_transition_typed),
PTEST(test_visituri_transition_embed),
};
#define TEST_NAME "IHistory"
#include "places_test_harness_tail.h"
|