| 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 <vector> | 5 #include <vector> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <cstdio> | 7 #include <cstdio> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/process_util.h" | |
| 13 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
| 14 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 15 #include "chrome/browser/visitedlink_master.h" | 14 #include "chrome/browser/visitedlink_master.h" |
| 16 #include "chrome/renderer/visitedlink_slave.h" | 15 #include "chrome/renderer/visitedlink_slave.h" |
| 17 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 // a nice long URL that we can append numbers to to get new URLs | 21 // a nice long URL that we can append numbers to to get new URLs |
| 23 const char g_test_prefix[] = | 22 const char g_test_prefix[] = |
| 24 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; | 23 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; |
| 25 const int g_test_count = 1000; | 24 const int g_test_count = 1000; |
| 26 | 25 |
| 27 // Returns a test URL for index |i| | 26 // Returns a test URL for index |i| |
| 28 GURL TestURL(int i) { | 27 GURL TestURL(int i) { |
| 29 return GURL(StringPrintf("%s%d", g_test_prefix, i)); | 28 return GURL(StringPrintf("%s%d", g_test_prefix, i)); |
| 30 } | 29 } |
| 31 | 30 |
| 31 // when testing in single-threaded mode |
| 32 VisitedLinkMaster* g_master = NULL; |
| 32 std::vector<VisitedLinkSlave*> g_slaves; | 33 std::vector<VisitedLinkSlave*> g_slaves; |
| 33 | 34 |
| 34 VisitedLinkMaster::PostNewTableEvent SynchronousBroadcastNewTableEvent; | 35 VisitedLinkMaster::PostNewTableEvent SynchronousBroadcastNewTableEvent; |
| 35 void SynchronousBroadcastNewTableEvent(base::SharedMemory* table) { | 36 void SynchronousBroadcastNewTableEvent(base::SharedMemory* table) { |
| 36 if (table) { | 37 if (table) { |
| 37 for (std::vector<VisitedLinkSlave>::size_type i = 0; | 38 for (std::vector<VisitedLinkSlave>::size_type i = 0; |
| 38 i < g_slaves.size(); i++) { | 39 i < (int)g_slaves.size(); i++) { |
| 39 base::SharedMemoryHandle new_handle = NULL; | 40 base::SharedMemoryHandle new_handle = NULL; |
| 40 table->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle); | 41 table->ShareToProcess(GetCurrentProcess(), &new_handle); |
| 41 g_slaves[i]->Init(new_handle); | 42 g_slaves[i]->Init(new_handle); |
| 42 } | 43 } |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 46 } // namespace | |
| 47 | |
| 48 class VisitedLinkTest : public testing::Test { | 47 class VisitedLinkTest : public testing::Test { |
| 49 protected: | 48 protected: |
| 50 // Initialize the history system. This should be called before InitVisited(). | 49 // Initialize the history system. This should be called before InitVisited(). |
| 51 bool InitHistory() { | 50 bool InitHistory() { |
| 52 history_service_ = new HistoryService; | 51 history_service_ = new HistoryService; |
| 53 return history_service_->Init(history_dir_.ToWStringHack(), NULL); | 52 return history_service_->Init(history_dir_, NULL); |
| 54 } | 53 } |
| 55 | 54 |
| 56 // Initializes the visited link objects. Pass in the size that you want a | 55 // Initializes the visited link objects. Pass in the size that you want a |
| 57 // freshly created table to be. 0 means use the default. | 56 // freshly created table to be. 0 means use the default. |
| 58 // | 57 // |
| 59 // |suppress_rebuild| is set when we're not testing rebuilding, see | 58 // |suppress_rebuild| is set when we're not testing rebuilding, see |
| 60 // the VisitedLinkMaster constructor. | 59 // the VisitedLinkMaster constructor. |
| 61 bool InitVisited(int initial_size, bool suppress_rebuild) { | 60 bool InitVisited(int initial_size, bool suppress_rebuild) { |
| 62 // Initialize the visited link system. | 61 // Initialize the visited link system. |
| 63 master_.reset(new VisitedLinkMaster(NULL, | 62 master_.reset(new VisitedLinkMaster(NULL, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 ASSERT_TRUE(InitVisited(0, true)); | 96 ASSERT_TRUE(InitVisited(0, true)); |
| 98 master_->DebugValidate(); | 97 master_->DebugValidate(); |
| 99 | 98 |
| 100 // check that the table has the proper number of entries | 99 // check that the table has the proper number of entries |
| 101 int used_count = master_->GetUsedCount(); | 100 int used_count = master_->GetUsedCount(); |
| 102 ASSERT_EQ(used_count, g_test_count); | 101 ASSERT_EQ(used_count, g_test_count); |
| 103 | 102 |
| 104 // Create a slave database. | 103 // Create a slave database. |
| 105 VisitedLinkSlave slave; | 104 VisitedLinkSlave slave; |
| 106 base::SharedMemoryHandle new_handle = NULL; | 105 base::SharedMemoryHandle new_handle = NULL; |
| 107 master_->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle); | 106 master_->ShareToProcess(GetCurrentProcess(), &new_handle); |
| 108 bool success = slave.Init(new_handle); | 107 bool success = slave.Init(new_handle); |
| 109 ASSERT_TRUE(success); | 108 ASSERT_TRUE(success); |
| 110 g_slaves.push_back(&slave); | 109 g_slaves.push_back(&slave); |
| 111 | 110 |
| 112 bool found; | 111 bool found; |
| 113 for (int i = 0; i < g_test_count; i++) { | 112 for (int i = 0; i < g_test_count; i++) { |
| 114 GURL cur = TestURL(i); | 113 GURL cur = TestURL(i); |
| 115 found = master_->IsVisited(cur); | 114 found = master_->IsVisited(cur); |
| 116 EXPECT_TRUE(found) << "URL " << i << "not found in master."; | 115 EXPECT_TRUE(found) << "URL " << i << "not found in master."; |
| 117 | 116 |
| 118 found = slave.IsVisited(cur); | 117 found = slave.IsVisited(cur); |
| 119 EXPECT_TRUE(found) << "URL " << i << "not found in slave."; | 118 EXPECT_TRUE(found) << "URL " << i << "not found in slave."; |
| 120 } | 119 } |
| 121 | 120 |
| 122 // test some random URL so we know that it returns false sometimes too | 121 // test some random URL so we know that it returns false sometimes too |
| 123 found = master_->IsVisited(GURL("http://unfound.site/")); | 122 found = master_->IsVisited(GURL("http://unfound.site/")); |
| 124 ASSERT_FALSE(found); | 123 ASSERT_FALSE(found); |
| 125 found = slave.IsVisited(GURL("http://unfound.site/")); | 124 found = slave.IsVisited(GURL("http://unfound.site/")); |
| 126 ASSERT_FALSE(found); | 125 ASSERT_FALSE(found); |
| 127 | 126 |
| 128 master_->DebugValidate(); | 127 master_->DebugValidate(); |
| 129 | 128 |
| 130 g_slaves.clear(); | 129 g_slaves.clear(); |
| 131 } | 130 } |
| 132 | 131 |
| 133 // testing::Test | 132 // testing::Test |
| 134 virtual void SetUp() { | 133 virtual void SetUp() { |
| 135 PathService::Get(base::DIR_TEMP, &history_dir_); | 134 PathService::Get(base::DIR_TEMP, &history_dir_); |
| 136 history_dir_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinkTest")); | 135 file_util::AppendToPath(&history_dir_, L"VisitedLinkTest"); |
| 137 file_util::Delete(history_dir_, true); | 136 file_util::Delete(history_dir_, true); |
| 138 file_util::CreateDirectory(history_dir_); | 137 file_util::CreateDirectory(history_dir_); |
| 139 | 138 |
| 140 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks")); | 139 visited_file_ = history_dir_; |
| 140 file_util::AppendToPath(&visited_file_, L"VisitedLinks"); |
| 141 } | 141 } |
| 142 | 142 |
| 143 virtual void TearDown() { | 143 virtual void TearDown() { |
| 144 ClearDB(); | 144 ClearDB(); |
| 145 file_util::Delete(history_dir_, true); | 145 file_util::Delete(history_dir_, true); |
| 146 } | 146 } |
| 147 | 147 |
| 148 MessageLoop message_loop_; | 148 MessageLoop message_loop_; |
| 149 | 149 |
| 150 // Filenames for the services; | 150 // Filenames for the services; |
| 151 FilePath history_dir_; | 151 std::wstring history_dir_; |
| 152 FilePath visited_file_; | 152 std::wstring visited_file_; |
| 153 | 153 |
| 154 scoped_ptr<VisitedLinkMaster> master_; | 154 scoped_ptr<VisitedLinkMaster> master_; |
| 155 scoped_refptr<HistoryService> history_service_; | 155 scoped_refptr<HistoryService> history_service_; |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 } // namespace |
| 159 |
| 158 // This test creates and reads some databases to make sure the data is | 160 // This test creates and reads some databases to make sure the data is |
| 159 // preserved throughout those operations. | 161 // preserved throughout those operations. |
| 160 TEST_F(VisitedLinkTest, DatabaseIO) { | 162 TEST_F(VisitedLinkTest, DatabaseIO) { |
| 161 ASSERT_TRUE(InitHistory()); | 163 ASSERT_TRUE(InitHistory()); |
| 162 ASSERT_TRUE(InitVisited(0, true)); | 164 ASSERT_TRUE(InitVisited(0, true)); |
| 163 | 165 |
| 164 for (int i = 0; i < g_test_count; i++) | 166 for (int i = 0; i < g_test_count; i++) |
| 165 master_->AddURL(TestURL(i)); | 167 master_->AddURL(TestURL(i)); |
| 166 | 168 |
| 167 // Test that the database was written properly | 169 // Test that the database was written properly |
| 168 Reload(); | 170 Reload(); |
| 169 } | 171 } |
| 170 | 172 |
| 171 // Checks that we can delete things properly when there are collisions. | 173 // Checks that we can delete things properly when there are collisions. |
| 172 TEST_F(VisitedLinkTest, Delete) { | 174 TEST_F(VisitedLinkTest, Delete) { |
| 173 static const int32 kInitialSize = 17; | 175 static const int32 kInitialSize = 17; |
| 174 ASSERT_TRUE(InitHistory()); | 176 ASSERT_TRUE(InitHistory()); |
| 175 ASSERT_TRUE(InitVisited(kInitialSize, true)); | 177 ASSERT_TRUE(InitVisited(kInitialSize, true)); |
| 176 | 178 |
| 177 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the | 179 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the |
| 178 // same value. | 180 // same value. |
| 179 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14; | 181 const int kFingerprint0 = kInitialSize * 0 + 14; |
| 180 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14; | 182 const int kFingerprint1 = kInitialSize * 1 + 14; |
| 181 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14; | 183 const int kFingerprint2 = kInitialSize * 2 + 14; |
| 182 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14; | 184 const int kFingerprint3 = kInitialSize * 3 + 14; |
| 183 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14; | 185 const int kFingerprint4 = kInitialSize * 4 + 14; |
| 184 master_->AddFingerprint(kFingerprint0); // @14 | 186 master_->AddFingerprint(kFingerprint0); // @14 |
| 185 master_->AddFingerprint(kFingerprint1); // @15 | 187 master_->AddFingerprint(kFingerprint1); // @15 |
| 186 master_->AddFingerprint(kFingerprint2); // @16 | 188 master_->AddFingerprint(kFingerprint2); // @16 |
| 187 master_->AddFingerprint(kFingerprint3); // @0 | 189 master_->AddFingerprint(kFingerprint3); // @0 |
| 188 master_->AddFingerprint(kFingerprint4); // @1 | 190 master_->AddFingerprint(kFingerprint4); // @1 |
| 189 | 191 |
| 190 // Deleting 14 should move the next value up one slot (we do not specify an | 192 // Deleting 14 should move the next value up one slot (we do not specify an |
| 191 // order). | 193 // order). |
| 192 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]); | 194 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]); |
| 193 master_->DeleteFingerprint(kFingerprint3, false); | 195 master_->DeleteFingerprint(kFingerprint3, false); |
| 194 VisitedLinkCommon::Fingerprint zero_fingerprint = 0; | 196 EXPECT_EQ(0, master_->hash_table_[1]); |
| 195 EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]); | 197 EXPECT_NE(0, master_->hash_table_[0]); |
| 196 EXPECT_NE(zero_fingerprint, master_->hash_table_[0]); | |
| 197 | 198 |
| 198 // Deleting the other four should leave the table empty. | 199 // Deleting the other four should leave the table empty. |
| 199 master_->DeleteFingerprint(kFingerprint0, false); | 200 master_->DeleteFingerprint(kFingerprint0, false); |
| 200 master_->DeleteFingerprint(kFingerprint1, false); | 201 master_->DeleteFingerprint(kFingerprint1, false); |
| 201 master_->DeleteFingerprint(kFingerprint2, false); | 202 master_->DeleteFingerprint(kFingerprint2, false); |
| 202 master_->DeleteFingerprint(kFingerprint4, false); | 203 master_->DeleteFingerprint(kFingerprint4, false); |
| 203 | 204 |
| 204 EXPECT_EQ(0, master_->used_items_); | 205 EXPECT_EQ(0, master_->used_items_); |
| 205 for (int i = 0; i < kInitialSize; i++) | 206 for (int i = 0; i < kInitialSize; i++) |
| 206 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) << | 207 EXPECT_EQ(0, master_->hash_table_[i]) << "Hash table has values in it."; |
| 207 "Hash table has values in it."; | |
| 208 } | 208 } |
| 209 | 209 |
| 210 // When we delete more than kBigDeleteThreshold we trigger different behavior | 210 // When we delete more than kBigDeleteThreshold we trigger different behavior |
| 211 // where the entire file is rewritten. | 211 // where the entire file is rewritten. |
| 212 TEST_F(VisitedLinkTest, BigDelete) { | 212 TEST_F(VisitedLinkTest, BigDelete) { |
| 213 ASSERT_TRUE(InitHistory()); | 213 ASSERT_TRUE(InitHistory()); |
| 214 ASSERT_TRUE(InitVisited(16381, true)); | 214 ASSERT_TRUE(InitVisited(16381, true)); |
| 215 | 215 |
| 216 // Add the base set of URLs that won't be deleted. | 216 // Add the base set of URLs that won't be deleted. |
| 217 // Reload() will test for these. | 217 // Reload() will test for these. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 233 Reload(); | 233 Reload(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 TEST_F(VisitedLinkTest, DeleteAll) { | 236 TEST_F(VisitedLinkTest, DeleteAll) { |
| 237 ASSERT_TRUE(InitHistory()); | 237 ASSERT_TRUE(InitHistory()); |
| 238 ASSERT_TRUE(InitVisited(0, true)); | 238 ASSERT_TRUE(InitVisited(0, true)); |
| 239 | 239 |
| 240 { | 240 { |
| 241 VisitedLinkSlave slave; | 241 VisitedLinkSlave slave; |
| 242 base::SharedMemoryHandle new_handle = NULL; | 242 base::SharedMemoryHandle new_handle = NULL; |
| 243 master_->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle); | 243 master_->ShareToProcess(GetCurrentProcess(), &new_handle); |
| 244 ASSERT_TRUE(slave.Init(new_handle)); | 244 ASSERT_TRUE(slave.Init(new_handle)); |
| 245 g_slaves.push_back(&slave); | 245 g_slaves.push_back(&slave); |
| 246 | 246 |
| 247 // Add the test URLs. | 247 // Add the test URLs. |
| 248 for (int i = 0; i < g_test_count; i++) { | 248 for (int i = 0; i < g_test_count; i++) { |
| 249 master_->AddURL(TestURL(i)); | 249 master_->AddURL(TestURL(i)); |
| 250 ASSERT_EQ(i + 1, master_->GetUsedCount()); | 250 ASSERT_EQ(i + 1, master_->GetUsedCount()); |
| 251 } | 251 } |
| 252 master_->DebugValidate(); | 252 master_->DebugValidate(); |
| 253 | 253 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 281 // full, notifies its slaves of the change, and updates the disk. | 281 // full, notifies its slaves of the change, and updates the disk. |
| 282 TEST_F(VisitedLinkTest, Resizing) { | 282 TEST_F(VisitedLinkTest, Resizing) { |
| 283 // Create a very small database. | 283 // Create a very small database. |
| 284 const int32 initial_size = 17; | 284 const int32 initial_size = 17; |
| 285 ASSERT_TRUE(InitHistory()); | 285 ASSERT_TRUE(InitHistory()); |
| 286 ASSERT_TRUE(InitVisited(initial_size, true)); | 286 ASSERT_TRUE(InitVisited(initial_size, true)); |
| 287 | 287 |
| 288 // ...and a slave | 288 // ...and a slave |
| 289 VisitedLinkSlave slave; | 289 VisitedLinkSlave slave; |
| 290 base::SharedMemoryHandle new_handle = NULL; | 290 base::SharedMemoryHandle new_handle = NULL; |
| 291 master_->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle); | 291 master_->ShareToProcess(GetCurrentProcess(), &new_handle); |
| 292 bool success = slave.Init(new_handle); | 292 bool success = slave.Init(new_handle); |
| 293 ASSERT_TRUE(success); | 293 ASSERT_TRUE(success); |
| 294 g_slaves.push_back(&slave); | 294 g_slaves.push_back(&slave); |
| 295 | 295 |
| 296 int32 used_count = master_->GetUsedCount(); | 296 int32 used_count = master_->GetUsedCount(); |
| 297 ASSERT_EQ(used_count, 0); | 297 ASSERT_EQ(used_count, 0); |
| 298 | 298 |
| 299 for (int i = 0; i < g_test_count; i++) { | 299 for (int i = 0; i < g_test_count; i++) { |
| 300 master_->AddURL(TestURL(i)); | 300 master_->AddURL(TestURL(i)); |
| 301 used_count = master_->GetUsedCount(); | 301 used_count = master_->GetUsedCount(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 master_->set_rebuild_complete_task(new MessageLoop::QuitTask); | 367 master_->set_rebuild_complete_task(new MessageLoop::QuitTask); |
| 368 MessageLoop::current()->Run(); | 368 MessageLoop::current()->Run(); |
| 369 | 369 |
| 370 // Test that all URLs were written to the database properly. | 370 // Test that all URLs were written to the database properly. |
| 371 Reload(); | 371 Reload(); |
| 372 | 372 |
| 373 // Make sure the extra one was *not* written (Reload won't test this). | 373 // Make sure the extra one was *not* written (Reload won't test this). |
| 374 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); | 374 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); |
| 375 } | 375 } |
| 376 | 376 |
| OLD | NEW |