sync_file_system_test_util.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "chrome/browser/sync_file_system/sync_file_system_test_util.h"
6
7#include "base/bind.h"
8#include "base/message_loop_proxy.h"
9#include "base/run_loop.h"
10#include "base/single_thread_task_runner.h"
11#include "base/threading/thread.h"
12#include "content/public/test/test_browser_thread.h"
13#include "webkit/fileapi/syncable/sync_status_code.h"
14
15using content::BrowserThread;
16using content::TestBrowserThread;
17
18namespace sync_file_system {
19
20template <typename R>
21void AssignAndQuit(base::RunLoop* run_loop, R* result_out, R result) {
22  DCHECK(result_out);
23  DCHECK(run_loop);
24  *result_out = result;
25  run_loop->Quit();
26}
27
28template <typename R> base::Callback<void(R)>
29AssignAndQuitCallback(base::RunLoop* run_loop, R* result) {
30  return base::Bind(&AssignAndQuit<R>, run_loop, base::Unretained(result));
31}
32
33// Instantiate versions we know callers will need.
34template base::Callback<void(fileapi::SyncStatusCode)>
35AssignAndQuitCallback(base::RunLoop*, fileapi::SyncStatusCode*);
36
37MultiThreadTestHelper::MultiThreadTestHelper()
38    : file_thread_(new base::Thread("File_Thread")),
39      io_thread_(new base::Thread("IO_Thread")) {}
40
41MultiThreadTestHelper::~MultiThreadTestHelper() {}
42
43void MultiThreadTestHelper::SetUp() {
44  file_thread_->Start();
45  io_thread_->StartWithOptions(
46      base::Thread::Options(MessageLoop::TYPE_IO, 0));
47
48  browser_ui_thread_.reset(
49      new TestBrowserThread(BrowserThread::UI,
50                            MessageLoop::current()));
51  browser_file_thread_.reset(
52      new TestBrowserThread(BrowserThread::FILE,
53                            file_thread_->message_loop()));
54  browser_io_thread_.reset(
55      new TestBrowserThread(BrowserThread::IO,
56                            io_thread_->message_loop()));
57
58  ui_task_runner_ =
59      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
60  file_task_runner_ =
61      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
62  io_task_runner_ =
63      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
64}
65
66void MultiThreadTestHelper::TearDown() {
67  file_thread_->Stop();
68  io_thread_->Stop();
69}
70
71}  // namespace sync_file_system
72