sync_file_system_test_util.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Use of this source code is governed by a BSD-style license that can be
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// found in the LICENSE file.
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/bind.h"
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/message_loop/message_loop_proxy.h"
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/run_loop.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/single_thread_task_runner.h"
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/threading/thread.h"
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "content/public/test/test_browser_thread.h"
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "content/public/test/test_browser_thread_bundle.h"
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "webkit/browser/fileapi/syncable/sync_status_code.h"
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
16baa3858d3f5d128a5c8466b700098109edcad5f2repo syncusing content::BrowserThread;
17baa3858d3f5d128a5c8466b700098109edcad5f2repo syncusing content::TestBrowserThread;
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
19baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace sync_file_system {
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
21baa3858d3f5d128a5c8466b700098109edcad5f2repo synctemplate <typename R>
22baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid AssignAndQuit(base::RunLoop* run_loop, R* result_out, R result) {
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  DCHECK(result_out);
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  DCHECK(run_loop);
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *result_out = result;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  run_loop->Quit();
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
29baa3858d3f5d128a5c8466b700098109edcad5f2repo synctemplate <typename R> base::Callback<void(R)>
30baa3858d3f5d128a5c8466b700098109edcad5f2repo syncAssignAndQuitCallback(base::RunLoop* run_loop, R* result) {
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return base::Bind(&AssignAndQuit<R>, run_loop, base::Unretained(result));
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Instantiate versions we know callers will need.
35baa3858d3f5d128a5c8466b700098109edcad5f2repo synctemplate base::Callback<void(SyncStatusCode)>
36baa3858d3f5d128a5c8466b700098109edcad5f2repo syncAssignAndQuitCallback(base::RunLoop*, SyncStatusCode*);
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
38baa3858d3f5d128a5c8466b700098109edcad5f2repo syncMultiThreadTestHelper::MultiThreadTestHelper()
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    : thread_bundle_(new content::TestBrowserThreadBundle(
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          content::TestBrowserThreadBundle::REAL_FILE_THREAD |
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          content::TestBrowserThreadBundle::REAL_IO_THREAD)) {
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
44baa3858d3f5d128a5c8466b700098109edcad5f2repo syncMultiThreadTestHelper::~MultiThreadTestHelper() {}
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
46baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid MultiThreadTestHelper::SetUp() {
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ui_task_runner_ =
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  file_task_runner_ =
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  io_task_runner_ =
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
55baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid MultiThreadTestHelper::TearDown() {
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // Make sure we give some more time to finish tasks on the FILE thread
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // before stopping IO/FILE threads.
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  base::RunLoop run_loop;
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  file_task_runner_->PostTaskAndReply(
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure());
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  run_loop.Run();
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}  // namespace sync_file_system
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync