OLD | NEW |
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> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 using browser_sync::UIModelWorker; | 22 using browser_sync::UIModelWorker; |
23 using syncer::SyncerError; | 23 using syncer::SyncerError; |
24 | 24 |
25 class UIModelWorkerVisitor { | 25 class UIModelWorkerVisitor { |
26 public: | 26 public: |
27 UIModelWorkerVisitor(base::WaitableEvent* was_run, | 27 UIModelWorkerVisitor(base::WaitableEvent* was_run, bool quit_loop) |
28 bool quit_loop) | 28 : quit_loop_when_run_(quit_loop), was_run_(was_run) {} |
29 : quit_loop_when_run_(quit_loop), | 29 virtual ~UIModelWorkerVisitor() {} |
30 was_run_(was_run) { } | |
31 virtual ~UIModelWorkerVisitor() { } | |
32 | 30 |
33 virtual syncer::SyncerError DoWork() { | 31 virtual syncer::SyncerError DoWork() { |
34 was_run_->Signal(); | 32 was_run_->Signal(); |
35 if (quit_loop_when_run_) | 33 if (quit_loop_when_run_) |
36 base::MessageLoop::current()->QuitWhenIdle(); | 34 base::MessageLoop::current()->QuitWhenIdle(); |
37 return syncer::SYNCER_OK; | 35 return syncer::SYNCER_OK; |
38 } | 36 } |
39 | 37 |
40 private: | 38 private: |
41 bool quit_loop_when_run_; | 39 bool quit_loop_when_run_; |
42 base::WaitableEvent* was_run_; | 40 base::WaitableEvent* was_run_; |
43 DISALLOW_COPY_AND_ASSIGN(UIModelWorkerVisitor); | 41 DISALLOW_COPY_AND_ASSIGN(UIModelWorkerVisitor); |
44 }; | 42 }; |
45 | 43 |
46 // A faux-syncer that only interacts with its model safe worker. | 44 // A faux-syncer that only interacts with its model safe worker. |
47 class Syncer { | 45 class Syncer { |
48 public: | 46 public: |
49 explicit Syncer(UIModelWorker* worker) : worker_(worker) {} | 47 explicit Syncer(UIModelWorker* worker) : worker_(worker) {} |
50 ~Syncer() {} | 48 ~Syncer() {} |
51 | 49 |
52 void SyncShare(UIModelWorkerVisitor* visitor) { | 50 void SyncShare(UIModelWorkerVisitor* visitor) { |
53 // We wait until the callback is executed. So it is safe to use Unretained. | 51 // We wait until the callback is executed. So it is safe to use Unretained. |
54 syncer::WorkCallback c = base::Bind(&UIModelWorkerVisitor::DoWork, | 52 syncer::WorkCallback c = |
55 base::Unretained(visitor)); | 53 base::Bind(&UIModelWorkerVisitor::DoWork, base::Unretained(visitor)); |
56 worker_->DoWorkAndWaitUntilDone(c); | 54 worker_->DoWorkAndWaitUntilDone(c); |
57 } | 55 } |
| 56 |
58 private: | 57 private: |
59 scoped_refptr<UIModelWorker> worker_; | 58 scoped_refptr<UIModelWorker> worker_; |
60 DISALLOW_COPY_AND_ASSIGN(Syncer); | 59 DISALLOW_COPY_AND_ASSIGN(Syncer); |
61 }; | 60 }; |
62 | 61 |
63 class SyncUIModelWorkerTest : public testing::Test { | 62 class SyncUIModelWorkerTest : public testing::Test { |
64 public: | 63 public: |
65 SyncUIModelWorkerTest() : faux_syncer_thread_("FauxSyncerThread"), | 64 SyncUIModelWorkerTest() |
66 faux_core_thread_("FauxCoreThread") { } | 65 : faux_syncer_thread_("FauxSyncerThread"), |
| 66 faux_core_thread_("FauxCoreThread") {} |
67 | 67 |
68 void SetUp() override { | 68 void SetUp() override { |
69 faux_syncer_thread_.Start(); | 69 faux_syncer_thread_.Start(); |
70 bmw_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get(), nullptr); | 70 bmw_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get(), nullptr); |
71 syncer_.reset(new Syncer(bmw_.get())); | 71 syncer_.reset(new Syncer(bmw_.get())); |
72 } | 72 } |
73 | 73 |
74 Syncer* syncer() { return syncer_.get(); } | 74 Syncer* syncer() { return syncer_.get(); } |
75 UIModelWorker* bmw() { return bmw_.get(); } | 75 UIModelWorker* bmw() { return bmw_.get(); } |
76 base::Thread* core_thread() { return &faux_core_thread_; } | 76 base::Thread* core_thread() { return &faux_core_thread_; } |
77 base::Thread* syncer_thread() { return &faux_syncer_thread_; } | 77 base::Thread* syncer_thread() { return &faux_syncer_thread_; } |
| 78 |
78 private: | 79 private: |
79 base::MessageLoop faux_ui_loop_; | 80 base::MessageLoop faux_ui_loop_; |
80 base::Thread faux_syncer_thread_; | 81 base::Thread faux_syncer_thread_; |
81 base::Thread faux_core_thread_; | 82 base::Thread faux_core_thread_; |
82 scoped_refptr<UIModelWorker> bmw_; | 83 scoped_refptr<UIModelWorker> bmw_; |
83 std::unique_ptr<Syncer> syncer_; | 84 std::unique_ptr<Syncer> syncer_; |
84 }; | 85 }; |
85 | 86 |
86 TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) { | 87 TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) { |
87 base::WaitableEvent v_was_run( | 88 base::WaitableEvent v_was_run( |
88 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 89 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
89 base::WaitableEvent::InitialState::NOT_SIGNALED); | 90 base::WaitableEvent::InitialState::NOT_SIGNALED); |
90 std::unique_ptr<UIModelWorkerVisitor> v( | 91 std::unique_ptr<UIModelWorkerVisitor> v( |
91 new UIModelWorkerVisitor(&v_was_run, true)); | 92 new UIModelWorkerVisitor(&v_was_run, true)); |
92 | 93 |
93 syncer_thread()->task_runner()->PostTask( | 94 syncer_thread()->task_runner()->PostTask( |
94 FROM_HERE, | 95 FROM_HERE, |
95 base::Bind(&Syncer::SyncShare, base::Unretained(syncer()), v.get())); | 96 base::Bind(&Syncer::SyncShare, base::Unretained(syncer()), v.get())); |
96 | 97 |
97 // We are on the UI thread, so run our loop to process the | 98 // We are on the UI thread, so run our loop to process the |
98 // (hopefully) scheduled task from a SyncShare invocation. | 99 // (hopefully) scheduled task from a SyncShare invocation. |
99 base::RunLoop().Run(); | 100 base::RunLoop().Run(); |
100 syncer_thread()->Stop(); | 101 syncer_thread()->Stop(); |
101 } | 102 } |
OLD | NEW |