Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / callback_helper_unittest.cc
blob3020c79a7bc367a0ce3b390ca9f232748d2f5ec0
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 {
17 namespace {
19 void SimpleCallback(bool* called, int) {
20 ASSERT_TRUE(called);
21 EXPECT_FALSE(*called);
22 *called = true;
25 void CallbackWithPassed(bool* called, scoped_ptr<int>) {
26 ASSERT_TRUE(called);
27 EXPECT_FALSE(*called);
28 *called = true;
31 void VerifyCalledOnTaskRunner(base::TaskRunner* task_runner,
32 bool* called) {
33 ASSERT_TRUE(called);
34 ASSERT_TRUE(task_runner);
36 EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread());
37 EXPECT_FALSE(*called);
38 *called = true;
41 } // namespace
43 TEST(DriveBackendCallbackHelperTest, BasicTest) {
44 base::MessageLoop message_loop;
46 bool called = false;
47 RelayCallbackToCurrentThread(
48 FROM_HERE,
49 base::Bind(&SimpleCallback, &called)).Run(0);
50 EXPECT_FALSE(called);
51 base::RunLoop().RunUntilIdle();
52 EXPECT_TRUE(called);
54 called = false;
55 RelayCallbackToCurrentThread(
56 FROM_HERE,
57 base::Bind(&CallbackWithPassed, &called))
58 .Run(scoped_ptr<int>(new int));
59 EXPECT_FALSE(called);
60 base::RunLoop().RunUntilIdle();
61 EXPECT_TRUE(called);
64 TEST(DriveBackendCallbackHelperTest, RunOnOtherThreadTest) {
65 base::MessageLoop message_loop;
66 base::Thread thread("WorkerThread");
67 thread.Start();
69 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner =
70 base::ThreadTaskRunnerHandle::Get();
71 scoped_refptr<base::SequencedTaskRunner> worker_task_runner =
72 thread.task_runner();
74 bool called = false;
75 base::RunLoop run_loop;
76 worker_task_runner->PostTask(
77 FROM_HERE,
78 RelayCallbackToTaskRunner(
79 ui_task_runner.get(),
80 FROM_HERE,
81 base::Bind(&VerifyCalledOnTaskRunner, ui_task_runner, &called)));
82 worker_task_runner->PostTask(
83 FROM_HERE,
84 RelayCallbackToTaskRunner(
85 ui_task_runner.get(), FROM_HERE, run_loop.QuitClosure()));
86 run_loop.Run();
87 EXPECT_TRUE(called);
89 thread.Stop();
90 base::RunLoop().RunUntilIdle();
93 TEST(DriveBackendCallbackHelperTest, PassNullFunctionTest) {
94 base::MessageLoop message_loop;
95 base::Closure closure = RelayCallbackToCurrentThread(
96 FROM_HERE,
97 base::Closure());
98 EXPECT_TRUE(closure.is_null());
101 } // namespace drive_backend
102 } // namespace sync_file_system