blob: 64f3714a46fdbb9f8440d978f2c444e1fb22c055 [file] [log] [blame]
Darin Petkovac635a82012-01-10 16:51:58 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovcb547732011-11-09 13:55:26 +01002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/cellular_capability_cdma.h"
6
Darin Petkov20c13ec2011-11-09 15:07:15 +01007#include <chromeos/dbus/service_constants.h>
Darin Petkovcb547732011-11-09 13:55:26 +01008#include <gtest/gtest.h>
Darin Petkov20c13ec2011-11-09 15:07:15 +01009#include <mm/mm-modem.h>
Darin Petkovcb547732011-11-09 13:55:26 +010010
11#include "shill/cellular.h"
Darin Petkov3e509242011-11-10 14:46:44 +010012#include "shill/cellular_service.h"
Darin Petkovcb547732011-11-09 13:55:26 +010013#include "shill/error.h"
14#include "shill/event_dispatcher.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050015#include "shill/mock_adaptors.h"
Eric Shienbrood04e63582012-01-19 13:41:12 -050016#include "shill/mock_glib.h"
17#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080018#include "shill/mock_metrics.h"
Darin Petkovcb547732011-11-09 13:55:26 +010019#include "shill/mock_modem_cdma_proxy.h"
20#include "shill/nice_mock_control.h"
21
Darin Petkov184c54e2011-11-15 12:44:39 +010022using testing::_;
Darin Petkovcb547732011-11-09 13:55:26 +010023using testing::Return;
Darin Petkov184c54e2011-11-15 12:44:39 +010024using testing::SetArgumentPointee;
Darin Petkovcb547732011-11-09 13:55:26 +010025
26namespace shill {
27
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050028class TestAsyncCallHandler : public AsyncCallHandler {
29public:
30 explicit TestAsyncCallHandler(ReturnerInterface *returner)
31 : AsyncCallHandler(returner) { }
32 virtual ~TestAsyncCallHandler() { }
33
34 bool CompleteOperationWithError(const Error &error) {
35 error_.Populate(error.type(), error.message());
36 return AsyncCallHandler::CompleteOperationWithError(error);
37 }
38
39 static const Error &error() { return error_; }
40
41private:
42 // static because AsyncCallHandlers are deleted before callbacks return
43 static Error error_;
44
45 DISALLOW_COPY_AND_ASSIGN(TestAsyncCallHandler);
46};
47
48Error TestAsyncCallHandler::error_;
49
Darin Petkovcb547732011-11-09 13:55:26 +010050class CellularCapabilityCDMATest : public testing::Test {
51 public:
52 CellularCapabilityCDMATest()
Eric Shienbrood04e63582012-01-19 13:41:12 -050053 : manager_(&control_, &dispatcher_, &metrics_, &glib_),
54 cellular_(new Cellular(&control_,
Darin Petkovcb547732011-11-09 13:55:26 +010055 &dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -080056 &metrics_,
Eric Shienbrood04e63582012-01-19 13:41:12 -050057 &manager_,
Darin Petkovcb547732011-11-09 13:55:26 +010058 "",
59 "",
60 0,
61 Cellular::kTypeCDMA,
62 "",
63 "",
64 NULL)),
65 proxy_(new MockModemCDMAProxy()),
Darin Petkov721ac932011-11-16 15:43:09 +010066 capability_(NULL) {}
Darin Petkovcb547732011-11-09 13:55:26 +010067
Darin Petkov3e509242011-11-10 14:46:44 +010068 virtual ~CellularCapabilityCDMATest() {
69 cellular_->service_ = NULL;
Darin Petkov721ac932011-11-16 15:43:09 +010070 capability_ = NULL;
Darin Petkov3e509242011-11-10 14:46:44 +010071 }
72
Darin Petkov721ac932011-11-16 15:43:09 +010073 virtual void SetUp() {
74 capability_ =
75 dynamic_cast<CellularCapabilityCDMA *>(cellular_->capability_.get());
Darin Petkov184c54e2011-11-15 12:44:39 +010076 }
77
Darin Petkovcb547732011-11-09 13:55:26 +010078 protected:
79 static const char kMEID[];
Darin Petkova3d3be52011-11-14 21:34:16 +010080 static const char kTestCarrier[];
Darin Petkovcb547732011-11-09 13:55:26 +010081
Darin Petkov20c13ec2011-11-09 15:07:15 +010082 void SetRegistrationStateEVDO(uint32 state) {
Darin Petkov721ac932011-11-16 15:43:09 +010083 capability_->registration_state_evdo_ = state;
Darin Petkov20c13ec2011-11-09 15:07:15 +010084 }
85
86 void SetRegistrationState1x(uint32 state) {
Darin Petkov721ac932011-11-16 15:43:09 +010087 capability_->registration_state_1x_ = state;
Darin Petkov20c13ec2011-11-09 15:07:15 +010088 }
89
Darin Petkov3e509242011-11-10 14:46:44 +010090 void SetProxy() {
Darin Petkov721ac932011-11-16 15:43:09 +010091 capability_->proxy_.reset(proxy_.release());
Darin Petkov3e509242011-11-10 14:46:44 +010092 }
93
94 void SetService() {
95 cellular_->service_ = new CellularService(
Eric Shienbrood04e63582012-01-19 13:41:12 -050096 &control_, &dispatcher_, &metrics_, &manager_, cellular_);
Darin Petkov3e509242011-11-10 14:46:44 +010097 }
98
Darin Petkova3d3be52011-11-14 21:34:16 +010099 void SetDeviceState(Cellular::State state) {
100 cellular_->state_ = state;
101 }
102
Darin Petkovcb547732011-11-09 13:55:26 +0100103 NiceMockControl control_;
104 EventDispatcher dispatcher_;
Eric Shienbrood04e63582012-01-19 13:41:12 -0500105 MockGLib glib_;
106 MockManager manager_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 MockMetrics metrics_;
Darin Petkovcb547732011-11-09 13:55:26 +0100108 CellularRefPtr cellular_;
109 scoped_ptr<MockModemCDMAProxy> proxy_;
Darin Petkov721ac932011-11-16 15:43:09 +0100110 CellularCapabilityCDMA *capability_; // Owned by |cellular_|.
Darin Petkovcb547732011-11-09 13:55:26 +0100111};
112
113const char CellularCapabilityCDMATest::kMEID[] = "D1234567EF8901";
Darin Petkova3d3be52011-11-14 21:34:16 +0100114const char CellularCapabilityCDMATest::kTestCarrier[] = "The Cellular Carrier";
115
Darin Petkov721ac932011-11-16 15:43:09 +0100116TEST_F(CellularCapabilityCDMATest, PropertyStore) {
117 EXPECT_TRUE(cellular_->store().Contains(flimflam::kPRLVersionProperty));
118}
119
Darin Petkova3d3be52011-11-14 21:34:16 +0100120TEST_F(CellularCapabilityCDMATest, Activate) {
Darin Petkova3d3be52011-11-14 21:34:16 +0100121 SetDeviceState(Cellular::kStateEnabled);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500122 EXPECT_CALL(*proxy_,
123 Activate(kTestCarrier, _, CellularCapability::kTimeoutActivate));
124 MockReturner returner;
125 scoped_ptr<TestAsyncCallHandler> handler(new TestAsyncCallHandler(&returner));
Darin Petkova3d3be52011-11-14 21:34:16 +0100126 SetProxy();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500127 capability_->Activate(kTestCarrier, handler.get());
Darin Petkova3d3be52011-11-14 21:34:16 +0100128 SetService();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500129 capability_->OnActivateCallback(MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR,
130 Error(), NULL);
131 EXPECT_TRUE(TestAsyncCallHandler::error().IsSuccess());
Darin Petkova3d3be52011-11-14 21:34:16 +0100132 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING,
Darin Petkov721ac932011-11-16 15:43:09 +0100133 capability_->activation_state());
Darin Petkova3d3be52011-11-14 21:34:16 +0100134 EXPECT_EQ(flimflam::kActivationStateActivating,
135 cellular_->service()->activation_state());
136 EXPECT_EQ("", cellular_->service()->error());
137}
138
139TEST_F(CellularCapabilityCDMATest, ActivateError) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500140 MockReturner returner;
141 TestAsyncCallHandler *handler = new TestAsyncCallHandler(&returner);
142 capability_->Activate(kTestCarrier, handler);
143 EXPECT_EQ(Error::kInvalidArguments, TestAsyncCallHandler::error().type());
Darin Petkova3d3be52011-11-14 21:34:16 +0100144
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500145 handler = new TestAsyncCallHandler(&returner);
Darin Petkova3d3be52011-11-14 21:34:16 +0100146 SetDeviceState(Cellular::kStateRegistered);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500147 EXPECT_CALL(*proxy_, Activate(kTestCarrier, handler,
148 CellularCapability::kTimeoutActivate));
Darin Petkova3d3be52011-11-14 21:34:16 +0100149 SetProxy();
150 SetService();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500151 capability_->Activate(kTestCarrier, handler);
152 capability_->OnActivateCallback(MM_MODEM_CDMA_ACTIVATION_ERROR_NO_SIGNAL,
153 Error(), handler);
154 EXPECT_TRUE(TestAsyncCallHandler::error().IsFailure());
Darin Petkova3d3be52011-11-14 21:34:16 +0100155 EXPECT_EQ(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED,
Darin Petkov721ac932011-11-16 15:43:09 +0100156 capability_->activation_state());
Darin Petkova3d3be52011-11-14 21:34:16 +0100157 EXPECT_EQ(flimflam::kActivationStateNotActivated,
158 cellular_->service()->activation_state());
159 EXPECT_EQ(flimflam::kErrorActivationFailed,
160 cellular_->service()->error());
161}
Darin Petkovcb547732011-11-09 13:55:26 +0100162
Darin Petkovae0c64e2011-11-15 15:50:27 +0100163TEST_F(CellularCapabilityCDMATest, GetActivationStateString) {
164 EXPECT_EQ(flimflam::kActivationStateActivated,
165 CellularCapabilityCDMA::GetActivationStateString(
166 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED));
167 EXPECT_EQ(flimflam::kActivationStateActivating,
168 CellularCapabilityCDMA::GetActivationStateString(
169 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING));
170 EXPECT_EQ(flimflam::kActivationStateNotActivated,
171 CellularCapabilityCDMA::GetActivationStateString(
172 MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED));
173 EXPECT_EQ(flimflam::kActivationStatePartiallyActivated,
174 CellularCapabilityCDMA::GetActivationStateString(
175 MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED));
176 EXPECT_EQ(flimflam::kActivationStateUnknown,
177 CellularCapabilityCDMA::GetActivationStateString(123));
178}
179
180TEST_F(CellularCapabilityCDMATest, GetActivationErrorString) {
181 EXPECT_EQ(flimflam::kErrorNeedEvdo,
182 CellularCapabilityCDMA::GetActivationErrorString(
183 MM_MODEM_CDMA_ACTIVATION_ERROR_WRONG_RADIO_INTERFACE));
184 EXPECT_EQ(flimflam::kErrorNeedHomeNetwork,
185 CellularCapabilityCDMA::GetActivationErrorString(
186 MM_MODEM_CDMA_ACTIVATION_ERROR_ROAMING));
187 EXPECT_EQ(flimflam::kErrorOtaspFailed,
188 CellularCapabilityCDMA::GetActivationErrorString(
189 MM_MODEM_CDMA_ACTIVATION_ERROR_COULD_NOT_CONNECT));
190 EXPECT_EQ(flimflam::kErrorOtaspFailed,
191 CellularCapabilityCDMA::GetActivationErrorString(
192 MM_MODEM_CDMA_ACTIVATION_ERROR_SECURITY_AUTHENTICATION_FAILED));
193 EXPECT_EQ(flimflam::kErrorOtaspFailed,
194 CellularCapabilityCDMA::GetActivationErrorString(
195 MM_MODEM_CDMA_ACTIVATION_ERROR_PROVISIONING_FAILED));
196 EXPECT_EQ("",
197 CellularCapabilityCDMA::GetActivationErrorString(
198 MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR));
199 EXPECT_EQ(flimflam::kErrorActivationFailed,
200 CellularCapabilityCDMA::GetActivationErrorString(
201 MM_MODEM_CDMA_ACTIVATION_ERROR_NO_SIGNAL));
202 EXPECT_EQ(flimflam::kErrorActivationFailed,
203 CellularCapabilityCDMA::GetActivationErrorString(1234));
204}
205
Darin Petkovb72cf402011-11-22 14:51:39 +0100206TEST_F(CellularCapabilityCDMATest, IsRegisteredEVDO) {
207 EXPECT_FALSE(capability_->IsRegistered());
208 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN);
209 EXPECT_FALSE(capability_->IsRegistered());
210 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED);
211 EXPECT_TRUE(capability_->IsRegistered());
212 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_HOME);
213 EXPECT_TRUE(capability_->IsRegistered());
214 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING);
215 EXPECT_TRUE(capability_->IsRegistered());
216}
217
218TEST_F(CellularCapabilityCDMATest, IsRegistered1x) {
219 EXPECT_FALSE(capability_->IsRegistered());
220 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN);
221 EXPECT_FALSE(capability_->IsRegistered());
222 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED);
223 EXPECT_TRUE(capability_->IsRegistered());
224 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_HOME);
225 EXPECT_TRUE(capability_->IsRegistered());
226 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING);
227 EXPECT_TRUE(capability_->IsRegistered());
228}
229
Darin Petkov20c13ec2011-11-09 15:07:15 +0100230TEST_F(CellularCapabilityCDMATest, GetNetworkTechnologyString) {
Darin Petkov721ac932011-11-16 15:43:09 +0100231 EXPECT_EQ("", capability_->GetNetworkTechnologyString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100232 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_HOME);
233 EXPECT_EQ(flimflam::kNetworkTechnologyEvdo,
Darin Petkov721ac932011-11-16 15:43:09 +0100234 capability_->GetNetworkTechnologyString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100235 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN);
236 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_HOME);
237 EXPECT_EQ(flimflam::kNetworkTechnology1Xrtt,
Darin Petkov721ac932011-11-16 15:43:09 +0100238 capability_->GetNetworkTechnologyString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100239}
240
241TEST_F(CellularCapabilityCDMATest, GetRoamingStateString) {
242 EXPECT_EQ(flimflam::kRoamingStateUnknown,
Darin Petkov721ac932011-11-16 15:43:09 +0100243 capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100244 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED);
245 EXPECT_EQ(flimflam::kRoamingStateUnknown,
Darin Petkov721ac932011-11-16 15:43:09 +0100246 capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100247 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_HOME);
Darin Petkov721ac932011-11-16 15:43:09 +0100248 EXPECT_EQ(flimflam::kRoamingStateHome, capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100249 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING);
250 EXPECT_EQ(flimflam::kRoamingStateRoaming,
Darin Petkov721ac932011-11-16 15:43:09 +0100251 capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100252 SetRegistrationStateEVDO(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN);
253 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED);
254 EXPECT_EQ(flimflam::kRoamingStateUnknown,
Darin Petkov721ac932011-11-16 15:43:09 +0100255 capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100256 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_HOME);
Darin Petkov721ac932011-11-16 15:43:09 +0100257 EXPECT_EQ(flimflam::kRoamingStateHome, capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100258 SetRegistrationState1x(MM_MODEM_CDMA_REGISTRATION_STATE_ROAMING);
259 EXPECT_EQ(flimflam::kRoamingStateRoaming,
Darin Petkov721ac932011-11-16 15:43:09 +0100260 capability_->GetRoamingStateString());
Darin Petkov20c13ec2011-11-09 15:07:15 +0100261}
262
Darin Petkov3e509242011-11-10 14:46:44 +0100263TEST_F(CellularCapabilityCDMATest, GetSignalQuality) {
264 const int kStrength = 90;
265 EXPECT_CALL(*proxy_, GetSignalQuality()).WillOnce(Return(kStrength));
266 SetProxy();
267 SetService();
268 EXPECT_EQ(0, cellular_->service()->strength());
Darin Petkov721ac932011-11-16 15:43:09 +0100269 capability_->GetSignalQuality();
Darin Petkov3e509242011-11-10 14:46:44 +0100270 EXPECT_EQ(kStrength, cellular_->service()->strength());
271}
272
Darin Petkov184c54e2011-11-15 12:44:39 +0100273TEST_F(CellularCapabilityCDMATest, GetRegistrationState) {
274 EXPECT_FALSE(cellular_->service().get());
275 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
Darin Petkov721ac932011-11-16 15:43:09 +0100276 capability_->registration_state_1x());
Darin Petkov184c54e2011-11-15 12:44:39 +0100277 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN,
Darin Petkov721ac932011-11-16 15:43:09 +0100278 capability_->registration_state_evdo());
Darin Petkov184c54e2011-11-15 12:44:39 +0100279 EXPECT_CALL(*proxy_, GetRegistrationState(_, _))
280 .WillOnce(DoAll(
281 SetArgumentPointee<0>(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED),
282 SetArgumentPointee<1>(MM_MODEM_CDMA_REGISTRATION_STATE_HOME)));
283 EXPECT_CALL(*proxy_, GetSignalQuality()).WillOnce(Return(90));
284 SetProxy();
Eric Shienbrood04e63582012-01-19 13:41:12 -0500285 EXPECT_CALL(manager_, RegisterService(_));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500286 capability_->GetRegistrationState(NULL);
Darin Petkov184c54e2011-11-15 12:44:39 +0100287 dispatcher_.DispatchPendingEvents();
288 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_REGISTERED,
Darin Petkov721ac932011-11-16 15:43:09 +0100289 capability_->registration_state_1x());
Darin Petkov184c54e2011-11-15 12:44:39 +0100290 EXPECT_EQ(MM_MODEM_CDMA_REGISTRATION_STATE_HOME,
Darin Petkov721ac932011-11-16 15:43:09 +0100291 capability_->registration_state_evdo());
Eric Shienbrood04e63582012-01-19 13:41:12 -0500292 EXPECT_TRUE(cellular_->service().get());
Darin Petkov184c54e2011-11-15 12:44:39 +0100293}
294
Darin Petkovac635a82012-01-10 16:51:58 +0100295TEST_F(CellularCapabilityCDMATest, CreateFriendlyServiceName) {
296 CellularCapabilityCDMA::friendly_service_name_id_ = 0;
297 EXPECT_EQ("CDMANetwork0", capability_->CreateFriendlyServiceName());
298 EXPECT_EQ("CDMANetwork1", capability_->CreateFriendlyServiceName());
299 static const char kTestCarrier[] = "A Carrier";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500300 capability_->carrier_ = kTestCarrier;
Darin Petkovac635a82012-01-10 16:51:58 +0100301 EXPECT_EQ(kTestCarrier, capability_->CreateFriendlyServiceName());
302}
303
Darin Petkovcb547732011-11-09 13:55:26 +0100304} // namespace shill