OLD | NEW |
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 <queue> | 5 #include <queue> |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
9 #include "content/browser/device_orientation/data_fetcher.h" | 9 #include "content/browser/device_orientation/data_fetcher.h" |
| 10 #include "content/browser/device_orientation/device_data.h" |
10 #include "content/browser/device_orientation/orientation.h" | 11 #include "content/browser/device_orientation/orientation.h" |
11 #include "content/browser/device_orientation/provider.h" | 12 #include "content/browser/device_orientation/provider.h" |
12 #include "content/browser/device_orientation/provider_impl.h" | 13 #include "content/browser/device_orientation/provider_impl.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace device_orientation { | 16 namespace device_orientation { |
16 namespace { | 17 namespace { |
17 | 18 |
18 // Class for checking expectations on orientation updates from the Provider. | 19 // Class for checking expectations on orientation updates from the Provider. |
19 class UpdateChecker : public Provider::Observer { | 20 class OrientationUpdateChecker : public Provider::Observer { |
20 public: | 21 public: |
21 explicit UpdateChecker(int* expectations_count_ptr) | 22 explicit OrientationUpdateChecker(int* expectations_count_ptr) |
22 : expectations_count_ptr_(expectations_count_ptr) { | 23 : Observer(DeviceData::kTypeOrientation), |
| 24 expectations_count_ptr_(expectations_count_ptr) { |
23 } | 25 } |
24 | 26 |
25 virtual ~UpdateChecker() {} | 27 virtual ~OrientationUpdateChecker() {} |
26 | 28 |
27 // From Provider::Observer. | 29 // From Provider::Observer. |
28 virtual void OnOrientationUpdate(const Orientation& orientation) { | 30 virtual void OnDeviceDataUpdate(const DeviceData* device_data, |
| 31 DeviceData::Type device_data_type) { |
29 ASSERT_FALSE(expectations_queue_.empty()); | 32 ASSERT_FALSE(expectations_queue_.empty()); |
30 | 33 |
31 Orientation expected = expectations_queue_.front(); | 34 scoped_refptr<const Orientation> orientation( |
| 35 static_cast<const Orientation*>(device_data)); |
| 36 if (orientation == NULL && device_data_type == DeviceData::kTypeOrientation) |
| 37 orientation = new Orientation(); |
| 38 else |
| 39 ASSERT_FALSE(orientation == NULL); |
| 40 |
| 41 scoped_refptr<const Orientation> expected = expectations_queue_.front(); |
32 expectations_queue_.pop(); | 42 expectations_queue_.pop(); |
33 | 43 |
34 EXPECT_EQ(expected.can_provide_alpha(), orientation.can_provide_alpha()); | 44 EXPECT_EQ(expected->can_provide_alpha(), orientation->can_provide_alpha()); |
35 EXPECT_EQ(expected.can_provide_beta(), orientation.can_provide_beta()); | 45 EXPECT_EQ(expected->can_provide_beta(), orientation->can_provide_beta()); |
36 EXPECT_EQ(expected.can_provide_gamma(), orientation.can_provide_gamma()); | 46 EXPECT_EQ(expected->can_provide_gamma(), orientation->can_provide_gamma()); |
37 EXPECT_EQ(expected.can_provide_absolute(), | 47 EXPECT_EQ(expected->can_provide_absolute(), |
38 orientation.can_provide_absolute()); | 48 orientation->can_provide_absolute()); |
39 if (expected.can_provide_alpha()) | 49 if (expected->can_provide_alpha()) |
40 EXPECT_EQ(expected.alpha(), orientation.alpha()); | 50 EXPECT_EQ(expected->alpha(), orientation->alpha()); |
41 if (expected.can_provide_beta()) | 51 if (expected->can_provide_beta()) |
42 EXPECT_EQ(expected.beta(), orientation.beta()); | 52 EXPECT_EQ(expected->beta(), orientation->beta()); |
43 if (expected.can_provide_gamma()) | 53 if (expected->can_provide_gamma()) |
44 EXPECT_EQ(expected.gamma(), orientation.gamma()); | 54 EXPECT_EQ(expected->gamma(), orientation->gamma()); |
45 if (expected.can_provide_absolute()) | 55 if (expected->can_provide_absolute()) |
46 EXPECT_EQ(expected.absolute(), orientation.absolute()); | 56 EXPECT_EQ(expected->absolute(), orientation->absolute()); |
47 | 57 |
48 --(*expectations_count_ptr_); | 58 --(*expectations_count_ptr_); |
49 | 59 |
50 if (*expectations_count_ptr_ == 0) { | 60 if (*expectations_count_ptr_ == 0) { |
51 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 61 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
52 } | 62 } |
53 } | 63 } |
54 | 64 |
55 void AddExpectation(const Orientation& orientation) { | 65 void AddExpectation(const Orientation* orientation) { |
56 expectations_queue_.push(orientation); | 66 scoped_refptr<const Orientation> expected_orientation(orientation); |
| 67 expectations_queue_.push(expected_orientation); |
57 ++(*expectations_count_ptr_); | 68 ++(*expectations_count_ptr_); |
58 } | 69 } |
59 | 70 |
60 private: | 71 private: |
61 // Set up by the test fixture, which then blocks while it is accessed | 72 // Set up by the test fixture, which then blocks while it is accessed |
62 // from OnOrientationUpdate which is executed on the test fixture's | 73 // from OnOrientationUpdate which is executed on the test fixture's |
63 // message_loop_. | 74 // message_loop_. |
64 int* expectations_count_ptr_; | 75 int* expectations_count_ptr_; |
65 std::queue<Orientation> expectations_queue_; | 76 std::queue<scoped_refptr<const Orientation> > expectations_queue_; |
66 }; | 77 }; |
67 | 78 |
68 // Class for injecting test orientation data into the Provider. | 79 // Class for injecting test orientation data into the Provider. |
69 class MockOrientationFactory : public base::RefCounted<MockOrientationFactory> { | 80 class MockOrientationFactory : public base::RefCounted<MockOrientationFactory> { |
70 public: | 81 public: |
71 MockOrientationFactory() | 82 MockOrientationFactory() |
72 : is_failing_(false) { | 83 : is_failing_(false) { |
73 EXPECT_FALSE(instance_); | 84 EXPECT_FALSE(instance_); |
74 instance_ = this; | 85 instance_ = this; |
75 } | 86 } |
76 | 87 |
77 static DataFetcher* CreateDataFetcher() { | 88 static DataFetcher* CreateDataFetcher() { |
78 EXPECT_TRUE(instance_); | 89 EXPECT_TRUE(instance_); |
79 return new MockDataFetcher(instance_); | 90 return new MockDataFetcher(instance_); |
80 } | 91 } |
81 | 92 |
82 void SetOrientation(const Orientation& orientation) { | 93 void SetOrientation(const Orientation* orientation) { |
83 base::AutoLock auto_lock(lock_); | 94 base::AutoLock auto_lock(lock_); |
84 orientation_ = orientation; | 95 orientation_ = orientation; |
85 } | 96 } |
86 | 97 |
87 void SetFailing(bool is_failing) { | 98 void SetFailing(bool is_failing) { |
88 base::AutoLock auto_lock(lock_); | 99 base::AutoLock auto_lock(lock_); |
89 is_failing_ = is_failing; | 100 is_failing_ = is_failing; |
90 } | 101 } |
91 | 102 |
92 private: | 103 private: |
93 friend class base::RefCounted<MockOrientationFactory>; | 104 friend class base::RefCounted<MockOrientationFactory>; |
94 | 105 |
95 ~MockOrientationFactory() { | 106 ~MockOrientationFactory() { |
96 instance_ = NULL; | 107 instance_ = NULL; |
97 } | 108 } |
98 | 109 |
99 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory. | 110 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory. |
100 class MockDataFetcher : public DataFetcher { | 111 class MockDataFetcher : public DataFetcher { |
101 public: | 112 public: |
102 explicit MockDataFetcher(MockOrientationFactory* orientation_factory) | 113 explicit MockDataFetcher(MockOrientationFactory* orientation_factory) |
103 : orientation_factory_(orientation_factory) { } | 114 : orientation_factory_(orientation_factory) { } |
104 | 115 |
105 // From DataFetcher. Called by the Provider. | 116 // From DataFetcher. Called by the Provider. |
106 virtual bool GetOrientation(Orientation* orientation) { | 117 virtual const DeviceData* GetDeviceData(DeviceData::Type device_data_type) { |
107 base::AutoLock auto_lock(orientation_factory_->lock_); | 118 base::AutoLock auto_lock(orientation_factory_->lock_); |
108 if (orientation_factory_->is_failing_) | 119 if (orientation_factory_->is_failing_) |
109 return false; | 120 return NULL; |
110 *orientation = orientation_factory_->orientation_; | 121 return orientation_factory_->orientation_; |
111 return true; | |
112 } | 122 } |
113 | 123 |
114 private: | 124 private: |
115 scoped_refptr<MockOrientationFactory> orientation_factory_; | 125 scoped_refptr<MockOrientationFactory> orientation_factory_; |
116 }; | 126 }; |
117 | 127 |
118 static MockOrientationFactory* instance_; | 128 static MockOrientationFactory* instance_; |
119 Orientation orientation_; | 129 scoped_refptr<const Orientation> orientation_; |
120 bool is_failing_; | 130 bool is_failing_; |
121 base::Lock lock_; | 131 base::Lock lock_; |
122 }; | 132 }; |
123 | 133 |
124 MockOrientationFactory* MockOrientationFactory::instance_; | 134 MockOrientationFactory* MockOrientationFactory::instance_; |
125 | 135 |
126 // Mock DataFetcher that always fails to provide any orientation data. | 136 // Mock DataFetcher that always fails to provide any orientation data. |
127 class FailingDataFetcher : public DataFetcher { | 137 class FailingDataFetcher : public DataFetcher { |
128 public: | 138 public: |
129 // Factory method; passed to and called by the ProviderImpl. | 139 // Factory method; passed to and called by the ProviderImpl. |
130 static DataFetcher* Create() { | 140 static DataFetcher* Create() { |
131 return new FailingDataFetcher(); | 141 return new FailingDataFetcher(); |
132 } | 142 } |
133 | 143 |
134 // From DataFetcher. | 144 // From DataFetcher. |
135 virtual bool GetOrientation(Orientation* orientation) { | 145 virtual const DeviceData* GetDeviceData(DeviceData::Type device_data_type) { |
136 return false; | 146 return NULL; |
137 } | 147 } |
138 | 148 |
139 private: | 149 private: |
140 FailingDataFetcher() {} | 150 FailingDataFetcher() {} |
141 }; | 151 }; |
142 | 152 |
143 class DeviceOrientationProviderTest : public testing::Test { | 153 class DeviceOrientationProviderTest : public testing::Test { |
144 public: | 154 public: |
145 DeviceOrientationProviderTest() | 155 DeviceOrientationProviderTest() |
146 : pending_expectations_(0) { | 156 : pending_expectations_(0) { |
(...skipping 30 matching lines...) Expand all Loading... |
177 // Provider instance under test. | 187 // Provider instance under test. |
178 scoped_refptr<Provider> provider_; | 188 scoped_refptr<Provider> provider_; |
179 | 189 |
180 // Message loop for the test thread. | 190 // Message loop for the test thread. |
181 MessageLoop message_loop_; | 191 MessageLoop message_loop_; |
182 }; | 192 }; |
183 | 193 |
184 TEST_F(DeviceOrientationProviderTest, FailingTest) { | 194 TEST_F(DeviceOrientationProviderTest, FailingTest) { |
185 Init(FailingDataFetcher::Create); | 195 Init(FailingDataFetcher::Create); |
186 | 196 |
187 scoped_ptr<UpdateChecker> checker_a( | 197 scoped_ptr<OrientationUpdateChecker> checker_a( |
188 new UpdateChecker(&pending_expectations_)); | 198 new OrientationUpdateChecker(&pending_expectations_)); |
189 scoped_ptr<UpdateChecker> checker_b( | 199 scoped_ptr<OrientationUpdateChecker> checker_b( |
190 new UpdateChecker(&pending_expectations_)); | 200 new OrientationUpdateChecker(&pending_expectations_)); |
191 | 201 |
192 checker_a->AddExpectation(Orientation::Empty()); | 202 checker_a->AddExpectation(new Orientation()); |
193 provider_->AddObserver(checker_a.get()); | 203 provider_->AddObserver(checker_a.get()); |
194 MessageLoop::current()->Run(); | 204 MessageLoop::current()->Run(); |
195 | 205 |
196 checker_b->AddExpectation(Orientation::Empty()); | 206 checker_b->AddExpectation(new Orientation()); |
197 provider_->AddObserver(checker_b.get()); | 207 provider_->AddObserver(checker_b.get()); |
198 MessageLoop::current()->Run(); | 208 MessageLoop::current()->Run(); |
199 } | 209 } |
200 | 210 |
201 TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) { | 211 TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) { |
202 Init(FailingDataFetcher::Create); | 212 Init(FailingDataFetcher::Create); |
203 | 213 |
204 scoped_refptr<Provider> provider_a(Provider::GetInstance()); | 214 scoped_refptr<Provider> provider_a(Provider::GetInstance()); |
205 scoped_refptr<Provider> provider_b(Provider::GetInstance()); | 215 scoped_refptr<Provider> provider_b(Provider::GetInstance()); |
206 | 216 |
207 EXPECT_EQ(provider_a.get(), provider_b.get()); | 217 EXPECT_EQ(provider_a.get(), provider_b.get()); |
208 } | 218 } |
209 | 219 |
210 TEST_F(DeviceOrientationProviderTest, BasicPushTest) { | 220 TEST_F(DeviceOrientationProviderTest, BasicPushTest) { |
211 scoped_refptr<MockOrientationFactory> orientation_factory( | 221 scoped_refptr<MockOrientationFactory> orientation_factory( |
212 new MockOrientationFactory()); | 222 new MockOrientationFactory()); |
213 Init(MockOrientationFactory::CreateDataFetcher); | 223 Init(MockOrientationFactory::CreateDataFetcher); |
214 Orientation test_orientation; | 224 scoped_refptr<Orientation> test_orientation(new Orientation()); |
215 test_orientation.set_alpha(1); | 225 test_orientation->set_alpha(1); |
216 test_orientation.set_beta(2); | 226 test_orientation->set_beta(2); |
217 test_orientation.set_gamma(3); | 227 test_orientation->set_gamma(3); |
218 test_orientation.set_absolute(true); | 228 test_orientation->set_absolute(true); |
219 | 229 |
220 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); | 230 scoped_ptr<OrientationUpdateChecker> checker( |
| 231 new OrientationUpdateChecker(&pending_expectations_)); |
221 checker->AddExpectation(test_orientation); | 232 checker->AddExpectation(test_orientation); |
222 orientation_factory->SetOrientation(test_orientation); | 233 orientation_factory->SetOrientation(test_orientation); |
223 provider_->AddObserver(checker.get()); | 234 provider_->AddObserver(checker.get()); |
224 MessageLoop::current()->Run(); | 235 MessageLoop::current()->Run(); |
225 | 236 |
226 provider_->RemoveObserver(checker.get()); | 237 provider_->RemoveObserver(checker.get()); |
227 } | 238 } |
228 | 239 |
229 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { | 240 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { |
230 scoped_refptr<MockOrientationFactory> orientation_factory( | 241 scoped_refptr<MockOrientationFactory> orientation_factory( |
231 new MockOrientationFactory()); | 242 new MockOrientationFactory()); |
232 Init(MockOrientationFactory::CreateDataFetcher); | 243 Init(MockOrientationFactory::CreateDataFetcher); |
233 | 244 |
234 Orientation test_orientations[] = {Orientation(), Orientation(), | 245 scoped_refptr<Orientation> test_orientations[] = {new Orientation(), |
235 Orientation()}; | 246 new Orientation(), new Orientation()}; |
236 test_orientations[0].set_alpha(1); | 247 test_orientations[0]->set_alpha(1); |
237 test_orientations[0].set_beta(2); | 248 test_orientations[0]->set_beta(2); |
238 test_orientations[0].set_gamma(3); | 249 test_orientations[0]->set_gamma(3); |
239 test_orientations[0].set_absolute(true); | 250 test_orientations[0]->set_absolute(true); |
240 | 251 |
241 test_orientations[1].set_alpha(4); | 252 test_orientations[1]->set_alpha(4); |
242 test_orientations[1].set_beta(5); | 253 test_orientations[1]->set_beta(5); |
243 test_orientations[1].set_gamma(6); | 254 test_orientations[1]->set_gamma(6); |
244 test_orientations[1].set_absolute(false); | 255 test_orientations[1]->set_absolute(false); |
245 | 256 |
246 test_orientations[2].set_alpha(7); | 257 test_orientations[2]->set_alpha(7); |
247 test_orientations[2].set_beta(8); | 258 test_orientations[2]->set_beta(8); |
248 test_orientations[2].set_gamma(9); | 259 test_orientations[2]->set_gamma(9); |
249 // can't provide absolute | 260 // can't provide absolute |
250 | 261 |
251 scoped_ptr<UpdateChecker> checker_a( | 262 scoped_ptr<OrientationUpdateChecker> checker_a( |
252 new UpdateChecker(&pending_expectations_)); | 263 new OrientationUpdateChecker(&pending_expectations_)); |
253 scoped_ptr<UpdateChecker> checker_b( | 264 scoped_ptr<OrientationUpdateChecker> checker_b( |
254 new UpdateChecker(&pending_expectations_)); | 265 new OrientationUpdateChecker(&pending_expectations_)); |
255 scoped_ptr<UpdateChecker> checker_c( | 266 scoped_ptr<OrientationUpdateChecker> checker_c( |
256 new UpdateChecker(&pending_expectations_)); | 267 new OrientationUpdateChecker(&pending_expectations_)); |
257 | 268 |
258 checker_a->AddExpectation(test_orientations[0]); | 269 checker_a->AddExpectation(test_orientations[0]); |
259 orientation_factory->SetOrientation(test_orientations[0]); | 270 orientation_factory->SetOrientation(test_orientations[0]); |
260 provider_->AddObserver(checker_a.get()); | 271 provider_->AddObserver(checker_a.get()); |
261 MessageLoop::current()->Run(); | 272 MessageLoop::current()->Run(); |
262 | 273 |
263 checker_a->AddExpectation(test_orientations[1]); | 274 checker_a->AddExpectation(test_orientations[1]); |
264 checker_b->AddExpectation(test_orientations[0]); | 275 checker_b->AddExpectation(test_orientations[0]); |
265 checker_b->AddExpectation(test_orientations[1]); | 276 checker_b->AddExpectation(test_orientations[1]); |
266 orientation_factory->SetOrientation(test_orientations[1]); | 277 orientation_factory->SetOrientation(test_orientations[1]); |
(...skipping 16 matching lines...) Expand all Loading... |
283 // Flakily DCHECKs on Linux. See crbug.com/104950. | 294 // Flakily DCHECKs on Linux. See crbug.com/104950. |
284 // FLAKY on Win. See crbug.com/104950. | 295 // FLAKY on Win. See crbug.com/104950. |
285 #define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved | 296 #define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved |
286 #else | 297 #else |
287 #define MAYBE_ObserverNotRemoved ObserverNotRemoved | 298 #define MAYBE_ObserverNotRemoved ObserverNotRemoved |
288 #endif | 299 #endif |
289 TEST_F(DeviceOrientationProviderTest, MAYBE_ObserverNotRemoved) { | 300 TEST_F(DeviceOrientationProviderTest, MAYBE_ObserverNotRemoved) { |
290 scoped_refptr<MockOrientationFactory> orientation_factory( | 301 scoped_refptr<MockOrientationFactory> orientation_factory( |
291 new MockOrientationFactory()); | 302 new MockOrientationFactory()); |
292 Init(MockOrientationFactory::CreateDataFetcher); | 303 Init(MockOrientationFactory::CreateDataFetcher); |
293 Orientation test_orientation; | 304 scoped_refptr<Orientation> test_orientation(new Orientation()); |
294 test_orientation.set_alpha(1); | 305 test_orientation->set_alpha(1); |
295 test_orientation.set_beta(2); | 306 test_orientation->set_beta(2); |
296 test_orientation.set_gamma(3); | 307 test_orientation->set_gamma(3); |
297 test_orientation.set_absolute(true); | 308 test_orientation->set_absolute(true); |
298 | 309 |
299 Orientation test_orientation2; | 310 scoped_refptr<Orientation> test_orientation2(new Orientation()); |
300 test_orientation2.set_alpha(4); | 311 test_orientation2->set_alpha(4); |
301 test_orientation2.set_beta(5); | 312 test_orientation2->set_beta(5); |
302 test_orientation2.set_gamma(6); | 313 test_orientation2->set_gamma(6); |
303 test_orientation2.set_absolute(false); | 314 test_orientation2->set_absolute(false); |
304 | 315 |
305 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); | 316 scoped_ptr<OrientationUpdateChecker> checker( |
| 317 new OrientationUpdateChecker(&pending_expectations_)); |
306 checker->AddExpectation(test_orientation); | 318 checker->AddExpectation(test_orientation); |
307 orientation_factory->SetOrientation(test_orientation); | 319 orientation_factory->SetOrientation(test_orientation); |
308 provider_->AddObserver(checker.get()); | 320 provider_->AddObserver(checker.get()); |
309 MessageLoop::current()->Run(); | 321 MessageLoop::current()->Run(); |
310 | 322 |
311 checker->AddExpectation(test_orientation2); | 323 checker->AddExpectation(test_orientation2); |
312 orientation_factory->SetOrientation(test_orientation2); | 324 orientation_factory->SetOrientation(test_orientation2); |
313 MessageLoop::current()->Run(); | 325 MessageLoop::current()->Run(); |
314 | 326 |
315 // Note that checker is not removed. This should not be a problem. | 327 // Note that checker is not removed. This should not be a problem. |
316 } | 328 } |
317 | 329 |
318 #if defined(OS_WIN) | 330 #if defined(OS_WIN) |
319 // FLAKY on Win. See crbug.com/104950. | 331 // FLAKY on Win. See crbug.com/104950. |
320 #define MAYBE_StartFailing DISABLED_StartFailing | 332 #define MAYBE_StartFailing DISABLED_StartFailing |
321 #else | 333 #else |
322 #define MAYBE_StartFailing StartFailing | 334 #define MAYBE_StartFailing StartFailing |
323 #endif | 335 #endif |
324 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { | 336 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { |
325 scoped_refptr<MockOrientationFactory> orientation_factory( | 337 scoped_refptr<MockOrientationFactory> orientation_factory( |
326 new MockOrientationFactory()); | 338 new MockOrientationFactory()); |
327 Init(MockOrientationFactory::CreateDataFetcher); | 339 Init(MockOrientationFactory::CreateDataFetcher); |
328 Orientation test_orientation; | 340 scoped_refptr<Orientation> test_orientation(new Orientation()); |
329 test_orientation.set_alpha(1); | 341 test_orientation->set_alpha(1); |
330 test_orientation.set_beta(2); | 342 test_orientation->set_beta(2); |
331 test_orientation.set_gamma(3); | 343 test_orientation->set_gamma(3); |
332 test_orientation.set_absolute(true); | 344 test_orientation->set_absolute(true); |
333 | 345 |
334 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( | 346 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker( |
335 &pending_expectations_)); | 347 &pending_expectations_)); |
336 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( | 348 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker( |
337 &pending_expectations_)); | 349 &pending_expectations_)); |
338 | 350 |
339 orientation_factory->SetOrientation(test_orientation); | 351 orientation_factory->SetOrientation(test_orientation); |
340 checker_a->AddExpectation(test_orientation); | 352 checker_a->AddExpectation(test_orientation); |
341 provider_->AddObserver(checker_a.get()); | 353 provider_->AddObserver(checker_a.get()); |
342 MessageLoop::current()->Run(); | 354 MessageLoop::current()->Run(); |
343 | 355 |
344 checker_a->AddExpectation(Orientation::Empty()); | 356 checker_a->AddExpectation(new Orientation()); |
345 orientation_factory->SetFailing(true); | 357 orientation_factory->SetFailing(true); |
346 MessageLoop::current()->Run(); | 358 MessageLoop::current()->Run(); |
347 | 359 |
348 checker_b->AddExpectation(Orientation::Empty()); | 360 checker_b->AddExpectation(new Orientation()); |
349 provider_->AddObserver(checker_b.get()); | 361 provider_->AddObserver(checker_b.get()); |
350 MessageLoop::current()->Run(); | 362 MessageLoop::current()->Run(); |
351 | 363 |
352 provider_->RemoveObserver(checker_a.get()); | 364 provider_->RemoveObserver(checker_a.get()); |
353 provider_->RemoveObserver(checker_b.get()); | 365 provider_->RemoveObserver(checker_b.get()); |
354 } | 366 } |
355 | 367 |
356 TEST_F(DeviceOrientationProviderTest, StartStopStart) { | 368 TEST_F(DeviceOrientationProviderTest, StartStopStart) { |
357 scoped_refptr<MockOrientationFactory> orientation_factory( | 369 scoped_refptr<MockOrientationFactory> orientation_factory( |
358 new MockOrientationFactory()); | 370 new MockOrientationFactory()); |
359 Init(MockOrientationFactory::CreateDataFetcher); | 371 Init(MockOrientationFactory::CreateDataFetcher); |
360 | 372 |
361 Orientation test_orientation; | 373 scoped_refptr<Orientation> test_orientation(new Orientation()); |
362 test_orientation.set_alpha(1); | 374 test_orientation->set_alpha(1); |
363 test_orientation.set_beta(2); | 375 test_orientation->set_beta(2); |
364 test_orientation.set_gamma(3); | 376 test_orientation->set_gamma(3); |
365 test_orientation.set_absolute(true); | 377 test_orientation->set_absolute(true); |
366 | 378 |
367 Orientation test_orientation2; | 379 scoped_refptr<Orientation> test_orientation2(new Orientation()); |
368 test_orientation2.set_alpha(4); | 380 test_orientation2->set_alpha(4); |
369 test_orientation2.set_beta(5); | 381 test_orientation2->set_beta(5); |
370 test_orientation2.set_gamma(6); | 382 test_orientation2->set_gamma(6); |
371 test_orientation2.set_absolute(false); | 383 test_orientation2->set_absolute(false); |
372 | 384 |
373 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( | 385 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker( |
374 &pending_expectations_)); | 386 &pending_expectations_)); |
375 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( | 387 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker( |
376 &pending_expectations_)); | 388 &pending_expectations_)); |
377 | 389 |
378 checker_a->AddExpectation(test_orientation); | 390 checker_a->AddExpectation(test_orientation); |
379 orientation_factory->SetOrientation(test_orientation); | 391 orientation_factory->SetOrientation(test_orientation); |
380 provider_->AddObserver(checker_a.get()); | 392 provider_->AddObserver(checker_a.get()); |
381 MessageLoop::current()->Run(); | 393 MessageLoop::current()->Run(); |
382 | 394 |
383 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. | 395 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. |
384 | 396 |
385 checker_b->AddExpectation(test_orientation2); | 397 checker_b->AddExpectation(test_orientation2); |
386 orientation_factory->SetOrientation(test_orientation2); | 398 orientation_factory->SetOrientation(test_orientation2); |
387 provider_->AddObserver(checker_b.get()); | 399 provider_->AddObserver(checker_b.get()); |
388 MessageLoop::current()->Run(); | 400 MessageLoop::current()->Run(); |
389 | 401 |
390 provider_->RemoveObserver(checker_b.get()); | 402 provider_->RemoveObserver(checker_b.get()); |
391 } | 403 } |
392 | 404 |
393 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { | 405 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { |
394 scoped_refptr<MockOrientationFactory> orientation_factory( | 406 scoped_refptr<MockOrientationFactory> orientation_factory( |
395 new MockOrientationFactory()); | 407 new MockOrientationFactory()); |
396 Init(MockOrientationFactory::CreateDataFetcher); | 408 Init(MockOrientationFactory::CreateDataFetcher); |
397 | 409 |
398 // Values that should be well below or above the implementation's | 410 // Values that should be well below or above the implementation's |
399 // significane threshold. | 411 // significane threshold. |
400 const double kInsignificantDifference = 1e-6; | 412 const double kInsignificantDifference = 1e-6; |
401 const double kSignificantDifference = 30; | 413 const double kSignificantDifference = 30; |
402 const double kAlpha = 4, kBeta = 5, kGamma = 6; | 414 const double kAlpha = 4, kBeta = 5, kGamma = 6; |
403 | 415 |
404 Orientation first_orientation; | 416 scoped_refptr<Orientation> first_orientation(new Orientation()); |
405 first_orientation.set_alpha(kAlpha); | 417 first_orientation->set_alpha(kAlpha); |
406 first_orientation.set_beta(kBeta); | 418 first_orientation->set_beta(kBeta); |
407 first_orientation.set_gamma(kGamma); | 419 first_orientation->set_gamma(kGamma); |
408 first_orientation.set_absolute(true); | 420 first_orientation->set_absolute(true); |
409 | 421 |
410 Orientation second_orientation; | 422 scoped_refptr<Orientation> second_orientation(new Orientation()); |
411 second_orientation.set_alpha(kAlpha + kInsignificantDifference); | 423 second_orientation->set_alpha(kAlpha + kInsignificantDifference); |
412 second_orientation.set_beta(kBeta + kInsignificantDifference); | 424 second_orientation->set_beta(kBeta + kInsignificantDifference); |
413 second_orientation.set_gamma(kGamma + kInsignificantDifference); | 425 second_orientation->set_gamma(kGamma + kInsignificantDifference); |
414 second_orientation.set_absolute(false); | 426 second_orientation->set_absolute(false); |
415 | 427 |
416 Orientation third_orientation; | 428 scoped_refptr<Orientation> third_orientation(new Orientation()); |
417 third_orientation.set_alpha(kAlpha + kSignificantDifference); | 429 third_orientation->set_alpha(kAlpha + kSignificantDifference); |
418 third_orientation.set_beta(kBeta + kSignificantDifference); | 430 third_orientation->set_beta(kBeta + kSignificantDifference); |
419 third_orientation.set_gamma(kGamma + kSignificantDifference); | 431 third_orientation->set_gamma(kGamma + kSignificantDifference); |
420 // can't provide absolute | 432 // can't provide absolute |
421 | 433 |
422 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( | 434 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker( |
423 &pending_expectations_)); | 435 &pending_expectations_)); |
424 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( | 436 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker( |
425 &pending_expectations_)); | 437 &pending_expectations_)); |
426 | 438 |
427 | |
428 orientation_factory->SetOrientation(first_orientation); | 439 orientation_factory->SetOrientation(first_orientation); |
429 checker_a->AddExpectation(first_orientation); | 440 checker_a->AddExpectation(first_orientation); |
430 provider_->AddObserver(checker_a.get()); | 441 provider_->AddObserver(checker_a.get()); |
431 MessageLoop::current()->Run(); | 442 MessageLoop::current()->Run(); |
432 | 443 |
433 // The observers should not see this insignificantly different orientation. | 444 // The observers should not see this insignificantly different orientation. |
434 orientation_factory->SetOrientation(second_orientation); | 445 orientation_factory->SetOrientation(second_orientation); |
435 checker_b->AddExpectation(first_orientation); | 446 checker_b->AddExpectation(first_orientation); |
436 provider_->AddObserver(checker_b.get()); | 447 provider_->AddObserver(checker_b.get()); |
437 MessageLoop::current()->Run(); | 448 MessageLoop::current()->Run(); |
438 | 449 |
439 orientation_factory->SetOrientation(third_orientation); | 450 orientation_factory->SetOrientation(third_orientation); |
440 checker_a->AddExpectation(third_orientation); | 451 checker_a->AddExpectation(third_orientation); |
441 checker_b->AddExpectation(third_orientation); | 452 checker_b->AddExpectation(third_orientation); |
442 MessageLoop::current()->Run(); | 453 MessageLoop::current()->Run(); |
443 | 454 |
444 provider_->RemoveObserver(checker_a.get()); | 455 provider_->RemoveObserver(checker_a.get()); |
445 provider_->RemoveObserver(checker_b.get()); | 456 provider_->RemoveObserver(checker_b.get()); |
446 } | 457 } |
447 | 458 |
448 } // namespace | 459 } // namespace |
449 | 460 |
450 } // namespace device_orientation | 461 } // namespace device_orientation |
OLD | NEW |