summaryrefslogtreecommitdiffstats
path: root/content/browser/device_orientation/provider_unittest.cc
blob: cc502486efc825ab500c11d4008b01a9ec6892c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <queue>

#include "base/message_loop/message_loop.h"
#include "base/synchronization/lock.h"
#include "content/browser/device_orientation/data_fetcher.h"
#include "content/browser/device_orientation/device_data.h"
#include "content/browser/device_orientation/motion.h"
#include "content/browser/device_orientation/orientation.h"
#include "content/browser/device_orientation/provider.h"
#include "content/browser/device_orientation/provider_impl.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
namespace {

// Class for testing multiple types of device data.
class TestData : public DeviceData {
 public:
  TestData()
      : value_(0) {
  }

  // From DeviceData.
  virtual IPC::Message* CreateIPCMessage(int render_view_id) const OVERRIDE {
    NOTREACHED();
    return NULL;
  }
  virtual bool ShouldFireEvent(const DeviceData* old_data) const OVERRIDE {
    return true;
  }

  void set_value(double value) { value_ = value; }
  double value() const { return value_; }

 private:
  virtual ~TestData() { }

  double value_;
};

// Class for checking expectations on device_data updates from the Provider.
class UpdateChecker : public Provider::Observer {
 public:
  UpdateChecker(DeviceData::Type device_data_type,
                int *expectations_count_ptr)
      : Observer(device_data_type),
        expectations_count_ptr_(expectations_count_ptr) {
  }
  virtual ~UpdateChecker() {}

  // From Provider::Observer.
  virtual void OnDeviceDataUpdate(const DeviceData* device_data,
      DeviceData::Type device_data_type) OVERRIDE = 0;

  void AddExpectation(const DeviceData* device_data) {
    scoped_refptr<const DeviceData> expected_device_data(device_data);
    expectations_queue_.push(expected_device_data);
    ++(*expectations_count_ptr_);
  }

 protected:
  // Set up by the test fixture, which then blocks while it is accessed
  // from OnDeviceDataUpdate which is executed on the test fixture's
  // message_loop_.
  int* expectations_count_ptr_;
  std::queue<scoped_refptr<const DeviceData> > expectations_queue_;
};

// Class for checking expectations on motion updates from the Provider.
class MotionUpdateChecker : public UpdateChecker {
 public:
  explicit MotionUpdateChecker(int* expectations_count_ptr)
      : UpdateChecker(DeviceData::kTypeMotion, expectations_count_ptr) {
  }

  virtual ~MotionUpdateChecker() {}

  // From UpdateChecker.
  virtual void OnDeviceDataUpdate(const DeviceData* device_data,
      DeviceData::Type device_data_type) OVERRIDE {
    ASSERT_FALSE(expectations_queue_.empty());
    ASSERT_EQ(DeviceData::kTypeMotion, device_data_type);

    scoped_refptr<const Motion> motion(static_cast<const Motion*>(device_data));
    if (motion.get() == NULL)
      motion = new Motion();

    scoped_refptr<const Motion> expected(static_cast<const Motion*>(
        (expectations_queue_.front().get())));
    expectations_queue_.pop();

    EXPECT_EQ(expected->can_provide_acceleration_x(),
              motion->can_provide_acceleration_x());
    EXPECT_EQ(expected->can_provide_acceleration_y(),
              motion->can_provide_acceleration_y());
    EXPECT_EQ(expected->can_provide_acceleration_z(),
              motion->can_provide_acceleration_z());

    EXPECT_EQ(expected->can_provide_acceleration_including_gravity_x(),
              motion->can_provide_acceleration_including_gravity_x());
    EXPECT_EQ(expected->can_provide_acceleration_including_gravity_y(),
              motion->can_provide_acceleration_including_gravity_y());
    EXPECT_EQ(expected->can_provide_acceleration_including_gravity_z(),
              motion->can_provide_acceleration_including_gravity_z());

    EXPECT_EQ(expected->can_provide_rotation_rate_alpha(),
              motion->can_provide_rotation_rate_alpha());
    EXPECT_EQ(expected->can_provide_rotation_rate_beta(),
              motion->can_provide_rotation_rate_beta());
    EXPECT_EQ(expected->can_provide_rotation_rate_gamma(),
              motion->can_provide_rotation_rate_gamma());

    EXPECT_EQ(expected->can_provide_interval(), motion->can_provide_interval());

    if (expected->can_provide_acceleration_x())
      EXPECT_EQ(expected->acceleration_x(), motion->acceleration_x());
    if (expected->can_provide_acceleration_y())
      EXPECT_EQ(expected->acceleration_y(), motion->acceleration_y());
    if (expected->can_provide_acceleration_z())
      EXPECT_EQ(expected->acceleration_z(), motion->acceleration_z());

    if (expected->can_provide_acceleration_including_gravity_x())
      EXPECT_EQ(expected->acceleration_including_gravity_x(),
                motion->acceleration_including_gravity_x());
    if (expected->can_provide_acceleration_including_gravity_y())
      EXPECT_EQ(expected->acceleration_including_gravity_y(),
                motion->acceleration_including_gravity_y());
    if (expected->can_provide_acceleration_including_gravity_z())
      EXPECT_EQ(expected->acceleration_including_gravity_z(),
                motion->acceleration_including_gravity_z());

    if (expected->can_provide_rotation_rate_alpha())
      EXPECT_EQ(expected->rotation_rate_alpha(),
                motion->rotation_rate_alpha());
    if (expected->can_provide_rotation_rate_beta())
      EXPECT_EQ(expected->rotation_rate_beta(),
                motion->rotation_rate_beta());
    if (expected->can_provide_rotation_rate_gamma())
      EXPECT_EQ(expected->rotation_rate_gamma(),
                motion->rotation_rate_gamma());

    if (expected->can_provide_interval())
      EXPECT_EQ(expected->interval(), motion->interval());

    --(*expectations_count_ptr_);

    if (*expectations_count_ptr_ == 0) {
      base::MessageLoop::current()->PostTask(FROM_HERE,
                                             base::MessageLoop::QuitClosure());
    }
  }
};

// Class for checking expectations on orientation updates from the Provider.
class OrientationUpdateChecker : public UpdateChecker {
 public:
  explicit OrientationUpdateChecker(int* expectations_count_ptr)
      : UpdateChecker(DeviceData::kTypeOrientation, expectations_count_ptr) {
  }

  virtual ~OrientationUpdateChecker() {}

  // From UpdateChecker.
  virtual void OnDeviceDataUpdate(const DeviceData* device_data,
      DeviceData::Type device_data_type) OVERRIDE {
    ASSERT_FALSE(expectations_queue_.empty());
    ASSERT_EQ(DeviceData::kTypeOrientation, device_data_type);

    scoped_refptr<const Orientation> orientation(
        static_cast<const Orientation*>(device_data));
    if (orientation.get() == NULL)
      orientation = new Orientation();

    scoped_refptr<const Orientation> expected(static_cast<const Orientation*>(
        (expectations_queue_.front().get())));
    expectations_queue_.pop();

    EXPECT_EQ(expected->can_provide_alpha(), orientation->can_provide_alpha());
    EXPECT_EQ(expected->can_provide_beta(),  orientation->can_provide_beta());
    EXPECT_EQ(expected->can_provide_gamma(), orientation->can_provide_gamma());
    EXPECT_EQ(expected->can_provide_absolute(),
              orientation->can_provide_absolute());
    if (expected->can_provide_alpha())
      EXPECT_EQ(expected->alpha(), orientation->alpha());
    if (expected->can_provide_beta())
      EXPECT_EQ(expected->beta(), orientation->beta());
    if (expected->can_provide_gamma())
      EXPECT_EQ(expected->gamma(), orientation->gamma());
    if (expected->can_provide_absolute())
      EXPECT_EQ(expected->absolute(), orientation->absolute());

    --(*expectations_count_ptr_);

    if (*expectations_count_ptr_ == 0) {
      base::MessageLoop::current()->PostTask(FROM_HERE,
                                             base::MessageLoop::QuitClosure());
    }
  }
};

// Class for checking expectations on test_data updates from the Provider.
class TestDataUpdateChecker : public UpdateChecker {
 public:
  explicit TestDataUpdateChecker(int* expectations_count_ptr)
      : UpdateChecker(DeviceData::kTypeTest, expectations_count_ptr) {
  }

  // From UpdateChecker.
  virtual void OnDeviceDataUpdate(const DeviceData* device_data,
      DeviceData::Type device_data_type) OVERRIDE {
    ASSERT_FALSE(expectations_queue_.empty());
    ASSERT_EQ(DeviceData::kTypeTest, device_data_type);

    scoped_refptr<const TestData> test_data(
        static_cast<const TestData*>(device_data));
    if (test_data.get() == NULL)
      test_data = new TestData();

    scoped_refptr<const TestData> expected(static_cast<const TestData*>(
        (expectations_queue_.front().get())));
    expectations_queue_.pop();

    EXPECT_EQ(expected->value(), test_data->value());

    --(*expectations_count_ptr_);

    if (*expectations_count_ptr_ == 0) {
      base::MessageLoop::current()->PostTask(FROM_HERE,
                                             base::MessageLoop::QuitClosure());
    }
  }
};

// Class for injecting test device data into the Provider.
class MockDeviceDataFactory
    : public base::RefCountedThreadSafe<MockDeviceDataFactory> {
 public:
  MockDeviceDataFactory()
      : is_failing_(false) {
  }

  static void SetCurInstance(MockDeviceDataFactory* instance) {
    if (instance) {
      EXPECT_FALSE(instance_);
    }
    else {
      EXPECT_TRUE(instance_);
    }
    instance_ = instance;
  }

  static DataFetcher* CreateDataFetcher() {
    EXPECT_TRUE(instance_);
    return new MockDataFetcher(instance_);
  }

  void SetDeviceData(const DeviceData* device_data, DeviceData::Type type) {
    base::AutoLock auto_lock(lock_);
    device_data_map_[type] = device_data;
  }

  void SetFailing(bool is_failing) {
    base::AutoLock auto_lock(lock_);
    is_failing_ = is_failing;
  }

 private:
  friend class base::RefCountedThreadSafe<MockDeviceDataFactory>;

  ~MockDeviceDataFactory() {
  }

  // Owned by ProviderImpl. Holds a reference back to MockDeviceDataFactory.
  class MockDataFetcher : public DataFetcher {
   public:
    explicit MockDataFetcher(MockDeviceDataFactory* device_data_factory)
        : device_data_factory_(device_data_factory) { }

    // From DataFetcher. Called by the Provider.
    virtual const DeviceData* GetDeviceData(
        DeviceData::Type device_data_type) OVERRIDE {
      base::AutoLock auto_lock(device_data_factory_->lock_);
      if (device_data_factory_->is_failing_)
        return NULL;
      return device_data_factory_->device_data_map_[device_data_type].get();
    }

   private:
    scoped_refptr<MockDeviceDataFactory> device_data_factory_;
  };

  static MockDeviceDataFactory* instance_;
  std::map<DeviceData::Type, scoped_refptr<const DeviceData> > device_data_map_;
  bool is_failing_;
  base::Lock lock_;
};

MockDeviceDataFactory* MockDeviceDataFactory::instance_;

class DeviceOrientationProviderTest : public testing::Test {
 public:
  DeviceOrientationProviderTest()
      : pending_expectations_(0) {
  }

  virtual void TearDown() {
    provider_ = NULL;

    // Make sure it is really gone.
    EXPECT_FALSE(Provider::GetInstanceForTests());

    // Clean up in any case, so as to not break subsequent test.
    Provider::SetInstanceForTests(NULL);
  }

  // Initialize the test fixture with a ProviderImpl that uses the
  // DataFetcherFactory factory.
  void Init(ProviderImpl::DataFetcherFactory factory) {
    provider_ = new ProviderImpl(factory);
    Provider::SetInstanceForTests(provider_.get());
  }

 protected:
  // Number of pending expectations.
  int pending_expectations_;

  // Provider instance under test.
  scoped_refptr<Provider> provider_;

  // Message loop for the test thread.
  base::MessageLoop message_loop_;
};

TEST_F(DeviceOrientationProviderTest, FailingTest) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  scoped_ptr<OrientationUpdateChecker> checker_a(
      new OrientationUpdateChecker(&pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> checker_b(
      new OrientationUpdateChecker(&pending_expectations_));

  checker_a->AddExpectation(new Orientation());
  provider_->AddObserver(checker_a.get());
  base::MessageLoop::current()->Run();

  checker_b->AddExpectation(new Orientation());
  provider_->AddObserver(checker_b.get());
  base::MessageLoop::current()->Run();

  MockDeviceDataFactory::SetCurInstance(NULL);
}

TEST_F(DeviceOrientationProviderTest, ProviderIsSingleton) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  scoped_refptr<Provider> provider_a(Provider::GetInstance());
  scoped_refptr<Provider> provider_b(Provider::GetInstance());

  EXPECT_EQ(provider_a.get(), provider_b.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

TEST_F(DeviceOrientationProviderTest, BasicPushTest) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);
  scoped_refptr<Orientation> test_orientation(new Orientation());
  test_orientation->set_alpha(1);
  test_orientation->set_beta(2);
  test_orientation->set_gamma(3);
  test_orientation->set_absolute(true);

  scoped_ptr<OrientationUpdateChecker> checker(
      new OrientationUpdateChecker(&pending_expectations_));
  checker->AddExpectation(test_orientation.get());
  device_data_factory->SetDeviceData(test_orientation.get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

// Tests multiple observers observing the same type of data.
TEST_F(DeviceOrientationProviderTest, MultipleObserversPushTest) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  scoped_refptr<Orientation> test_orientations[] = {new Orientation(),
      new Orientation(), new Orientation()};
  test_orientations[0]->set_alpha(1);
  test_orientations[0]->set_beta(2);
  test_orientations[0]->set_gamma(3);
  test_orientations[0]->set_absolute(true);

  test_orientations[1]->set_alpha(4);
  test_orientations[1]->set_beta(5);
  test_orientations[1]->set_gamma(6);
  test_orientations[1]->set_absolute(false);

  test_orientations[2]->set_alpha(7);
  test_orientations[2]->set_beta(8);
  test_orientations[2]->set_gamma(9);
  // can't provide absolute

  scoped_ptr<OrientationUpdateChecker> checker_a(
      new OrientationUpdateChecker(&pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> checker_b(
      new OrientationUpdateChecker(&pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> checker_c(
      new OrientationUpdateChecker(&pending_expectations_));

  checker_a->AddExpectation(test_orientations[0].get());
  device_data_factory->SetDeviceData(test_orientations[0].get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker_a.get());
  base::MessageLoop::current()->Run();

  checker_a->AddExpectation(test_orientations[1].get());
  checker_b->AddExpectation(test_orientations[0].get());
  checker_b->AddExpectation(test_orientations[1].get());
  device_data_factory->SetDeviceData(test_orientations[1].get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker_b.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker_a.get());
  checker_b->AddExpectation(test_orientations[2].get());
  checker_c->AddExpectation(test_orientations[1].get());
  checker_c->AddExpectation(test_orientations[2].get());
  device_data_factory->SetDeviceData(test_orientations[2].get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker_c.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker_b.get());
  provider_->RemoveObserver(checker_c.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

// Test for when the fetcher cannot provide the first type of data but can
// provide the second type.
TEST_F(DeviceOrientationProviderTest, FailingFirstDataTypeTest) {

  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  scoped_ptr<TestDataUpdateChecker> test_data_checker(
      new TestDataUpdateChecker(&pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> orientation_checker(
      new OrientationUpdateChecker(&pending_expectations_));

  scoped_refptr<Orientation> test_orientation(new Orientation());
  test_orientation->set_alpha(1);
  test_orientation->set_beta(2);
  test_orientation->set_gamma(3);
  test_orientation->set_absolute(true);

  test_data_checker->AddExpectation(new TestData());
  provider_->AddObserver(test_data_checker.get());
  base::MessageLoop::current()->Run();

  orientation_checker->AddExpectation(test_orientation.get());
  device_data_factory->SetDeviceData(test_orientation.get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(orientation_checker.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(test_data_checker.get());
  provider_->RemoveObserver(orientation_checker.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

#if defined(OS_LINUX) || defined(OS_WIN)
// Flakily DCHECKs on Linux. See crbug.com/104950.
// FLAKY on Win. See crbug.com/104950.
#define MAYBE_ObserverNotRemoved DISABLED_ObserverNotRemoved
#else
#define MAYBE_ObserverNotRemoved ObserverNotRemoved
#endif
TEST_F(DeviceOrientationProviderTest, MAYBE_ObserverNotRemoved) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);
  scoped_refptr<Orientation> test_orientation(new Orientation());
  test_orientation->set_alpha(1);
  test_orientation->set_beta(2);
  test_orientation->set_gamma(3);
  test_orientation->set_absolute(true);

  scoped_refptr<Orientation> test_orientation2(new Orientation());
  test_orientation2->set_alpha(4);
  test_orientation2->set_beta(5);
  test_orientation2->set_gamma(6);
  test_orientation2->set_absolute(false);

  scoped_ptr<OrientationUpdateChecker> checker(
      new OrientationUpdateChecker(&pending_expectations_));
  checker->AddExpectation(test_orientation.get());
  device_data_factory->SetDeviceData(test_orientation.get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker.get());
  base::MessageLoop::current()->Run();

  checker->AddExpectation(test_orientation2.get());
  device_data_factory->SetDeviceData(test_orientation2.get(),
                                     DeviceData::kTypeOrientation);
  base::MessageLoop::current()->Run();

  MockDeviceDataFactory::SetCurInstance(NULL);

  // Note that checker is not removed. This should not be a problem.
}

#if defined(OS_WIN)
// FLAKY on Win. See crbug.com/104950.
#define MAYBE_StartFailing DISABLED_StartFailing
#else
#define MAYBE_StartFailing StartFailing
#endif
TEST_F(DeviceOrientationProviderTest, MAYBE_StartFailing) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);
  scoped_refptr<Orientation> test_orientation(new Orientation());
  test_orientation->set_alpha(1);
  test_orientation->set_beta(2);
  test_orientation->set_gamma(3);
  test_orientation->set_absolute(true);

  scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
      &pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
      &pending_expectations_));

  device_data_factory->SetDeviceData(test_orientation.get(),
                                     DeviceData::kTypeOrientation);
  checker_a->AddExpectation(test_orientation.get());
  provider_->AddObserver(checker_a.get());
  base::MessageLoop::current()->Run();

  checker_a->AddExpectation(new Orientation());
  device_data_factory->SetFailing(true);
  base::MessageLoop::current()->Run();

  checker_b->AddExpectation(new Orientation());
  provider_->AddObserver(checker_b.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker_a.get());
  provider_->RemoveObserver(checker_b.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

TEST_F(DeviceOrientationProviderTest, StartStopStart) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  scoped_refptr<Orientation> test_orientation(new Orientation());
  test_orientation->set_alpha(1);
  test_orientation->set_beta(2);
  test_orientation->set_gamma(3);
  test_orientation->set_absolute(true);

  scoped_refptr<Orientation> test_orientation2(new Orientation());
  test_orientation2->set_alpha(4);
  test_orientation2->set_beta(5);
  test_orientation2->set_gamma(6);
  test_orientation2->set_absolute(false);

  scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
      &pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
      &pending_expectations_));

  checker_a->AddExpectation(test_orientation.get());
  device_data_factory->SetDeviceData(test_orientation.get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker_a.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker_a.get()); // This stops the Provider.

  checker_b->AddExpectation(test_orientation2.get());
  device_data_factory->SetDeviceData(test_orientation2.get(),
                                     DeviceData::kTypeOrientation);
  provider_->AddObserver(checker_b.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker_b.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

// Tests that Motion events always fire, even if the motion is unchanged.
TEST_F(DeviceOrientationProviderTest, FLAKY_MotionAlwaysFires) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  scoped_refptr<Motion> test_motion(new Motion());
  test_motion->set_acceleration_x(1);
  test_motion->set_acceleration_y(2);
  test_motion->set_acceleration_z(3);
  test_motion->set_acceleration_including_gravity_x(4);
  test_motion->set_acceleration_including_gravity_y(5);
  test_motion->set_acceleration_including_gravity_z(6);
  test_motion->set_rotation_rate_alpha(7);
  test_motion->set_rotation_rate_beta(8);
  test_motion->set_rotation_rate_gamma(9);
  test_motion->set_interval(10);

  scoped_ptr<MotionUpdateChecker> checker(new MotionUpdateChecker(
      &pending_expectations_));

  device_data_factory->SetDeviceData(test_motion.get(),
                                     DeviceData::kTypeMotion);
  checker->AddExpectation(test_motion.get());
  provider_->AddObserver(checker.get());
  base::MessageLoop::current()->Run();

  // The observer should receive the same motion again.
  device_data_factory->SetDeviceData(test_motion.get(),
                                     DeviceData::kTypeMotion);
  checker->AddExpectation(test_motion.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

// Tests that Orientation events only fire if the change is significant.
TEST_F(DeviceOrientationProviderTest, OrientationSignificantlyDifferent) {
  scoped_refptr<MockDeviceDataFactory> device_data_factory(
      new MockDeviceDataFactory());
  MockDeviceDataFactory::SetCurInstance(device_data_factory.get());
  Init(MockDeviceDataFactory::CreateDataFetcher);

  // Values that should be well below or above the implementation's
  // significane threshold.
  const double kInsignificantDifference = 1e-6;
  const double kSignificantDifference = 30;
  const double kAlpha = 4, kBeta = 5, kGamma = 6;

  scoped_refptr<Orientation> first_orientation(new Orientation());
  first_orientation->set_alpha(kAlpha);
  first_orientation->set_beta(kBeta);
  first_orientation->set_gamma(kGamma);
  first_orientation->set_absolute(true);

  scoped_refptr<Orientation> second_orientation(new Orientation());
  second_orientation->set_alpha(kAlpha + kInsignificantDifference);
  second_orientation->set_beta(kBeta + kInsignificantDifference);
  second_orientation->set_gamma(kGamma + kInsignificantDifference);
  second_orientation->set_absolute(false);

  scoped_refptr<Orientation> third_orientation(new Orientation());
  third_orientation->set_alpha(kAlpha + kSignificantDifference);
  third_orientation->set_beta(kBeta + kSignificantDifference);
  third_orientation->set_gamma(kGamma + kSignificantDifference);
  // can't provide absolute

  scoped_ptr<OrientationUpdateChecker> checker_a(new OrientationUpdateChecker(
      &pending_expectations_));
  scoped_ptr<OrientationUpdateChecker> checker_b(new OrientationUpdateChecker(
      &pending_expectations_));

  device_data_factory->SetDeviceData(first_orientation.get(),
                                     DeviceData::kTypeOrientation);
  checker_a->AddExpectation(first_orientation.get());
  provider_->AddObserver(checker_a.get());
  base::MessageLoop::current()->Run();

  // The observers should not see this insignificantly different orientation.
  device_data_factory->SetDeviceData(second_orientation.get(),
                                     DeviceData::kTypeOrientation);
  checker_b->AddExpectation(first_orientation.get());
  provider_->AddObserver(checker_b.get());
  base::MessageLoop::current()->Run();

  device_data_factory->SetDeviceData(third_orientation.get(),
                                     DeviceData::kTypeOrientation);
  checker_a->AddExpectation(third_orientation.get());
  checker_b->AddExpectation(third_orientation.get());
  base::MessageLoop::current()->Run();

  provider_->RemoveObserver(checker_a.get());
  provider_->RemoveObserver(checker_b.get());
  MockDeviceDataFactory::SetCurInstance(NULL);
}

}  // namespace

}  // namespace content