| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/callback.h" | 6 #include "base/callback.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| 11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 12 #include "base/timer.h" | 12 #include "base/timer.h" |
| 13 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" | 13 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using base::OneShotTimer; | 17 using base::OneShotTimer; |
| 18 using base::Thread; | 18 using base::Thread; |
| 19 using base::TimeDelta; | 19 using base::TimeDelta; |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace browser_sync { | 22 namespace browser_sync { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 class SyncBrowserThreadModelWorkerTest : public testing::Test { | 26 class SyncBrowserThreadModelWorkerTest : public testing::Test { |
| 27 public: | 27 public: |
| 28 SyncBrowserThreadModelWorkerTest() : | 28 SyncBrowserThreadModelWorkerTest() : |
| 29 did_do_work_(false), | 29 did_do_work_(false), |
| 30 db_thread_(BrowserThread::DB), | 30 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | |
| 31 io_thread_(BrowserThread::IO, &io_loop_), | 31 content::TestBrowserThreadBundle::REAL_DB_THREAD), |
| 32 weak_factory_(this) {} | 32 weak_factory_(this) {} |
| 33 | 33 |
| 34 bool did_do_work() { return did_do_work_; } | 34 bool did_do_work() { return did_do_work_; } |
| 35 BrowserThreadModelWorker* worker() { return worker_.get(); } | 35 BrowserThreadModelWorker* worker() { return worker_.get(); } |
| 36 OneShotTimer<SyncBrowserThreadModelWorkerTest>* timer() { return &timer_; } | 36 OneShotTimer<SyncBrowserThreadModelWorkerTest>* timer() { return &timer_; } |
| 37 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() { | 37 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() { |
| 38 return &weak_factory_; | 38 return &weak_factory_; |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Schedule DoWork to be executed on the DB thread and have the test fail if | 41 // Schedule DoWork to be executed on the DB thread and have the test fail if |
| (...skipping 24 matching lines...) Expand all Loading... |
| 66 // This will be called by the OneShotTimer and make the test fail unless | 66 // This will be called by the OneShotTimer and make the test fail unless |
| 67 // DoWork is called first. | 67 // DoWork is called first. |
| 68 void Timeout() { | 68 void Timeout() { |
| 69 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; | 69 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; |
| 70 BrowserThread::PostTask( | 70 BrowserThread::PostTask( |
| 71 BrowserThread::IO, FROM_HERE, base::MessageLoop::QuitClosure()); | 71 BrowserThread::IO, FROM_HERE, base::MessageLoop::QuitClosure()); |
| 72 } | 72 } |
| 73 | 73 |
| 74 protected: | 74 protected: |
| 75 virtual void SetUp() { | 75 virtual void SetUp() { |
| 76 db_thread_.Start(); | |
| 77 worker_ = new DatabaseModelWorker(NULL); | 76 worker_ = new DatabaseModelWorker(NULL); |
| 78 } | 77 } |
| 79 | 78 |
| 80 virtual void Teardown() { | 79 virtual void Teardown() { |
| 81 worker_ = NULL; | 80 worker_ = NULL; |
| 82 db_thread_.Stop(); | |
| 83 } | 81 } |
| 84 | 82 |
| 85 private: | 83 private: |
| 86 bool did_do_work_; | 84 bool did_do_work_; |
| 87 scoped_refptr<BrowserThreadModelWorker> worker_; | 85 scoped_refptr<BrowserThreadModelWorker> worker_; |
| 88 OneShotTimer<SyncBrowserThreadModelWorkerTest> timer_; | 86 OneShotTimer<SyncBrowserThreadModelWorkerTest> timer_; |
| 89 | 87 |
| 90 content::TestBrowserThread db_thread_; | 88 content::TestBrowserThreadBundle thread_bundle_; |
| 91 base::MessageLoopForIO io_loop_; | |
| 92 content::TestBrowserThread io_thread_; | |
| 93 | 89 |
| 94 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_; | 90 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_; |
| 95 }; | 91 }; |
| 96 | 92 |
| 97 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | 93 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { |
| 98 base::MessageLoop::current()->PostTask(FROM_HERE, | 94 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 99 base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork, | 95 base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork, |
| 100 factory()->GetWeakPtr())); | 96 factory()->GetWeakPtr())); |
| 101 base::MessageLoop::current()->Run(); | 97 base::MessageLoop::current()->Run(); |
| 102 EXPECT_TRUE(did_do_work()); | 98 EXPECT_TRUE(did_do_work()); |
| 103 } | 99 } |
| 104 | 100 |
| 105 } // namespace | 101 } // namespace |
| 106 | 102 |
| 107 } // namespace browser_sync | 103 } // namespace browser_sync |
| OLD | NEW |