Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: components/sync/driver/glue/ui_model_worker_unittest.cc

Issue 2386393002: [Sync] Clean up UIModelWorkerTest. (Closed)
Patch Set: Rebase. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "components/sync/driver/glue/ui_model_worker.h" 5 #include "components/sync/driver/glue/ui_model_worker.h"
6 6
7 #include <memory>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
11 #include "base/location.h" 9 #include "base/location.h"
12 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h" 11 #include "base/run_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
16 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
17 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
18 15
19 namespace syncer { 16 namespace syncer {
20 namespace { 17 namespace {
21 18
22 class UIModelWorkerVisitor { 19 // Makes a Closure into a WorkCallback.
20 // Does |work| and checks that we're on the |thread_verifier| thread.
21 SyncerError DoWork(
22 const scoped_refptr<base::SingleThreadTaskRunner>& thread_verifier,
23 base::Closure work) {
24 DCHECK(thread_verifier->BelongsToCurrentThread());
25 work.Run();
26 return SYNCER_OK;
27 }
28
29 // Converts |work| to a WorkCallback that will verify that it's run on the
30 // thread it was constructed on.
31 WorkCallback ClosureToWorkCallback(base::Closure work) {
32 return base::Bind(&DoWork, base::ThreadTaskRunnerHandle::Get(), work);
33 }
34
35 class SyncUIModelWorkerTest : public testing::Test {
23 public: 36 public:
24 UIModelWorkerVisitor(base::WaitableEvent* was_run, bool quit_loop) 37 SyncUIModelWorkerTest() : sync_thread_("SyncThreadForTest") {
25 : quit_loop_when_run_(quit_loop), was_run_(was_run) {} 38 sync_thread_.Start();
26 virtual ~UIModelWorkerVisitor() {} 39 worker_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get(), nullptr);
40 }
27 41
28 virtual SyncerError DoWork() { 42 void PostWorkToSyncThread(WorkCallback work) {
29 was_run_->Signal(); 43 sync_thread_.task_runner()->PostTask(
30 if (quit_loop_when_run_) 44 FROM_HERE,
31 base::MessageLoop::current()->QuitWhenIdle(); 45 base::Bind(base::IgnoreResult(&UIModelWorker::DoWorkAndWaitUntilDone),
32 return SYNCER_OK; 46 worker_, work));
33 } 47 }
34 48
35 private: 49 private:
36 bool quit_loop_when_run_; 50 base::MessageLoop ui_loop_;
37 base::WaitableEvent* was_run_; 51 base::Thread sync_thread_;
38 DISALLOW_COPY_AND_ASSIGN(UIModelWorkerVisitor);
39 };
40
41 // A fake syncer that only interacts with its model safe worker.
42 class FakeSyncer {
43 public:
44 explicit FakeSyncer(UIModelWorker* worker) : worker_(worker) {}
45 ~FakeSyncer() {}
46
47 void SyncShare(UIModelWorkerVisitor* visitor) {
48 // We wait until the callback is executed. So it is safe to use Unretained.
49 WorkCallback c =
50 base::Bind(&UIModelWorkerVisitor::DoWork, base::Unretained(visitor));
51 worker_->DoWorkAndWaitUntilDone(c);
52 }
53
54 private:
55 scoped_refptr<UIModelWorker> worker_; 52 scoped_refptr<UIModelWorker> worker_;
56 DISALLOW_COPY_AND_ASSIGN(FakeSyncer);
57 };
58
59 class SyncUIModelWorkerTest : public testing::Test {
60 public:
61 SyncUIModelWorkerTest()
62 : faux_syncer_thread_("FauxSyncerThread"),
63 faux_core_thread_("FauxCoreThread") {}
64
65 void SetUp() override {
66 faux_syncer_thread_.Start();
67 bmw_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get(), nullptr);
68 syncer_.reset(new FakeSyncer(bmw_.get()));
69 }
70
71 FakeSyncer* syncer() { return syncer_.get(); }
72 UIModelWorker* bmw() { return bmw_.get(); }
73 base::Thread* core_thread() { return &faux_core_thread_; }
74 base::Thread* syncer_thread() { return &faux_syncer_thread_; }
75
76 private:
77 base::MessageLoop faux_ui_loop_;
78 base::Thread faux_syncer_thread_;
79 base::Thread faux_core_thread_;
80 scoped_refptr<UIModelWorker> bmw_;
81 std::unique_ptr<FakeSyncer> syncer_;
82 }; 53 };
83 54
84 TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) { 55 TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) {
85 base::WaitableEvent v_was_run( 56 base::RunLoop run_loop;
86 base::WaitableEvent::ResetPolicy::AUTOMATIC, 57 PostWorkToSyncThread(ClosureToWorkCallback(run_loop.QuitClosure()));
87 base::WaitableEvent::InitialState::NOT_SIGNALED); 58 // This won't quit until the QuitClosure is run.
88 std::unique_ptr<UIModelWorkerVisitor> v( 59 run_loop.Run();
89 new UIModelWorkerVisitor(&v_was_run, true));
90
91 syncer_thread()->task_runner()->PostTask(
92 FROM_HERE,
93 base::Bind(&FakeSyncer::SyncShare, base::Unretained(syncer()), v.get()));
94
95 // We are on the UI thread, so run our loop to process the
96 // (hopefully) scheduled task from a SyncShare invocation.
97 base::RunLoop().Run();
98 syncer_thread()->Stop();
99 } 60 }
100 61
101 } // namespace 62 } // namespace
102 } // namespace syncer 63 } // namespace syncer
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698