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