Chromium Code Reviews| 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::ContextID; | |
| 9 using history::VisitTracker; | 10 using history::VisitTracker; |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 struct VisitToTest { | 14 struct VisitToTest { |
| 14 // Identifies the host, we'll cast this to a pointer when querying (the | 15 // Identifies the context. |
| 15 // tracker isn't allowed to dereference this pointer). | 16 int context_id_int; |
| 16 int host; | |
| 17 int32 page_id; | 17 int32 page_id; |
| 18 | 18 |
| 19 // Used when adding this to the tracker | 19 // Used when adding this to the tracker |
| 20 const char* url; | 20 const char* url; |
| 21 const history::VisitID visit_id; | 21 const history::VisitID visit_id; |
| 22 | 22 |
| 23 // Used when finding the referrer | 23 // Used when finding the referrer |
| 24 const char* referrer; | 24 const char* referrer; |
| 25 | 25 |
| 26 // the correct referring visit ID to compare to the computed one | 26 // the correct referring visit ID to compare to the computed one |
| 27 history::VisitID referring_visit_id; | 27 history::VisitID referring_visit_id; |
| 28 }; | 28 }; |
| 29 | 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. | |
| 33 void* MakeFakeHost(int id) { | |
| 34 void* host = 0; | |
| 35 memcpy(&host, &id, sizeof(int)); | |
| 36 return host; | |
| 37 } | |
| 38 | |
| 39 void RunTest(VisitTracker* tracker, VisitToTest* test, int test_count) { | 30 void RunTest(VisitTracker* tracker, VisitToTest* test, int test_count) { |
| 40 for (int i = 0; i < test_count; i++) { | 31 for (int i = 0; i < test_count; i++) { |
| 41 // Our host pointer is actually just an int, convert it (it will not get | 32 // Our host pointer is actually just an int, convert it (it will not get |
| 42 // dereferenced). | 33 // dereferenced). |
| 43 void* host = MakeFakeHost(test[i].host); | 34 ContextID context_id = reinterpret_cast<ContextID>(test[i].context_id_int); |
| 44 | 35 |
| 45 // Check the referrer for this visit. | 36 // Check the referrer for this visit. |
| 46 history::VisitID ref_visit = tracker->GetLastVisit( | 37 history::VisitID ref_visit = tracker->GetLastVisit( |
| 47 host, test[i].page_id, GURL(test[i].referrer)); | 38 context_id, test[i].page_id, GURL(test[i].referrer)); |
| 48 EXPECT_EQ(test[i].referring_visit_id, ref_visit); | 39 EXPECT_EQ(test[i].referring_visit_id, ref_visit); |
| 49 | 40 |
| 50 // Now add this visit. | 41 // Now add this visit. |
| 51 tracker->AddVisit(host, test[i].page_id, GURL(test[i].url), | 42 tracker->AddVisit( |
| 52 test[i].visit_id); | 43 context_id, test[i].page_id, GURL(test[i].url), test[i].visit_id); |
| 53 } | 44 } |
| 54 } | 45 } |
| 55 | 46 |
| 56 } // namespace | 47 } // namespace |
| 57 | 48 |
| 58 // A simple test that makes sure we transition between main pages in the | 49 // A simple test that makes sure we transition between main pages in the |
| 59 // presence of back/forward. | 50 // presence of back/forward. |
| 60 TEST(VisitTracker, SimpleTransitions) { | 51 TEST(VisitTracker, SimpleTransitions) { |
| 61 VisitToTest test_simple[] = { | 52 VisitToTest test_simple[] = { |
| 62 // Started here: | 53 // Started here: |
| 63 {1, 1, "http://www.google.com/", 1, "", 0}, | 54 {1, 1, "http://www.google.com/", 1, "", 0}, |
| 64 // Clicked a link: | 55 // Clicked a link: |
| 65 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1}, | 56 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1}, |
| 66 // Went back, then clicked a link: | 57 // Went back, then clicked a link: |
| 67 {1, 3, "http://video.google.com/", 3, "http://www.google.com/", 1}, | 58 {1, 3, "http://video.google.com/", 3, "http://www.google.com/", 1}, |
| 68 }; | 59 }; |
| 69 | 60 |
| 70 VisitTracker tracker; | 61 VisitTracker tracker; |
| 71 RunTest(&tracker, test_simple, arraysize(test_simple)); | 62 RunTest(&tracker, test_simple, arraysize(test_simple)); |
| 72 } | 63 } |
| 73 | 64 |
| 74 // Test that referrer is properly computed when there are different frame | 65 // Test that referrer is properly computed when there are different frame |
| 75 // navigations happening. | 66 // navigations happening. |
| 76 TEST(VisitTracker, Frames) { | 67 TEST(VisitTracker, Frames) { |
| 77 VisitToTest test_frames[] = { | 68 VisitToTest test_frames[] = { |
| 78 // Started here: | 69 // Started here: |
| 79 {1, 1, "http://foo.com/", 1, "", 0}, | 70 {1, 1, "http://foo.com/", 1, "", 0}, |
| 80 // Which had an auto-loaded subframe: | 71 // Which had an auto-loaded subframe: |
| 81 {1, 1, "http://foo.com/ad.html", 2, "http://foo.com/", 1}, | 72 {1, 1, "http://foo.com/ad.html", 2, "http://foo.com/", 1}, |
| 82 // ...and another auto-loaded subframe: | 73 // ...and another auto-loaded subframe: |
| 83 {1, 1, "http://foo.com/ad2.html", 3, "http://foo.com/", 1}, | 74 {1, 1, "http://foo.com/ad2.html", 3, "http://foo.com/", 1}, |
| 84 // ...and the user navigated the first subframe to somwhere else | 75 // ...and the user navigated the first subframe to somwhere else |
| 85 {1, 2, "http://bar.com/", 4, "http://foo.com/ad.html", 2}, | 76 {1, 2, "http://bar.com/", 4, "http://foo.com/ad.html", 2}, |
| 86 // ...and then the second subframe somewhere else | 77 // ...and then the second subframe somewhere else |
| 87 {1, 3, "http://fud.com/", 5, "http://foo.com/ad2.html", 3}, | 78 {1, 3, "http://fud.com/", 5, "http://foo.com/ad2.html", 3}, |
| 88 // ...and then the main frame somewhere else. | 79 // ...and then the main frame somewhere else. |
| 89 {1, 4, "http://www.google.com/", 6, "http://foo.com/", 1}, | 80 {1, 4, "http://www.google.com/", 6, "http://foo.com/", 1}, |
| 90 }; | 81 }; |
| 91 | 82 |
| 92 VisitTracker tracker; | 83 VisitTracker tracker; |
| 93 RunTest(&tracker, test_frames, arraysize(test_frames)); | 84 RunTest(&tracker, test_frames, arraysize(test_frames)); |
| 94 } | 85 } |
| 95 | 86 |
| 96 // Test frame navigation to make sure that the referrer is properly computed | 87 // Test frame navigation to make sure that the referrer is properly computed |
| 97 // when there are multiple processes navigating the same pages. | 88 // when there are multiple processes navigating the same pages. |
| 98 TEST(VisitTracker, MultiProcess) { | 89 TEST(VisitTracker, MultiProcess) { |
| 99 VisitToTest test_processes[] = { | 90 VisitToTest test_processes[] = { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 112 }; | 103 }; |
| 113 | 104 |
| 114 VisitTracker tracker; | 105 VisitTracker tracker; |
| 115 RunTest(&tracker, test_processes, arraysize(test_processes)); | 106 RunTest(&tracker, test_processes, arraysize(test_processes)); |
| 116 } | 107 } |
| 117 | 108 |
| 118 // Test that processes get removed properly. | 109 // Test that processes get removed properly. |
| 119 TEST(VisitTracker, ProcessRemove) { | 110 TEST(VisitTracker, ProcessRemove) { |
| 120 // Simple navigation from one process. | 111 // Simple navigation from one process. |
| 121 VisitToTest part1[] = { | 112 VisitToTest part1[] = { |
| 122 {1, 1, "http://www.google.com/", 1, "", 0}, | 113 {1, 1, "http://www.google.com/", 1, "", 0}, |
| 123 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1}, | 114 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1}, |
| 124 }; | 115 }; |
| 125 | 116 |
| 126 VisitTracker tracker; | 117 VisitTracker tracker; |
| 127 RunTest(&tracker, part1, arraysize(part1)); | 118 RunTest(&tracker, part1, arraysize(part1)); |
| 128 | 119 |
| 129 // Say that process has been destroyed. | 120 // Say that context has been destroyed. |
|
blundell
2014/06/11 12:17:48
s/destroyed/invalidated
方觉(Fang Jue)
2014/06/11 12:41:47
Done.
| |
| 130 tracker.NotifyRenderProcessHostDestruction(MakeFakeHost(1)); | 121 tracker.ClearCachedDataForContextID(reinterpret_cast<ContextID>(1)); |
| 131 | 122 |
| 132 // Simple navigation from a new process with the same ID, it should not find | 123 // Simple navigation from a new process with the same ID, it should not find |
| 133 // a referrer. | 124 // a referrer. |
| 134 VisitToTest part2[] = { | 125 VisitToTest part2[] = { |
| 135 {1, 1, "http://images.google.com/", 2, "http://www.google.com/", 0}, | 126 {1, 1, "http://images.google.com/", 2, "http://www.google.com/", 0}, |
| 136 }; | 127 }; |
| 137 RunTest(&tracker, part2, arraysize(part2)); | 128 RunTest(&tracker, part2, arraysize(part2)); |
| 138 } | 129 } |
| OLD | NEW |