[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / geolocation / win7_location_provider_unittest_win.cc
blob9c2e29992cb2c7c7346490b303f06c3dfc016af5
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/message_loop.h"
6 #include "content/browser/geolocation/win7_location_provider_win.h"
7 #include "content/browser/geolocation/win7_location_api_win.h"
8 #include "content/public/common/geoposition.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using testing::_;
13 using testing::AtLeast;
14 using testing::DoDefault;
15 using testing::Invoke;
16 using testing::Return;
18 namespace content {
20 class MockWin7LocationApi : public Win7LocationApi {
21 public:
22 static MockWin7LocationApi* CreateMock() {
23 return new MockWin7LocationApi();
26 // Used to signal when the destructor is called.
27 MOCK_METHOD0(Die, void());
28 // Win7LocationApi
29 MOCK_METHOD1(GetPosition, void(Geoposition*));
30 MOCK_METHOD1(SetHighAccuracy, bool(bool));
32 virtual ~MockWin7LocationApi() {
33 Die();
36 void GetPositionValid(Geoposition* position) {
37 position->latitude = 4.5;
38 position->longitude = -34.1;
39 position->accuracy = 0.5;
40 position->timestamp = base::Time::FromDoubleT(200);
41 position->error_code = Geoposition::ERROR_CODE_NONE;
43 void GetPositionInvalid(Geoposition* position) {
44 position->latitude = 4.5;
45 position->longitude = -340000.1;
46 position->accuracy = 0.5;
47 position->timestamp = base::Time::FromDoubleT(200);
48 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
51 private:
52 MockWin7LocationApi() {
53 ON_CALL(*this, GetPosition(_))
54 .WillByDefault(Invoke(this,
55 &MockWin7LocationApi::GetPositionValid));
56 ON_CALL(*this, SetHighAccuracy(true))
57 .WillByDefault(Return(true));
58 ON_CALL(*this, SetHighAccuracy(false))
59 .WillByDefault(Return(false));
63 class LocationProviderListenerLoopQuitter
64 : public LocationProviderBase::ListenerInterface {
65 public:
66 explicit LocationProviderListenerLoopQuitter(MessageLoop* message_loop)
67 : message_loop_to_quit_(message_loop) {
68 CHECK(message_loop_to_quit_ != NULL);
70 virtual void LocationUpdateAvailable(LocationProviderBase* provider) {
71 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_);
72 provider_ = provider;
73 message_loop_to_quit_->Quit();
76 MessageLoop* message_loop_to_quit_;
77 LocationProviderBase* provider_;
80 class GeolocationProviderWin7Tests : public testing::Test {
81 public:
82 GeolocationProviderWin7Tests(): location_listener_(&main_message_loop_) {
85 virtual void SetUp() {
86 api_ = MockWin7LocationApi::CreateMock();
87 provider_ = new Win7LocationProvider(api_);
88 provider_->RegisterListener(&location_listener_);
90 virtual void TearDown() {
91 provider_->UnregisterListener(&location_listener_);
92 provider_->StopProvider();
93 delete provider_;
94 main_message_loop_.RunUntilIdle();
97 protected:
98 MockWin7LocationApi* api_;
99 LocationProviderListenerLoopQuitter location_listener_;
100 MessageLoop main_message_loop_;
101 Win7LocationProvider* provider_;
104 TEST_F(GeolocationProviderWin7Tests, StartStop) {
105 EXPECT_CALL(*api_, SetHighAccuracy(true))
106 .WillOnce(Return(true));
107 EXPECT_TRUE(provider_->StartProvider(true));
108 provider_->StopProvider();
109 EXPECT_CALL(*api_, SetHighAccuracy(false))
110 .WillOnce(Return(true));
111 EXPECT_TRUE(provider_->StartProvider(false));
114 TEST_F(GeolocationProviderWin7Tests, GetValidPosition) {
115 EXPECT_CALL(*api_, GetPosition(_))
116 .Times(AtLeast(1));
117 EXPECT_CALL(*api_, SetHighAccuracy(true))
118 .WillOnce(Return(true));
119 EXPECT_TRUE(provider_->StartProvider(true));
120 main_message_loop_.Run();
121 Geoposition position;
122 provider_->GetPosition(&position);
123 EXPECT_TRUE(position.Validate());
126 TEST_F(GeolocationProviderWin7Tests, GetInvalidPosition) {
127 EXPECT_CALL(*api_, GetPosition(_))
128 .Times(AtLeast(1))
129 .WillRepeatedly(Invoke(api_,
130 &MockWin7LocationApi::GetPositionInvalid));
131 EXPECT_CALL(*api_, SetHighAccuracy(true))
132 .WillOnce(Return(true));
133 EXPECT_TRUE(provider_->StartProvider(true));
134 main_message_loop_.Run();
135 Geoposition position;
136 provider_->GetPosition(&position);
137 EXPECT_FALSE(position.Validate());
140 } // namespace content