| 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/location.h" | 7 #include "base/location.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 16 #include "components/sync_driver/glue/browser_thread_model_worker.h" | 16 #include "components/sync_driver/glue/browser_thread_model_worker.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using base::OneShotTimer; | |
| 22 using base::Thread; | 21 using base::Thread; |
| 23 using base::TimeDelta; | 22 using base::TimeDelta; |
| 24 using content::BrowserThread; | 23 using content::BrowserThread; |
| 25 | 24 |
| 26 namespace browser_sync { | 25 namespace browser_sync { |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 class SyncBrowserThreadModelWorkerTest : public testing::Test { | 29 class SyncBrowserThreadModelWorkerTest : public testing::Test { |
| 31 public: | 30 public: |
| 32 SyncBrowserThreadModelWorkerTest() : | 31 SyncBrowserThreadModelWorkerTest() : |
| 33 did_do_work_(false), | 32 did_do_work_(false), |
| 34 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | | 33 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP | |
| 35 content::TestBrowserThreadBundle::REAL_DB_THREAD), | 34 content::TestBrowserThreadBundle::REAL_DB_THREAD), |
| 36 weak_factory_(this) {} | 35 weak_factory_(this) {} |
| 37 | 36 |
| 38 bool did_do_work() { return did_do_work_; } | 37 bool did_do_work() { return did_do_work_; } |
| 39 BrowserThreadModelWorker* worker() { return worker_.get(); } | 38 BrowserThreadModelWorker* worker() { return worker_.get(); } |
| 40 OneShotTimer<SyncBrowserThreadModelWorkerTest>* timer() { return &timer_; } | 39 base::OneShotTimer* timer() { return &timer_; } |
| 41 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() { | 40 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() { |
| 42 return &weak_factory_; | 41 return &weak_factory_; |
| 43 } | 42 } |
| 44 | 43 |
| 45 // Schedule DoWork to be executed on the DB thread and have the test fail if | 44 // Schedule DoWork to be executed on the DB thread and have the test fail if |
| 46 // DoWork hasn't executed within action_timeout(). | 45 // DoWork hasn't executed within action_timeout(). |
| 47 void ScheduleWork() { | 46 void ScheduleWork() { |
| 48 // We wait until the callback is done. So it is safe to use unretained. | 47 // We wait until the callback is done. So it is safe to use unretained. |
| 49 syncer::WorkCallback c = | 48 syncer::WorkCallback c = |
| 50 base::Bind(&SyncBrowserThreadModelWorkerTest::DoWork, | 49 base::Bind(&SyncBrowserThreadModelWorkerTest::DoWork, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 syncer::GROUP_DB, NULL); | 81 syncer::GROUP_DB, NULL); |
| 83 } | 82 } |
| 84 | 83 |
| 85 virtual void Teardown() { | 84 virtual void Teardown() { |
| 86 worker_ = NULL; | 85 worker_ = NULL; |
| 87 } | 86 } |
| 88 | 87 |
| 89 private: | 88 private: |
| 90 bool did_do_work_; | 89 bool did_do_work_; |
| 91 scoped_refptr<BrowserThreadModelWorker> worker_; | 90 scoped_refptr<BrowserThreadModelWorker> worker_; |
| 92 OneShotTimer<SyncBrowserThreadModelWorkerTest> timer_; | 91 base::OneShotTimer timer_; |
| 93 | 92 |
| 94 content::TestBrowserThreadBundle thread_bundle_; | 93 content::TestBrowserThreadBundle thread_bundle_; |
| 95 | 94 |
| 96 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_; | 95 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_; |
| 97 }; | 96 }; |
| 98 | 97 |
| 99 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | 98 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { |
| 100 base::ThreadTaskRunnerHandle::Get()->PostTask( | 99 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 101 FROM_HERE, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork, | 100 FROM_HERE, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork, |
| 102 factory()->GetWeakPtr())); | 101 factory()->GetWeakPtr())); |
| 103 base::MessageLoop::current()->Run(); | 102 base::MessageLoop::current()->Run(); |
| 104 EXPECT_TRUE(did_do_work()); | 103 EXPECT_TRUE(did_do_work()); |
| 105 } | 104 } |
| 106 | 105 |
| 107 } // namespace | 106 } // namespace |
| 108 | 107 |
| 109 } // namespace browser_sync | 108 } // namespace browser_sync |
| OLD | NEW |