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

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

Powered by Google App Engine
This is Rietveld 408576698