Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/sync_file_system/drive_backend/callback_helper_unittest.cc

Issue 259503004: [SyncFS] Refine callback helpers (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h" 5 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/threading/thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 namespace sync_file_system { 12 namespace sync_file_system {
12 namespace drive_backend { 13 namespace drive_backend {
13 14
14 namespace { 15 namespace {
15 16
16 void SimpleCallback(bool* called, int) { 17 void SimpleCallback(bool* called, int) {
17 ASSERT_TRUE(called); 18 ASSERT_TRUE(called);
18 EXPECT_FALSE(*called); 19 EXPECT_FALSE(*called);
19 *called = true; 20 *called = true;
20 } 21 }
21 22
22 void CallbackWithPassed(bool* called, scoped_ptr<int>) { 23 void CallbackWithPassed(bool* called, scoped_ptr<int>) {
23 ASSERT_TRUE(called); 24 ASSERT_TRUE(called);
24 EXPECT_FALSE(*called); 25 EXPECT_FALSE(*called);
25 *called = true; 26 *called = true;
26 } 27 }
27 28
29 void 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
28 } // namespace 39 } // namespace
29 40
30 TEST(DriveBackendCallbackHelperTest, CreateRelayedCallbackTest) { 41 TEST(DriveBackendCallbackHelperTest, BasicTest) {
31 base::MessageLoop message_loop; 42 base::MessageLoop message_loop;
32 43
33 bool called = false; 44 bool called = false;
34 CreateRelayedCallback(base::Bind(&SimpleCallback, &called)).Run(0); 45 RelayCallbackToCurrentThread(
46 FROM_HERE,
47 base::Bind(&SimpleCallback, &called)).Run(0);
35 EXPECT_FALSE(called); 48 EXPECT_FALSE(called);
36 base::RunLoop().RunUntilIdle(); 49 base::RunLoop().RunUntilIdle();
37 EXPECT_TRUE(called); 50 EXPECT_TRUE(called);
38 51
39 called = false; 52 called = false;
40 CreateRelayedCallback( 53 RelayCallbackToCurrentThread(
54 FROM_HERE,
41 base::Bind(&CallbackWithPassed, &called)) 55 base::Bind(&CallbackWithPassed, &called))
42 .Run(scoped_ptr<int>(new int)); 56 .Run(scoped_ptr<int>(new int));
43 EXPECT_FALSE(called); 57 EXPECT_FALSE(called);
44 base::RunLoop().RunUntilIdle(); 58 base::RunLoop().RunUntilIdle();
45 EXPECT_TRUE(called); 59 EXPECT_TRUE(called);
46 } 60 }
47 61
62 TEST(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
48 } // namespace drive_backend 91 } // namespace drive_backend
49 } // namespace sync_file_system 92 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698