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
|
// Copyright (c) 2010 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 "base/message_loop.h"
#include "content/browser/geolocation/win7_location_provider_win.h"
#include "content/browser/geolocation/win7_location_api_win.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
struct Geoposition;
using testing::_;
using testing::AtLeast;
using testing::DoDefault;
using testing::Invoke;
using testing::Return;
namespace {
class MockWin7LocationApi : public Win7LocationApi {
public:
static MockWin7LocationApi* CreateMock() {
return new MockWin7LocationApi();
}
// Used to signal when the destructor is called.
MOCK_METHOD0(Die, void());
// Win7LocationApi
MOCK_METHOD1(GetPosition, void(Geoposition*));
MOCK_METHOD1(SetHighAccuracy, bool(bool));
virtual ~MockWin7LocationApi() {
Die();
}
void GetPositionValid(Geoposition* position) {
position->latitude = 4.5;
position->longitude = -34.1;
position->accuracy = 0.5;
position->timestamp = base::Time::FromDoubleT(200);
position->error_code = Geoposition::ERROR_CODE_NONE;
}
void GetPositionInvalid(Geoposition* position) {
position->latitude = 4.5;
position->longitude = -340000.1;
position->accuracy = 0.5;
position->timestamp = base::Time::FromDoubleT(200);
position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
}
private:
MockWin7LocationApi() {
ON_CALL(*this, GetPosition(_))
.WillByDefault(Invoke(this,
&MockWin7LocationApi::GetPositionValid));
ON_CALL(*this, SetHighAccuracy(true))
.WillByDefault(Return(true));
ON_CALL(*this, SetHighAccuracy(false))
.WillByDefault(Return(false));
}
};
class LocationProviderListenerLoopQuitter
: public LocationProviderBase::ListenerInterface {
public:
explicit LocationProviderListenerLoopQuitter(MessageLoop* message_loop)
: message_loop_to_quit_(message_loop) {
CHECK(message_loop_to_quit_ != NULL);
}
virtual void LocationUpdateAvailable(LocationProviderBase* provider) {
EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_);
provider_ = provider;
message_loop_to_quit_->Quit();
}
MessageLoop* message_loop_to_quit_;
LocationProviderBase* provider_;
};
class GeolocationProviderWin7Tests : public testing::Test {
public:
GeolocationProviderWin7Tests(): location_listener_(&main_message_loop_) {
}
virtual void SetUp() {
api_ = MockWin7LocationApi::CreateMock();
provider_ = new Win7LocationProvider(api_);
provider_->RegisterListener(&location_listener_);
}
virtual void TearDown() {
provider_->UnregisterListener(&location_listener_);
provider_->StopProvider();
delete provider_;
main_message_loop_.RunAllPending();
}
protected:
MockWin7LocationApi* api_;
LocationProviderListenerLoopQuitter location_listener_;
MessageLoop main_message_loop_;
Win7LocationProvider* provider_;
};
TEST_F(GeolocationProviderWin7Tests, StartStop) {
EXPECT_CALL(*api_, SetHighAccuracy(true))
.WillOnce(Return(true));
EXPECT_TRUE(provider_->StartProvider(true));
provider_->StopProvider();
EXPECT_CALL(*api_, SetHighAccuracy(false))
.WillOnce(Return(true));
EXPECT_TRUE(provider_->StartProvider(false));
}
TEST_F(GeolocationProviderWin7Tests, GetValidPosition) {
EXPECT_CALL(*api_, GetPosition(_))
.Times(AtLeast(1));
EXPECT_CALL(*api_, SetHighAccuracy(true))
.WillOnce(Return(true));
EXPECT_TRUE(provider_->StartProvider(true));
main_message_loop_.Run();
Geoposition position;
provider_->GetPosition(&position);
EXPECT_TRUE(position.IsValidFix());
}
TEST_F(GeolocationProviderWin7Tests, GetInvalidPosition) {
EXPECT_CALL(*api_, GetPosition(_))
.Times(AtLeast(1))
.WillRepeatedly(Invoke(api_,
&MockWin7LocationApi::GetPositionInvalid));
EXPECT_CALL(*api_, SetHighAccuracy(true))
.WillOnce(Return(true));
EXPECT_TRUE(provider_->StartProvider(true));
main_message_loop_.Run();
Geoposition position;
provider_->GetPosition(&position);
EXPECT_FALSE(position.IsValidFix());
}
} // namespace
|