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/orientation.h" | 10 #include "content/browser/device_orientation/orientation.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 // message_loop_. | 63 // message_loop_. |
64 int* expectations_count_ptr_; | 64 int* expectations_count_ptr_; |
65 std::queue<Orientation> expectations_queue_; | 65 std::queue<Orientation> expectations_queue_; |
66 }; | 66 }; |
67 | 67 |
68 // Class for injecting test orientation data into the Provider. | 68 // Class for injecting test orientation data into the Provider. |
69 class MockOrientationFactory : public base::RefCounted<MockOrientationFactory> { | 69 class MockOrientationFactory : public base::RefCounted<MockOrientationFactory> { |
70 public: | 70 public: |
71 MockOrientationFactory() | 71 MockOrientationFactory() |
72 : is_failing_(false) { | 72 : is_failing_(false) { |
73 EXPECT_FALSE(instance_); | 73 } |
74 instance_ = this; | 74 |
75 static void SetCurInstance(MockOrientationFactory* instance) { | |
76 if (instance) { | |
77 EXPECT_FALSE(instance_); | |
78 } | |
79 else { | |
80 EXPECT_TRUE(instance_); | |
81 } | |
82 instance_ = instance; | |
75 } | 83 } |
76 | 84 |
77 static DataFetcher* CreateDataFetcher() { | 85 static DataFetcher* CreateDataFetcher() { |
78 EXPECT_TRUE(instance_); | 86 EXPECT_TRUE(instance_); |
79 return new MockDataFetcher(instance_); | 87 return new MockDataFetcher(instance_); |
80 } | 88 } |
81 | 89 |
82 void SetOrientation(const Orientation& orientation) { | 90 void SetOrientation(const Orientation& orientation) { |
83 base::AutoLock auto_lock(lock_); | 91 base::AutoLock auto_lock(lock_); |
84 orientation_ = orientation; | 92 orientation_ = orientation; |
85 } | 93 } |
86 | 94 |
87 void SetFailing(bool is_failing) { | 95 void SetFailing(bool is_failing) { |
88 base::AutoLock auto_lock(lock_); | 96 base::AutoLock auto_lock(lock_); |
89 is_failing_ = is_failing; | 97 is_failing_ = is_failing; |
90 } | 98 } |
91 | 99 |
92 private: | 100 private: |
93 friend class base::RefCounted<MockOrientationFactory>; | 101 friend class base::RefCounted<MockOrientationFactory>; |
94 | 102 |
95 ~MockOrientationFactory() { | 103 ~MockOrientationFactory() { |
96 instance_ = NULL; | |
97 } | 104 } |
98 | 105 |
99 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory. | 106 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory. |
100 class MockDataFetcher : public DataFetcher { | 107 class MockDataFetcher : public DataFetcher { |
101 public: | 108 public: |
102 explicit MockDataFetcher(MockOrientationFactory* orientation_factory) | 109 explicit MockDataFetcher(MockOrientationFactory* orientation_factory) |
103 : orientation_factory_(orientation_factory) { } | 110 : orientation_factory_(orientation_factory) { } |
104 | 111 |
105 // From DataFetcher. Called by the Provider. | 112 // From DataFetcher. Called by the Provider. |
106 virtual bool GetOrientation(Orientation* orientation) { | 113 virtual bool GetOrientation(Orientation* orientation) { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 | 210 |
204 scoped_refptr<Provider> provider_a(Provider::GetInstance()); | 211 scoped_refptr<Provider> provider_a(Provider::GetInstance()); |
205 scoped_refptr<Provider> provider_b(Provider::GetInstance()); | 212 scoped_refptr<Provider> provider_b(Provider::GetInstance()); |
206 | 213 |
207 EXPECT_EQ(provider_a.get(), provider_b.get()); | 214 EXPECT_EQ(provider_a.get(), provider_b.get()); |
208 } | 215 } |
209 | 216 |
210 TEST_F(DeviceOrientationProviderTest, BasicPushTest) { | 217 TEST_F(DeviceOrientationProviderTest, BasicPushTest) { |
211 scoped_refptr<MockOrientationFactory> orientation_factory( | 218 scoped_refptr<MockOrientationFactory> orientation_factory( |
212 new MockOrientationFactory()); | 219 new MockOrientationFactory()); |
220 MockOrientationFactory::SetCurInstance(orientation_factory.get()); | |
213 Init(MockOrientationFactory::CreateDataFetcher); | 221 Init(MockOrientationFactory::CreateDataFetcher); |
214 Orientation test_orientation; | 222 Orientation test_orientation; |
215 test_orientation.set_alpha(1); | 223 test_orientation.set_alpha(1); |
216 test_orientation.set_beta(2); | 224 test_orientation.set_beta(2); |
217 test_orientation.set_gamma(3); | 225 test_orientation.set_gamma(3); |
218 test_orientation.set_absolute(true); | 226 test_orientation.set_absolute(true); |
219 | 227 |
220 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); | 228 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); |
221 checker->AddExpectation(test_orientation); | 229 checker->AddExpectation(test_orientation); |
222 orientation_factory->SetOrientation(test_orientation); | 230 orientation_factory->SetOrientation(test_orientation); |
223 provider_->AddObserver(checker.get()); | 231 provider_->AddObserver(checker.get()); |
224 MessageLoop::current()->Run(); | 232 MessageLoop::current()->Run(); |
225 | 233 |
226 provider_->RemoveObserver(checker.get()); | 234 provider_->RemoveObserver(checker.get()); |
235 MockOrientationFactory::SetCurInstance(NULL); | |
227 } | 236 } |
228 | 237 |
229 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { | 238 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { |
230 scoped_refptr<MockOrientationFactory> orientation_factory( | 239 scoped_refptr<MockOrientationFactory> orientation_factory( |
231 new MockOrientationFactory()); | 240 new MockOrientationFactory()); |
241 MockOrientationFactory::SetCurInstance(orientation_factory.get()); | |
232 Init(MockOrientationFactory::CreateDataFetcher); | 242 Init(MockOrientationFactory::CreateDataFetcher); |
233 | 243 |
234 Orientation test_orientations[] = {Orientation(), Orientation(), | 244 Orientation test_orientations[] = {Orientation(), Orientation(), |
235 Orientation()}; | 245 Orientation()}; |
236 test_orientations[0].set_alpha(1); | 246 test_orientations[0].set_alpha(1); |
237 test_orientations[0].set_beta(2); | 247 test_orientations[0].set_beta(2); |
238 test_orientations[0].set_gamma(3); | 248 test_orientations[0].set_gamma(3); |
239 test_orientations[0].set_absolute(true); | 249 test_orientations[0].set_absolute(true); |
240 | 250 |
241 test_orientations[1].set_alpha(4); | 251 test_orientations[1].set_alpha(4); |
(...skipping 28 matching lines...) Expand all Loading... | |
270 provider_->RemoveObserver(checker_a.get()); | 280 provider_->RemoveObserver(checker_a.get()); |
271 checker_b->AddExpectation(test_orientations[2]); | 281 checker_b->AddExpectation(test_orientations[2]); |
272 checker_c->AddExpectation(test_orientations[1]); | 282 checker_c->AddExpectation(test_orientations[1]); |
273 checker_c->AddExpectation(test_orientations[2]); | 283 checker_c->AddExpectation(test_orientations[2]); |
274 orientation_factory->SetOrientation(test_orientations[2]); | 284 orientation_factory->SetOrientation(test_orientations[2]); |
275 provider_->AddObserver(checker_c.get()); | 285 provider_->AddObserver(checker_c.get()); |
276 MessageLoop::current()->Run(); | 286 MessageLoop::current()->Run(); |
277 | 287 |
278 provider_->RemoveObserver(checker_b.get()); | 288 provider_->RemoveObserver(checker_b.get()); |
279 provider_->RemoveObserver(checker_c.get()); | 289 provider_->RemoveObserver(checker_c.get()); |
290 MockOrientationFactory::SetCurInstance(NULL); | |
280 } | 291 } |
281 | 292 |
282 #if defined(OS_LINUX) || defined(OS_WIN) | 293 #if defined(OS_LINUX) || defined(OS_WIN) |
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()); |
303 MockOrientationFactory::SetCurInstance(orientation_factory.get()); | |
292 Init(MockOrientationFactory::CreateDataFetcher); | 304 Init(MockOrientationFactory::CreateDataFetcher); |
293 Orientation test_orientation; | 305 Orientation test_orientation; |
294 test_orientation.set_alpha(1); | 306 test_orientation.set_alpha(1); |
295 test_orientation.set_beta(2); | 307 test_orientation.set_beta(2); |
296 test_orientation.set_gamma(3); | 308 test_orientation.set_gamma(3); |
297 test_orientation.set_absolute(true); | 309 test_orientation.set_absolute(true); |
298 | 310 |
299 Orientation test_orientation2; | 311 Orientation test_orientation2; |
300 test_orientation2.set_alpha(4); | 312 test_orientation2.set_alpha(4); |
301 test_orientation2.set_beta(5); | 313 test_orientation2.set_beta(5); |
302 test_orientation2.set_gamma(6); | 314 test_orientation2.set_gamma(6); |
303 test_orientation2.set_absolute(false); | 315 test_orientation2.set_absolute(false); |
304 | 316 |
305 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); | 317 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&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. |
328 MockOrientationFactory::SetCurInstance(NULL); | |
hans
2012/07/30 12:56:40
maybe insert a blank line before this, so it doesn
| |
316 } | 329 } |
317 | 330 |
318 #if defined(OS_WIN) | 331 #if defined(OS_WIN) |
319 // FLAKY on Win. See crbug.com/104950. | 332 // FLAKY on Win. See crbug.com/104950. |
320 #define MAYBE_StartFailing DISABLED_StartFailing | 333 #define MAYBE_StartFailing DISABLED_StartFailing |
321 #else | 334 #else |
322 #define MAYBE_StartFailing StartFailing | 335 #define MAYBE_StartFailing StartFailing |
323 #endif | 336 #endif |
324 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { | 337 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { |
325 scoped_refptr<MockOrientationFactory> orientation_factory( | 338 scoped_refptr<MockOrientationFactory> orientation_factory( |
326 new MockOrientationFactory()); | 339 new MockOrientationFactory()); |
340 MockOrientationFactory::SetCurInstance(orientation_factory.get()); | |
327 Init(MockOrientationFactory::CreateDataFetcher); | 341 Init(MockOrientationFactory::CreateDataFetcher); |
328 Orientation test_orientation; | 342 Orientation test_orientation; |
329 test_orientation.set_alpha(1); | 343 test_orientation.set_alpha(1); |
330 test_orientation.set_beta(2); | 344 test_orientation.set_beta(2); |
331 test_orientation.set_gamma(3); | 345 test_orientation.set_gamma(3); |
332 test_orientation.set_absolute(true); | 346 test_orientation.set_absolute(true); |
333 | 347 |
334 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( | 348 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( |
335 &pending_expectations_)); | 349 &pending_expectations_)); |
336 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( | 350 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( |
337 &pending_expectations_)); | 351 &pending_expectations_)); |
338 | 352 |
339 orientation_factory->SetOrientation(test_orientation); | 353 orientation_factory->SetOrientation(test_orientation); |
340 checker_a->AddExpectation(test_orientation); | 354 checker_a->AddExpectation(test_orientation); |
341 provider_->AddObserver(checker_a.get()); | 355 provider_->AddObserver(checker_a.get()); |
342 MessageLoop::current()->Run(); | 356 MessageLoop::current()->Run(); |
343 | 357 |
344 checker_a->AddExpectation(Orientation::Empty()); | 358 checker_a->AddExpectation(Orientation::Empty()); |
345 orientation_factory->SetFailing(true); | 359 orientation_factory->SetFailing(true); |
346 MessageLoop::current()->Run(); | 360 MessageLoop::current()->Run(); |
347 | 361 |
348 checker_b->AddExpectation(Orientation::Empty()); | 362 checker_b->AddExpectation(Orientation::Empty()); |
349 provider_->AddObserver(checker_b.get()); | 363 provider_->AddObserver(checker_b.get()); |
350 MessageLoop::current()->Run(); | 364 MessageLoop::current()->Run(); |
351 | 365 |
352 provider_->RemoveObserver(checker_a.get()); | 366 provider_->RemoveObserver(checker_a.get()); |
353 provider_->RemoveObserver(checker_b.get()); | 367 provider_->RemoveObserver(checker_b.get()); |
368 MockOrientationFactory::SetCurInstance(NULL); | |
354 } | 369 } |
355 | 370 |
356 TEST_F(DeviceOrientationProviderTest, StartStopStart) { | 371 TEST_F(DeviceOrientationProviderTest, StartStopStart) { |
357 scoped_refptr<MockOrientationFactory> orientation_factory( | 372 scoped_refptr<MockOrientationFactory> orientation_factory( |
358 new MockOrientationFactory()); | 373 new MockOrientationFactory()); |
374 MockOrientationFactory::SetCurInstance(orientation_factory.get()); | |
359 Init(MockOrientationFactory::CreateDataFetcher); | 375 Init(MockOrientationFactory::CreateDataFetcher); |
360 | 376 |
361 Orientation test_orientation; | 377 Orientation test_orientation; |
362 test_orientation.set_alpha(1); | 378 test_orientation.set_alpha(1); |
363 test_orientation.set_beta(2); | 379 test_orientation.set_beta(2); |
364 test_orientation.set_gamma(3); | 380 test_orientation.set_gamma(3); |
365 test_orientation.set_absolute(true); | 381 test_orientation.set_absolute(true); |
366 | 382 |
367 Orientation test_orientation2; | 383 Orientation test_orientation2; |
368 test_orientation2.set_alpha(4); | 384 test_orientation2.set_alpha(4); |
(...skipping 12 matching lines...) Expand all Loading... | |
381 MessageLoop::current()->Run(); | 397 MessageLoop::current()->Run(); |
382 | 398 |
383 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. | 399 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. |
384 | 400 |
385 checker_b->AddExpectation(test_orientation2); | 401 checker_b->AddExpectation(test_orientation2); |
386 orientation_factory->SetOrientation(test_orientation2); | 402 orientation_factory->SetOrientation(test_orientation2); |
387 provider_->AddObserver(checker_b.get()); | 403 provider_->AddObserver(checker_b.get()); |
388 MessageLoop::current()->Run(); | 404 MessageLoop::current()->Run(); |
389 | 405 |
390 provider_->RemoveObserver(checker_b.get()); | 406 provider_->RemoveObserver(checker_b.get()); |
407 MockOrientationFactory::SetCurInstance(NULL); | |
391 } | 408 } |
392 | 409 |
393 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { | 410 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { |
394 scoped_refptr<MockOrientationFactory> orientation_factory( | 411 scoped_refptr<MockOrientationFactory> orientation_factory( |
395 new MockOrientationFactory()); | 412 new MockOrientationFactory()); |
413 MockOrientationFactory::SetCurInstance(orientation_factory.get()); | |
396 Init(MockOrientationFactory::CreateDataFetcher); | 414 Init(MockOrientationFactory::CreateDataFetcher); |
397 | 415 |
398 // Values that should be well below or above the implementation's | 416 // Values that should be well below or above the implementation's |
399 // significane threshold. | 417 // significane threshold. |
400 const double kInsignificantDifference = 1e-6; | 418 const double kInsignificantDifference = 1e-6; |
401 const double kSignificantDifference = 30; | 419 const double kSignificantDifference = 30; |
402 const double kAlpha = 4, kBeta = 5, kGamma = 6; | 420 const double kAlpha = 4, kBeta = 5, kGamma = 6; |
403 | 421 |
404 Orientation first_orientation; | 422 Orientation first_orientation; |
405 first_orientation.set_alpha(kAlpha); | 423 first_orientation.set_alpha(kAlpha); |
(...skipping 30 matching lines...) Expand all Loading... | |
436 provider_->AddObserver(checker_b.get()); | 454 provider_->AddObserver(checker_b.get()); |
437 MessageLoop::current()->Run(); | 455 MessageLoop::current()->Run(); |
438 | 456 |
439 orientation_factory->SetOrientation(third_orientation); | 457 orientation_factory->SetOrientation(third_orientation); |
440 checker_a->AddExpectation(third_orientation); | 458 checker_a->AddExpectation(third_orientation); |
441 checker_b->AddExpectation(third_orientation); | 459 checker_b->AddExpectation(third_orientation); |
442 MessageLoop::current()->Run(); | 460 MessageLoop::current()->Run(); |
443 | 461 |
444 provider_->RemoveObserver(checker_a.get()); | 462 provider_->RemoveObserver(checker_a.get()); |
445 provider_->RemoveObserver(checker_b.get()); | 463 provider_->RemoveObserver(checker_b.get()); |
464 MockOrientationFactory::SetCurInstance(NULL); | |
446 } | 465 } |
447 | 466 |
448 } // namespace | 467 } // namespace |
449 | 468 |
450 } // namespace device_orientation | 469 } // namespace device_orientation |
OLD | NEW |