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

Side by Side Diff: content/browser/device_orientation/provider_unittest.cc

Issue 10755002: Refactors DeviceOrientation to make it more extensible (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 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
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 <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/orientation.h" 10 #include "content/browser/device_orientation/orientation.h"
11 #include "content/browser/device_orientation/provider.h" 11 #include "content/browser/device_orientation/provider.h"
12 #include "content/browser/device_orientation/provider_impl.h" 12 #include "content/browser/device_orientation/provider_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace device_orientation { 15 namespace device_orientation {
16 namespace { 16 namespace {
17 17
18 // Class for checking expectations on orientation updates from the Provider. 18 // Class for checking expectations on orientation updates from the Provider.
19 class UpdateChecker : public Provider::Observer { 19 class OrientationUpdateChecker : public Provider::Observer {
20 public: 20 public:
21 explicit UpdateChecker(int* expectations_count_ptr) 21 explicit OrientationUpdateChecker(int* expectations_count_ptr)
22 : expectations_count_ptr_(expectations_count_ptr) { 22 : expectations_count_ptr_(expectations_count_ptr) {
23 } 23 }
24 24
25 virtual ~UpdateChecker() {} 25 virtual ~OrientationUpdateChecker() {}
26 26
27 // From Provider::Observer. 27 // From Provider::Observer.
28 virtual void OnOrientationUpdate(const Orientation& orientation) { 28 virtual void OnDeviceDataUpdate(const DeviceData& device_data) {
29 ASSERT_FALSE(expectations_queue_.empty()); 29 ASSERT_FALSE(expectations_queue_.empty());
30 30
31 const Orientation& orientation = static_cast<const Orientation&>(
32 device_data);
31 Orientation expected = expectations_queue_.front(); 33 Orientation expected = expectations_queue_.front();
32 expectations_queue_.pop(); 34 expectations_queue_.pop();
33 35
34 EXPECT_EQ(expected.can_provide_alpha(), orientation.can_provide_alpha()); 36 EXPECT_EQ(expected.can_provide_alpha(), orientation.can_provide_alpha());
35 EXPECT_EQ(expected.can_provide_beta(), orientation.can_provide_beta()); 37 EXPECT_EQ(expected.can_provide_beta(), orientation.can_provide_beta());
36 EXPECT_EQ(expected.can_provide_gamma(), orientation.can_provide_gamma()); 38 EXPECT_EQ(expected.can_provide_gamma(), orientation.can_provide_gamma());
37 EXPECT_EQ(expected.can_provide_absolute(), 39 EXPECT_EQ(expected.can_provide_absolute(),
38 orientation.can_provide_absolute()); 40 orientation.can_provide_absolute());
39 if (expected.can_provide_alpha()) 41 if (expected.can_provide_alpha())
40 EXPECT_EQ(expected.alpha(), orientation.alpha()); 42 EXPECT_EQ(expected.alpha(), orientation.alpha());
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 instance_ = NULL; 98 instance_ = NULL;
97 } 99 }
98 100
99 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory. 101 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory.
100 class MockDataFetcher : public DataFetcher { 102 class MockDataFetcher : public DataFetcher {
101 public: 103 public:
102 explicit MockDataFetcher(MockOrientationFactory* orientation_factory) 104 explicit MockDataFetcher(MockOrientationFactory* orientation_factory)
103 : orientation_factory_(orientation_factory) { } 105 : orientation_factory_(orientation_factory) { }
104 106
105 // From DataFetcher. Called by the Provider. 107 // From DataFetcher. Called by the Provider.
106 virtual bool GetOrientation(Orientation* orientation) { 108 virtual bool GetDeviceData(DeviceData* device_data) {
107 base::AutoLock auto_lock(orientation_factory_->lock_); 109 base::AutoLock auto_lock(orientation_factory_->lock_);
108 if (orientation_factory_->is_failing_) 110 if (orientation_factory_->is_failing_)
109 return false; 111 return false;
112 Orientation* orientation = static_cast<Orientation*>(device_data);
110 *orientation = orientation_factory_->orientation_; 113 *orientation = orientation_factory_->orientation_;
111 return true; 114 return true;
112 } 115 }
113 116
114 private: 117 private:
115 scoped_refptr<MockOrientationFactory> orientation_factory_; 118 scoped_refptr<MockOrientationFactory> orientation_factory_;
116 }; 119 };
117 120
118 static MockOrientationFactory* instance_; 121 static MockOrientationFactory* instance_;
119 Orientation orientation_; 122 Orientation orientation_;
120 bool is_failing_; 123 bool is_failing_;
121 base::Lock lock_; 124 base::Lock lock_;
122 }; 125 };
123 126
124 MockOrientationFactory* MockOrientationFactory::instance_; 127 MockOrientationFactory* MockOrientationFactory::instance_;
125 128
126 // Mock DataFetcher that always fails to provide any orientation data. 129 // Mock DataFetcher that always fails to provide any orientation data.
127 class FailingDataFetcher : public DataFetcher { 130 class FailingDataFetcher : public DataFetcher {
128 public: 131 public:
129 // Factory method; passed to and called by the ProviderImpl. 132 // Factory method; passed to and called by the ProviderImpl.
130 static DataFetcher* Create() { 133 static DataFetcher* Create() {
131 return new FailingDataFetcher(); 134 return new FailingDataFetcher();
132 } 135 }
133 136
134 // From DataFetcher. 137 // From DataFetcher.
135 virtual bool GetOrientation(Orientation* orientation) { 138 virtual bool GetDeviceData(DeviceData* device_data) {
136 return false; 139 return false;
137 } 140 }
138 141
139 private: 142 private:
140 FailingDataFetcher() {} 143 FailingDataFetcher() {}
141 }; 144 };
142 145
143 class DeviceOrientationProviderTest : public testing::Test { 146 class DeviceOrientationProviderTest : public testing::Test {
144 public: 147 public:
145 DeviceOrientationProviderTest() 148 DeviceOrientationProviderTest()
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // Provider instance under test. 180 // Provider instance under test.
178 scoped_refptr<Provider> provider_; 181 scoped_refptr<Provider> provider_;
179 182
180 // Message loop for the test thread. 183 // Message loop for the test thread.
181 MessageLoop message_loop_; 184 MessageLoop message_loop_;
182 }; 185 };
183 186
184 TEST_F(DeviceOrientationProviderTest, FailingTest) { 187 TEST_F(DeviceOrientationProviderTest, FailingTest) {
185 Init(FailingDataFetcher::Create); 188 Init(FailingDataFetcher::Create);
186 189
187 scoped_ptr<UpdateChecker> checker_a( 190 scoped_ptr<OrientationUpdateChecker> checker_a(
188 new UpdateChecker(&pending_expectations_)); 191 new OrientationUpdateChecker(&pending_expectations_));
189 scoped_ptr<UpdateChecker> checker_b( 192 scoped_ptr<OrientationUpdateChecker> checker_b(
190 new UpdateChecker(&pending_expectations_)); 193 new OrientationUpdateChecker(&pending_expectations_));
191 194
192 checker_a->AddExpectation(Orientation::Empty()); 195 checker_a->AddExpectation(Orientation::Empty());
193 provider_->AddObserver(checker_a.get()); 196 provider_->AddObserver(checker_a.get());
194 MessageLoop::current()->Run(); 197 MessageLoop::current()->Run();
195 198
196 checker_b->AddExpectation(Orientation::Empty()); 199 checker_b->AddExpectation(Orientation::Empty());
197 provider_->AddObserver(checker_b.get()); 200 provider_->AddObserver(checker_b.get());
198 MessageLoop::current()->Run(); 201 MessageLoop::current()->Run();
199 } 202 }
200 203
201 TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) { 204 TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) {
202 Init(FailingDataFetcher::Create); 205 Init(FailingDataFetcher::Create);
203 206
204 scoped_refptr<Provider> provider_a(Provider::GetInstance()); 207 scoped_refptr<Provider> provider_a(Provider::GetInstance());
205 scoped_refptr<Provider> provider_b(Provider::GetInstance()); 208 scoped_refptr<Provider> provider_b(Provider::GetInstance());
206 209
207 EXPECT_EQ(provider_a.get(), provider_b.get()); 210 EXPECT_EQ(provider_a.get(), provider_b.get());
208 } 211 }
209 212
210 TEST_F(DeviceOrientationProviderTest, BasicPushTest) { 213 TEST_F(DeviceOrientationProviderTest, BasicPushTest) {
211 scoped_refptr<MockOrientationFactory> orientation_factory( 214 scoped_refptr<MockOrientationFactory> orientation_factory(
212 new MockOrientationFactory()); 215 new MockOrientationFactory());
213 Init(MockOrientationFactory::CreateDataFetcher); 216 Init(MockOrientationFactory::CreateDataFetcher);
214 Orientation test_orientation; 217 Orientation test_orientation;
215 test_orientation.set_alpha(1); 218 test_orientation.set_alpha(1);
216 test_orientation.set_beta(2); 219 test_orientation.set_beta(2);
217 test_orientation.set_gamma(3); 220 test_orientation.set_gamma(3);
218 test_orientation.set_absolute(true); 221 test_orientation.set_absolute(true);
219 222
220 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); 223 scoped_ptr<OrientationUpdateChecker> checker(
224 new OrientationUpdateChecker(&pending_expectations_));
221 checker->AddExpectation(test_orientation); 225 checker->AddExpectation(test_orientation);
222 orientation_factory->SetOrientation(test_orientation); 226 orientation_factory->SetOrientation(test_orientation);
223 provider_->AddObserver(checker.get()); 227 provider_->AddObserver(checker.get());
224 MessageLoop::current()->Run(); 228 MessageLoop::current()->Run();
225 229
226 provider_->RemoveObserver(checker.get()); 230 provider_->RemoveObserver(checker.get());
227 } 231 }
228 232
229 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { 233 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) {
230 scoped_refptr<MockOrientationFactory> orientation_factory( 234 scoped_refptr<MockOrientationFactory> orientation_factory(
(...skipping 10 matching lines...) Expand all
241 test_orientations[1].set_alpha(4); 245 test_orientations[1].set_alpha(4);
242 test_orientations[1].set_beta(5); 246 test_orientations[1].set_beta(5);
243 test_orientations[1].set_gamma(6); 247 test_orientations[1].set_gamma(6);
244 test_orientations[1].set_absolute(false); 248 test_orientations[1].set_absolute(false);
245 249
246 test_orientations[2].set_alpha(7); 250 test_orientations[2].set_alpha(7);
247 test_orientations[2].set_beta(8); 251 test_orientations[2].set_beta(8);
248 test_orientations[2].set_gamma(9); 252 test_orientations[2].set_gamma(9);
249 // can't provide absolute 253 // can't provide absolute
250 254
251 scoped_ptr<UpdateChecker> checker_a( 255 scoped_ptr<OrientationUpdateChecker> checker_a(
252 new UpdateChecker(&pending_expectations_)); 256 new OrientationUpdateChecker(&pending_expectations_));
253 scoped_ptr<UpdateChecker> checker_b( 257 scoped_ptr<OrientationUpdateChecker> checker_b(
254 new UpdateChecker(&pending_expectations_)); 258 new OrientationUpdateChecker(&pending_expectations_));
255 scoped_ptr<UpdateChecker> checker_c( 259 scoped_ptr<OrientationUpdateChecker> checker_c(
256 new UpdateChecker(&pending_expectations_)); 260 new OrientationUpdateChecker(&pending_expectations_));
257 261
258 checker_a->AddExpectation(test_orientations[0]); 262 checker_a->AddExpectation(test_orientations[0]);
259 orientation_factory->SetOrientation(test_orientations[0]); 263 orientation_factory->SetOrientation(test_orientations[0]);
260 provider_->AddObserver(checker_a.get()); 264 provider_->AddObserver(checker_a.get());
261 MessageLoop::current()->Run(); 265 MessageLoop::current()->Run();
262 266
263 checker_a->AddExpectation(test_orientations[1]); 267 checker_a->AddExpectation(test_orientations[1]);
264 checker_b->AddExpectation(test_orientations[0]); 268 checker_b->AddExpectation(test_orientations[0]);
265 checker_b->AddExpectation(test_orientations[1]); 269 checker_b->AddExpectation(test_orientations[1]);
266 orientation_factory->SetOrientation(test_orientations[1]); 270 orientation_factory->SetOrientation(test_orientations[1]);
(...skipping 28 matching lines...) Expand all
295 test_orientation.set_beta(2); 299 test_orientation.set_beta(2);
296 test_orientation.set_gamma(3); 300 test_orientation.set_gamma(3);
297 test_orientation.set_absolute(true); 301 test_orientation.set_absolute(true);
298 302
299 Orientation test_orientation2; 303 Orientation test_orientation2;
300 test_orientation2.set_alpha(4); 304 test_orientation2.set_alpha(4);
301 test_orientation2.set_beta(5); 305 test_orientation2.set_beta(5);
302 test_orientation2.set_gamma(6); 306 test_orientation2.set_gamma(6);
303 test_orientation2.set_absolute(false); 307 test_orientation2.set_absolute(false);
304 308
305 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); 309 scoped_ptr<OrientationUpdateChecker> checker(
310 new OrientationUpdateChecker(&pending_expectations_));
306 checker->AddExpectation(test_orientation); 311 checker->AddExpectation(test_orientation);
307 orientation_factory->SetOrientation(test_orientation); 312 orientation_factory->SetOrientation(test_orientation);
308 provider_->AddObserver(checker.get()); 313 provider_->AddObserver(checker.get());
309 MessageLoop::current()->Run(); 314 MessageLoop::current()->Run();
310 315
311 checker->AddExpectation(test_orientation2); 316 checker->AddExpectation(test_orientation2);
312 orientation_factory->SetOrientation(test_orientation2); 317 orientation_factory->SetOrientation(test_orientation2);
313 MessageLoop::current()->Run(); 318 MessageLoop::current()->Run();
314 319
315 // Note that checker is not removed. This should not be a problem. 320 // Note that checker is not removed. This should not be a problem.
316 } 321 }
317 322
318 #if defined(OS_WIN) 323 #if defined(OS_WIN)
319 // FLAKY on Win. See crbug.com/104950. 324 // FLAKY on Win. See crbug.com/104950.
320 #define MAYBE_StartFailing DISABLED_StartFailing 325 #define MAYBE_StartFailing DISABLED_StartFailing
321 #else 326 #else
322 #define MAYBE_StartFailing StartFailing 327 #define MAYBE_StartFailing StartFailing
323 #endif 328 #endif
324 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { 329 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) {
325 scoped_refptr<MockOrientationFactory> orientation_factory( 330 scoped_refptr<MockOrientationFactory> orientation_factory(
326 new MockOrientationFactory()); 331 new MockOrientationFactory());
327 Init(MockOrientationFactory::CreateDataFetcher); 332 Init(MockOrientationFactory::CreateDataFetcher);
328 Orientation test_orientation; 333 Orientation test_orientation;
329 test_orientation.set_alpha(1); 334 test_orientation.set_alpha(1);
330 test_orientation.set_beta(2); 335 test_orientation.set_beta(2);
331 test_orientation.set_gamma(3); 336 test_orientation.set_gamma(3);
332 test_orientation.set_absolute(true); 337 test_orientation.set_absolute(true);
333 338
334 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 339 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
335 &pending_expectations_)); 340 &pending_expectations_));
336 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 341 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
337 &pending_expectations_)); 342 &pending_expectations_));
338 343
339 orientation_factory->SetOrientation(test_orientation); 344 orientation_factory->SetOrientation(test_orientation);
340 checker_a->AddExpectation(test_orientation); 345 checker_a->AddExpectation(test_orientation);
341 provider_->AddObserver(checker_a.get()); 346 provider_->AddObserver(checker_a.get());
342 MessageLoop::current()->Run(); 347 MessageLoop::current()->Run();
343 348
344 checker_a->AddExpectation(Orientation::Empty()); 349 checker_a->AddExpectation(Orientation::Empty());
345 orientation_factory->SetFailing(true); 350 orientation_factory->SetFailing(true);
346 MessageLoop::current()->Run(); 351 MessageLoop::current()->Run();
(...skipping 16 matching lines...) Expand all
363 test_orientation.set_beta(2); 368 test_orientation.set_beta(2);
364 test_orientation.set_gamma(3); 369 test_orientation.set_gamma(3);
365 test_orientation.set_absolute(true); 370 test_orientation.set_absolute(true);
366 371
367 Orientation test_orientation2; 372 Orientation test_orientation2;
368 test_orientation2.set_alpha(4); 373 test_orientation2.set_alpha(4);
369 test_orientation2.set_beta(5); 374 test_orientation2.set_beta(5);
370 test_orientation2.set_gamma(6); 375 test_orientation2.set_gamma(6);
371 test_orientation2.set_absolute(false); 376 test_orientation2.set_absolute(false);
372 377
373 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 378 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
374 &pending_expectations_)); 379 &pending_expectations_));
375 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 380 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
376 &pending_expectations_)); 381 &pending_expectations_));
377 382
378 checker_a->AddExpectation(test_orientation); 383 checker_a->AddExpectation(test_orientation);
379 orientation_factory->SetOrientation(test_orientation); 384 orientation_factory->SetOrientation(test_orientation);
380 provider_->AddObserver(checker_a.get()); 385 provider_->AddObserver(checker_a.get());
381 MessageLoop::current()->Run(); 386 MessageLoop::current()->Run();
382 387
383 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. 388 provider_->RemoveObserver(checker_a.get()); // This stops the Provider.
384 389
385 checker_b->AddExpectation(test_orientation2); 390 checker_b->AddExpectation(test_orientation2);
(...skipping 26 matching lines...) Expand all
412 second_orientation.set_beta(kBeta + kInsignificantDifference); 417 second_orientation.set_beta(kBeta + kInsignificantDifference);
413 second_orientation.set_gamma(kGamma + kInsignificantDifference); 418 second_orientation.set_gamma(kGamma + kInsignificantDifference);
414 second_orientation.set_absolute(false); 419 second_orientation.set_absolute(false);
415 420
416 Orientation third_orientation; 421 Orientation third_orientation;
417 third_orientation.set_alpha(kAlpha + kSignificantDifference); 422 third_orientation.set_alpha(kAlpha + kSignificantDifference);
418 third_orientation.set_beta(kBeta + kSignificantDifference); 423 third_orientation.set_beta(kBeta + kSignificantDifference);
419 third_orientation.set_gamma(kGamma + kSignificantDifference); 424 third_orientation.set_gamma(kGamma + kSignificantDifference);
420 // can't provide absolute 425 // can't provide absolute
421 426
422 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 427 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
423 &pending_expectations_)); 428 &pending_expectations_));
424 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 429 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
425 &pending_expectations_)); 430 &pending_expectations_));
426 431
427 432
428 orientation_factory->SetOrientation(first_orientation); 433 orientation_factory->SetOrientation(first_orientation);
429 checker_a->AddExpectation(first_orientation); 434 checker_a->AddExpectation(first_orientation);
430 provider_->AddObserver(checker_a.get()); 435 provider_->AddObserver(checker_a.get());
431 MessageLoop::current()->Run(); 436 MessageLoop::current()->Run();
432 437
433 // The observers should not see this insignificantly different orientation. 438 // The observers should not see this insignificantly different orientation.
434 orientation_factory->SetOrientation(second_orientation); 439 orientation_factory->SetOrientation(second_orientation);
435 checker_b->AddExpectation(first_orientation); 440 checker_b->AddExpectation(first_orientation);
436 provider_->AddObserver(checker_b.get()); 441 provider_->AddObserver(checker_b.get());
437 MessageLoop::current()->Run(); 442 MessageLoop::current()->Run();
438 443
439 orientation_factory->SetOrientation(third_orientation); 444 orientation_factory->SetOrientation(third_orientation);
440 checker_a->AddExpectation(third_orientation); 445 checker_a->AddExpectation(third_orientation);
441 checker_b->AddExpectation(third_orientation); 446 checker_b->AddExpectation(third_orientation);
442 MessageLoop::current()->Run(); 447 MessageLoop::current()->Run();
443 448
444 provider_->RemoveObserver(checker_a.get()); 449 provider_->RemoveObserver(checker_a.get());
445 provider_->RemoveObserver(checker_b.get()); 450 provider_->RemoveObserver(checker_b.get());
446 } 451 }
447 452
448 } // namespace 453 } // namespace
449 454
450 } // namespace device_orientation 455 } // namespace device_orientation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698