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
));
90 if (motion
.get() == NULL
)
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 base::MessageLoop::current()->PostTask(FROM_HERE
,
154 base::MessageLoop::QuitClosure());
159 // Class for checking expectations on orientation updates from the Provider.
160 class OrientationUpdateChecker
: public UpdateChecker
{
162 explicit OrientationUpdateChecker(int* expectations_count_ptr
)
163 : UpdateChecker(DeviceData::kTypeOrientation
, expectations_count_ptr
) {
166 virtual ~OrientationUpdateChecker() {}
168 // From UpdateChecker.
169 virtual void OnDeviceDataUpdate(const DeviceData
* device_data
,
170 DeviceData::Type device_data_type
) OVERRIDE
{
171 ASSERT_FALSE(expectations_queue_
.empty());
172 ASSERT_EQ(DeviceData::kTypeOrientation
, device_data_type
);
174 scoped_refptr
<const Orientation
> orientation(
175 static_cast<const Orientation
*>(device_data
));
176 if (orientation
.get() == NULL
)
177 orientation
= new Orientation();
179 scoped_refptr
<const Orientation
> expected(static_cast<const Orientation
*>(
180 (expectations_queue_
.front().get())));
181 expectations_queue_
.pop();
183 EXPECT_EQ(expected
->can_provide_alpha(), orientation
->can_provide_alpha());
184 EXPECT_EQ(expected
->can_provide_beta(), orientation
->can_provide_beta());
185 EXPECT_EQ(expected
->can_provide_gamma(), orientation
->can_provide_gamma());
186 EXPECT_EQ(expected
->can_provide_absolute(),
187 orientation
->can_provide_absolute());
188 if (expected
->can_provide_alpha())
189 EXPECT_EQ(expected
->alpha(), orientation
->alpha());
190 if (expected
->can_provide_beta())
191 EXPECT_EQ(expected
->beta(), orientation
->beta());
192 if (expected
->can_provide_gamma())
193 EXPECT_EQ(expected
->gamma(), orientation
->gamma());
194 if (expected
->can_provide_absolute())
195 EXPECT_EQ(expected
->absolute(), orientation
->absolute());
197 --(*expectations_count_ptr_
);
199 if (*expectations_count_ptr_
== 0) {
200 base::MessageLoop::current()->PostTask(FROM_HERE
,
201 base::MessageLoop::QuitClosure());
206 // Class for checking expectations on test_data updates from the Provider.
207 class TestDataUpdateChecker
: public UpdateChecker
{
209 explicit TestDataUpdateChecker(int* expectations_count_ptr
)
210 : UpdateChecker(DeviceData::kTypeTest
, expectations_count_ptr
) {
213 // From UpdateChecker.
214 virtual void OnDeviceDataUpdate(const DeviceData
* device_data
,
215 DeviceData::Type device_data_type
) OVERRIDE
{
216 ASSERT_FALSE(expectations_queue_
.empty());
217 ASSERT_EQ(DeviceData::kTypeTest
, device_data_type
);
219 scoped_refptr
<const TestData
> test_data(
220 static_cast<const TestData
*>(device_data
));
221 if (test_data
.get() == NULL
)
222 test_data
= new TestData();
224 scoped_refptr
<const TestData
> expected(static_cast<const TestData
*>(
225 (expectations_queue_
.front().get())));
226 expectations_queue_
.pop();
228 EXPECT_EQ(expected
->value(), test_data
->value());
230 --(*expectations_count_ptr_
);
232 if (*expectations_count_ptr_
== 0) {
233 base::MessageLoop::current()->PostTask(FROM_HERE
,
234 base::MessageLoop::QuitClosure());
239 // Class for injecting test device data into the Provider.
240 class MockDeviceDataFactory
241 : public base::RefCountedThreadSafe
<MockDeviceDataFactory
> {
243 MockDeviceDataFactory()
244 : is_failing_(false) {
247 static void SetCurInstance(MockDeviceDataFactory
* instance
) {
249 EXPECT_FALSE(instance_
);
252 EXPECT_TRUE(instance_
);
254 instance_
= instance
;
257 static DataFetcher
* CreateDataFetcher() {
258 EXPECT_TRUE(instance_
);
259 return new MockDataFetcher(instance_
);
262 void SetDeviceData(const DeviceData
* device_data
, DeviceData::Type type
) {
263 base::AutoLock
auto_lock(lock_
);
264 device_data_map_
[type
] = device_data
;
267 void SetFailing(bool is_failing
) {
268 base::AutoLock
auto_lock(lock_
);
269 is_failing_
= is_failing
;
273 friend class base::RefCountedThreadSafe
<MockDeviceDataFactory
>;
275 ~MockDeviceDataFactory() {
278 // Owned by ProviderImpl. Holds a reference back to MockDeviceDataFactory.
279 class MockDataFetcher
: public DataFetcher
{
281 explicit MockDataFetcher(MockDeviceDataFactory
* device_data_factory
)
282 : device_data_factory_(device_data_factory
) { }
284 // From DataFetcher. Called by the Provider.
285 virtual const DeviceData
* GetDeviceData(
286 DeviceData::Type device_data_type
) OVERRIDE
{
287 base::AutoLock
auto_lock(device_data_factory_
->lock_
);
288 if (device_data_factory_
->is_failing_
)
290 return device_data_factory_
->device_data_map_
[device_data_type
].get();
294 scoped_refptr
<MockDeviceDataFactory
> device_data_factory_
;
297 static MockDeviceDataFactory
* instance_
;
298 std::map
<DeviceData::Type
, scoped_refptr
<const DeviceData
> > device_data_map_
;
303 MockDeviceDataFactory
* MockDeviceDataFactory::instance_
;
305 class DeviceOrientationProviderTest
: public testing::Test
{
307 DeviceOrientationProviderTest()
308 : pending_expectations_(0) {
311 virtual void TearDown() {
314 // Make sure it is really gone.
315 EXPECT_FALSE(Provider::GetInstanceForTests());
317 // Clean up in any case, so as to not break subsequent test.
318 Provider::SetInstanceForTests(NULL
);
321 // Initialize the test fixture with a ProviderImpl that uses the
322 // DataFetcherFactory factory.
323 void Init(ProviderImpl::DataFetcherFactory factory
) {
324 provider_
= new ProviderImpl(factory
);
325 Provider::SetInstanceForTests(provider_
.get());
329 // Number of pending expectations.
330 int pending_expectations_
;
332 // Provider instance under test.
333 scoped_refptr
<Provider
> provider_
;
335 // Message loop for the test thread.
336 base::MessageLoop message_loop_
;
339 TEST_F(DeviceOrientationProviderTest
, FailingTest
) {
340 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
341 new MockDeviceDataFactory());
342 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
343 Init(MockDeviceDataFactory::CreateDataFetcher
);
345 scoped_ptr
<OrientationUpdateChecker
> checker_a(
346 new OrientationUpdateChecker(&pending_expectations_
));
347 scoped_ptr
<OrientationUpdateChecker
> checker_b(
348 new OrientationUpdateChecker(&pending_expectations_
));
350 checker_a
->AddExpectation(new Orientation());
351 provider_
->AddObserver(checker_a
.get());
352 base::MessageLoop::current()->Run();
354 checker_b
->AddExpectation(new Orientation());
355 provider_
->AddObserver(checker_b
.get());
356 base::MessageLoop::current()->Run();
358 MockDeviceDataFactory::SetCurInstance(NULL
);
361 TEST_F(DeviceOrientationProviderTest
, ProviderIsSingleton
) {
362 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
363 new MockDeviceDataFactory());
364 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
365 Init(MockDeviceDataFactory::CreateDataFetcher
);
367 scoped_refptr
<Provider
> provider_a(Provider::GetInstance());
368 scoped_refptr
<Provider
> provider_b(Provider::GetInstance());
370 EXPECT_EQ(provider_a
.get(), provider_b
.get());
371 MockDeviceDataFactory::SetCurInstance(NULL
);
374 TEST_F(DeviceOrientationProviderTest
, BasicPushTest
) {
375 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
376 new MockDeviceDataFactory());
377 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
378 Init(MockDeviceDataFactory::CreateDataFetcher
);
379 scoped_refptr
<Orientation
> test_orientation(new Orientation());
380 test_orientation
->set_alpha(1);
381 test_orientation
->set_beta(2);
382 test_orientation
->set_gamma(3);
383 test_orientation
->set_absolute(true);
385 scoped_ptr
<OrientationUpdateChecker
> checker(
386 new OrientationUpdateChecker(&pending_expectations_
));
387 checker
->AddExpectation(test_orientation
.get());
388 device_data_factory
->SetDeviceData(test_orientation
.get(),
389 DeviceData::kTypeOrientation
);
390 provider_
->AddObserver(checker
.get());
391 base::MessageLoop::current()->Run();
393 provider_
->RemoveObserver(checker
.get());
394 MockDeviceDataFactory::SetCurInstance(NULL
);
397 // Tests multiple observers observing the same type of data.
398 TEST_F(DeviceOrientationProviderTest
, MultipleObserversPushTest
) {
399 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
400 new MockDeviceDataFactory());
401 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
402 Init(MockDeviceDataFactory::CreateDataFetcher
);
404 scoped_refptr
<Orientation
> test_orientations
[] = {new Orientation(),
405 new Orientation(), new Orientation()};
406 test_orientations
[0]->set_alpha(1);
407 test_orientations
[0]->set_beta(2);
408 test_orientations
[0]->set_gamma(3);
409 test_orientations
[0]->set_absolute(true);
411 test_orientations
[1]->set_alpha(4);
412 test_orientations
[1]->set_beta(5);
413 test_orientations
[1]->set_gamma(6);
414 test_orientations
[1]->set_absolute(false);
416 test_orientations
[2]->set_alpha(7);
417 test_orientations
[2]->set_beta(8);
418 test_orientations
[2]->set_gamma(9);
419 // can't provide absolute
421 scoped_ptr
<OrientationUpdateChecker
> checker_a(
422 new OrientationUpdateChecker(&pending_expectations_
));
423 scoped_ptr
<OrientationUpdateChecker
> checker_b(
424 new OrientationUpdateChecker(&pending_expectations_
));
425 scoped_ptr
<OrientationUpdateChecker
> checker_c(
426 new OrientationUpdateChecker(&pending_expectations_
));
428 checker_a
->AddExpectation(test_orientations
[0].get());
429 device_data_factory
->SetDeviceData(test_orientations
[0].get(),
430 DeviceData::kTypeOrientation
);
431 provider_
->AddObserver(checker_a
.get());
432 base::MessageLoop::current()->Run();
434 checker_a
->AddExpectation(test_orientations
[1].get());
435 checker_b
->AddExpectation(test_orientations
[0].get());
436 checker_b
->AddExpectation(test_orientations
[1].get());
437 device_data_factory
->SetDeviceData(test_orientations
[1].get(),
438 DeviceData::kTypeOrientation
);
439 provider_
->AddObserver(checker_b
.get());
440 base::MessageLoop::current()->Run();
442 provider_
->RemoveObserver(checker_a
.get());
443 checker_b
->AddExpectation(test_orientations
[2].get());
444 checker_c
->AddExpectation(test_orientations
[1].get());
445 checker_c
->AddExpectation(test_orientations
[2].get());
446 device_data_factory
->SetDeviceData(test_orientations
[2].get(),
447 DeviceData::kTypeOrientation
);
448 provider_
->AddObserver(checker_c
.get());
449 base::MessageLoop::current()->Run();
451 provider_
->RemoveObserver(checker_b
.get());
452 provider_
->RemoveObserver(checker_c
.get());
453 MockDeviceDataFactory::SetCurInstance(NULL
);
456 // Test for when the fetcher cannot provide the first type of data but can
457 // provide the second type.
458 TEST_F(DeviceOrientationProviderTest
, FailingFirstDataTypeTest
) {
460 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
461 new MockDeviceDataFactory());
462 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
463 Init(MockDeviceDataFactory::CreateDataFetcher
);
465 scoped_ptr
<TestDataUpdateChecker
> test_data_checker(
466 new TestDataUpdateChecker(&pending_expectations_
));
467 scoped_ptr
<OrientationUpdateChecker
> orientation_checker(
468 new OrientationUpdateChecker(&pending_expectations_
));
470 scoped_refptr
<Orientation
> test_orientation(new Orientation());
471 test_orientation
->set_alpha(1);
472 test_orientation
->set_beta(2);
473 test_orientation
->set_gamma(3);
474 test_orientation
->set_absolute(true);
476 test_data_checker
->AddExpectation(new TestData());
477 provider_
->AddObserver(test_data_checker
.get());
478 base::MessageLoop::current()->Run();
480 orientation_checker
->AddExpectation(test_orientation
.get());
481 device_data_factory
->SetDeviceData(test_orientation
.get(),
482 DeviceData::kTypeOrientation
);
483 provider_
->AddObserver(orientation_checker
.get());
484 base::MessageLoop::current()->Run();
486 provider_
->RemoveObserver(test_data_checker
.get());
487 provider_
->RemoveObserver(orientation_checker
.get());
488 MockDeviceDataFactory::SetCurInstance(NULL
);
491 #if defined(OS_LINUX) || defined(OS_WIN)
492 // Flakily DCHECKs on Linux. See crbug.com/104950.
493 // FLAKY on Win. See crbug.com/104950.
494 #define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved
496 #define MAYBE_ObserverNotRemoved ObserverNotRemoved
498 TEST_F(DeviceOrientationProviderTest
, MAYBE_ObserverNotRemoved
) {
499 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
500 new MockDeviceDataFactory());
501 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
502 Init(MockDeviceDataFactory::CreateDataFetcher
);
503 scoped_refptr
<Orientation
> test_orientation(new Orientation());
504 test_orientation
->set_alpha(1);
505 test_orientation
->set_beta(2);
506 test_orientation
->set_gamma(3);
507 test_orientation
->set_absolute(true);
509 scoped_refptr
<Orientation
> test_orientation2(new Orientation());
510 test_orientation2
->set_alpha(4);
511 test_orientation2
->set_beta(5);
512 test_orientation2
->set_gamma(6);
513 test_orientation2
->set_absolute(false);
515 scoped_ptr
<OrientationUpdateChecker
> checker(
516 new OrientationUpdateChecker(&pending_expectations_
));
517 checker
->AddExpectation(test_orientation
.get());
518 device_data_factory
->SetDeviceData(test_orientation
.get(),
519 DeviceData::kTypeOrientation
);
520 provider_
->AddObserver(checker
.get());
521 base::MessageLoop::current()->Run();
523 checker
->AddExpectation(test_orientation2
.get());
524 device_data_factory
->SetDeviceData(test_orientation2
.get(),
525 DeviceData::kTypeOrientation
);
526 base::MessageLoop::current()->Run();
528 MockDeviceDataFactory::SetCurInstance(NULL
);
530 // Note that checker is not removed. This should not be a problem.
534 // FLAKY on Win. See crbug.com/104950.
535 #define MAYBE_StartFailing DISABLED_StartFailing
537 #define MAYBE_StartFailing StartFailing
539 TEST_F(DeviceOrientationProviderTest
, MAYBE_StartFailing
) {
540 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
541 new MockDeviceDataFactory());
542 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
543 Init(MockDeviceDataFactory::CreateDataFetcher
);
544 scoped_refptr
<Orientation
> test_orientation(new Orientation());
545 test_orientation
->set_alpha(1);
546 test_orientation
->set_beta(2);
547 test_orientation
->set_gamma(3);
548 test_orientation
->set_absolute(true);
550 scoped_ptr
<OrientationUpdateChecker
> checker_a(new OrientationUpdateChecker(
551 &pending_expectations_
));
552 scoped_ptr
<OrientationUpdateChecker
> checker_b(new OrientationUpdateChecker(
553 &pending_expectations_
));
555 device_data_factory
->SetDeviceData(test_orientation
.get(),
556 DeviceData::kTypeOrientation
);
557 checker_a
->AddExpectation(test_orientation
.get());
558 provider_
->AddObserver(checker_a
.get());
559 base::MessageLoop::current()->Run();
561 checker_a
->AddExpectation(new Orientation());
562 device_data_factory
->SetFailing(true);
563 base::MessageLoop::current()->Run();
565 checker_b
->AddExpectation(new Orientation());
566 provider_
->AddObserver(checker_b
.get());
567 base::MessageLoop::current()->Run();
569 provider_
->RemoveObserver(checker_a
.get());
570 provider_
->RemoveObserver(checker_b
.get());
571 MockDeviceDataFactory::SetCurInstance(NULL
);
574 TEST_F(DeviceOrientationProviderTest
, StartStopStart
) {
575 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
576 new MockDeviceDataFactory());
577 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
578 Init(MockDeviceDataFactory::CreateDataFetcher
);
580 scoped_refptr
<Orientation
> test_orientation(new Orientation());
581 test_orientation
->set_alpha(1);
582 test_orientation
->set_beta(2);
583 test_orientation
->set_gamma(3);
584 test_orientation
->set_absolute(true);
586 scoped_refptr
<Orientation
> test_orientation2(new Orientation());
587 test_orientation2
->set_alpha(4);
588 test_orientation2
->set_beta(5);
589 test_orientation2
->set_gamma(6);
590 test_orientation2
->set_absolute(false);
592 scoped_ptr
<OrientationUpdateChecker
> checker_a(new OrientationUpdateChecker(
593 &pending_expectations_
));
594 scoped_ptr
<OrientationUpdateChecker
> checker_b(new OrientationUpdateChecker(
595 &pending_expectations_
));
597 checker_a
->AddExpectation(test_orientation
.get());
598 device_data_factory
->SetDeviceData(test_orientation
.get(),
599 DeviceData::kTypeOrientation
);
600 provider_
->AddObserver(checker_a
.get());
601 base::MessageLoop::current()->Run();
603 provider_
->RemoveObserver(checker_a
.get()); // This stops the Provider.
605 checker_b
->AddExpectation(test_orientation2
.get());
606 device_data_factory
->SetDeviceData(test_orientation2
.get(),
607 DeviceData::kTypeOrientation
);
608 provider_
->AddObserver(checker_b
.get());
609 base::MessageLoop::current()->Run();
611 provider_
->RemoveObserver(checker_b
.get());
612 MockDeviceDataFactory::SetCurInstance(NULL
);
615 // Tests that Motion events always fire, even if the motion is unchanged.
616 TEST_F(DeviceOrientationProviderTest
, FLAKY_MotionAlwaysFires
) {
617 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
618 new MockDeviceDataFactory());
619 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
620 Init(MockDeviceDataFactory::CreateDataFetcher
);
622 scoped_refptr
<Motion
> test_motion(new Motion());
623 test_motion
->set_acceleration_x(1);
624 test_motion
->set_acceleration_y(2);
625 test_motion
->set_acceleration_z(3);
626 test_motion
->set_acceleration_including_gravity_x(4);
627 test_motion
->set_acceleration_including_gravity_y(5);
628 test_motion
->set_acceleration_including_gravity_z(6);
629 test_motion
->set_rotation_rate_alpha(7);
630 test_motion
->set_rotation_rate_beta(8);
631 test_motion
->set_rotation_rate_gamma(9);
632 test_motion
->set_interval(10);
634 scoped_ptr
<MotionUpdateChecker
> checker(new MotionUpdateChecker(
635 &pending_expectations_
));
637 device_data_factory
->SetDeviceData(test_motion
.get(),
638 DeviceData::kTypeMotion
);
639 checker
->AddExpectation(test_motion
.get());
640 provider_
->AddObserver(checker
.get());
641 base::MessageLoop::current()->Run();
643 // The observer should receive the same motion again.
644 device_data_factory
->SetDeviceData(test_motion
.get(),
645 DeviceData::kTypeMotion
);
646 checker
->AddExpectation(test_motion
.get());
647 base::MessageLoop::current()->Run();
649 provider_
->RemoveObserver(checker
.get());
650 MockDeviceDataFactory::SetCurInstance(NULL
);
653 // Tests that Orientation events only fire if the change is significant.
654 TEST_F(DeviceOrientationProviderTest
, OrientationSignificantlyDifferent
) {
655 scoped_refptr
<MockDeviceDataFactory
> device_data_factory(
656 new MockDeviceDataFactory());
657 MockDeviceDataFactory::SetCurInstance(device_data_factory
.get());
658 Init(MockDeviceDataFactory::CreateDataFetcher
);
660 // Values that should be well below or above the implementation's
661 // significane threshold.
662 const double kInsignificantDifference
= 1e-6;
663 const double kSignificantDifference
= 30;
664 const double kAlpha
= 4, kBeta
= 5, kGamma
= 6;
666 scoped_refptr
<Orientation
> first_orientation(new Orientation());
667 first_orientation
->set_alpha(kAlpha
);
668 first_orientation
->set_beta(kBeta
);
669 first_orientation
->set_gamma(kGamma
);
670 first_orientation
->set_absolute(true);
672 scoped_refptr
<Orientation
> second_orientation(new Orientation());
673 second_orientation
->set_alpha(kAlpha
+ kInsignificantDifference
);
674 second_orientation
->set_beta(kBeta
+ kInsignificantDifference
);
675 second_orientation
->set_gamma(kGamma
+ kInsignificantDifference
);
676 second_orientation
->set_absolute(false);
678 scoped_refptr
<Orientation
> third_orientation(new Orientation());
679 third_orientation
->set_alpha(kAlpha
+ kSignificantDifference
);
680 third_orientation
->set_beta(kBeta
+ kSignificantDifference
);
681 third_orientation
->set_gamma(kGamma
+ kSignificantDifference
);
682 // can't provide absolute
684 scoped_ptr
<OrientationUpdateChecker
> checker_a(new OrientationUpdateChecker(
685 &pending_expectations_
));
686 scoped_ptr
<OrientationUpdateChecker
> checker_b(new OrientationUpdateChecker(
687 &pending_expectations_
));
689 device_data_factory
->SetDeviceData(first_orientation
.get(),
690 DeviceData::kTypeOrientation
);
691 checker_a
->AddExpectation(first_orientation
.get());
692 provider_
->AddObserver(checker_a
.get());
693 base::MessageLoop::current()->Run();
695 // The observers should not see this insignificantly different orientation.
696 device_data_factory
->SetDeviceData(second_orientation
.get(),
697 DeviceData::kTypeOrientation
);
698 checker_b
->AddExpectation(first_orientation
.get());
699 provider_
->AddObserver(checker_b
.get());
700 base::MessageLoop::current()->Run();
702 device_data_factory
->SetDeviceData(third_orientation
.get(),
703 DeviceData::kTypeOrientation
);
704 checker_a
->AddExpectation(third_orientation
.get());
705 checker_b
->AddExpectation(third_orientation
.get());
706 base::MessageLoop::current()->Run();
708 provider_
->RemoveObserver(checker_a
.get());
709 provider_
->RemoveObserver(checker_b
.get());
710 MockDeviceDataFactory::SetCurInstance(NULL
);
715 } // namespace content