Chromium Code Reviews| Index: chrome/browser/sync/glue/file_model_worker_unittest.cc |
| diff --git a/chrome/browser/sync/glue/file_model_worker_unittest.cc b/chrome/browser/sync/glue/file_model_worker_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..95fb3f3994c2ddcd75c1cf502406a6fcc0f1caa2 |
| --- /dev/null |
| +++ b/chrome/browser/sync/glue/file_model_worker_unittest.cc |
| @@ -0,0 +1,94 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/threading/thread.h" |
| +#include "base/timer.h" |
| +#include "chrome/browser/sync/glue/file_model_worker.h" |
| +#include "content/browser/browser_thread.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::OneShotTimer; |
| +using base::Thread; |
| +using base::TimeDelta; |
| +using browser_sync::FileModelWorker; |
| + |
| +namespace { |
| + |
| +class FileModelWorkerTest : public testing::Test { |
| + public: |
| + FileModelWorkerTest() : |
| + did_do_work_(false), |
| + file_thread_(BrowserThread::FILE), |
| + io_thread_(BrowserThread::IO, &io_loop_), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
| + |
| + bool did_do_work() { return did_do_work_; } |
| + FileModelWorker* worker() { return worker_.get(); } |
| + OneShotTimer<FileModelWorkerTest>* timer() { return &timer_; } |
| + ScopedRunnableMethodFactory<FileModelWorkerTest>* factory() { |
| + return &method_factory_; |
| + } |
| + |
| + // Schedule DoWork to be executed on the FILE thread and have the test fail if |
| + // DoWork hasn't executed within 10 seconds. |
| + void ScheduleWork() { |
| + scoped_ptr<Callback0::Type> c(NewCallback(this, |
| + &FileModelWorkerTest::DoWork)); |
| + timer()->Start(FROM_HERE, TimeDelta::FromSeconds(10), |
|
Paweł Hajdan Jr.
2011/09/14 18:33:01
Do not hardcode timeouts. Use base/test_timeouts.
not at google - send to devlin
2011/09/14 19:21:59
Done (in the other test file that I kinda moved th
|
| + this, &FileModelWorkerTest::Timeout); |
| + worker()->DoWorkAndWaitUntilDone(c.get()); |
| + } |
| + |
| + // This is the work that will be scheduled to be done on the FILE thread. |
| + void DoWork() { |
| + EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + timer_.Stop(); // Stop the failure timer so the test succeeds. |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
| + did_do_work_ = true; |
| + } |
| + |
| + // This will be called by the OneShotTimer and make the test fail unless |
| + // DoWork is called first. |
| + void Timeout() { |
| + ADD_FAILURE() << |
| + "Timed out waiting for work to be done on the FILE thread."; |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
| + } |
| + |
| + protected: |
| + virtual void SetUp() { |
| + file_thread_.Start(); |
| + worker_ = new FileModelWorker(); |
| + } |
| + |
| + virtual void Teardown() { |
| + worker_.release(); |
| + file_thread_.Stop(); |
| + } |
| + |
| + private: |
| + bool did_do_work_; |
| + scoped_refptr<FileModelWorker> worker_; |
| + OneShotTimer<FileModelWorkerTest> timer_; |
| + |
| + BrowserThread file_thread_; |
| + MessageLoopForIO io_loop_; |
| + BrowserThread io_thread_; |
| + |
| + ScopedRunnableMethodFactory<FileModelWorkerTest> method_factory_; |
| +}; |
| + |
| +TEST_F(FileModelWorkerTest, DoesWorkOnFileThread) { |
| + MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( |
| + &FileModelWorkerTest::ScheduleWork)); |
| + MessageLoop::current()->Run(); |
| + EXPECT_TRUE(did_do_work()); |
| +} |
| + |
| +} // namespace |