| Index: components/sync/driver/glue/ui_model_worker_unittest.cc
|
| diff --git a/components/sync/driver/glue/ui_model_worker_unittest.cc b/components/sync/driver/glue/ui_model_worker_unittest.cc
|
| index 525df6e710c820fec987d3dd29e679295a036db7..2c83554c25b8938b46499edbfca9a60610d8a873 100644
|
| --- a/components/sync/driver/glue/ui_model_worker_unittest.cc
|
| +++ b/components/sync/driver/glue/ui_model_worker_unittest.cc
|
| @@ -4,14 +4,11 @@
|
|
|
| #include "components/sync/driver/glue/ui_model_worker.h"
|
|
|
| -#include <memory>
|
| -
|
| #include "base/bind.h"
|
| #include "base/bind_helpers.h"
|
| #include "base/location.h"
|
| #include "base/memory/ref_counted.h"
|
| #include "base/run_loop.h"
|
| -#include "base/synchronization/waitable_event.h"
|
| #include "base/threading/thread.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -19,83 +16,47 @@
|
| namespace syncer {
|
| namespace {
|
|
|
| -class UIModelWorkerVisitor {
|
| - public:
|
| - UIModelWorkerVisitor(base::WaitableEvent* was_run, bool quit_loop)
|
| - : quit_loop_when_run_(quit_loop), was_run_(was_run) {}
|
| - virtual ~UIModelWorkerVisitor() {}
|
| -
|
| - virtual SyncerError DoWork() {
|
| - was_run_->Signal();
|
| - if (quit_loop_when_run_)
|
| - base::MessageLoop::current()->QuitWhenIdle();
|
| - return SYNCER_OK;
|
| - }
|
| -
|
| - private:
|
| - bool quit_loop_when_run_;
|
| - base::WaitableEvent* was_run_;
|
| - DISALLOW_COPY_AND_ASSIGN(UIModelWorkerVisitor);
|
| -};
|
| -
|
| -// A fake syncer that only interacts with its model safe worker.
|
| -class FakeSyncer {
|
| - public:
|
| - explicit FakeSyncer(UIModelWorker* worker) : worker_(worker) {}
|
| - ~FakeSyncer() {}
|
| -
|
| - void SyncShare(UIModelWorkerVisitor* visitor) {
|
| - // We wait until the callback is executed. So it is safe to use Unretained.
|
| - WorkCallback c =
|
| - base::Bind(&UIModelWorkerVisitor::DoWork, base::Unretained(visitor));
|
| - worker_->DoWorkAndWaitUntilDone(c);
|
| - }
|
| +// Makes a Closure into a WorkCallback.
|
| +// Does |work| and checks that we're on the |thread_verifier| thread.
|
| +SyncerError DoWork(
|
| + const scoped_refptr<base::SingleThreadTaskRunner>& thread_verifier,
|
| + base::Closure work) {
|
| + DCHECK(thread_verifier->BelongsToCurrentThread());
|
| + work.Run();
|
| + return SYNCER_OK;
|
| +}
|
|
|
| - private:
|
| - scoped_refptr<UIModelWorker> worker_;
|
| - DISALLOW_COPY_AND_ASSIGN(FakeSyncer);
|
| -};
|
| +// Converts |work| to a WorkCallback that will verify that it's run on the
|
| +// thread it was constructed on.
|
| +WorkCallback ClosureToWorkCallback(base::Closure work) {
|
| + return base::Bind(&DoWork, base::ThreadTaskRunnerHandle::Get(), work);
|
| +}
|
|
|
| class SyncUIModelWorkerTest : public testing::Test {
|
| public:
|
| - SyncUIModelWorkerTest()
|
| - : faux_syncer_thread_("FauxSyncerThread"),
|
| - faux_core_thread_("FauxCoreThread") {}
|
| -
|
| - void SetUp() override {
|
| - faux_syncer_thread_.Start();
|
| - bmw_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get(), nullptr);
|
| - syncer_.reset(new FakeSyncer(bmw_.get()));
|
| + SyncUIModelWorkerTest() : sync_thread_("SyncThreadForTest") {
|
| + sync_thread_.Start();
|
| + worker_ = new UIModelWorker(base::ThreadTaskRunnerHandle::Get(), nullptr);
|
| }
|
|
|
| - FakeSyncer* syncer() { return syncer_.get(); }
|
| - UIModelWorker* bmw() { return bmw_.get(); }
|
| - base::Thread* core_thread() { return &faux_core_thread_; }
|
| - base::Thread* syncer_thread() { return &faux_syncer_thread_; }
|
| + void PostWorkToSyncThread(WorkCallback work) {
|
| + sync_thread_.task_runner()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(base::IgnoreResult(&UIModelWorker::DoWorkAndWaitUntilDone),
|
| + worker_, work));
|
| + }
|
|
|
| private:
|
| - base::MessageLoop faux_ui_loop_;
|
| - base::Thread faux_syncer_thread_;
|
| - base::Thread faux_core_thread_;
|
| - scoped_refptr<UIModelWorker> bmw_;
|
| - std::unique_ptr<FakeSyncer> syncer_;
|
| + base::MessageLoop ui_loop_;
|
| + base::Thread sync_thread_;
|
| + scoped_refptr<UIModelWorker> worker_;
|
| };
|
|
|
| TEST_F(SyncUIModelWorkerTest, ScheduledWorkRunsOnUILoop) {
|
| - base::WaitableEvent v_was_run(
|
| - base::WaitableEvent::ResetPolicy::AUTOMATIC,
|
| - base::WaitableEvent::InitialState::NOT_SIGNALED);
|
| - std::unique_ptr<UIModelWorkerVisitor> v(
|
| - new UIModelWorkerVisitor(&v_was_run, true));
|
| -
|
| - syncer_thread()->task_runner()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(&FakeSyncer::SyncShare, base::Unretained(syncer()), v.get()));
|
| -
|
| - // We are on the UI thread, so run our loop to process the
|
| - // (hopefully) scheduled task from a SyncShare invocation.
|
| - base::RunLoop().Run();
|
| - syncer_thread()->Stop();
|
| + base::RunLoop run_loop;
|
| + PostWorkToSyncThread(ClosureToWorkCallback(run_loop.QuitClosure()));
|
| + // This won't quit until the QuitClosure is run.
|
| + run_loop.Run();
|
| }
|
|
|
| } // namespace
|
|
|