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

Side by Side Diff: media/base/bind_to_loop_unittest.cc

Issue 11092054: Teach BindToLoop to create callbacks that accept scoped parameters. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: xhwang CR Created 8 years, 2 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
« no previous file with comments | « media/base/bind_to_loop.h.pump ('k') | webkit/media/webmediaplayer_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/base/bind_to_loop.h" 5 #include "media/base/bind_to_loop.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace media { 11 namespace media {
12 12
13 void BoundBoolSet(bool* var, bool val) { 13 void BoundBoolSet(bool* var, bool val) {
14 *var = val; 14 *var = val;
15 } 15 }
16 16
17 void BoundBoolSetFromScopedPtr(bool* var, scoped_ptr<bool> val) { 17 void BoundBoolSetFromScopedPtr(bool* var, scoped_ptr<bool> val) {
18 *var = *val; 18 *var = *val;
19 } 19 }
20 20
21 void BoundBoolSetFromScopedArray(bool* var, scoped_array<bool> val) {
22 *var = val[0];
23 }
24
21 void BoundBoolSetFromConstRef(bool* var, const bool& val) { 25 void BoundBoolSetFromConstRef(bool* var, const bool& val) {
22 *var = val; 26 *var = val;
23 } 27 }
24 28
25 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { 29 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) {
26 *a_var = a_val; 30 *a_var = a_val;
27 *b_var = b_val; 31 *b_var = b_val;
28 } 32 }
29 33
30 // Various tests that check that the bound function is only actually executed 34 // Various tests that check that the bound function is only actually executed
(...skipping 21 matching lines...) Expand all
52 TEST_F(BindToLoopTest, Bool) { 56 TEST_F(BindToLoopTest, Bool) {
53 bool bool_var = false; 57 bool bool_var = false;
54 base::Callback<void(bool)> cb = BindToLoop(proxy_, base::Bind( 58 base::Callback<void(bool)> cb = BindToLoop(proxy_, base::Bind(
55 &BoundBoolSet, &bool_var)); 59 &BoundBoolSet, &bool_var));
56 cb.Run(true); 60 cb.Run(true);
57 EXPECT_FALSE(bool_var); 61 EXPECT_FALSE(bool_var);
58 loop_.RunUntilIdle(); 62 loop_.RunUntilIdle();
59 EXPECT_TRUE(bool_var); 63 EXPECT_TRUE(bool_var);
60 } 64 }
61 65
62 TEST_F(BindToLoopTest, ScopedPtrBool) { 66 TEST_F(BindToLoopTest, BoundScopedPtrBool) {
63 bool bool_val = false; 67 bool bool_val = false;
64 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); 68 scoped_ptr<bool> scoped_ptr_bool(new bool(true));
65 base::Closure cb = BindToLoop(proxy_, base::Bind( 69 base::Closure cb = BindToLoop(proxy_, base::Bind(
66 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); 70 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool)));
67 cb.Run(); 71 cb.Run();
68 EXPECT_FALSE(bool_val); 72 EXPECT_FALSE(bool_val);
69 loop_.RunUntilIdle(); 73 loop_.RunUntilIdle();
70 EXPECT_TRUE(bool_val); 74 EXPECT_TRUE(bool_val);
71 } 75 }
72 76
77 TEST_F(BindToLoopTest, PassedScopedPtrBool) {
78 bool bool_val = false;
79 scoped_ptr<bool> scoped_ptr_bool(new bool(true));
80 base::Callback<void(scoped_ptr<bool>)> cb = BindToLoop(proxy_, base::Bind(
81 &BoundBoolSetFromScopedPtr, &bool_val));
82 cb.Run(scoped_ptr_bool.Pass());
83 EXPECT_FALSE(bool_val);
84 loop_.RunUntilIdle();
85 EXPECT_TRUE(bool_val);
86 }
87
88 TEST_F(BindToLoopTest, BoundScopedArrayBool) {
89 bool bool_val = false;
90 scoped_array<bool> scoped_array_bool(new bool[1]);
91 scoped_array_bool[0] = true;
92 base::Closure cb = BindToLoop(proxy_, base::Bind(
93 &BoundBoolSetFromScopedArray, &bool_val,
94 base::Passed(&scoped_array_bool)));
95 cb.Run();
96 EXPECT_FALSE(bool_val);
97 loop_.RunUntilIdle();
98 EXPECT_TRUE(bool_val);
99 }
100
101 TEST_F(BindToLoopTest, PassedScopedArrayBool) {
102 bool bool_val = false;
103 scoped_array<bool> scoped_array_bool(new bool[1]);
104 scoped_array_bool[0] = true;
105 base::Callback<void(scoped_array<bool>)> cb = BindToLoop(proxy_, base::Bind(
106 &BoundBoolSetFromScopedArray, &bool_val));
107 cb.Run(scoped_array_bool.Pass());
108 EXPECT_FALSE(bool_val);
109 loop_.RunUntilIdle();
110 EXPECT_TRUE(bool_val);
111 }
112
73 TEST_F(BindToLoopTest, BoolConstRef) { 113 TEST_F(BindToLoopTest, BoolConstRef) {
74 bool bool_var = false; 114 bool bool_var = false;
75 bool true_var = true; 115 bool true_var = true;
76 const bool& true_ref = true_var; 116 const bool& true_ref = true_var;
77 base::Closure cb = BindToLoop(proxy_, base::Bind( 117 base::Closure cb = BindToLoop(proxy_, base::Bind(
78 &BoundBoolSetFromConstRef, &bool_var, true_ref)); 118 &BoundBoolSetFromConstRef, &bool_var, true_ref));
79 cb.Run(); 119 cb.Run();
80 EXPECT_FALSE(bool_var); 120 EXPECT_FALSE(bool_var);
81 loop_.RunUntilIdle(); 121 loop_.RunUntilIdle();
82 EXPECT_TRUE(bool_var); 122 EXPECT_TRUE(bool_var);
83 } 123 }
84 124
85 TEST_F(BindToLoopTest, Integers) { 125 TEST_F(BindToLoopTest, Integers) {
86 int a = 0; 126 int a = 0;
87 int b = 0; 127 int b = 0;
88 base::Callback<void(int, int)> cb = BindToLoop(proxy_, base::Bind( 128 base::Callback<void(int, int)> cb = BindToLoop(proxy_, base::Bind(
89 &BoundIntegersSet, &a, &b)); 129 &BoundIntegersSet, &a, &b));
90 cb.Run(1, -1); 130 cb.Run(1, -1);
91 EXPECT_EQ(a, 0); 131 EXPECT_EQ(a, 0);
92 EXPECT_EQ(b, 0); 132 EXPECT_EQ(b, 0);
93 loop_.RunUntilIdle(); 133 loop_.RunUntilIdle();
94 EXPECT_EQ(a, 1); 134 EXPECT_EQ(a, 1);
95 EXPECT_EQ(b, -1); 135 EXPECT_EQ(b, -1);
96 } 136 }
97 137
98 } // namespace media 138 } // namespace media
OLDNEW
« no previous file with comments | « media/base/bind_to_loop.h.pump ('k') | webkit/media/webmediaplayer_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698