| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
| 9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/timer.h" | 10 #include "base/timer.h" |
| 11 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" | 11 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" |
| 12 #include "chrome/browser/sync/sessions/unrecoverable_error_info.h" |
| 12 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using base::OneShotTimer; | 16 using base::OneShotTimer; |
| 16 using base::Thread; | 17 using base::Thread; |
| 17 using base::TimeDelta; | 18 using base::TimeDelta; |
| 18 | 19 |
| 19 namespace browser_sync { | 20 namespace browser_sync { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 class BrowserThreadModelWorkerTest : public testing::Test { | 24 class BrowserThreadModelWorkerTest : public testing::Test { |
| 24 public: | 25 public: |
| 25 BrowserThreadModelWorkerTest() : | 26 BrowserThreadModelWorkerTest() : |
| 26 did_do_work_(false), | 27 did_do_work_(false), |
| 27 db_thread_(BrowserThread::DB), | 28 db_thread_(BrowserThread::DB), |
| 28 io_thread_(BrowserThread::IO, &io_loop_), | 29 io_thread_(BrowserThread::IO, &io_loop_), |
| 29 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 30 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
| 30 | 31 |
| 31 bool did_do_work() { return did_do_work_; } | 32 bool did_do_work() { return did_do_work_; } |
| 32 BrowserThreadModelWorker* worker() { return worker_.get(); } | 33 BrowserThreadModelWorker* worker() { return worker_.get(); } |
| 33 OneShotTimer<BrowserThreadModelWorkerTest>* timer() { return &timer_; } | 34 OneShotTimer<BrowserThreadModelWorkerTest>* timer() { return &timer_; } |
| 34 ScopedRunnableMethodFactory<BrowserThreadModelWorkerTest>* factory() { | 35 ScopedRunnableMethodFactory<BrowserThreadModelWorkerTest>* factory() { |
| 35 return &method_factory_; | 36 return &method_factory_; |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Schedule DoWork to be executed on the DB thread and have the test fail if | 39 // Schedule DoWork to be executed on the DB thread and have the test fail if |
| 39 // DoWork hasn't executed within action_timeout_ms() ms. | 40 // DoWork hasn't executed within action_timeout_ms() ms. |
| 40 void ScheduleWork() { | 41 void ScheduleWork() { |
| 41 scoped_ptr<Callback0::Type> c(NewCallback(this, | 42 scoped_ptr<Callback1<sessions::UnrecoverableErrorInfo*>::Type> c( |
| 43 NewCallback(this, |
| 42 &BrowserThreadModelWorkerTest::DoWork)); | 44 &BrowserThreadModelWorkerTest::DoWork)); |
| 43 timer()->Start( | 45 timer()->Start( |
| 44 FROM_HERE, | 46 FROM_HERE, |
| 45 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), | 47 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), |
| 46 this, | 48 this, |
| 47 &BrowserThreadModelWorkerTest::Timeout); | 49 &BrowserThreadModelWorkerTest::Timeout); |
| 48 worker()->DoWorkAndWaitUntilDone(c.get()); | 50 worker()->DoWorkAndWaitUntilDone(c.get()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 // This is the work that will be scheduled to be done on the DB thread. | 53 // This is the work that will be scheduled to be done on the DB thread. |
| 52 void DoWork() { | 54 void DoWork(sessions::UnrecoverableErrorInfo* error_info) { |
| 53 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 55 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 54 timer_.Stop(); // Stop the failure timer so the test succeeds. | 56 timer_.Stop(); // Stop the failure timer so the test succeeds. |
| 55 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
| 56 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 58 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
| 57 did_do_work_ = true; | 59 did_do_work_ = true; |
| 58 } | 60 } |
| 59 | 61 |
| 60 // This will be called by the OneShotTimer and make the test fail unless | 62 // This will be called by the OneShotTimer and make the test fail unless |
| 61 // DoWork is called first. | 63 // DoWork is called first. |
| 62 void Timeout() { | 64 void Timeout() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 91 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | 93 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { |
| 92 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( | 94 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( |
| 93 &BrowserThreadModelWorkerTest::ScheduleWork)); | 95 &BrowserThreadModelWorkerTest::ScheduleWork)); |
| 94 MessageLoop::current()->Run(); | 96 MessageLoop::current()->Run(); |
| 95 EXPECT_TRUE(did_do_work()); | 97 EXPECT_TRUE(did_do_work()); |
| 96 } | 98 } |
| 97 | 99 |
| 98 } // namespace | 100 } // namespace |
| 99 | 101 |
| 100 } // namespace browser_sync | 102 } // namespace browser_sync |
| OLD | NEW |