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/timer.h" | 10 #include "base/timer.h" |
10 #include "chrome/browser/sync/glue/database_model_worker.h" | 11 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" |
11 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 using base::OneShotTimer; | 15 using base::OneShotTimer; |
15 using base::Thread; | 16 using base::Thread; |
16 using base::TimeDelta; | 17 using base::TimeDelta; |
17 using browser_sync::DatabaseModelWorker; | 18 using browser_sync::BrowserThreadModelWorker; |
| 19 using browser_sync::GROUP_DB; |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 class DatabaseModelWorkerTest : public testing::Test { | 23 class BrowserThreadModelWorkerTest : public testing::Test { |
22 public: | 24 public: |
23 DatabaseModelWorkerTest() : | 25 BrowserThreadModelWorkerTest() : |
24 did_do_work_(false), | 26 did_do_work_(false), |
25 db_thread_(BrowserThread::DB), | 27 db_thread_(BrowserThread::DB), |
26 io_thread_(BrowserThread::IO, &io_loop_), | 28 io_thread_(BrowserThread::IO, &io_loop_), |
27 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 29 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
28 | 30 |
29 bool did_do_work() { return did_do_work_; } | 31 bool did_do_work() { return did_do_work_; } |
30 DatabaseModelWorker* worker() { return worker_.get(); } | 32 BrowserThreadModelWorker* worker() { return worker_.get(); } |
31 OneShotTimer<DatabaseModelWorkerTest>* timer() { return &timer_; } | 33 OneShotTimer<BrowserThreadModelWorkerTest>* timer() { return &timer_; } |
32 ScopedRunnableMethodFactory<DatabaseModelWorkerTest>* factory() { | 34 ScopedRunnableMethodFactory<BrowserThreadModelWorkerTest>* factory() { |
33 return &method_factory_; | 35 return &method_factory_; |
34 } | 36 } |
35 | 37 |
36 // Schedule DoWork to be executed on the DB thread and have the test fail if | 38 // Schedule DoWork to be executed on the DB thread and have the test fail if |
37 // DoWork hasn't executed within 10 seconds. | 39 // DoWork hasn't executed within action_timeout_ms() ms. |
38 void ScheduleWork() { | 40 void ScheduleWork() { |
39 scoped_ptr<Callback0::Type> c(NewCallback(this, | 41 scoped_ptr<Callback0::Type> c(NewCallback(this, |
40 &DatabaseModelWorkerTest::DoWork)); | 42 &BrowserThreadModelWorkerTest::DoWork)); |
41 timer()->Start(FROM_HERE, TimeDelta::FromSeconds(10), | 43 timer()->Start( |
42 this, &DatabaseModelWorkerTest::Timeout); | 44 FROM_HERE, |
| 45 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), |
| 46 this, |
| 47 &BrowserThreadModelWorkerTest::Timeout); |
43 worker()->DoWorkAndWaitUntilDone(c.get()); | 48 worker()->DoWorkAndWaitUntilDone(c.get()); |
44 } | 49 } |
45 | 50 |
46 // This is the work that will be scheduled to be done on the DB thread. | 51 // This is the work that will be scheduled to be done on the DB thread. |
47 void DoWork() { | 52 void DoWork() { |
48 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 53 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
49 timer_.Stop(); // Stop the failure timer so the test succeeds. | 54 timer_.Stop(); // Stop the failure timer so the test succeeds. |
50 BrowserThread::PostTask( | 55 BrowserThread::PostTask( |
51 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 56 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
52 did_do_work_ = true; | 57 did_do_work_ = true; |
53 } | 58 } |
54 | 59 |
55 // This will be called by the OneShotTimer and make the test fail unless | 60 // This will be called by the OneShotTimer and make the test fail unless |
56 // DoWork is called first. | 61 // DoWork is called first. |
57 void Timeout() { | 62 void Timeout() { |
58 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; | 63 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; |
59 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
60 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 65 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
61 } | 66 } |
62 | 67 |
63 protected: | 68 protected: |
64 virtual void SetUp() { | 69 virtual void SetUp() { |
65 db_thread_.Start(); | 70 db_thread_.Start(); |
66 worker_ = new DatabaseModelWorker(); | 71 worker_ = new BrowserThreadModelWorker(BrowserThread::DB, GROUP_DB); |
67 } | 72 } |
68 | 73 |
69 virtual void Teardown() { | 74 virtual void Teardown() { |
70 worker_.release(); | 75 worker_.release(); |
71 db_thread_.Stop(); | 76 db_thread_.Stop(); |
72 } | 77 } |
73 | 78 |
74 private: | 79 private: |
75 bool did_do_work_; | 80 bool did_do_work_; |
76 scoped_refptr<DatabaseModelWorker> worker_; | 81 scoped_refptr<BrowserThreadModelWorker> worker_; |
77 OneShotTimer<DatabaseModelWorkerTest> timer_; | 82 OneShotTimer<BrowserThreadModelWorkerTest> timer_; |
78 | 83 |
79 BrowserThread db_thread_; | 84 BrowserThread db_thread_; |
80 MessageLoopForIO io_loop_; | 85 MessageLoopForIO io_loop_; |
81 BrowserThread io_thread_; | 86 BrowserThread io_thread_; |
82 | 87 |
83 ScopedRunnableMethodFactory<DatabaseModelWorkerTest> method_factory_; | 88 ScopedRunnableMethodFactory<BrowserThreadModelWorkerTest> method_factory_; |
84 }; | 89 }; |
85 | 90 |
86 TEST_F(DatabaseModelWorkerTest, DoesWorkOnDatabaseThread) { | 91 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { |
87 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( | 92 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( |
88 &DatabaseModelWorkerTest::ScheduleWork)); | 93 &BrowserThreadModelWorkerTest::ScheduleWork)); |
89 MessageLoop::current()->Run(); | 94 MessageLoop::current()->Run(); |
90 EXPECT_TRUE(did_do_work()); | 95 EXPECT_TRUE(did_do_work()); |
91 } | 96 } |
92 | 97 |
93 } // namespace | 98 } // namespace |
OLD | NEW |