| 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 namespace syncer { |
| 23 using syncer::SyncerError; | 23 namespace { |
| 24 | 24 |
| 25 class UIModelWorkerVisitor { | 25 class UIModelWorkerVisitor { |
| 26 public: | 26 public: |
| 27 UIModelWorkerVisitor(base::WaitableEvent* was_run, bool quit_loop) | 27 UIModelWorkerVisitor(base::WaitableEvent* was_run, bool quit_loop) |
| 28 : quit_loop_when_run_(quit_loop), was_run_(was_run) {} | 28 : quit_loop_when_run_(quit_loop), was_run_(was_run) {} |
| 29 virtual ~UIModelWorkerVisitor() {} | 29 virtual ~UIModelWorkerVisitor() {} |
| 30 | 30 |
| 31 virtual syncer::SyncerError DoWork() { | 31 virtual SyncerError DoWork() { |
| 32 was_run_->Signal(); | 32 was_run_->Signal(); |
| 33 if (quit_loop_when_run_) | 33 if (quit_loop_when_run_) |
| 34 base::MessageLoop::current()->QuitWhenIdle(); | 34 base::MessageLoop::current()->QuitWhenIdle(); |
| 35 return syncer::SYNCER_OK; | 35 return SYNCER_OK; |
| 36 } | 36 } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 bool quit_loop_when_run_; | 39 bool quit_loop_when_run_; |
| 40 base::WaitableEvent* was_run_; | 40 base::WaitableEvent* was_run_; |
| 41 DISALLOW_COPY_AND_ASSIGN(UIModelWorkerVisitor); | 41 DISALLOW_COPY_AND_ASSIGN(UIModelWorkerVisitor); |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 // A faux-syncer that only interacts with its model safe worker. | 44 // A fake syncer that only interacts with its model safe worker. |
| 45 class Syncer { | 45 class FakeSyncer { |
| 46 public: | 46 public: |
| 47 explicit Syncer(UIModelWorker* worker) : worker_(worker) {} | 47 explicit FakeSyncer(UIModelWorker* worker) : worker_(worker) {} |
| 48 ~Syncer() {} | 48 ~FakeSyncer() {} |
| 49 | 49 |
| 50 void SyncShare(UIModelWorkerVisitor* visitor) { | 50 void SyncShare(UIModelWorkerVisitor* visitor) { |
| 51 // 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. |
| 52 syncer::WorkCallback c = | 52 WorkCallback c = |
| 53 base::Bind(&UIModelWorkerVisitor::DoWork, base::Unretained(visitor)); | 53 base::Bind(&UIModelWorkerVisitor::DoWork, base::Unretained(visitor)); |
| 54 worker_->DoWorkAndWaitUntilDone(c); | 54 worker_->DoWorkAndWaitUntilDone(c); |
| 55 } | 55 } |
| 56 | 56 |
| 57 private: | 57 private: |
| 58 scoped_refptr<UIModelWorker> worker_; | 58 scoped_refptr<UIModelWorker> worker_; |
| 59 DISALLOW_COPY_AND_ASSIGN(Syncer); | 59 DISALLOW_COPY_AND_ASSIGN(FakeSyncer); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 class SyncUIModelWorkerTest : public testing::Test { | 62 class SyncUIModelWorkerTest : public testing::Test { |
| 63 public: | 63 public: |
| 64 SyncUIModelWorkerTest() | 64 SyncUIModelWorkerTest() |
| 65 : faux_syncer_thread_("FauxSyncerThread"), | 65 : faux_syncer_thread_("FauxSyncerThread"), |
| 66 faux_core_thread_("FauxCoreThread") {} | 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 FakeSyncer(bmw_.get())); |
| 72 } | 72 } |
| 73 | 73 |
| 74 Syncer* syncer() { return syncer_.get(); } | 74 FakeSyncer* 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 |
| 79 private: | 79 private: |
| 80 base::MessageLoop faux_ui_loop_; | 80 base::MessageLoop faux_ui_loop_; |
| 81 base::Thread faux_syncer_thread_; | 81 base::Thread faux_syncer_thread_; |
| 82 base::Thread faux_core_thread_; | 82 base::Thread faux_core_thread_; |
| 83 scoped_refptr<UIModelWorker> bmw_; | 83 scoped_refptr<UIModelWorker> bmw_; |
| 84 std::unique_ptr<Syncer> syncer_; | 84 std::unique_ptr<FakeSyncer> syncer_; |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) { | 87 TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) { |
| 88 base::WaitableEvent v_was_run( | 88 base::WaitableEvent v_was_run( |
| 89 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 89 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 90 base::WaitableEvent::InitialState::NOT_SIGNALED); | 90 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 91 std::unique_ptr<UIModelWorkerVisitor> v( | 91 std::unique_ptr<UIModelWorkerVisitor> v( |
| 92 new UIModelWorkerVisitor(&v_was_run, true)); | 92 new UIModelWorkerVisitor(&v_was_run, true)); |
| 93 | 93 |
| 94 syncer_thread()->task_runner()->PostTask( | 94 syncer_thread()->task_runner()->PostTask( |
| 95 FROM_HERE, | 95 FROM_HERE, |
| 96 base::Bind(&Syncer::SyncShare, base::Unretained(syncer()), v.get())); | 96 base::Bind(&FakeSyncer::SyncShare, base::Unretained(syncer()), v.get())); |
| 97 | 97 |
| 98 // 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 |
| 99 // (hopefully) scheduled task from a SyncShare invocation. | 99 // (hopefully) scheduled task from a SyncShare invocation. |
| 100 base::RunLoop().Run(); | 100 base::RunLoop().Run(); |
| 101 syncer_thread()->Stop(); | 101 syncer_thread()->Stop(); |
| 102 } | 102 } |
| 103 |
| 104 } // namespace |
| 105 } // namespace syncer |
| OLD | NEW |