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

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

Issue 10835030: device_orientation::ProviderImpl to stop polling thread asynchronously (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 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
« no previous file with comments | « content/browser/device_orientation/provider_impl.cc ('k') | no next file » | 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 <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
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
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
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
327 MockOrientationFactory::SetCurInstance(NULL);
328
315 // Note that checker is not removed. This should not be a problem. 329 // Note that checker is not removed. This should not be a problem.
316 } 330 }
317 331
318 #if defined(OS_WIN) 332 #if defined(OS_WIN)
319 // FLAKY on Win. See crbug.com/104950. 333 // FLAKY on Win. See crbug.com/104950.
320 #define MAYBE_StartFailing DISABLED_StartFailing 334 #define MAYBE_StartFailing DISABLED_StartFailing
321 #else 335 #else
322 #define MAYBE_StartFailing StartFailing 336 #define MAYBE_StartFailing StartFailing
323 #endif 337 #endif
324 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { 338 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) {
325 scoped_refptr<MockOrientationFactory> orientation_factory( 339 scoped_refptr<MockOrientationFactory> orientation_factory(
326 new MockOrientationFactory()); 340 new MockOrientationFactory());
341 MockOrientationFactory::SetCurInstance(orientation_factory.get());
327 Init(MockOrientationFactory::CreateDataFetcher); 342 Init(MockOrientationFactory::CreateDataFetcher);
328 Orientation test_orientation; 343 Orientation test_orientation;
329 test_orientation.set_alpha(1); 344 test_orientation.set_alpha(1);
330 test_orientation.set_beta(2); 345 test_orientation.set_beta(2);
331 test_orientation.set_gamma(3); 346 test_orientation.set_gamma(3);
332 test_orientation.set_absolute(true); 347 test_orientation.set_absolute(true);
333 348
334 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 349 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker(
335 &pending_expectations_)); 350 &pending_expectations_));
336 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 351 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker(
337 &pending_expectations_)); 352 &pending_expectations_));
338 353
339 orientation_factory->SetOrientation(test_orientation); 354 orientation_factory->SetOrientation(test_orientation);
340 checker_a->AddExpectation(test_orientation); 355 checker_a->AddExpectation(test_orientation);
341 provider_->AddObserver(checker_a.get()); 356 provider_->AddObserver(checker_a.get());
342 MessageLoop::current()->Run(); 357 MessageLoop::current()->Run();
343 358
344 checker_a->AddExpectation(Orientation::Empty()); 359 checker_a->AddExpectation(Orientation::Empty());
345 orientation_factory->SetFailing(true); 360 orientation_factory->SetFailing(true);
346 MessageLoop::current()->Run(); 361 MessageLoop::current()->Run();
347 362
348 checker_b->AddExpectation(Orientation::Empty()); 363 checker_b->AddExpectation(Orientation::Empty());
349 provider_->AddObserver(checker_b.get()); 364 provider_->AddObserver(checker_b.get());
350 MessageLoop::current()->Run(); 365 MessageLoop::current()->Run();
351 366
352 provider_->RemoveObserver(checker_a.get()); 367 provider_->RemoveObserver(checker_a.get());
353 provider_->RemoveObserver(checker_b.get()); 368 provider_->RemoveObserver(checker_b.get());
369 MockOrientationFactory::SetCurInstance(NULL);
354 } 370 }
355 371
356 TEST_F(DeviceOrientationProviderTest, StartStopStart) { 372 TEST_F(DeviceOrientationProviderTest, StartStopStart) {
357 scoped_refptr<MockOrientationFactory> orientation_factory( 373 scoped_refptr<MockOrientationFactory> orientation_factory(
358 new MockOrientationFactory()); 374 new MockOrientationFactory());
375 MockOrientationFactory::SetCurInstance(orientation_factory.get());
359 Init(MockOrientationFactory::CreateDataFetcher); 376 Init(MockOrientationFactory::CreateDataFetcher);
360 377
361 Orientation test_orientation; 378 Orientation test_orientation;
362 test_orientation.set_alpha(1); 379 test_orientation.set_alpha(1);
363 test_orientation.set_beta(2); 380 test_orientation.set_beta(2);
364 test_orientation.set_gamma(3); 381 test_orientation.set_gamma(3);
365 test_orientation.set_absolute(true); 382 test_orientation.set_absolute(true);
366 383
367 Orientation test_orientation2; 384 Orientation test_orientation2;
368 test_orientation2.set_alpha(4); 385 test_orientation2.set_alpha(4);
(...skipping 12 matching lines...) Expand all
381 MessageLoop::current()->Run(); 398 MessageLoop::current()->Run();
382 399
383 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. 400 provider_->RemoveObserver(checker_a.get()); // This stops the Provider.
384 401
385 checker_b->AddExpectation(test_orientation2); 402 checker_b->AddExpectation(test_orientation2);
386 orientation_factory->SetOrientation(test_orientation2); 403 orientation_factory->SetOrientation(test_orientation2);
387 provider_->AddObserver(checker_b.get()); 404 provider_->AddObserver(checker_b.get());
388 MessageLoop::current()->Run(); 405 MessageLoop::current()->Run();
389 406
390 provider_->RemoveObserver(checker_b.get()); 407 provider_->RemoveObserver(checker_b.get());
408 MockOrientationFactory::SetCurInstance(NULL);
391 } 409 }
392 410
393 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { 411 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) {
394 scoped_refptr<MockOrientationFactory> orientation_factory( 412 scoped_refptr<MockOrientationFactory> orientation_factory(
395 new MockOrientationFactory()); 413 new MockOrientationFactory());
414 MockOrientationFactory::SetCurInstance(orientation_factory.get());
396 Init(MockOrientationFactory::CreateDataFetcher); 415 Init(MockOrientationFactory::CreateDataFetcher);
397 416
398 // Values that should be well below or above the implementation's 417 // Values that should be well below or above the implementation's
399 // significane threshold. 418 // significane threshold.
400 const double kInsignificantDifference = 1e-6; 419 const double kInsignificantDifference = 1e-6;
401 const double kSignificantDifference = 30; 420 const double kSignificantDifference = 30;
402 const double kAlpha = 4, kBeta = 5, kGamma = 6; 421 const double kAlpha = 4, kBeta = 5, kGamma = 6;
403 422
404 Orientation first_orientation; 423 Orientation first_orientation;
405 first_orientation.set_alpha(kAlpha); 424 first_orientation.set_alpha(kAlpha);
(...skipping 30 matching lines...) Expand all
436 provider_->AddObserver(checker_b.get()); 455 provider_->AddObserver(checker_b.get());
437 MessageLoop::current()->Run(); 456 MessageLoop::current()->Run();
438 457
439 orientation_factory->SetOrientation(third_orientation); 458 orientation_factory->SetOrientation(third_orientation);
440 checker_a->AddExpectation(third_orientation); 459 checker_a->AddExpectation(third_orientation);
441 checker_b->AddExpectation(third_orientation); 460 checker_b->AddExpectation(third_orientation);
442 MessageLoop::current()->Run(); 461 MessageLoop::current()->Run();
443 462
444 provider_->RemoveObserver(checker_a.get()); 463 provider_->RemoveObserver(checker_a.get());
445 provider_->RemoveObserver(checker_b.get()); 464 provider_->RemoveObserver(checker_b.get());
465 MockOrientationFactory::SetCurInstance(NULL);
446 } 466 }
447 467
448 } // namespace 468 } // namespace
449 469
450 } // namespace device_orientation 470 } // namespace device_orientation
OLDNEW
« no previous file with comments | « content/browser/device_orientation/provider_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698