1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/history/visit_tracker.h"
6 #include "base/basictypes.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 using history::VisitTracker;
10
11 namespace {
12
13 struct VisitToTest {
14 // Identifies the host, we'll cast this to a pointer when querying (the
15 // tracker isn't allowed to dereference this pointer).
16 int host;
17 int32 page_id;
18
19 // Used when adding this to the tracker
20 const char* url;
21 const history::VisitID visit_id;
22
23 // Used when finding the referrer
24 const char* referrer;
25
26 // the correct referring visit ID to compare to the computed one
27 history::VisitID referring_visit_id;
28 };
29
30 // The tracker uses RenderProcessHost pointers for scoping but never
31 // dereferences them. We use ints because it's easier. This function converts
32 // between the two.
MakeFakeHost(int id)33 void* MakeFakeHost(int id) {
34 void* host = 0;
35 memcpy(&host, &id, sizeof(int));
36 return host;
37 }
38
RunTest(VisitTracker * tracker,VisitToTest * test,int test_count)39 void RunTest(VisitTracker* tracker, VisitToTest* test, int test_count) {
40 for (int i = 0; i < test_count; i++) {
41 // Our host pointer is actually just an int, convert it (it will not get
42 // dereferenced).
43 void* host = MakeFakeHost(test[i].host);
44
45 // Check the referrer for this visit.
46 history::VisitID ref_visit = tracker->GetLastVisit(
47 host, test[i].page_id, GURL(test[i].referrer));
48 EXPECT_EQ(test[i].referring_visit_id, ref_visit);
49
50 // Now add this visit.
51 tracker->AddVisit(host, test[i].page_id, GURL(test[i].url),
52 test[i].visit_id);
53 }
54 }
55
56 } // namespace
57
58 // A simple test that makes sure we transition between main pages in the
59 // presence of back/forward.
TEST(VisitTracker,SimpleTransitions)60 TEST(VisitTracker, SimpleTransitions) {
61 VisitToTest test_simple[] = {
62 // Started here:
63 {1, 1, "http://www.google.com/", 1, "", 0},
64 // Clicked a link:
65 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1},
66 // Went back, then clicked a link:
67 {1, 3, "http://video.google.com/", 3, "http://www.google.com/", 1},
68 };
69
70 VisitTracker tracker;
71 RunTest(&tracker, test_simple, arraysize(test_simple));
72 }
73
74 // Test that referrer is properly computed when there are different frame
75 // navigations happening.
TEST(VisitTracker,Frames)76 TEST(VisitTracker, Frames) {
77 VisitToTest test_frames[] = {
78 // Started here:
79 {1, 1, "http://foo.com/", 1, "", 0},
80 // Which had an auto-loaded subframe:
81 {1, 1, "http://foo.com/ad.html", 2, "http://foo.com/", 1},
82 // ...and another auto-loaded subframe:
83 {1, 1, "http://foo.com/ad2.html", 3, "http://foo.com/", 1},
84 // ...and the user navigated the first subframe to somwhere else
85 {1, 2, "http://bar.com/", 4, "http://foo.com/ad.html", 2},
86 // ...and then the second subframe somewhere else
87 {1, 3, "http://fud.com/", 5, "http://foo.com/ad2.html", 3},
88 // ...and then the main frame somewhere else.
89 {1, 4, "http://www.google.com/", 6, "http://foo.com/", 1},
90 };
91
92 VisitTracker tracker;
93 RunTest(&tracker, test_frames, arraysize(test_frames));
94 }
95
96 // Test frame navigation to make sure that the referrer is properly computed
97 // when there are multiple processes navigating the same pages.
TEST(VisitTracker,MultiProcess)98 TEST(VisitTracker, MultiProcess) {
99 VisitToTest test_processes[] = {
100 // Process 1 and 2 start here:
101 {1, 1, "http://foo.com/", 1, "", 0},
102 {2, 1, "http://foo.com/", 2, "", 0},
103 // They have some subframes:
104 {1, 1, "http://foo.com/ad.html", 3, "http://foo.com/", 1},
105 {2, 1, "http://foo.com/ad.html", 4, "http://foo.com/", 2},
106 // Subframes are navigated:
107 {1, 2, "http://bar.com/", 5, "http://foo.com/ad.html", 3},
108 {2, 2, "http://bar.com/", 6, "http://foo.com/ad.html", 4},
109 // Main frame is navigated:
110 {1, 3, "http://www.google.com/", 7, "http://foo.com/", 1},
111 {2, 3, "http://www.google.com/", 8, "http://foo.com/", 2},
112 };
113
114 VisitTracker tracker;
115 RunTest(&tracker, test_processes, arraysize(test_processes));
116 }
117
118 // Test that processes get removed properly.
TEST(VisitTracker,ProcessRemove)119 TEST(VisitTracker, ProcessRemove) {
120 // Simple navigation from one process.
121 VisitToTest part1[] = {
122 {1, 1, "http://www.google.com/", 1, "", 0},
123 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1},
124 };
125
126 VisitTracker tracker;
127 RunTest(&tracker, part1, arraysize(part1));
128
129 // Say that process has been destroyed.
130 tracker.NotifyRenderProcessHostDestruction(MakeFakeHost(1));
131
132 // Simple navigation from a new process with the same ID, it should not find
133 // a referrer.
134 VisitToTest part2[] = {
135 {1, 1, "http://images.google.com/", 2, "http://www.google.com/", 0},
136 };
137 RunTest(&tracker, part2, arraysize(part2));
138 }
139