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/location.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace sync_file_system
{
15 namespace drive_backend
{
19 void SimpleCallback(bool* called
, int) {
21 EXPECT_FALSE(*called
);
25 void CallbackWithPassed(bool* called
, scoped_ptr
<int>) {
27 EXPECT_FALSE(*called
);
31 void VerifyCalledOnTaskRunner(base::TaskRunner
* task_runner
,
34 ASSERT_TRUE(task_runner
);
36 EXPECT_TRUE(task_runner
->RunsTasksOnCurrentThread());
37 EXPECT_FALSE(*called
);
43 TEST(DriveBackendCallbackHelperTest
, BasicTest
) {
44 base::MessageLoop message_loop
;
47 RelayCallbackToCurrentThread(
49 base::Bind(&SimpleCallback
, &called
)).Run(0);
51 base::RunLoop().RunUntilIdle();
55 RelayCallbackToCurrentThread(
57 base::Bind(&CallbackWithPassed
, &called
))
58 .Run(scoped_ptr
<int>(new int));
60 base::RunLoop().RunUntilIdle();
64 TEST(DriveBackendCallbackHelperTest
, RunOnOtherThreadTest
) {
65 base::MessageLoop message_loop
;
66 base::Thread
thread("WorkerThread");
69 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
=
70 base::ThreadTaskRunnerHandle::Get();
71 scoped_refptr
<base::SequencedTaskRunner
> worker_task_runner
=
75 base::RunLoop run_loop
;
76 worker_task_runner
->PostTask(
78 RelayCallbackToTaskRunner(
81 base::Bind(&VerifyCalledOnTaskRunner
, ui_task_runner
, &called
)));
82 worker_task_runner
->PostTask(
84 RelayCallbackToTaskRunner(
85 ui_task_runner
.get(), FROM_HERE
, run_loop
.QuitClosure()));
90 base::RunLoop().RunUntilIdle();
93 TEST(DriveBackendCallbackHelperTest
, PassNullFunctionTest
) {
94 base::MessageLoop message_loop
;
95 base::Closure closure
= RelayCallbackToCurrentThread(
98 EXPECT_TRUE(closure
.is_null());
101 } // namespace drive_backend
102 } // namespace sync_file_system