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 <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 | 7 |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
10 #include "content/browser/device_orientation/data_fetcher.h" | 10 #include "content/browser/device_orientation/data_fetcher.h" |
11 #include "content/browser/device_orientation/device_data.h" | 11 #include "content/browser/device_orientation/device_data.h" |
12 #include "content/browser/device_orientation/motion.h" | |
12 #include "content/browser/device_orientation/orientation.h" | 13 #include "content/browser/device_orientation/orientation.h" |
13 #include "content/browser/device_orientation/provider.h" | 14 #include "content/browser/device_orientation/provider.h" |
14 #include "content/browser/device_orientation/provider_impl.h" | 15 #include "content/browser/device_orientation/provider_impl.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace device_orientation { | 18 namespace device_orientation { |
18 namespace { | 19 namespace { |
19 | 20 |
20 // Class for testing multiple types of device data. | 21 // Class for testing multiple types of device data. |
21 class TestData : public DeviceData { | 22 class TestData : public DeviceData { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 } | 64 } |
64 | 65 |
65 protected: | 66 protected: |
66 // Set up by the test fixture, which then blocks while it is accessed | 67 // Set up by the test fixture, which then blocks while it is accessed |
67 // from OnDeviceDataUpdate which is executed on the test fixture's | 68 // from OnDeviceDataUpdate which is executed on the test fixture's |
68 // message_loop_. | 69 // message_loop_. |
69 int* expectations_count_ptr_; | 70 int* expectations_count_ptr_; |
70 std::queue<scoped_refptr<const DeviceData> > expectations_queue_; | 71 std::queue<scoped_refptr<const DeviceData> > expectations_queue_; |
71 }; | 72 }; |
72 | 73 |
74 // Class for checking expectations on motion updates from the Provider. | |
75 class MotionUpdateChecker : public UpdateChecker { | |
76 public: | |
77 explicit MotionUpdateChecker(int* expectations_count_ptr) | |
78 : UpdateChecker(DeviceData::kTypeMotion, expectations_count_ptr) { | |
79 } | |
80 | |
81 virtual ~MotionUpdateChecker() {} | |
82 | |
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); | |
88 | |
89 scoped_refptr<const Motion> motion(static_cast<const Motion*>(device_data)); | |
90 if (motion == NULL) | |
91 motion = new Motion(); | |
92 | |
93 scoped_refptr<const Motion> expected(static_cast<const Motion*>( | |
94 (expectations_queue_.front().get()))); | |
95 expectations_queue_.pop(); | |
96 | |
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()); | |
103 | |
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()); | |
110 | |
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()); | |
117 | |
118 EXPECT_EQ(expected->can_provide_interval(), motion->can_provide_interval()); | |
119 | |
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()); | |
126 | |
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()); | |
136 | |
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()); | |
146 | |
147 if (expected->can_provide_interval()) | |
148 EXPECT_EQ(expected->interval(), motion->interval()); | |
149 | |
150 --(*expectations_count_ptr_); | |
151 | |
152 if (*expectations_count_ptr_ == 0) { | |
153 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
154 } | |
155 } | |
156 }; | |
157 | |
73 // Class for checking expectations on orientation updates from the Provider. | 158 // Class for checking expectations on orientation updates from the Provider. |
74 class OrientationUpdateChecker : public UpdateChecker { | 159 class OrientationUpdateChecker : public UpdateChecker { |
75 public: | 160 public: |
76 explicit OrientationUpdateChecker(int* expectations_count_ptr) | 161 explicit OrientationUpdateChecker(int* expectations_count_ptr) |
77 : UpdateChecker(DeviceData::kTypeOrientation, expectations_count_ptr) { | 162 : UpdateChecker(DeviceData::kTypeOrientation, expectations_count_ptr) { |
78 } | 163 } |
79 | 164 |
80 virtual ~OrientationUpdateChecker() {} | 165 virtual ~OrientationUpdateChecker() {} |
81 | 166 |
82 // From UpdateChecker. | 167 // From UpdateChecker. |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 checker_b->AddExpectation(test_orientation2); | 601 checker_b->AddExpectation(test_orientation2); |
517 device_data_factory->SetDeviceData(test_orientation2, | 602 device_data_factory->SetDeviceData(test_orientation2, |
518 DeviceData::kTypeOrientation); | 603 DeviceData::kTypeOrientation); |
519 provider_->AddObserver(checker_b.get()); | 604 provider_->AddObserver(checker_b.get()); |
520 MessageLoop::current()->Run(); | 605 MessageLoop::current()->Run(); |
521 | 606 |
522 provider_->RemoveObserver(checker_b.get()); | 607 provider_->RemoveObserver(checker_b.get()); |
523 MockDeviceDataFactory::SetCurInstance(NULL); | 608 MockDeviceDataFactory::SetCurInstance(NULL); |
524 } | 609 } |
525 | 610 |
526 // Tests that Orientation events only fire if the change is significant | 611 // Tests that Motion events always fire, even if the motion is unchanged. |
hans
2012/08/13 13:35:19
i was just about to ask for a test like that :)
| |
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); | |
617 | |
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); | |
629 | |
630 scoped_ptr<MotionUpdateChecker> checker(new MotionUpdateChecker( | |
631 &pending_expectations_)); | |
632 | |
633 device_data_factory->SetDeviceData(test_motion, DeviceData::kTypeMotion); | |
634 checker->AddExpectation(test_motion); | |
635 provider_->AddObserver(checker.get()); | |
636 MessageLoop::current()->Run(); | |
637 | |
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(); | |
642 | |
643 provider_->RemoveObserver(checker.get()); | |
644 MockDeviceDataFactory::SetCurInstance(NULL); | |
645 } | |
646 | |
647 // Tests that Orientation events only fire if the change is significant. | |
527 TEST_F(DeviceOrientationProviderTest, OrientationSignificantlyDifferent) { | 648 TEST_F(DeviceOrientationProviderTest, OrientationSignificantlyDifferent) { |
528 scoped_refptr<MockDeviceDataFactory> device_data_factory( | 649 scoped_refptr<MockDeviceDataFactory> device_data_factory( |
529 new MockDeviceDataFactory()); | 650 new MockDeviceDataFactory()); |
530 MockDeviceDataFactory::SetCurInstance(device_data_factory.get()); | 651 MockDeviceDataFactory::SetCurInstance(device_data_factory.get()); |
531 Init(MockDeviceDataFactory::CreateDataFetcher); | 652 Init(MockDeviceDataFactory::CreateDataFetcher); |
532 | 653 |
533 // Values that should be well below or above the implementation's | 654 // Values that should be well below or above the implementation's |
534 // significane threshold. | 655 // significane threshold. |
535 const double kInsignificantDifference = 1e-6; | 656 const double kInsignificantDifference = 1e-6; |
536 const double kSignificantDifference = 30; | 657 const double kSignificantDifference = 30; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
579 MessageLoop::current()->Run(); | 700 MessageLoop::current()->Run(); |
580 | 701 |
581 provider_->RemoveObserver(checker_a.get()); | 702 provider_->RemoveObserver(checker_a.get()); |
582 provider_->RemoveObserver(checker_b.get()); | 703 provider_->RemoveObserver(checker_b.get()); |
583 MockDeviceDataFactory::SetCurInstance(NULL); | 704 MockDeviceDataFactory::SetCurInstance(NULL); |
584 } | 705 } |
585 | 706 |
586 } // namespace | 707 } // namespace |
587 | 708 |
588 } // namespace device_orientation | 709 } // namespace device_orientation |
OLD | NEW |