callback_helper_unittest.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
1// Copyright 2014 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/drive_backend/callback_helper.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/run_loop.h"
9#include "base/threading/thread.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace sync_file_system {
13namespace drive_backend {
14
15namespace {
16
17void SimpleCallback(bool* called, int) {
18  ASSERT_TRUE(called);
19  EXPECT_FALSE(*called);
20  *called = true;
21}
22
23void CallbackWithPassed(bool* called, scoped_ptr<int>) {
24  ASSERT_TRUE(called);
25  EXPECT_FALSE(*called);
26  *called = true;
27}
28
29void VerifyCalledOnTaskRunner(base::TaskRunner* task_runner,
30                              bool* called) {
31  ASSERT_TRUE(called);
32  ASSERT_TRUE(task_runner);
33
34  EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread());
35  EXPECT_FALSE(*called);
36  *called = true;
37}
38
39}  // namespace
40
41TEST(DriveBackendCallbackHelperTest, BasicTest) {
42  base::MessageLoop message_loop;
43
44  bool called = false;
45  RelayCallbackToCurrentThread(
46      FROM_HERE,
47      base::Bind(&SimpleCallback, &called)).Run(0);
48  EXPECT_FALSE(called);
49  base::RunLoop().RunUntilIdle();
50  EXPECT_TRUE(called);
51
52  called = false;
53  RelayCallbackToCurrentThread(
54      FROM_HERE,
55      base::Bind(&CallbackWithPassed, &called))
56      .Run(scoped_ptr<int>(new int));
57  EXPECT_FALSE(called);
58  base::RunLoop().RunUntilIdle();
59  EXPECT_TRUE(called);
60}
61
62TEST(DriveBackendCallbackHelperTest, RunOnOtherThreadTest) {
63  base::MessageLoop message_loop;
64  base::Thread thread("WorkerThread");
65  thread.Start();
66
67  scoped_refptr<base::MessageLoopProxy> ui_task_runner =
68      base::MessageLoopProxy::current();
69  scoped_refptr<base::MessageLoopProxy> worker_task_runner =
70      thread.message_loop_proxy();
71
72  bool called = false;
73  base::RunLoop run_loop;
74  worker_task_runner->PostTask(
75      FROM_HERE,
76      RelayCallbackToTaskRunner(
77          ui_task_runner, FROM_HERE,
78          base::Bind(&VerifyCalledOnTaskRunner,
79                     ui_task_runner, &called)));
80  worker_task_runner->PostTask(
81      FROM_HERE,
82      RelayCallbackToTaskRunner(
83          ui_task_runner, FROM_HERE,
84          run_loop.QuitClosure()));
85  run_loop.Run();
86  EXPECT_TRUE(called);
87
88  thread.Stop();
89}
90
91}  // namespace drive_backend
92}  // namespace sync_file_system
93