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.
5 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/threading/thread.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace sync_file_system
{
14 namespace drive_backend
{
18 void SimpleCallback(bool* called
, int) {
20 EXPECT_FALSE(*called
);
24 void CallbackWithPassed(bool* called
, scoped_ptr
<int>) {
26 EXPECT_FALSE(*called
);
30 void VerifyCalledOnTaskRunner(base::TaskRunner
* task_runner
,
33 ASSERT_TRUE(task_runner
);
35 EXPECT_TRUE(task_runner
->RunsTasksOnCurrentThread());
36 EXPECT_FALSE(*called
);
42 TEST(DriveBackendCallbackHelperTest
, BasicTest
) {
43 base::MessageLoop message_loop
;
46 RelayCallbackToCurrentThread(
48 base::Bind(&SimpleCallback
, &called
)).Run(0);
50 base::RunLoop().RunUntilIdle();
54 RelayCallbackToCurrentThread(
56 base::Bind(&CallbackWithPassed
, &called
))
57 .Run(scoped_ptr
<int>(new int));
59 base::RunLoop().RunUntilIdle();
63 TEST(DriveBackendCallbackHelperTest
, RunOnOtherThreadTest
) {
64 base::MessageLoop message_loop
;
65 base::Thread
thread("WorkerThread");
68 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
=
69 base::ThreadTaskRunnerHandle::Get();
70 scoped_refptr
<base::SequencedTaskRunner
> worker_task_runner
=
71 thread
.message_loop_proxy();
74 base::RunLoop run_loop
;
75 worker_task_runner
->PostTask(
77 RelayCallbackToTaskRunner(
80 base::Bind(&VerifyCalledOnTaskRunner
, ui_task_runner
, &called
)));
81 worker_task_runner
->PostTask(
83 RelayCallbackToTaskRunner(
84 ui_task_runner
.get(), FROM_HERE
, run_loop
.QuitClosure()));
89 base::RunLoop().RunUntilIdle();
92 TEST(DriveBackendCallbackHelperTest
, PassNullFunctionTest
) {
93 base::MessageLoop message_loop
;
94 base::Closure closure
= RelayCallbackToCurrentThread(
97 EXPECT_TRUE(closure
.is_null());
100 } // namespace drive_backend
101 } // namespace sync_file_system