Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/history/history_unittest.cc

Issue 387923002: Make HistoryDBTask not refcounted, and ensure it's destroyed on its origin thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // History unit tests come in two flavors: 5 // History unit tests come in two flavors:
6 // 6 //
7 // 1. The more complicated style is that the unit test creates a full history 7 // 1. The more complicated style is that the unit test creates a full history
8 // service. This spawns a background thread for the history backend, and 8 // service. This spawns a background thread for the history backend, and
9 // all communication is asynchronous. This is useful for testing more 9 // all communication is asynchronous. This is useful for testing more
10 // complicated things or end-to-end behavior. 10 // complicated things or end-to-end behavior.
(...skipping 1484 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 1495
1496 // A HistoryDBTask implementation. Each time RunOnDBThread is invoked 1496 // A HistoryDBTask implementation. Each time RunOnDBThread is invoked
1497 // invoke_count is increment. When invoked kWantInvokeCount times, true is 1497 // invoke_count is increment. When invoked kWantInvokeCount times, true is
1498 // returned from RunOnDBThread which should stop RunOnDBThread from being 1498 // returned from RunOnDBThread which should stop RunOnDBThread from being
1499 // invoked again. When DoneRunOnMainThread is invoked, done_invoked is set to 1499 // invoked again. When DoneRunOnMainThread is invoked, done_invoked is set to
1500 // true. 1500 // true.
1501 class HistoryDBTaskImpl : public HistoryDBTask { 1501 class HistoryDBTaskImpl : public HistoryDBTask {
1502 public: 1502 public:
1503 static const int kWantInvokeCount; 1503 static const int kWantInvokeCount;
1504 1504
1505 HistoryDBTaskImpl() : invoke_count(0), done_invoked(false) {} 1505 HistoryDBTaskImpl(int* invoke_count, bool* done_invoked)
1506 : invoke_count_(invoke_count), done_invoked_(done_invoked) {}
1506 1507
1507 virtual bool RunOnDBThread(HistoryBackend* backend, 1508 virtual bool RunOnDBThread(HistoryBackend* backend,
1508 HistoryDatabase* db) OVERRIDE { 1509 HistoryDatabase* db) OVERRIDE {
1509 return (++invoke_count == kWantInvokeCount); 1510 return (++*invoke_count_ == kWantInvokeCount);
1510 } 1511 }
1511 1512
1512 virtual void DoneRunOnMainThread() OVERRIDE { 1513 virtual void DoneRunOnMainThread() OVERRIDE {
1513 done_invoked = true; 1514 *done_invoked_ = true;
1514 base::MessageLoop::current()->Quit(); 1515 base::MessageLoop::current()->Quit();
1515 } 1516 }
1516 1517
1517 int invoke_count; 1518 int* invoke_count_;
1518 bool done_invoked; 1519 bool* done_invoked_;
1519 1520
1520 private: 1521 private:
1521 virtual ~HistoryDBTaskImpl() {} 1522 virtual ~HistoryDBTaskImpl() {}
1522 1523
1523 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl); 1524 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl);
1524 }; 1525 };
1525 1526
1526 // static 1527 // static
1527 const int HistoryDBTaskImpl::kWantInvokeCount = 2; 1528 const int HistoryDBTaskImpl::kWantInvokeCount = 2;
1528 1529
1529 } // namespace 1530 } // namespace
1530 1531
1531 TEST_F(HistoryTest, HistoryDBTask) { 1532 TEST_F(HistoryTest, HistoryDBTask) {
1532 ASSERT_TRUE(history_service_.get()); 1533 ASSERT_TRUE(history_service_.get());
1533 base::CancelableTaskTracker task_tracker; 1534 base::CancelableTaskTracker task_tracker;
1534 scoped_refptr<HistoryDBTaskImpl> task(new HistoryDBTaskImpl()); 1535 int invoke_count = 0;
1535 history_service_->ScheduleDBTask(task.get(), &task_tracker); 1536 bool done_invoked = false;
1537 history_service_->ScheduleDBTask(
1538 new HistoryDBTaskImpl(&invoke_count, &done_invoked), &task_tracker);
1536 // Run the message loop. When HistoryDBTaskImpl::DoneRunOnMainThread runs, 1539 // Run the message loop. When HistoryDBTaskImpl::DoneRunOnMainThread runs,
1537 // it will stop the message loop. If the test hangs here, it means 1540 // it will stop the message loop. If the test hangs here, it means
1538 // DoneRunOnMainThread isn't being invoked correctly. 1541 // DoneRunOnMainThread isn't being invoked correctly.
1539 base::MessageLoop::current()->Run(); 1542 base::MessageLoop::current()->Run();
1540 CleanupHistoryService(); 1543 CleanupHistoryService();
1541 // WARNING: history has now been deleted. 1544 // WARNING: history has now been deleted.
1542 history_service_.reset(); 1545 history_service_.reset();
1543 ASSERT_EQ(HistoryDBTaskImpl::kWantInvokeCount, task->invoke_count); 1546 ASSERT_EQ(HistoryDBTaskImpl::kWantInvokeCount, invoke_count);
1544 ASSERT_TRUE(task->done_invoked); 1547 ASSERT_TRUE(done_invoked);
1545 } 1548 }
1546 1549
1547 TEST_F(HistoryTest, HistoryDBTaskCanceled) { 1550 TEST_F(HistoryTest, HistoryDBTaskCanceled) {
1548 ASSERT_TRUE(history_service_.get()); 1551 ASSERT_TRUE(history_service_.get());
1549 base::CancelableTaskTracker task_tracker; 1552 base::CancelableTaskTracker task_tracker;
1550 scoped_refptr<HistoryDBTaskImpl> task(new HistoryDBTaskImpl()); 1553 int invoke_count = 0;
1551 history_service_->ScheduleDBTask(task.get(), &task_tracker); 1554 bool done_invoked = false;
1555 history_service_->ScheduleDBTask(
1556 new HistoryDBTaskImpl(&invoke_count, &done_invoked), &task_tracker);
1552 task_tracker.TryCancelAll(); 1557 task_tracker.TryCancelAll();
1553 CleanupHistoryService(); 1558 CleanupHistoryService();
1554 // WARNING: history has now been deleted. 1559 // WARNING: history has now been deleted.
1555 history_service_.reset(); 1560 history_service_.reset();
1556 ASSERT_FALSE(task->done_invoked); 1561 ASSERT_FALSE(done_invoked);
1557 } 1562 }
1558 1563
1559 // Create a local delete directive and process it while sync is 1564 // Create a local delete directive and process it while sync is
1560 // online, and then when offline. The delete directive should be sent to sync, 1565 // online, and then when offline. The delete directive should be sent to sync,
1561 // no error should be returned for the first time, and an error should be 1566 // no error should be returned for the first time, and an error should be
1562 // returned for the second time. 1567 // returned for the second time.
1563 TEST_F(HistoryTest, ProcessLocalDeleteDirectiveSyncOnline) { 1568 TEST_F(HistoryTest, ProcessLocalDeleteDirectiveSyncOnline) {
1564 ASSERT_TRUE(history_service_.get()); 1569 ASSERT_TRUE(history_service_.get());
1565 1570
1566 const GURL test_url("http://www.google.com/"); 1571 const GURL test_url("http://www.google.com/");
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
1859 std::vector<PageUsageData*> results; 1864 std::vector<PageUsageData*> results;
1860 db_->QuerySegmentUsage(segment_time, 10, &results); 1865 db_->QuerySegmentUsage(segment_time, 10, &results);
1861 ASSERT_EQ(1u, results.size()); 1866 ASSERT_EQ(1u, results.size());
1862 EXPECT_EQ(url, results[0]->GetURL()); 1867 EXPECT_EQ(url, results[0]->GetURL());
1863 EXPECT_EQ(segment_id, results[0]->GetID()); 1868 EXPECT_EQ(segment_id, results[0]->GetID());
1864 EXPECT_EQ(title, results[0]->GetTitle()); 1869 EXPECT_EQ(title, results[0]->GetTitle());
1865 STLDeleteElements(&results); 1870 STLDeleteElements(&results);
1866 } 1871 }
1867 1872
1868 } // namespace history 1873 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698