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

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

Issue 10755002: Refactors DeviceOrientation to make it more extensible (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Adds a unit test, responds to other comments 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
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 <map>
5 #include <queue> 6 #include <queue>
6 7
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "base/synchronization/lock.h" 9 #include "base/synchronization/lock.h"
9 #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"
10 #include "content/browser/device_orientation/orientation.h" 12 #include "content/browser/device_orientation/orientation.h"
11 #include "content/browser/device_orientation/provider.h" 13 #include "content/browser/device_orientation/provider.h"
12 #include "content/browser/device_orientation/provider_impl.h" 14 #include "content/browser/device_orientation/provider_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 namespace device_orientation { 17 namespace device_orientation {
16 namespace { 18 namespace {
17 19
18 // Class for checking expectations on orientation updates from the Provider. 20 // Class for testing multiple types of device data.
21 class TestData : public DeviceData {
22 public:
23 TestData()
24 : value_(0) {
25 }
26
27 // From DeviceData.
28 virtual IPC::Message* CreateIPCMessage(int render_view_id) const OVERRIDE {
29 return NULL;
hans 2012/08/03 13:05:41 maybe add a CHECK or DCHECK here, just to make sur
aousterh 2012/08/03 13:42:18 Done.
30 }
31 virtual bool ShouldFireEvent(const DeviceData* old_data) const OVERRIDE {
32 return true;
33 }
34
35 void set_value(double value) { value_ = value; }
36 double value() const { return value_; }
37
38 private:
39 virtual ~TestData() { }
40
41 double value_;
42 };
43
44 // Class for checking expectations on device_data updates from the Provider.
19 class UpdateChecker : public Provider::Observer { 45 class UpdateChecker : public Provider::Observer {
20 public: 46 public:
21 explicit UpdateChecker(int* expectations_count_ptr) 47 UpdateChecker(DeviceData::Type device_data_type,
22 : expectations_count_ptr_(expectations_count_ptr) { 48 int *expectations_count_ptr)
49 : Observer(device_data_type),
50 expectations_count_ptr_(expectations_count_ptr) {
23 } 51 }
24
25 virtual ~UpdateChecker() {} 52 virtual ~UpdateChecker() {}
26 53
27 // From Provider::Observer. 54 // From Provider::Observer.
28 virtual void OnOrientationUpdate(const Orientation& orientation) { 55 virtual void OnDeviceDataUpdate(const DeviceData* device_data,
56 DeviceData::Type device_data_type) OVERRIDE = 0;
57
58 void AddExpectation(const DeviceData* device_data) {
59 scoped_refptr<const DeviceData> expected_device_data(device_data);
60 expectations_queue_.push(expected_device_data);
61 ++(*expectations_count_ptr_);
62 }
63
64 protected:
65 // Set up by the test fixture, which then blocks while it is accessed
66 // from OnDeviceDataUpdate which is executed on the test fixture's
67 // message_loop_.
68 int* expectations_count_ptr_;
69 std::queue<scoped_refptr<const DeviceData> > expectations_queue_;
70 };
71
72 // Class for checking expectations on orientation updates from the Provider.
73 class OrientationUpdateChecker : public UpdateChecker {
74 public:
75 explicit OrientationUpdateChecker(int* expectations_count_ptr)
76 : UpdateChecker(DeviceData::kTypeOrientation, expectations_count_ptr) {
77 }
78
79 virtual ~OrientationUpdateChecker() {}
80
81 // From UpdateChecker.
82 virtual void OnDeviceDataUpdate(const DeviceData* device_data,
83 DeviceData::Type device_data_type) OVERRIDE {
29 ASSERT_FALSE(expectations_queue_.empty()); 84 ASSERT_FALSE(expectations_queue_.empty());
85 ASSERT_EQ(DeviceData::kTypeOrientation, device_data_type);
30 86
31 Orientation expected = expectations_queue_.front(); 87 scoped_refptr<const Orientation> orientation(
88 static_cast<const Orientation*>(device_data));
89 if (orientation == NULL)
90 orientation = new Orientation();
91
92 scoped_refptr<const Orientation> expected(static_cast<const Orientation*>(
93 (expectations_queue_.front().get())));
32 expectations_queue_.pop(); 94 expectations_queue_.pop();
33 95
34 EXPECT_EQ(expected.can_provide_alpha(), orientation.can_provide_alpha()); 96 EXPECT_EQ(expected->can_provide_alpha(), orientation->can_provide_alpha());
35 EXPECT_EQ(expected.can_provide_beta(), orientation.can_provide_beta()); 97 EXPECT_EQ(expected->can_provide_beta(), orientation->can_provide_beta());
36 EXPECT_EQ(expected.can_provide_gamma(), orientation.can_provide_gamma()); 98 EXPECT_EQ(expected->can_provide_gamma(), orientation->can_provide_gamma());
37 EXPECT_EQ(expected.can_provide_absolute(), 99 EXPECT_EQ(expected->can_provide_absolute(),
38 orientation.can_provide_absolute()); 100 orientation->can_provide_absolute());
39 if (expected.can_provide_alpha()) 101 if (expected->can_provide_alpha())
40 EXPECT_EQ(expected.alpha(), orientation.alpha()); 102 EXPECT_EQ(expected->alpha(), orientation->alpha());
41 if (expected.can_provide_beta()) 103 if (expected->can_provide_beta())
42 EXPECT_EQ(expected.beta(), orientation.beta()); 104 EXPECT_EQ(expected->beta(), orientation->beta());
43 if (expected.can_provide_gamma()) 105 if (expected->can_provide_gamma())
44 EXPECT_EQ(expected.gamma(), orientation.gamma()); 106 EXPECT_EQ(expected->gamma(), orientation->gamma());
45 if (expected.can_provide_absolute()) 107 if (expected->can_provide_absolute())
46 EXPECT_EQ(expected.absolute(), orientation.absolute()); 108 EXPECT_EQ(expected->absolute(), orientation->absolute());
47 109
48 --(*expectations_count_ptr_); 110 --(*expectations_count_ptr_);
49 111
50 if (*expectations_count_ptr_ == 0) { 112 if (*expectations_count_ptr_ == 0) {
51 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 113 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
52 } 114 }
53 } 115 }
116 };
54 117
55 void AddExpectation(const Orientation& orientation) { 118 // Class for checking expectations on test_data updates from the Provider.
56 expectations_queue_.push(orientation); 119 class TestDataUpdateChecker : public UpdateChecker {
57 ++(*expectations_count_ptr_); 120 public:
121 explicit TestDataUpdateChecker(int* expectations_count_ptr)
122 : UpdateChecker(DeviceData::kTypeTest, expectations_count_ptr) {
58 } 123 }
59 124
60 private: 125 // From UpdateChecker.
61 // Set up by the test fixture, which then blocks while it is accessed 126 virtual void OnDeviceDataUpdate(const DeviceData* device_data,
62 // from OnOrientationUpdate which is executed on the test fixture's 127 DeviceData::Type device_data_type) OVERRIDE {
63 // message_loop_. 128 ASSERT_FALSE(expectations_queue_.empty());
64 int* expectations_count_ptr_; 129 ASSERT_EQ(DeviceData::kTypeTest, device_data_type);
65 std::queue<Orientation> expectations_queue_; 130
131 scoped_refptr<const TestData> test_data(
132 static_cast<const TestData*>(device_data));
133 if (test_data == NULL)
134 test_data = new TestData();
135
136 scoped_refptr<const TestData> expected(static_cast<const TestData*>(
137 (expectations_queue_.front().get())));
138 expectations_queue_.pop();
139
140 EXPECT_EQ(expected->value(), test_data->value());
141
142 --(*expectations_count_ptr_);
143
144 if (*expectations_count_ptr_ == 0) {
145 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
146 }
147 }
66 }; 148 };
67 149
68 // Class for injecting test orientation data into the Provider. 150 // Class for injecting test device data into the Provider.
69 class MockOrientationFactory 151 class MockDeviceDataFactory
70 : public base::RefCountedThreadSafe<MockOrientationFactory> { 152 : public base::RefCountedThreadSafe<MockDeviceDataFactory> {
71 public: 153 public:
72 MockOrientationFactory() 154 MockDeviceDataFactory()
73 : is_failing_(false) { 155 : is_failing_(false) {
74 } 156 }
75 157
76 static void SetCurInstance(MockOrientationFactory* instance) { 158 static void SetCurInstance(MockDeviceDataFactory* instance) {
77 if (instance) { 159 if (instance) {
78 EXPECT_FALSE(instance_); 160 EXPECT_FALSE(instance_);
79 } 161 }
80 else { 162 else {
81 EXPECT_TRUE(instance_); 163 EXPECT_TRUE(instance_);
82 } 164 }
83 instance_ = instance; 165 instance_ = instance;
84 } 166 }
85 167
86 static DataFetcher* CreateDataFetcher() { 168 static DataFetcher* CreateDataFetcher() {
87 EXPECT_TRUE(instance_); 169 EXPECT_TRUE(instance_);
88 return new MockDataFetcher(instance_); 170 return new MockDataFetcher(instance_);
89 } 171 }
90 172
91 void SetOrientation(const Orientation& orientation) { 173 void SetDeviceData(const DeviceData* device_data, DeviceData::Type type) {
92 base::AutoLock auto_lock(lock_); 174 base::AutoLock auto_lock(lock_);
93 orientation_ = orientation; 175 device_data_map_[type] = device_data;
94 } 176 }
95 177
96 void SetFailing(bool is_failing) { 178 void SetFailing(bool is_failing) {
97 base::AutoLock auto_lock(lock_); 179 base::AutoLock auto_lock(lock_);
98 is_failing_ = is_failing; 180 is_failing_ = is_failing;
99 } 181 }
100 182
101 private: 183 private:
102 friend class base::RefCountedThreadSafe<MockOrientationFactory>; 184 friend class base::RefCountedThreadSafe<MockDeviceDataFactory>;
103 185
104 ~MockOrientationFactory() { 186 ~MockDeviceDataFactory() {
105 } 187 }
106 188
107 // Owned by ProviderImpl. Holds a reference back to MockOrientationFactory. 189 // Owned by ProviderImpl. Holds a reference back to MockDeviceDataFactory.
108 class MockDataFetcher : public DataFetcher { 190 class MockDataFetcher : public DataFetcher {
109 public: 191 public:
110 explicit MockDataFetcher(MockOrientationFactory* orientation_factory) 192 explicit MockDataFetcher(MockDeviceDataFactory* device_data_factory)
111 : orientation_factory_(orientation_factory) { } 193 : device_data_factory_(device_data_factory) { }
112 194
113 // From DataFetcher. Called by the Provider. 195 // From DataFetcher. Called by the Provider.
114 virtual bool GetOrientation(Orientation* orientation) { 196 virtual const DeviceData* GetDeviceData(DeviceData::Type device_data_type) {
115 base::AutoLock auto_lock(orientation_factory_->lock_); 197 base::AutoLock auto_lock(device_data_factory_->lock_);
116 if (orientation_factory_->is_failing_) 198 if (device_data_factory_->is_failing_)
117 return false; 199 return NULL;
118 *orientation = orientation_factory_->orientation_; 200 return device_data_factory_->device_data_map_[device_data_type];
119 return true;
120 } 201 }
121 202
122 private: 203 private:
123 scoped_refptr<MockOrientationFactory> orientation_factory_; 204 scoped_refptr<MockDeviceDataFactory> device_data_factory_;
124 }; 205 };
125 206
126 static MockOrientationFactory* instance_; 207 static MockDeviceDataFactory* instance_;
127 Orientation orientation_; 208 std::map<DeviceData::Type, scoped_refptr<const DeviceData> > device_data_map_;
128 bool is_failing_; 209 bool is_failing_;
129 base::Lock lock_; 210 base::Lock lock_;
130 }; 211 };
131 212
132 MockOrientationFactory* MockOrientationFactory::instance_; 213 MockDeviceDataFactory* MockDeviceDataFactory::instance_;
133
134 // Mock DataFetcher that always fails to provide any orientation data.
135 class FailingDataFetcher : public DataFetcher {
136 public:
137 // Factory method; passed to and called by the ProviderImpl.
138 static DataFetcher* Create() {
139 return new FailingDataFetcher();
140 }
141
142 // From DataFetcher.
143 virtual bool GetOrientation(Orientation* orientation) {
144 return false;
145 }
146
147 private:
148 FailingDataFetcher() {}
149 };
150 214
151 class DeviceOrientationProviderTest : public testing::Test { 215 class DeviceOrientationProviderTest : public testing::Test {
152 public: 216 public:
153 DeviceOrientationProviderTest() 217 DeviceOrientationProviderTest()
154 : pending_expectations_(0) { 218 : pending_expectations_(0) {
155 } 219 }
156 220
157 virtual void TearDown() { 221 virtual void TearDown() {
158 provider_ = NULL; 222 provider_ = NULL;
159 223
(...skipping 16 matching lines...) Expand all
176 int pending_expectations_; 240 int pending_expectations_;
177 241
178 // Provider instance under test. 242 // Provider instance under test.
179 scoped_refptr<Provider> provider_; 243 scoped_refptr<Provider> provider_;
180 244
181 // Message loop for the test thread. 245 // Message loop for the test thread.
182 MessageLoop message_loop_; 246 MessageLoop message_loop_;
183 }; 247 };
184 248
185 TEST_F(DeviceOrientationProviderTest, FailingTest) { 249 TEST_F(DeviceOrientationProviderTest, FailingTest) {
186 Init(FailingDataFetcher::Create); 250 scoped_refptr<MockDeviceDataFactory> device_data_factory(
251 new MockDeviceDataFactory());
252 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
253 Init(MockDeviceDataFactory::CreateDataFetcher);
187 254
188 scoped_ptr<UpdateChecker> checker_a( 255 scoped_ptr<OrientationUpdateChecker> checker_a(
189 new UpdateChecker(&pending_expectations_)); 256 new OrientationUpdateChecker(&pending_expectations_));
190 scoped_ptr<UpdateChecker> checker_b( 257 scoped_ptr<OrientationUpdateChecker> checker_b(
191 new UpdateChecker(&pending_expectations_)); 258 new OrientationUpdateChecker(&pending_expectations_));
192 259
193 checker_a->AddExpectation(Orientation::Empty()); 260 checker_a->AddExpectation(new Orientation());
194 provider_->AddObserver(checker_a.get()); 261 provider_->AddObserver(checker_a.get());
195 MessageLoop::current()->Run(); 262 MessageLoop::current()->Run();
196 263
197 checker_b->AddExpectation(Orientation::Empty()); 264 checker_b->AddExpectation(new Orientation());
198 provider_->AddObserver(checker_b.get()); 265 provider_->AddObserver(checker_b.get());
199 MessageLoop::current()->Run(); 266 MessageLoop::current()->Run();
267
268 MockDeviceDataFactory::SetCurInstance(NULL);
200 } 269 }
201 270
202 TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) { 271 TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) {
203 Init(FailingDataFetcher::Create); 272 scoped_refptr<MockDeviceDataFactory> device_data_factory(
273 new MockDeviceDataFactory());
274 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
275 Init(MockDeviceDataFactory::CreateDataFetcher);
204 276
205 scoped_refptr<Provider> provider_a(Provider::GetInstance()); 277 scoped_refptr<Provider> provider_a(Provider::GetInstance());
206 scoped_refptr<Provider> provider_b(Provider::GetInstance()); 278 scoped_refptr<Provider> provider_b(Provider::GetInstance());
207 279
208 EXPECT_EQ(provider_a.get(), provider_b.get()); 280 EXPECT_EQ(provider_a.get(), provider_b.get());
281 MockDeviceDataFactory::SetCurInstance(NULL);
209 } 282 }
210 283
211 TEST_F(DeviceOrientationProviderTest, BasicPushTest) { 284 TEST_F(DeviceOrientationProviderTest, BasicPushTest) {
212 scoped_refptr<MockOrientationFactory> orientation_factory( 285 scoped_refptr<MockDeviceDataFactory> device_data_factory(
213 new MockOrientationFactory()); 286 new MockDeviceDataFactory());
214 MockOrientationFactory::SetCurInstance(orientation_factory.get()); 287 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
215 Init(MockOrientationFactory::CreateDataFetcher); 288 Init(MockDeviceDataFactory::CreateDataFetcher);
216 Orientation test_orientation; 289 scoped_refptr<Orientation> test_orientation(new Orientation());
217 test_orientation.set_alpha(1); 290 test_orientation->set_alpha(1);
218 test_orientation.set_beta(2); 291 test_orientation->set_beta(2);
219 test_orientation.set_gamma(3); 292 test_orientation->set_gamma(3);
220 test_orientation.set_absolute(true); 293 test_orientation->set_absolute(true);
221 294
222 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); 295 scoped_ptr<OrientationUpdateChecker> checker(
296 new OrientationUpdateChecker(&pending_expectations_));
223 checker->AddExpectation(test_orientation); 297 checker->AddExpectation(test_orientation);
224 orientation_factory->SetOrientation(test_orientation); 298 device_data_factory->SetDeviceData(test_orientation,
299 DeviceData::kTypeOrientation);
225 provider_->AddObserver(checker.get()); 300 provider_->AddObserver(checker.get());
226 MessageLoop::current()->Run(); 301 MessageLoop::current()->Run();
227 302
228 provider_->RemoveObserver(checker.get()); 303 provider_->RemoveObserver(checker.get());
229 MockOrientationFactory::SetCurInstance(NULL); 304 MockDeviceDataFactory::SetCurInstance(NULL);
230 } 305 }
231 306
307 // Tests multiple observers observing the same type of data.
232 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) { 308 TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) {
233 scoped_refptr<MockOrientationFactory> orientation_factory( 309 scoped_refptr<MockDeviceDataFactory> device_data_factory(
234 new MockOrientationFactory()); 310 new MockDeviceDataFactory());
235 MockOrientationFactory::SetCurInstance(orientation_factory.get()); 311 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
236 Init(MockOrientationFactory::CreateDataFetcher); 312 Init(MockDeviceDataFactory::CreateDataFetcher);
237 313
238 Orientation test_orientations[] = {Orientation(), Orientation(), 314 scoped_refptr<Orientation> test_orientations[] = {new Orientation(),
239 Orientation()}; 315 new Orientation(), new Orientation()};
240 test_orientations[0].set_alpha(1); 316 test_orientations[0]->set_alpha(1);
241 test_orientations[0].set_beta(2); 317 test_orientations[0]->set_beta(2);
242 test_orientations[0].set_gamma(3); 318 test_orientations[0]->set_gamma(3);
243 test_orientations[0].set_absolute(true); 319 test_orientations[0]->set_absolute(true);
244 320
245 test_orientations[1].set_alpha(4); 321 test_orientations[1]->set_alpha(4);
246 test_orientations[1].set_beta(5); 322 test_orientations[1]->set_beta(5);
247 test_orientations[1].set_gamma(6); 323 test_orientations[1]->set_gamma(6);
248 test_orientations[1].set_absolute(false); 324 test_orientations[1]->set_absolute(false);
249 325
250 test_orientations[2].set_alpha(7); 326 test_orientations[2]->set_alpha(7);
251 test_orientations[2].set_beta(8); 327 test_orientations[2]->set_beta(8);
252 test_orientations[2].set_gamma(9); 328 test_orientations[2]->set_gamma(9);
253 // can't provide absolute 329 // can't provide absolute
254 330
255 scoped_ptr<UpdateChecker> checker_a( 331 scoped_ptr<OrientationUpdateChecker> checker_a(
256 new UpdateChecker(&pending_expectations_)); 332 new OrientationUpdateChecker(&pending_expectations_));
257 scoped_ptr<UpdateChecker> checker_b( 333 scoped_ptr<OrientationUpdateChecker> checker_b(
258 new UpdateChecker(&pending_expectations_)); 334 new OrientationUpdateChecker(&pending_expectations_));
259 scoped_ptr<UpdateChecker> checker_c( 335 scoped_ptr<OrientationUpdateChecker> checker_c(
260 new UpdateChecker(&pending_expectations_)); 336 new OrientationUpdateChecker(&pending_expectations_));
261 337
262 checker_a->AddExpectation(test_orientations[0]); 338 checker_a->AddExpectation(test_orientations[0]);
263 orientation_factory->SetOrientation(test_orientations[0]); 339 device_data_factory->SetDeviceData(test_orientations[0],
340 DeviceData::kTypeOrientation);
264 provider_->AddObserver(checker_a.get()); 341 provider_->AddObserver(checker_a.get());
265 MessageLoop::current()->Run(); 342 MessageLoop::current()->Run();
266 343
267 checker_a->AddExpectation(test_orientations[1]); 344 checker_a->AddExpectation(test_orientations[1]);
268 checker_b->AddExpectation(test_orientations[0]); 345 checker_b->AddExpectation(test_orientations[0]);
269 checker_b->AddExpectation(test_orientations[1]); 346 checker_b->AddExpectation(test_orientations[1]);
270 orientation_factory->SetOrientation(test_orientations[1]); 347 device_data_factory->SetDeviceData(test_orientations[1],
348 DeviceData::kTypeOrientation);
271 provider_->AddObserver(checker_b.get()); 349 provider_->AddObserver(checker_b.get());
272 MessageLoop::current()->Run(); 350 MessageLoop::current()->Run();
273 351
274 provider_->RemoveObserver(checker_a.get()); 352 provider_->RemoveObserver(checker_a.get());
275 checker_b->AddExpectation(test_orientations[2]); 353 checker_b->AddExpectation(test_orientations[2]);
276 checker_c->AddExpectation(test_orientations[1]); 354 checker_c->AddExpectation(test_orientations[1]);
277 checker_c->AddExpectation(test_orientations[2]); 355 checker_c->AddExpectation(test_orientations[2]);
278 orientation_factory->SetOrientation(test_orientations[2]); 356 device_data_factory->SetDeviceData(test_orientations[2],
357 DeviceData::kTypeOrientation);
279 provider_->AddObserver(checker_c.get()); 358 provider_->AddObserver(checker_c.get());
280 MessageLoop::current()->Run(); 359 MessageLoop::current()->Run();
281 360
282 provider_->RemoveObserver(checker_b.get()); 361 provider_->RemoveObserver(checker_b.get());
283 provider_->RemoveObserver(checker_c.get()); 362 provider_->RemoveObserver(checker_c.get());
284 MockOrientationFactory::SetCurInstance(NULL); 363 MockDeviceDataFactory::SetCurInstance(NULL);
364 }
365
366 // Test for when the fetcher cannot provide the first type of data but can
367 // provide the second type.
368 TEST_F(DeviceOrientationProviderTest, FailingFirstDataTypeTest) {
369
370 scoped_refptr<MockDeviceDataFactory> device_data_factory(
371 new MockDeviceDataFactory());
372 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
373 Init(MockDeviceDataFactory::CreateDataFetcher);
374
375 scoped_ptr<TestDataUpdateChecker> test_data_checker(
376 new TestDataUpdateChecker(&pending_expectations_));
377 scoped_ptr<OrientationUpdateChecker> orientation_checker(
378 new OrientationUpdateChecker(&pending_expectations_));
379
380 scoped_refptr<Orientation> test_orientation(new Orientation());
381 test_orientation->set_alpha(1);
382 test_orientation->set_beta(2);
383 test_orientation->set_gamma(3);
384 test_orientation->set_absolute(true);
385
386 test_data_checker->AddExpectation(new TestData());
387 provider_->AddObserver(test_data_checker.get());
388 MessageLoop::current()->Run();
389
390 orientation_checker->AddExpectation(test_orientation);
391 device_data_factory->SetDeviceData(test_orientation,
392 DeviceData::kTypeOrientation);
393 provider_->AddObserver(orientation_checker.get());
394 MessageLoop::current()->Run();
395
396 provider_->RemoveObserver(test_data_checker.get());
397 provider_->RemoveObserver(orientation_checker.get());
398 MockDeviceDataFactory::SetCurInstance(NULL);
285 } 399 }
286 400
287 #if defined(OS_LINUX) || defined(OS_WIN) 401 #if defined(OS_LINUX) || defined(OS_WIN)
288 // Flakily DCHECKs on Linux. See crbug.com/104950. 402 // Flakily DCHECKs on Linux. See crbug.com/104950.
289 // FLAKY on Win. See crbug.com/104950. 403 // FLAKY on Win. See crbug.com/104950.
290 #define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved 404 #define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved
291 #else 405 #else
292 #define MAYBE_ObserverNotRemoved ObserverNotRemoved 406 #define MAYBE_ObserverNotRemoved ObserverNotRemoved
293 #endif 407 #endif
294 TEST_F(DeviceOrientationProviderTest, MAYBE_ObserverNotRemoved) { 408 TEST_F(DeviceOrientationProviderTest, MAYBE_ObserverNotRemoved) {
295 scoped_refptr<MockOrientationFactory> orientation_factory( 409 scoped_refptr<MockDeviceDataFactory> device_data_factory(
296 new MockOrientationFactory()); 410 new MockDeviceDataFactory());
297 MockOrientationFactory::SetCurInstance(orientation_factory.get()); 411 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
298 Init(MockOrientationFactory::CreateDataFetcher); 412 Init(MockDeviceDataFactory::CreateDataFetcher);
299 Orientation test_orientation; 413 scoped_refptr<Orientation> test_orientation(new Orientation());
300 test_orientation.set_alpha(1); 414 test_orientation->set_alpha(1);
301 test_orientation.set_beta(2); 415 test_orientation->set_beta(2);
302 test_orientation.set_gamma(3); 416 test_orientation->set_gamma(3);
303 test_orientation.set_absolute(true); 417 test_orientation->set_absolute(true);
304 418
305 Orientation test_orientation2; 419 scoped_refptr<Orientation> test_orientation2(new Orientation());
306 test_orientation2.set_alpha(4); 420 test_orientation2->set_alpha(4);
307 test_orientation2.set_beta(5); 421 test_orientation2->set_beta(5);
308 test_orientation2.set_gamma(6); 422 test_orientation2->set_gamma(6);
309 test_orientation2.set_absolute(false); 423 test_orientation2->set_absolute(false);
310 424
311 scoped_ptr<UpdateChecker> checker(new UpdateChecker(&pending_expectations_)); 425 scoped_ptr<OrientationUpdateChecker> checker(
426 new OrientationUpdateChecker(&pending_expectations_));
312 checker->AddExpectation(test_orientation); 427 checker->AddExpectation(test_orientation);
313 orientation_factory->SetOrientation(test_orientation); 428 device_data_factory->SetDeviceData(test_orientation,
429 DeviceData::kTypeOrientation);
314 provider_->AddObserver(checker.get()); 430 provider_->AddObserver(checker.get());
315 MessageLoop::current()->Run(); 431 MessageLoop::current()->Run();
316 432
317 checker->AddExpectation(test_orientation2); 433 checker->AddExpectation(test_orientation2);
318 orientation_factory->SetOrientation(test_orientation2); 434 device_data_factory->SetDeviceData(test_orientation2,
435 DeviceData::kTypeOrientation);
319 MessageLoop::current()->Run(); 436 MessageLoop::current()->Run();
320 437
321 MockOrientationFactory::SetCurInstance(NULL); 438 MockDeviceDataFactory::SetCurInstance(NULL);
322 439
323 // Note that checker is not removed. This should not be a problem. 440 // Note that checker is not removed. This should not be a problem.
324 } 441 }
325 442
326 #if defined(OS_WIN) 443 #if defined(OS_WIN)
327 // FLAKY on Win. See crbug.com/104950. 444 // FLAKY on Win. See crbug.com/104950.
328 #define MAYBE_StartFailing DISABLED_StartFailing 445 #define MAYBE_StartFailing DISABLED_StartFailing
329 #else 446 #else
330 #define MAYBE_StartFailing StartFailing 447 #define MAYBE_StartFailing StartFailing
331 #endif 448 #endif
332 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) { 449 TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) {
333 scoped_refptr<MockOrientationFactory> orientation_factory( 450 scoped_refptr<MockDeviceDataFactory> device_data_factory(
334 new MockOrientationFactory()); 451 new MockDeviceDataFactory());
335 MockOrientationFactory::SetCurInstance(orientation_factory.get()); 452 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
336 Init(MockOrientationFactory::CreateDataFetcher); 453 Init(MockDeviceDataFactory::CreateDataFetcher);
337 Orientation test_orientation; 454 scoped_refptr<Orientation> test_orientation(new Orientation());
338 test_orientation.set_alpha(1); 455 test_orientation->set_alpha(1);
339 test_orientation.set_beta(2); 456 test_orientation->set_beta(2);
340 test_orientation.set_gamma(3); 457 test_orientation->set_gamma(3);
341 test_orientation.set_absolute(true); 458 test_orientation->set_absolute(true);
342 459
343 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 460 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
344 &pending_expectations_)); 461 &pending_expectations_));
345 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 462 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
346 &pending_expectations_)); 463 &pending_expectations_));
347 464
348 orientation_factory->SetOrientation(test_orientation); 465 device_data_factory->SetDeviceData(test_orientation,
466 DeviceData::kTypeOrientation);
349 checker_a->AddExpectation(test_orientation); 467 checker_a->AddExpectation(test_orientation);
350 provider_->AddObserver(checker_a.get()); 468 provider_->AddObserver(checker_a.get());
351 MessageLoop::current()->Run(); 469 MessageLoop::current()->Run();
352 470
353 checker_a->AddExpectation(Orientation::Empty()); 471 checker_a->AddExpectation(new Orientation());
354 orientation_factory->SetFailing(true); 472 device_data_factory->SetFailing(true);
355 MessageLoop::current()->Run(); 473 MessageLoop::current()->Run();
356 474
357 checker_b->AddExpectation(Orientation::Empty()); 475 checker_b->AddExpectation(new Orientation());
358 provider_->AddObserver(checker_b.get()); 476 provider_->AddObserver(checker_b.get());
359 MessageLoop::current()->Run(); 477 MessageLoop::current()->Run();
360 478
361 provider_->RemoveObserver(checker_a.get()); 479 provider_->RemoveObserver(checker_a.get());
362 provider_->RemoveObserver(checker_b.get()); 480 provider_->RemoveObserver(checker_b.get());
363 MockOrientationFactory::SetCurInstance(NULL); 481 MockDeviceDataFactory::SetCurInstance(NULL);
364 } 482 }
365 483
366 TEST_F(DeviceOrientationProviderTest, StartStopStart) { 484 TEST_F(DeviceOrientationProviderTest, StartStopStart) {
367 scoped_refptr<MockOrientationFactory> orientation_factory( 485 scoped_refptr<MockDeviceDataFactory> device_data_factory(
368 new MockOrientationFactory()); 486 new MockDeviceDataFactory());
369 MockOrientationFactory::SetCurInstance(orientation_factory.get()); 487 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
370 Init(MockOrientationFactory::CreateDataFetcher); 488 Init(MockDeviceDataFactory::CreateDataFetcher);
371 489
372 Orientation test_orientation; 490 scoped_refptr<Orientation> test_orientation(new Orientation());
373 test_orientation.set_alpha(1); 491 test_orientation->set_alpha(1);
374 test_orientation.set_beta(2); 492 test_orientation->set_beta(2);
375 test_orientation.set_gamma(3); 493 test_orientation->set_gamma(3);
376 test_orientation.set_absolute(true); 494 test_orientation->set_absolute(true);
377 495
378 Orientation test_orientation2; 496 scoped_refptr<Orientation> test_orientation2(new Orientation());
379 test_orientation2.set_alpha(4); 497 test_orientation2->set_alpha(4);
380 test_orientation2.set_beta(5); 498 test_orientation2->set_beta(5);
381 test_orientation2.set_gamma(6); 499 test_orientation2->set_gamma(6);
382 test_orientation2.set_absolute(false); 500 test_orientation2->set_absolute(false);
383 501
384 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 502 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
385 &pending_expectations_)); 503 &pending_expectations_));
386 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 504 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
387 &pending_expectations_)); 505 &pending_expectations_));
388 506
389 checker_a->AddExpectation(test_orientation); 507 checker_a->AddExpectation(test_orientation);
390 orientation_factory->SetOrientation(test_orientation); 508 device_data_factory->SetDeviceData(test_orientation,
509 DeviceData::kTypeOrientation);
391 provider_->AddObserver(checker_a.get()); 510 provider_->AddObserver(checker_a.get());
392 MessageLoop::current()->Run(); 511 MessageLoop::current()->Run();
393 512
394 provider_->RemoveObserver(checker_a.get()); // This stops the Provider. 513 provider_->RemoveObserver(checker_a.get()); // This stops the Provider.
395 514
396 checker_b->AddExpectation(test_orientation2); 515 checker_b->AddExpectation(test_orientation2);
397 orientation_factory->SetOrientation(test_orientation2); 516 device_data_factory->SetDeviceData(test_orientation2,
517 DeviceData::kTypeOrientation);
398 provider_->AddObserver(checker_b.get()); 518 provider_->AddObserver(checker_b.get());
399 MessageLoop::current()->Run(); 519 MessageLoop::current()->Run();
400 520
401 provider_->RemoveObserver(checker_b.get()); 521 provider_->RemoveObserver(checker_b.get());
402 MockOrientationFactory::SetCurInstance(NULL); 522 MockDeviceDataFactory::SetCurInstance(NULL);
403 } 523 }
404 524
405 TEST_F(DeviceOrientationProviderTest, SignificantlyDifferent) { 525 // Tests that Orientation events only fire if the change is significant
406 scoped_refptr<MockOrientationFactory> orientation_factory( 526 TEST_F(DeviceOrientationProviderTest, OrientationSignificantlyDifferent) {
407 new MockOrientationFactory()); 527 scoped_refptr<MockDeviceDataFactory> device_data_factory(
408 MockOrientationFactory::SetCurInstance(orientation_factory.get()); 528 new MockDeviceDataFactory());
409 Init(MockOrientationFactory::CreateDataFetcher); 529 MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
530 Init(MockDeviceDataFactory::CreateDataFetcher);
410 531
411 // Values that should be well below or above the implementation's 532 // Values that should be well below or above the implementation's
412 // significane threshold. 533 // significane threshold.
413 const double kInsignificantDifference = 1e-6; 534 const double kInsignificantDifference = 1e-6;
414 const double kSignificantDifference = 30; 535 const double kSignificantDifference = 30;
415 const double kAlpha = 4, kBeta = 5, kGamma = 6; 536 const double kAlpha = 4, kBeta = 5, kGamma = 6;
416 537
417 Orientation first_orientation; 538 scoped_refptr<Orientation> first_orientation(new Orientation());
418 first_orientation.set_alpha(kAlpha); 539 first_orientation->set_alpha(kAlpha);
419 first_orientation.set_beta(kBeta); 540 first_orientation->set_beta(kBeta);
420 first_orientation.set_gamma(kGamma); 541 first_orientation->set_gamma(kGamma);
421 first_orientation.set_absolute(true); 542 first_orientation->set_absolute(true);
422 543
423 Orientation second_orientation; 544 scoped_refptr<Orientation> second_orientation(new Orientation());
424 second_orientation.set_alpha(kAlpha + kInsignificantDifference); 545 second_orientation->set_alpha(kAlpha + kInsignificantDifference);
425 second_orientation.set_beta(kBeta + kInsignificantDifference); 546 second_orientation->set_beta(kBeta + kInsignificantDifference);
426 second_orientation.set_gamma(kGamma + kInsignificantDifference); 547 second_orientation->set_gamma(kGamma + kInsignificantDifference);
427 second_orientation.set_absolute(false); 548 second_orientation->set_absolute(false);
428 549
429 Orientation third_orientation; 550 scoped_refptr<Orientation> third_orientation(new Orientation());
430 third_orientation.set_alpha(kAlpha + kSignificantDifference); 551 third_orientation->set_alpha(kAlpha + kSignificantDifference);
431 third_orientation.set_beta(kBeta + kSignificantDifference); 552 third_orientation->set_beta(kBeta + kSignificantDifference);
432 third_orientation.set_gamma(kGamma + kSignificantDifference); 553 third_orientation->set_gamma(kGamma + kSignificantDifference);
433 // can't provide absolute 554 // can't provide absolute
434 555
435 scoped_ptr<UpdateChecker> checker_a(new UpdateChecker( 556 scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
436 &pending_expectations_)); 557 &pending_expectations_));
437 scoped_ptr<UpdateChecker> checker_b(new UpdateChecker( 558 scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
438 &pending_expectations_)); 559 &pending_expectations_));
439 560
440 561 device_data_factory->SetDeviceData(first_orientation,
441 orientation_factory->SetOrientation(first_orientation); 562 DeviceData::kTypeOrientation);
442 checker_a->AddExpectation(first_orientation); 563 checker_a->AddExpectation(first_orientation);
443 provider_->AddObserver(checker_a.get()); 564 provider_->AddObserver(checker_a.get());
444 MessageLoop::current()->Run(); 565 MessageLoop::current()->Run();
445 566
446 // The observers should not see this insignificantly different orientation. 567 // The observers should not see this insignificantly different orientation.
447 orientation_factory->SetOrientation(second_orientation); 568 device_data_factory->SetDeviceData(second_orientation,
569 DeviceData::kTypeOrientation);
448 checker_b->AddExpectation(first_orientation); 570 checker_b->AddExpectation(first_orientation);
449 provider_->AddObserver(checker_b.get()); 571 provider_->AddObserver(checker_b.get());
450 MessageLoop::current()->Run(); 572 MessageLoop::current()->Run();
451 573
452 orientation_factory->SetOrientation(third_orientation); 574 device_data_factory->SetDeviceData(third_orientation,
575 DeviceData::kTypeOrientation);
453 checker_a->AddExpectation(third_orientation); 576 checker_a->AddExpectation(third_orientation);
454 checker_b->AddExpectation(third_orientation); 577 checker_b->AddExpectation(third_orientation);
455 MessageLoop::current()->Run(); 578 MessageLoop::current()->Run();
456 579
457 provider_->RemoveObserver(checker_a.get()); 580 provider_->RemoveObserver(checker_a.get());
458 provider_->RemoveObserver(checker_b.get()); 581 provider_->RemoveObserver(checker_b.get());
459 MockOrientationFactory::SetCurInstance(NULL); 582 MockDeviceDataFactory::SetCurInstance(NULL);
460 } 583 }
461 584
462 } // namespace 585 } // namespace
463 586
464 } // namespace device_orientation 587 } // namespace device_orientation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698