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