| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/history_backend.h" | 5 #include "chrome/browser/history/history_backend.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 private: | 131 private: |
| 132 // Not owned by us. | 132 // Not owned by us. |
| 133 HistoryBackendTestBase* test_; | 133 HistoryBackendTestBase* test_; |
| 134 | 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestDelegate); | 135 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestDelegate); |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 class HistoryBackendTestBase : public testing::Test { | 138 class HistoryBackendTestBase : public testing::Test { |
| 139 public: | 139 public: |
| 140 typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; | 140 typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; |
| 141 typedef std::vector<std::pair<ui::PageTransition, URLRow>> URLVisitedList; |
| 141 | 142 |
| 142 HistoryBackendTestBase() | 143 HistoryBackendTestBase() |
| 143 : loaded_(false), | 144 : loaded_(false), |
| 144 favicon_changed_notifications_(0), | 145 favicon_changed_notifications_(0), |
| 145 ui_thread_(content::BrowserThread::UI, &message_loop_) {} | 146 ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
| 146 | 147 |
| 147 virtual ~HistoryBackendTestBase() { | 148 virtual ~HistoryBackendTestBase() { |
| 148 STLDeleteValues(&broadcasted_notifications_); | 149 STLDeleteValues(&broadcasted_notifications_); |
| 149 } | 150 } |
| 150 | 151 |
| 151 protected: | 152 protected: |
| 152 int favicon_changed_notifications() const { | 153 int favicon_changed_notifications() const { |
| 153 return favicon_changed_notifications_; | 154 return favicon_changed_notifications_; |
| 154 } | 155 } |
| 155 | 156 |
| 156 void ClearFaviconChangedNotificationCounter() { | 157 void ClearFaviconChangedNotificationCounter() { |
| 157 favicon_changed_notifications_ = 0; | 158 favicon_changed_notifications_ = 0; |
| 158 } | 159 } |
| 159 | 160 |
| 161 int num_url_visited_notifications() const { |
| 162 return url_visited_notifications_.size(); |
| 163 } |
| 164 |
| 165 const URLVisitedList& url_visited_notifications() const { |
| 166 return url_visited_notifications_; |
| 167 } |
| 168 |
| 160 int num_broadcasted_notifications() const { | 169 int num_broadcasted_notifications() const { |
| 161 return broadcasted_notifications_.size(); | 170 return broadcasted_notifications_.size(); |
| 162 } | 171 } |
| 163 | 172 |
| 164 const NotificationList& broadcasted_notifications() const { | 173 const NotificationList& broadcasted_notifications() const { |
| 165 return broadcasted_notifications_; | 174 return broadcasted_notifications_; |
| 166 } | 175 } |
| 167 | 176 |
| 168 void ClearBroadcastedNotifications() { | 177 void ClearBroadcastedNotifications() { |
| 178 url_visited_notifications_.clear(); |
| 169 STLDeleteValues(&broadcasted_notifications_); | 179 STLDeleteValues(&broadcasted_notifications_); |
| 170 } | 180 } |
| 171 | 181 |
| 172 base::FilePath test_dir() { | 182 base::FilePath test_dir() { |
| 173 return test_dir_; | 183 return test_dir_; |
| 174 } | 184 } |
| 175 | 185 |
| 176 void NotifyFaviconChanged(const std::set<GURL>& changed_favicons) { | 186 void NotifyFaviconChanged(const std::set<GURL>& changed_favicons) { |
| 177 ++favicon_changed_notifications_; | 187 ++favicon_changed_notifications_; |
| 178 } | 188 } |
| 179 | 189 |
| 190 void NotifyURLVisited(ui::PageTransition transition, |
| 191 const URLRow& row, |
| 192 const RedirectList& redirects, |
| 193 base::Time visit_time) { |
| 194 url_visited_notifications_.push_back(std::make_pair(transition, row)); |
| 195 } |
| 196 |
| 180 void BroadcastNotifications(int type, scoped_ptr<HistoryDetails> details) { | 197 void BroadcastNotifications(int type, scoped_ptr<HistoryDetails> details) { |
| 181 // Send the notifications directly to the in-memory database. | 198 // Send the notifications directly to the in-memory database. |
| 182 content::Details<HistoryDetails> det(details.get()); | 199 content::Details<HistoryDetails> det(details.get()); |
| 183 mem_backend_->Observe( | 200 mem_backend_->Observe( |
| 184 type, content::Source<HistoryBackendTestBase>(NULL), det); | 201 type, content::Source<HistoryBackendTestBase>(NULL), det); |
| 185 | 202 |
| 186 // The backend passes ownership of the details pointer to us. | 203 // The backend passes ownership of the details pointer to us. |
| 187 broadcasted_notifications_.push_back( | 204 broadcasted_notifications_.push_back( |
| 188 std::make_pair(type, details.release())); | 205 std::make_pair(type, details.release())); |
| 189 } | 206 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 217 history_client_.ClearAllBookmarks(); | 234 history_client_.ClearAllBookmarks(); |
| 218 } | 235 } |
| 219 | 236 |
| 220 void SetInMemoryBackend(scoped_ptr<InMemoryHistoryBackend> backend) { | 237 void SetInMemoryBackend(scoped_ptr<InMemoryHistoryBackend> backend) { |
| 221 mem_backend_.swap(backend); | 238 mem_backend_.swap(backend); |
| 222 } | 239 } |
| 223 | 240 |
| 224 // The types and details of notifications which were broadcasted. | 241 // The types and details of notifications which were broadcasted. |
| 225 NotificationList broadcasted_notifications_; | 242 NotificationList broadcasted_notifications_; |
| 226 int favicon_changed_notifications_; | 243 int favicon_changed_notifications_; |
| 244 URLVisitedList url_visited_notifications_; |
| 227 | 245 |
| 228 base::MessageLoop message_loop_; | 246 base::MessageLoop message_loop_; |
| 229 base::FilePath test_dir_; | 247 base::FilePath test_dir_; |
| 230 content::TestBrowserThread ui_thread_; | 248 content::TestBrowserThread ui_thread_; |
| 231 | 249 |
| 232 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestBase); | 250 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestBase); |
| 233 }; | 251 }; |
| 234 | 252 |
| 235 void HistoryBackendTestDelegate::SetInMemoryBackend( | 253 void HistoryBackendTestDelegate::SetInMemoryBackend( |
| 236 scoped_ptr<InMemoryHistoryBackend> backend) { | 254 scoped_ptr<InMemoryHistoryBackend> backend) { |
| 237 test_->SetInMemoryBackend(backend.Pass()); | 255 test_->SetInMemoryBackend(backend.Pass()); |
| 238 } | 256 } |
| 239 | 257 |
| 240 void HistoryBackendTestDelegate::NotifyFaviconChanged( | 258 void HistoryBackendTestDelegate::NotifyFaviconChanged( |
| 241 const std::set<GURL>& changed_favicons) { | 259 const std::set<GURL>& changed_favicons) { |
| 242 test_->NotifyFaviconChanged(changed_favicons); | 260 test_->NotifyFaviconChanged(changed_favicons); |
| 243 } | 261 } |
| 244 | 262 |
| 245 void HistoryBackendTestDelegate::NotifyURLVisited(ui::PageTransition transition, | 263 void HistoryBackendTestDelegate::NotifyURLVisited(ui::PageTransition transition, |
| 246 const URLRow& row, | 264 const URLRow& row, |
| 247 const RedirectList& redirects, | 265 const RedirectList& redirects, |
| 248 base::Time visit_time) { | 266 base::Time visit_time) { |
| 249 scoped_ptr<URLVisitedDetails> details(new URLVisitedDetails()); | 267 test_->NotifyURLVisited(transition, row, redirects, visit_time); |
| 250 details->transition = transition; | |
| 251 details->row = row; | |
| 252 details->redirects = redirects; | |
| 253 details->visit_time = visit_time; | |
| 254 test_->BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URL_VISITED, | |
| 255 details.Pass()); | |
| 256 } | 268 } |
| 257 | 269 |
| 258 void HistoryBackendTestDelegate::BroadcastNotifications( | 270 void HistoryBackendTestDelegate::BroadcastNotifications( |
| 259 int type, | 271 int type, |
| 260 scoped_ptr<HistoryDetails> details) { | 272 scoped_ptr<HistoryDetails> details) { |
| 261 test_->BroadcastNotifications(type, details.Pass()); | 273 test_->BroadcastNotifications(type, details.Pass()); |
| 262 } | 274 } |
| 263 | 275 |
| 264 void HistoryBackendTestDelegate::DBLoaded() { | 276 void HistoryBackendTestDelegate::DBLoaded() { |
| 265 test_->loaded_ = true; | 277 test_->loaded_ = true; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 const URLRow* row3 = NULL) { | 442 const URLRow* row3 = NULL) { |
| 431 URLRows rows; | 443 URLRows rows; |
| 432 rows.push_back(*row1); | 444 rows.push_back(*row1); |
| 433 if (row2) rows.push_back(*row2); | 445 if (row2) rows.push_back(*row2); |
| 434 if (row3) rows.push_back(*row3); | 446 if (row3) rows.push_back(*row3); |
| 435 | 447 |
| 436 if (type == chrome::NOTIFICATION_HISTORY_URLS_MODIFIED) { | 448 if (type == chrome::NOTIFICATION_HISTORY_URLS_MODIFIED) { |
| 437 scoped_ptr<URLsModifiedDetails> details(new URLsModifiedDetails()); | 449 scoped_ptr<URLsModifiedDetails> details(new URLsModifiedDetails()); |
| 438 details->changed_urls.swap(rows); | 450 details->changed_urls.swap(rows); |
| 439 BroadcastNotifications(type, details.PassAs<HistoryDetails>()); | 451 BroadcastNotifications(type, details.PassAs<HistoryDetails>()); |
| 440 } else if (type == chrome::NOTIFICATION_HISTORY_URL_VISITED) { | |
| 441 for (URLRows::const_iterator it = rows.begin(); it != rows.end(); ++it) { | |
| 442 scoped_ptr<URLVisitedDetails> details(new URLVisitedDetails()); | |
| 443 details->row = *it; | |
| 444 BroadcastNotifications(type, details.PassAs<HistoryDetails>()); | |
| 445 } | |
| 446 } else if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { | 452 } else if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { |
| 447 scoped_ptr<URLsDeletedDetails> details(new URLsDeletedDetails()); | 453 scoped_ptr<URLsDeletedDetails> details(new URLsDeletedDetails()); |
| 448 details->rows = rows; | 454 details->rows = rows; |
| 449 BroadcastNotifications(type, details.PassAs<HistoryDetails>()); | 455 BroadcastNotifications(type, details.PassAs<HistoryDetails>()); |
| 450 } else { | 456 } else { |
| 451 NOTREACHED(); | 457 NOTREACHED(); |
| 452 } | 458 } |
| 453 } | 459 } |
| 454 | 460 |
| 455 protected: | 461 protected: |
| (...skipping 811 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1267 for (int i = 0; i < 2; ++i) { | 1273 for (int i = 0; i < 2; ++i) { |
| 1268 backend_->AddPageVisit(url2, base::Time::Now(), 0, | 1274 backend_->AddPageVisit(url2, base::Time::Now(), 0, |
| 1269 ui::PAGE_TRANSITION_TYPED, | 1275 ui::PAGE_TRANSITION_TYPED, |
| 1270 history::SOURCE_BROWSED); | 1276 history::SOURCE_BROWSED); |
| 1271 } | 1277 } |
| 1272 | 1278 |
| 1273 URLRow stored_row1, stored_row2; | 1279 URLRow stored_row1, stored_row2; |
| 1274 EXPECT_NE(0, backend_->db_->GetRowForURL(url1, &stored_row1)); | 1280 EXPECT_NE(0, backend_->db_->GetRowForURL(url1, &stored_row1)); |
| 1275 EXPECT_NE(0, backend_->db_->GetRowForURL(url2, &stored_row2)); | 1281 EXPECT_NE(0, backend_->db_->GetRowForURL(url2, &stored_row2)); |
| 1276 | 1282 |
| 1277 // Expect that NOTIFICATION_HISTORY_URLS_VISITED has been fired 3x, and that | 1283 // Expect that HistoryServiceObserver::OnURLVisited has been called 3 times, |
| 1278 // each time, the URLRows have the correct URLs and IDs set. | 1284 // and that each time the URLRows have the correct URLs and IDs set. |
| 1279 ASSERT_EQ(3, num_broadcasted_notifications()); | 1285 ASSERT_EQ(3, num_url_visited_notifications()); |
| 1280 ASSERT_EQ(chrome::NOTIFICATION_HISTORY_URL_VISITED, | 1286 EXPECT_TRUE(ui::PageTransitionCoreTypeIs(url_visited_notifications()[0].first, |
| 1281 broadcasted_notifications()[0].first); | 1287 ui::PAGE_TRANSITION_LINK)); |
| 1282 const URLVisitedDetails* details = static_cast<const URLVisitedDetails*>( | 1288 EXPECT_EQ(stored_row1.id(), url_visited_notifications()[0].second.id()); |
| 1283 broadcasted_notifications()[0].second); | 1289 EXPECT_EQ(stored_row1.url(), url_visited_notifications()[0].second.url()); |
| 1284 EXPECT_EQ(ui::PAGE_TRANSITION_LINK, | |
| 1285 ui::PageTransitionStripQualifier(details->transition)); | |
| 1286 EXPECT_EQ(stored_row1.id(), details->row.id()); | |
| 1287 EXPECT_EQ(stored_row1.url(), details->row.url()); | |
| 1288 | 1290 |
| 1289 // No further checking, this case analogous to the first one. | 1291 EXPECT_TRUE(ui::PageTransitionCoreTypeIs(url_visited_notifications()[1].first, |
| 1290 ASSERT_EQ(chrome::NOTIFICATION_HISTORY_URL_VISITED, | 1292 ui::PAGE_TRANSITION_TYPED)); |
| 1291 broadcasted_notifications()[1].first); | 1293 EXPECT_EQ(stored_row2.id(), url_visited_notifications()[1].second.id()); |
| 1294 EXPECT_EQ(stored_row2.url(), url_visited_notifications()[1].second.url()); |
| 1292 | 1295 |
| 1293 ASSERT_EQ(chrome::NOTIFICATION_HISTORY_URL_VISITED, | 1296 EXPECT_TRUE(ui::PageTransitionCoreTypeIs(url_visited_notifications()[2].first, |
| 1294 broadcasted_notifications()[2].first); | 1297 ui::PAGE_TRANSITION_TYPED)); |
| 1295 details = static_cast<const URLVisitedDetails*>( | 1298 EXPECT_EQ(stored_row2.id(), url_visited_notifications()[2].second.id()); |
| 1296 broadcasted_notifications()[2].second); | 1299 EXPECT_EQ(stored_row2.url(), url_visited_notifications()[2].second.url()); |
| 1297 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED, | |
| 1298 ui::PageTransitionStripQualifier(details->transition)); | |
| 1299 EXPECT_EQ(stored_row2.id(), details->row.id()); | |
| 1300 EXPECT_EQ(stored_row2.url(), details->row.url()); | |
| 1301 } | 1300 } |
| 1302 | 1301 |
| 1303 TEST_F(HistoryBackendTest, AddPageArgsSource) { | 1302 TEST_F(HistoryBackendTest, AddPageArgsSource) { |
| 1304 ASSERT_TRUE(backend_.get()); | 1303 ASSERT_TRUE(backend_.get()); |
| 1305 | 1304 |
| 1306 GURL url("http://testpageargs.com"); | 1305 GURL url("http://testpageargs.com"); |
| 1307 | 1306 |
| 1308 // Assume this page is browsed by user. | 1307 // Assume this page is browsed by user. |
| 1309 HistoryAddPageArgs request1(url, base::Time::Now(), NULL, 0, GURL(), | 1308 HistoryAddPageArgs request1(url, base::Time::Now(), NULL, 0, GURL(), |
| 1310 history::RedirectList(), | 1309 history::RedirectList(), |
| (...skipping 1952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3263 // Verify that the second term is no longer returned as result, and also check | 3262 // Verify that the second term is no longer returned as result, and also check |
| 3264 // at the low level that it is gone for good. The term corresponding to the | 3263 // at the low level that it is gone for good. The term corresponding to the |
| 3265 // first URLRow should not be affected. | 3264 // first URLRow should not be affected. |
| 3266 EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1)); | 3265 EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1)); |
| 3267 EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2)); | 3266 EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2)); |
| 3268 EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL)); | 3267 EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL)); |
| 3269 EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL)); | 3268 EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL)); |
| 3270 } | 3269 } |
| 3271 | 3270 |
| 3272 } // namespace history | 3271 } // namespace history |
| OLD | NEW |