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::ContextID; 10 using history::VisitTracker; 11 12 namespace { 13 14 struct VisitToTest { 15 // Identifies the context. 16 int context_id_int; 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 void RunTest(VisitTracker* tracker, VisitToTest* test, int test_count) { 31 for (int i = 0; i < test_count; i++) { 32 // Our host pointer is actually just an int, convert it (it will not get 33 // dereferenced). 34 ContextID context_id = reinterpret_cast<ContextID>(test[i].context_id_int); 35 36 // Check the referrer for this visit. 37 history::VisitID ref_visit = tracker->GetLastVisit( 38 context_id, test[i].page_id, GURL(test[i].referrer)); 39 EXPECT_EQ(test[i].referring_visit_id, ref_visit); 40 41 // Now add this visit. 42 tracker->AddVisit( 43 context_id, test[i].page_id, GURL(test[i].url), test[i].visit_id); 44 } 45 } 46 47 } // namespace 48 49 // A simple test that makes sure we transition between main pages in the 50 // presence of back/forward. 51 TEST(VisitTracker, SimpleTransitions) { 52 VisitToTest test_simple[] = { 53 // Started here: 54 {1, 1, "http://www.google.com/", 1, "", 0}, 55 // Clicked a link: 56 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1}, 57 // Went back, then clicked a link: 58 {1, 3, "http://video.google.com/", 3, "http://www.google.com/", 1}, 59 }; 60 61 VisitTracker tracker; 62 RunTest(&tracker, test_simple, arraysize(test_simple)); 63 } 64 65 // Test that referrer is properly computed when there are different frame 66 // navigations happening. 67 TEST(VisitTracker, Frames) { 68 VisitToTest test_frames[] = { 69 // Started here: 70 {1, 1, "http://foo.com/", 1, "", 0}, 71 // Which had an auto-loaded subframe: 72 {1, 1, "http://foo.com/ad.html", 2, "http://foo.com/", 1}, 73 // ...and another auto-loaded subframe: 74 {1, 1, "http://foo.com/ad2.html", 3, "http://foo.com/", 1}, 75 // ...and the user navigated the first subframe to somwhere else 76 {1, 2, "http://bar.com/", 4, "http://foo.com/ad.html", 2}, 77 // ...and then the second subframe somewhere else 78 {1, 3, "http://fud.com/", 5, "http://foo.com/ad2.html", 3}, 79 // ...and then the main frame somewhere else. 80 {1, 4, "http://www.google.com/", 6, "http://foo.com/", 1}, 81 }; 82 83 VisitTracker tracker; 84 RunTest(&tracker, test_frames, arraysize(test_frames)); 85 } 86 87 // Test frame navigation to make sure that the referrer is properly computed 88 // when there are multiple processes navigating the same pages. 89 TEST(VisitTracker, MultiProcess) { 90 VisitToTest test_processes[] = { 91 // Process 1 and 2 start here: 92 {1, 1, "http://foo.com/", 1, "", 0}, 93 {2, 1, "http://foo.com/", 2, "", 0}, 94 // They have some subframes: 95 {1, 1, "http://foo.com/ad.html", 3, "http://foo.com/", 1}, 96 {2, 1, "http://foo.com/ad.html", 4, "http://foo.com/", 2}, 97 // Subframes are navigated: 98 {1, 2, "http://bar.com/", 5, "http://foo.com/ad.html", 3}, 99 {2, 2, "http://bar.com/", 6, "http://foo.com/ad.html", 4}, 100 // Main frame is navigated: 101 {1, 3, "http://www.google.com/", 7, "http://foo.com/", 1}, 102 {2, 3, "http://www.google.com/", 8, "http://foo.com/", 2}, 103 }; 104 105 VisitTracker tracker; 106 RunTest(&tracker, test_processes, arraysize(test_processes)); 107 } 108 109 // Test that processes get removed properly. 110 TEST(VisitTracker, ProcessRemove) { 111 // Simple navigation from one process. 112 VisitToTest part1[] = { 113 {1, 1, "http://www.google.com/", 1, "", 0}, 114 {1, 2, "http://images.google.com/", 2, "http://www.google.com/", 1}, 115 }; 116 117 VisitTracker tracker; 118 RunTest(&tracker, part1, arraysize(part1)); 119 120 // Say that context has been invalidated. 121 tracker.ClearCachedDataForContextID(reinterpret_cast<ContextID>(1)); 122 123 // Simple navigation from a new process with the same ID, it should not find 124 // a referrer. 125 VisitToTest part2[] = { 126 {1, 1, "http://images.google.com/", 2, "http://www.google.com/", 0}, 127 }; 128 RunTest(&tracker, part2, arraysize(part2)); 129 } 130