Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / glue / browser_thread_model_worker_unittest.cc
blob1b165ea8337ba12f64283a4d0058f4048c4ef190
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.
5 #include "base/bind.h"
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;
22 using base::Thread;
23 using base::TimeDelta;
24 using content::BrowserThread;
26 namespace browser_sync {
28 namespace {
30 class SyncBrowserThreadModelWorkerTest : public testing::Test {
31 public:
32 SyncBrowserThreadModelWorkerTest() :
33 did_do_work_(false),
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().
47 void ScheduleWork() {
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));
52 timer()->Start(
53 FROM_HERE,
54 TestTimeouts::action_timeout(),
55 this,
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());
66 did_do_work_ = true;
67 return syncer::SYNCER_OK;
70 // This will be called by the OneShotTimer and make the test fail unless
71 // DoWork is called first.
72 void Timeout() {
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());
78 protected:
79 void SetUp() override {
80 worker_ = new BrowserThreadModelWorker(
81 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
82 syncer::GROUP_DB, NULL);
85 virtual void Teardown() {
86 worker_ = NULL;
89 private:
90 bool did_do_work_;
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());
107 } // namespace
109 } // namespace browser_sync