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