| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/sync_driver/glue/browser_thread_model_worker.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/run_loop.h" | |
| 13 #include "base/single_thread_task_runner.h" | |
| 14 #include "base/test/test_timeouts.h" | |
| 15 #include "base/threading/thread.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/timer/timer.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using base::Thread; | |
| 21 using base::TimeDelta; | |
| 22 | |
| 23 namespace browser_sync { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class SyncBrowserThreadModelWorkerTest : public testing::Test { | |
| 28 public: | |
| 29 SyncBrowserThreadModelWorkerTest() | |
| 30 : db_thread_("DB_Thread"), did_do_work_(false), weak_factory_(this) {} | |
| 31 | |
| 32 bool did_do_work() { return did_do_work_; } | |
| 33 BrowserThreadModelWorker* worker() { return worker_.get(); } | |
| 34 base::OneShotTimer* timer() { return &timer_; } | |
| 35 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() { | |
| 36 return &weak_factory_; | |
| 37 } | |
| 38 | |
| 39 // Schedule DoWork to be executed on the DB thread and have the test fail if | |
| 40 // DoWork hasn't executed within action_timeout(). | |
| 41 void ScheduleWork() { | |
| 42 // We wait until the callback is done. So it is safe to use unretained. | |
| 43 syncer::WorkCallback c = base::Bind( | |
| 44 &SyncBrowserThreadModelWorkerTest::DoWork, base::Unretained(this)); | |
| 45 timer()->Start(FROM_HERE, TestTimeouts::action_timeout(), this, | |
| 46 &SyncBrowserThreadModelWorkerTest::Timeout); | |
| 47 worker()->DoWorkAndWaitUntilDone(c); | |
| 48 } | |
| 49 | |
| 50 // This is the work that will be scheduled to be done on the DB thread. | |
| 51 syncer::SyncerError DoWork() { | |
| 52 EXPECT_TRUE(db_thread_.task_runner()->BelongsToCurrentThread()); | |
| 53 timer_.Stop(); // Stop the failure timer so the test succeeds. | |
| 54 main_message_loop_.task_runner()->PostTask( | |
| 55 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
| 56 did_do_work_ = true; | |
| 57 return syncer::SYNCER_OK; | |
| 58 } | |
| 59 | |
| 60 // This will be called by the OneShotTimer and make the test fail unless | |
| 61 // DoWork is called first. | |
| 62 void Timeout() { | |
| 63 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; | |
| 64 main_message_loop_.task_runner()->PostTask( | |
| 65 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 void SetUp() override { | |
| 70 db_thread_.Start(); | |
| 71 worker_ = new BrowserThreadModelWorker(db_thread_.task_runner(), | |
| 72 syncer::GROUP_DB, NULL); | |
| 73 } | |
| 74 | |
| 75 virtual void Teardown() { | |
| 76 worker_ = NULL; | |
| 77 db_thread_.Stop(); | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 base::MessageLoop main_message_loop_; | |
| 82 Thread db_thread_; | |
| 83 bool did_do_work_; | |
| 84 scoped_refptr<BrowserThreadModelWorker> worker_; | |
| 85 base::OneShotTimer timer_; | |
| 86 | |
| 87 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_; | |
| 88 }; | |
| 89 | |
| 90 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | |
| 91 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 92 FROM_HERE, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork, | |
| 93 factory()->GetWeakPtr())); | |
| 94 base::RunLoop().Run(); | |
| 95 EXPECT_TRUE(did_do_work()); | |
| 96 } | |
| 97 | |
| 98 } // namespace | |
| 99 | |
| 100 } // namespace browser_sync | |
| OLD | NEW |