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