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
3 // found in the LICENSE file.
8 #include "base/message_loop.h"
9 #include "base/synchronization/lock.h"
10 #include "content/browser/device_orientation/data_fetcher.h"
11 #include "content/browser/device_orientation/device_data.h"
12 #include "content/browser/device_orientation/motion.h"
13 #include "content/browser/device_orientation/orientation.h"
14 #include "content/browser/device_orientation/provider.h"
15 #include "content/browser/device_orientation/provider_impl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 // Class for testing multiple types of device data.
22 class TestData
: public DeviceData
{
29 virtual IPC::Message
* CreateIPCMessage(int render_view_id
) const OVERRIDE
{
33 virtual bool ShouldFireEvent(const DeviceData
* old_data
) const OVERRIDE
{
37 void set_value(double value
) { value_
= value
; }
38 double value() const { return value_
; }
41 virtual ~TestData() { }
46 // Class for checking expectations on device_data updates from the Provider.
47 class UpdateChecker
: public Provider::Observer
{
49 UpdateChecker(DeviceData::Type device_data_type
,
50 int *expectations_count_ptr
)
51 : Observer(device_data_type
),
52 expectations_count_ptr_(expectations_count_ptr
) {
54 virtual ~UpdateChecker() {}
56 // From Provider::Observer.
57 virtual void OnDeviceDataUpdate(const DeviceData
* device_data
,
58 DeviceData::Type device_data_type
) OVERRIDE
= 0;
60 void AddExpectation(const DeviceData
* device_data
) {
61 scoped_refptr
<const DeviceData
> expected_device_data(device_data
);
62 expectations_queue_
.push(expected_device_data
);
63 ++(*expectations_count_ptr_
);
67 // Set up by the test fixture, which then blocks while it is accessed
68 // from OnDeviceDataUpdate which is executed on the test fixture's
70 int* expectations_count_ptr_
;
71 std::queue
<scoped_refptr
<const DeviceData
> > expectations_queue_
;
74 // Class for checking expectations on motion updates from the Provider.
75 class MotionUpdateChecker
: public UpdateChecker
{
77 explicit MotionUpdateChecker(int* expectations_count_ptr
)
78 : UpdateChecker(DeviceData::kTypeMotion
, expectations_count_ptr
) {
81 virtual ~MotionUpdateChecker() {}
83 // From UpdateChecker.
84 virtual void OnDeviceDataUpdate(const DeviceData
* device_data
,
85 DeviceData::Type device_data_type
) OVERRIDE
{
86 ASSERT_FALSE(expectations_queue_
.empty());
87 ASSERT_EQ(DeviceData::kTypeMotion
, device_data_type
);
89 scoped_refptr
<const Motion
> motion(static_cast<const Motion
*>(device_data
));
91 motion
= new Motion();
93 scoped_refptr
<const Motion
> expected(static_cast<const Motion
*>(
94 (expectations_queue_
.front().get())));
95 expectations_queue_
.pop();
97 EXPECT_EQ(expected
->can_provide_acceleration_x(),
98 motion
->can_provide_acceleration_x());
99 EXPECT_EQ(expected
->can_provide_acceleration_y(),
100 motion
->can_provide_acceleration_y());
101 EXPECT_EQ(expected
->can_provide_acceleration_z(),
102 motion
->can_provide_acceleration_z());
104 EXPECT_EQ(expected
->can_provide_acceleration_including_gravity_x(),
105 motion
->can_provide_acceleration_including_gravity_x());
106 EXPECT_EQ(expected
->can_provide_acceleration_including_gravity_y(),
107 motion
->can_provide_acceleration_including_gravity_y());
108 EXPECT_EQ(expected
->can_provide_acceleration_including_gravity_z(),
109 motion
->can_provide_acceleration_including_gravity_z());
111 EXPECT_EQ(expected
->can_provide_rotation_rate_alpha(),
112 motion
->can_provide_rotation_rate_alpha());
113 EXPECT_EQ(expected
->can_provide_rotation_rate_beta(),
114 motion
->can_provide_rotation_rate_beta());
115 EXPECT_EQ(expected
->can_provide_rotation_rate_gamma(),
116 motion
->can_provide_rotation_rate_gamma());
118 EXPECT_EQ(expected
->can_provide_interval(), motion
->can_provide_interval());
120 if (expected
->can_provide_acceleration_x())
121 EXPECT_EQ(expected
->acceleration_x(), motion
->acceleration_x());
122 if (expected
->can_provide_acceleration_y())
123 EXPECT_EQ(expected
->acceleration_y(), motion
->acceleration_y());
124 if (expected
->can_provide_acceleration_z())
125 EXPECT_EQ(expected
->acceleration_z(), motion
->acceleration_z());
127 if (expected
->can_provide_acceleration_including_gravity_x())
128 EXPECT_EQ(expected
->acceleration_including_gravity_x(),
129 motion
->acceleration_including_gravity_x());
130 if (expected
->can_provide_acceleration_including_gravity_y())
131 EXPECT_EQ(expected
->acceleration_including_gravity_y(),
132 motion
->acceleration_including_gravity_y());
133 if (expected
->can_provide_acceleration_including_gravity_z())
134 EXPECT_EQ(expected
->acceleration_including_gravity_z(),
135 motion
->acceleration_including_gravity_z());
137 if (expected
->can_provide_rotation_rate_alpha())
138 EXPECT_EQ(expected
->rotation_rate_alpha(),
139 motion
->rotation_rate_alpha());
140 if (expected
->can_provide_rotation_rate_beta())
141 EXPECT_EQ(expected
->rotation_rate_beta(),
142 motion
->rotation_rate_beta());
143 if (expected
->can_provide_rotation_rate_gamma())
144 EXPECT_EQ(expected
->rotation_rate_gamma(),
145 motion
->rotation_rate_gamma());
147 if (expected
->can_provide_interval())
148 EXPECT_EQ(expected
->interval(), motion
->interval());
150 --(*expectations_count_ptr_
);
152 if (*expectations_count_ptr_
== 0) {
153 MessageLoop::current()->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
158 // Class for checking expectations on orientation updates from the Provider.
159 class OrientationUpdateChecker
: public UpdateChecker
{
161 explicit OrientationUpdateChecker(int* expectations_count_ptr
)
162 : UpdateChecker(DeviceData::kTypeOrientation
, expectations_count_ptr
) {
165 virtual ~OrientationUpdateChecker() {}
167 // From UpdateChecker.
168 virtual void OnDeviceDataUpdate(const DeviceData
* device_data
,
169 DeviceData::Type device_data_type
) OVERRIDE
{
170 ASSERT_FALSE(expectations_queue_
.empty());
171 ASSERT_EQ(DeviceData::kTypeOrientation
, device_data_type
);
173 scoped_refptr
<const Orientation
> orientation(
174 static_cast<const Orientation
*>(device_data
));
175 if (orientation
== NULL
)
176 orientation
= new Orientation();
178 scoped_refptr
<const Orientation
> expected(static_cast<const Orientation
*>(
179 (expectations_queue_
.front().get())));
180 expectations_queue_
.pop();
182 EXPECT_EQ(expected
->can_provide_alpha(), orientation
->can_provide_alpha());
183 EXPECT_EQ(expected
->can_provide_beta(), orientation
->can_provide_beta());
184 EXPECT_EQ(expected
->can_provide_gamma(), orientation
->can_provide_gamma());
185 EXPECT_EQ(expected
->can_provide_absolute(),
186 orientation
->can_provide_absolute());
187 if (expected
->can_provide_alpha())
188 EXPECT_EQ(expected
->alpha(), orientation
->alpha());
189 if (expected
->can_provide_beta())
190 EXPECT_EQ(expected
->beta(), orientation
->beta());
191 if (expected
->can_provide_gamma())
192 EXPECT_EQ(expected
->gamma(), orientation
->gamma());
193 if (expected
->can_provide_absolute())
194 EXPECT_EQ(expected
->absolute(), orientation
->absolute());
196 --(*expectations_count_ptr_
);
198 if (*expectations_count_ptr_
== 0) {
199 MessageLoop::current()->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
204 // Class for checking expectations on test_data updates from the Provider.
205 class TestDataUpdateChecker
: public UpdateChecker
{
207 explicit TestDataUpdateChecker(int* expectations_count_ptr
)
208 : UpdateChecker(DeviceData::kTypeTest
, expectations_count_ptr
) {
211 // From UpdateChecker.
212 virtual void OnDeviceDataUpdate(const DeviceData
* device_data
,
213 DeviceData::Type device_data_type
) OVERRIDE
{
214 ASSERT_FALSE(expectations_queue_
.empty());
215 ASSERT_EQ(DeviceData::kTypeTest
, device_data_type
);
217 scoped_refptr
<const TestData
> test_data(
218 static_cast<const TestData
*>(device_data
));
219 if (test_data
== NULL
)
220 test_data
= new TestData();
222 scoped_refptr
<const TestData
> expected(static_cast<const TestData
*>(
223 (expectations_queue_
.front().get())));
224 expectations_queue_
.pop();
226 EXPECT_EQ(expected
->value(), test_data
->value());
228 --(*expectations_count_ptr_
);
230 if (*expectations_count_ptr_
== 0) {
231 MessageLoop::current()->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
236 // Class for injecting test device data into the Provider.
237 class MockDeviceDataFactory
238 : public base::RefCountedThreadSafe
<MockDeviceDataFactory
> {
240 MockDeviceDataFactory()
241 : is_failing_(false) {
244 static void SetCurInstance(MockDeviceDataFactory
* instance
) {
246 EXPECT_FALSE(instance_
);
249 EXPECT_TRUE(instance_
);
251 instance_
= instance
;
254 static DataFetcher
* CreateDataFetcher() {
255 EXPECT_TRUE(instance_
);
256 return new MockDataFetcher(instance_
);
259 void SetDeviceData(const DeviceData
* device_data
, DeviceData::Type type
) {
260 base::AutoLock
auto_lock(lock_
);
261 device_data_map_
[type
] = device_data
;
264 void SetFailing(bool is_failing
) {
265 base::AutoLock
auto_lock(lock_
);
266 is_failing_
= is_failing
;
270 friend class base::RefCountedThreadSafe
<MockDeviceDataFactory
>;
272 ~MockDeviceDataFactory() {
275 // Owned by ProviderImpl. Holds a reference back to MockDeviceDataFactory.
276 class MockDataFetcher
: public DataFetcher
{
278 explicit MockDataFetcher(MockDeviceDataFactory
* device_data_factory
)
279 : device_data_factory_(device_data_factory
) { }
281 // From DataFetcher. Called by the Provider.
282 virtual const DeviceData
* GetDeviceData(DeviceData::Type device_data_type
) {
283 base::AutoLock
auto_lock(device_data_factory_
->lock_
);
284 if (device_data_factory_
->is_failing_
)
286 return device_data_factory_
->device_data_map_
[device_data_type
];
290 scoped_refptr
<MockDeviceDataFactory
> device_data_factory_
;
293 static MockDeviceDataFactory
* instance_
;
294 std::map
<DeviceData::Type
, scoped_refptr
<const DeviceData
> > device_data_map_
;
299 MockDeviceDataFactory
* MockDeviceDataFactory::instance_
;
301 class DeviceOrientationProviderTest
: public testing::Test
{
303 DeviceOrientationProviderTest()
304 : pending_expectations_(0) {
307 virtual void TearDown() {
310 // Make sure it is really gone.
311 EXPECT_FALSE(Provider::GetInstanceForTests());
313 // Clean up in any case, so as to not break subsequent test.
314 Provider::SetInstanceForTests(NULL
);
317 // Initialize the test fixture with a ProviderImpl that uses the
318 // DataFetcherFactory factory.
319 void Init(ProviderImpl::DataFetcherFactory factory
) {
320 provider_
= new ProviderImpl(factory
);
321 Provider::SetInstanceForTests(provider_
);
325 // Number of pending expectations.
326 int pending_expectations_
;
328 // Provider instance under test.
329 scoped_refptr
<Provider
> provider_
;
331 // Message loop for the test thread.
332 MessageLoop message_loop_
;
335 TEST_F(DeviceOrientationProviderTest
, FailingTest
) {
336 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
337 new MockDeviceDataFactory());
338 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
339 Init(MockDeviceDataFactory::CreateDataFetcher
);
341 scoped_ptr
<OrientationUpdateChecker
> checker_a(
342 new OrientationUpdateChecker(&pending_expectations_
));
343 scoped_ptr
<OrientationUpdateChecker
> checker_b(
344 new OrientationUpdateChecker(&pending_expectations_
));
346 checker_a
->AddExpectation(new Orientation());
347 provider_
->AddObserver(checker_a
.get());
348 MessageLoop::current()->Run();
350 checker_b
->AddExpectation(new Orientation());
351 provider_
->AddObserver(checker_b
.get());
352 MessageLoop::current()->Run();
354 MockDeviceDataFactory::SetCurInstance(NULL
);
357 TEST_F(DeviceOrientationProviderTest
, ProviderIsSingleton
) {
358 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
359 new MockDeviceDataFactory());
360 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
361 Init(MockDeviceDataFactory::CreateDataFetcher
);
363 scoped_refptr
<Provider
> provider_a(Provider::GetInstance());
364 scoped_refptr
<Provider
> provider_b(Provider::GetInstance());
366 EXPECT_EQ(provider_a
.get(), provider_b
.get());
367 MockDeviceDataFactory::SetCurInstance(NULL
);
370 TEST_F(DeviceOrientationProviderTest
, BasicPushTest
) {
371 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
372 new MockDeviceDataFactory());
373 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
374 Init(MockDeviceDataFactory::CreateDataFetcher
);
375 scoped_refptr
<Orientation
> test_orientation(new Orientation());
376 test_orientation
->set_alpha(1);
377 test_orientation
->set_beta(2);
378 test_orientation
->set_gamma(3);
379 test_orientation
->set_absolute(true);
381 scoped_ptr
<OrientationUpdateChecker
> checker(
382 new OrientationUpdateChecker(&pending_expectations_
));
383 checker
->AddExpectation(test_orientation
);
384 device_data_factory
->SetDeviceData(test_orientation
,
385 DeviceData::kTypeOrientation
);
386 provider_
->AddObserver(checker
.get());
387 MessageLoop::current()->Run();
389 provider_
->RemoveObserver(checker
.get());
390 MockDeviceDataFactory::SetCurInstance(NULL
);
393 // Tests multiple observers observing the same type of data.
394 TEST_F(DeviceOrientationProviderTest
, MultipleObserversPushTest
) {
395 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
396 new MockDeviceDataFactory());
397 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
398 Init(MockDeviceDataFactory::CreateDataFetcher
);
400 scoped_refptr
<Orientation
> test_orientations
[] = {new Orientation(),
401 new Orientation(), new Orientation()};
402 test_orientations
[0]->set_alpha(1);
403 test_orientations
[0]->set_beta(2);
404 test_orientations
[0]->set_gamma(3);
405 test_orientations
[0]->set_absolute(true);
407 test_orientations
[1]->set_alpha(4);
408 test_orientations
[1]->set_beta(5);
409 test_orientations
[1]->set_gamma(6);
410 test_orientations
[1]->set_absolute(false);
412 test_orientations
[2]->set_alpha(7);
413 test_orientations
[2]->set_beta(8);
414 test_orientations
[2]->set_gamma(9);
415 // can't provide absolute
417 scoped_ptr
<OrientationUpdateChecker
> checker_a(
418 new OrientationUpdateChecker(&pending_expectations_
));
419 scoped_ptr
<OrientationUpdateChecker
> checker_b(
420 new OrientationUpdateChecker(&pending_expectations_
));
421 scoped_ptr
<OrientationUpdateChecker
> checker_c(
422 new OrientationUpdateChecker(&pending_expectations_
));
424 checker_a
->AddExpectation(test_orientations
[0]);
425 device_data_factory
->SetDeviceData(test_orientations
[0],
426 DeviceData::kTypeOrientation
);
427 provider_
->AddObserver(checker_a
.get());
428 MessageLoop::current()->Run();
430 checker_a
->AddExpectation(test_orientations
[1]);
431 checker_b
->AddExpectation(test_orientations
[0]);
432 checker_b
->AddExpectation(test_orientations
[1]);
433 device_data_factory
->SetDeviceData(test_orientations
[1],
434 DeviceData::kTypeOrientation
);
435 provider_
->AddObserver(checker_b
.get());
436 MessageLoop::current()->Run();
438 provider_
->RemoveObserver(checker_a
.get());
439 checker_b
->AddExpectation(test_orientations
[2]);
440 checker_c
->AddExpectation(test_orientations
[1]);
441 checker_c
->AddExpectation(test_orientations
[2]);
442 device_data_factory
->SetDeviceData(test_orientations
[2],
443 DeviceData::kTypeOrientation
);
444 provider_
->AddObserver(checker_c
.get());
445 MessageLoop::current()->Run();
447 provider_
->RemoveObserver(checker_b
.get());
448 provider_
->RemoveObserver(checker_c
.get());
449 MockDeviceDataFactory::SetCurInstance(NULL
);
452 // Test for when the fetcher cannot provide the first type of data but can
453 // provide the second type.
454 TEST_F(DeviceOrientationProviderTest
, FailingFirstDataTypeTest
) {
456 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
457 new MockDeviceDataFactory());
458 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
459 Init(MockDeviceDataFactory::CreateDataFetcher
);
461 scoped_ptr
<TestDataUpdateChecker
> test_data_checker(
462 new TestDataUpdateChecker(&pending_expectations_
));
463 scoped_ptr
<OrientationUpdateChecker
> orientation_checker(
464 new OrientationUpdateChecker(&pending_expectations_
));
466 scoped_refptr
<Orientation
> test_orientation(new Orientation());
467 test_orientation
->set_alpha(1);
468 test_orientation
->set_beta(2);
469 test_orientation
->set_gamma(3);
470 test_orientation
->set_absolute(true);
472 test_data_checker
->AddExpectation(new TestData());
473 provider_
->AddObserver(test_data_checker
.get());
474 MessageLoop::current()->Run();
476 orientation_checker
->AddExpectation(test_orientation
);
477 device_data_factory
->SetDeviceData(test_orientation
,
478 DeviceData::kTypeOrientation
);
479 provider_
->AddObserver(orientation_checker
.get());
480 MessageLoop::current()->Run();
482 provider_
->RemoveObserver(test_data_checker
.get());
483 provider_
->RemoveObserver(orientation_checker
.get());
484 MockDeviceDataFactory::SetCurInstance(NULL
);
487 #if defined(OS_LINUX) || defined(OS_WIN)
488 // Flakily DCHECKs on Linux. See crbug.com/104950.
489 // FLAKY on Win. See crbug.com/104950.
490 #define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved
492 #define MAYBE_ObserverNotRemoved ObserverNotRemoved
494 TEST_F(DeviceOrientationProviderTest
, MAYBE_ObserverNotRemoved
) {
495 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
496 new MockDeviceDataFactory());
497 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
498 Init(MockDeviceDataFactory::CreateDataFetcher
);
499 scoped_refptr
<Orientation
> test_orientation(new Orientation());
500 test_orientation
->set_alpha(1);
501 test_orientation
->set_beta(2);
502 test_orientation
->set_gamma(3);
503 test_orientation
->set_absolute(true);
505 scoped_refptr
<Orientation
> test_orientation2(new Orientation());
506 test_orientation2
->set_alpha(4);
507 test_orientation2
->set_beta(5);
508 test_orientation2
->set_gamma(6);
509 test_orientation2
->set_absolute(false);
511 scoped_ptr
<OrientationUpdateChecker
> checker(
512 new OrientationUpdateChecker(&pending_expectations_
));
513 checker
->AddExpectation(test_orientation
);
514 device_data_factory
->SetDeviceData(test_orientation
,
515 DeviceData::kTypeOrientation
);
516 provider_
->AddObserver(checker
.get());
517 MessageLoop::current()->Run();
519 checker
->AddExpectation(test_orientation2
);
520 device_data_factory
->SetDeviceData(test_orientation2
,
521 DeviceData::kTypeOrientation
);
522 MessageLoop::current()->Run();
524 MockDeviceDataFactory::SetCurInstance(NULL
);
526 // Note that checker is not removed. This should not be a problem.
530 // FLAKY on Win. See crbug.com/104950.
531 #define MAYBE_StartFailing DISABLED_StartFailing
533 #define MAYBE_StartFailing StartFailing
535 TEST_F(DeviceOrientationProviderTest
, MAYBE_StartFailing
) {
536 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
537 new MockDeviceDataFactory());
538 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
539 Init(MockDeviceDataFactory::CreateDataFetcher
);
540 scoped_refptr
<Orientation
> test_orientation(new Orientation());
541 test_orientation
->set_alpha(1);
542 test_orientation
->set_beta(2);
543 test_orientation
->set_gamma(3);
544 test_orientation
->set_absolute(true);
546 scoped_ptr
<OrientationUpdateChecker
> checker_a(new OrientationUpdateChecker(
547 &pending_expectations_
));
548 scoped_ptr
<OrientationUpdateChecker
> checker_b(new OrientationUpdateChecker(
549 &pending_expectations_
));
551 device_data_factory
->SetDeviceData(test_orientation
,
552 DeviceData::kTypeOrientation
);
553 checker_a
->AddExpectation(test_orientation
);
554 provider_
->AddObserver(checker_a
.get());
555 MessageLoop::current()->Run();
557 checker_a
->AddExpectation(new Orientation());
558 device_data_factory
->SetFailing(true);
559 MessageLoop::current()->Run();
561 checker_b
->AddExpectation(new Orientation());
562 provider_
->AddObserver(checker_b
.get());
563 MessageLoop::current()->Run();
565 provider_
->RemoveObserver(checker_a
.get());
566 provider_
->RemoveObserver(checker_b
.get());
567 MockDeviceDataFactory::SetCurInstance(NULL
);
570 TEST_F(DeviceOrientationProviderTest
, StartStopStart
) {
571 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
572 new MockDeviceDataFactory());
573 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
574 Init(MockDeviceDataFactory::CreateDataFetcher
);
576 scoped_refptr
<Orientation
> test_orientation(new Orientation());
577 test_orientation
->set_alpha(1);
578 test_orientation
->set_beta(2);
579 test_orientation
->set_gamma(3);
580 test_orientation
->set_absolute(true);
582 scoped_refptr
<Orientation
> test_orientation2(new Orientation());
583 test_orientation2
->set_alpha(4);
584 test_orientation2
->set_beta(5);
585 test_orientation2
->set_gamma(6);
586 test_orientation2
->set_absolute(false);
588 scoped_ptr
<OrientationUpdateChecker
> checker_a(new OrientationUpdateChecker(
589 &pending_expectations_
));
590 scoped_ptr
<OrientationUpdateChecker
> checker_b(new OrientationUpdateChecker(
591 &pending_expectations_
));
593 checker_a
->AddExpectation(test_orientation
);
594 device_data_factory
->SetDeviceData(test_orientation
,
595 DeviceData::kTypeOrientation
);
596 provider_
->AddObserver(checker_a
.get());
597 MessageLoop::current()->Run();
599 provider_
->RemoveObserver(checker_a
.get()); // This stops the Provider.
601 checker_b
->AddExpectation(test_orientation2
);
602 device_data_factory
->SetDeviceData(test_orientation2
,
603 DeviceData::kTypeOrientation
);
604 provider_
->AddObserver(checker_b
.get());
605 MessageLoop::current()->Run();
607 provider_
->RemoveObserver(checker_b
.get());
608 MockDeviceDataFactory::SetCurInstance(NULL
);
611 // Tests that Motion events always fire, even if the motion is unchanged.
612 TEST_F(DeviceOrientationProviderTest
, MotionAlwaysFires
) {
613 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
614 new MockDeviceDataFactory());
615 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
616 Init(MockDeviceDataFactory::CreateDataFetcher
);
618 scoped_refptr
<Motion
> test_motion(new Motion());
619 test_motion
->set_acceleration_x(1);
620 test_motion
->set_acceleration_y(2);
621 test_motion
->set_acceleration_z(3);
622 test_motion
->set_acceleration_including_gravity_x(4);
623 test_motion
->set_acceleration_including_gravity_y(5);
624 test_motion
->set_acceleration_including_gravity_z(6);
625 test_motion
->set_rotation_rate_alpha(7);
626 test_motion
->set_rotation_rate_beta(8);
627 test_motion
->set_rotation_rate_gamma(9);
628 test_motion
->set_interval(10);
630 scoped_ptr
<MotionUpdateChecker
> checker(new MotionUpdateChecker(
631 &pending_expectations_
));
633 device_data_factory
->SetDeviceData(test_motion
, DeviceData::kTypeMotion
);
634 checker
->AddExpectation(test_motion
);
635 provider_
->AddObserver(checker
.get());
636 MessageLoop::current()->Run();
638 // The observer should receive the same motion again.
639 device_data_factory
->SetDeviceData(test_motion
, DeviceData::kTypeMotion
);
640 checker
->AddExpectation(test_motion
);
641 MessageLoop::current()->Run();
643 provider_
->RemoveObserver(checker
.get());
644 MockDeviceDataFactory::SetCurInstance(NULL
);
647 // Tests that Orientation events only fire if the change is significant.
648 TEST_F(DeviceOrientationProviderTest
, OrientationSignificantlyDifferent
) {
649 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
650 new MockDeviceDataFactory());
651 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
652 Init(MockDeviceDataFactory::CreateDataFetcher
);
654 // Values that should be well below or above the implementation's
655 // significane threshold.
656 const double kInsignificantDifference
= 1e-6;
657 const double kSignificantDifference
= 30;
658 const double kAlpha
= 4, kBeta
= 5, kGamma
= 6;
660 scoped_refptr
<Orientation
> first_orientation(new Orientation());
661 first_orientation
->set_alpha(kAlpha
);
662 first_orientation
->set_beta(kBeta
);
663 first_orientation
->set_gamma(kGamma
);
664 first_orientation
->set_absolute(true);
666 scoped_refptr
<Orientation
> second_orientation(new Orientation());
667 second_orientation
->set_alpha(kAlpha
+ kInsignificantDifference
);
668 second_orientation
->set_beta(kBeta
+ kInsignificantDifference
);
669 second_orientation
->set_gamma(kGamma
+ kInsignificantDifference
);
670 second_orientation
->set_absolute(false);
672 scoped_refptr
<Orientation
> third_orientation(new Orientation());
673 third_orientation
->set_alpha(kAlpha
+ kSignificantDifference
);
674 third_orientation
->set_beta(kBeta
+ kSignificantDifference
);
675 third_orientation
->set_gamma(kGamma
+ kSignificantDifference
);
676 // can't provide absolute
678 scoped_ptr
<OrientationUpdateChecker
> checker_a(new OrientationUpdateChecker(
679 &pending_expectations_
));
680 scoped_ptr
<OrientationUpdateChecker
> checker_b(new OrientationUpdateChecker(
681 &pending_expectations_
));
683 device_data_factory
->SetDeviceData(first_orientation
,
684 DeviceData::kTypeOrientation
);
685 checker_a
->AddExpectation(first_orientation
);
686 provider_
->AddObserver(checker_a
.get());
687 MessageLoop::current()->Run();
689 // The observers should not see this insignificantly different orientation.
690 device_data_factory
->SetDeviceData(second_orientation
,
691 DeviceData::kTypeOrientation
);
692 checker_b
->AddExpectation(first_orientation
);
693 provider_
->AddObserver(checker_b
.get());
694 MessageLoop::current()->Run();
696 device_data_factory
->SetDeviceData(third_orientation
,
697 DeviceData::kTypeOrientation
);
698 checker_a
->AddExpectation(third_orientation
);
699 checker_b
->AddExpectation(third_orientation
);
700 MessageLoop::current()->Run();
702 provider_
->RemoveObserver(checker_a
.get());
703 provider_
->RemoveObserver(checker_b
.get());
704 MockDeviceDataFactory::SetCurInstance(NULL
);
709 } // namespace content