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 "components/sync_driver/glue/browser_thread_model_worker.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::OneShotTimer
;
23 using base::TimeDelta
;
24 using content::BrowserThread
;
26 namespace browser_sync
{
30 class SyncBrowserThreadModelWorkerTest
: public testing::Test
{
32 SyncBrowserThreadModelWorkerTest() :
34 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
|
35 content::TestBrowserThreadBundle::REAL_DB_THREAD
),
36 weak_factory_(this) {}
38 bool did_do_work() { return did_do_work_
; }
39 BrowserThreadModelWorker
* worker() { return worker_
.get(); }
40 OneShotTimer
<SyncBrowserThreadModelWorkerTest
>* timer() { return &timer_
; }
41 base::WeakPtrFactory
<SyncBrowserThreadModelWorkerTest
>* factory() {
42 return &weak_factory_
;
45 // Schedule DoWork to be executed on the DB thread and have the test fail if
46 // DoWork hasn't executed within action_timeout().
48 // We wait until the callback is done. So it is safe to use unretained.
49 syncer::WorkCallback c
=
50 base::Bind(&SyncBrowserThreadModelWorkerTest::DoWork
,
51 base::Unretained(this));
54 TestTimeouts::action_timeout(),
56 &SyncBrowserThreadModelWorkerTest::Timeout
);
57 worker()->DoWorkAndWaitUntilDone(c
);
60 // This is the work that will be scheduled to be done on the DB thread.
61 syncer::SyncerError
DoWork() {
62 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB
));
63 timer_
.Stop(); // Stop the failure timer so the test succeeds.
64 BrowserThread::PostTask(
65 BrowserThread::IO
, FROM_HERE
, base::MessageLoop::QuitClosure());
67 return syncer::SYNCER_OK
;
70 // This will be called by the OneShotTimer and make the test fail unless
71 // DoWork is called first.
73 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread.";
74 BrowserThread::PostTask(
75 BrowserThread::IO
, FROM_HERE
, base::MessageLoop::QuitClosure());
79 void SetUp() override
{
80 worker_
= new BrowserThreadModelWorker(
81 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
),
82 syncer::GROUP_DB
, NULL
);
85 virtual void Teardown() {
91 scoped_refptr
<BrowserThreadModelWorker
> worker_
;
92 OneShotTimer
<SyncBrowserThreadModelWorkerTest
> timer_
;
94 content::TestBrowserThreadBundle thread_bundle_
;
96 base::WeakPtrFactory
<SyncBrowserThreadModelWorkerTest
> weak_factory_
;
99 TEST_F(SyncBrowserThreadModelWorkerTest
, DoesWorkOnDatabaseThread
) {
100 base::ThreadTaskRunnerHandle::Get()->PostTask(
101 FROM_HERE
, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork
,
102 factory()->GetWeakPtr()));
103 base::MessageLoop::current()->Run();
104 EXPECT_TRUE(did_do_work());
109 } // namespace browser_sync