blob: 99d6ef28144aa7b6b079f37e2e7348c5d4c2e522 [file] [log] [blame]
Eric Shienbrood5de44ab2011-12-05 10:46:27 -05001// Copyright (c) 2012 The Chromium OS 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.
4
5#include "shill/cellular_capability_gsm.h"
6
Eric Shienbrood9a245532012-03-07 14:20:39 -05007#include <base/bind.h>
Eric Shienbrood5de44ab2011-12-05 10:46:27 -05008#include <chromeos/dbus/service_constants.h>
9#include <gtest/gtest.h>
10#include <mm/mm-modem.h>
11#include <mobile_provider.h>
12
13#include "shill/cellular.h"
14#include "shill/cellular_service.h"
15#include "shill/error.h"
16#include "shill/event_dispatcher.h"
17#include "shill/mock_adaptors.h"
18#include "shill/mock_modem_cdma_proxy.h"
Darin Petkovc37a9c42012-09-06 15:28:22 +020019#include "shill/mock_modem_gobi_proxy.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050020#include "shill/mock_modem_gsm_card_proxy.h"
21#include "shill/mock_modem_gsm_network_proxy.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070022#include "shill/mock_modem_info.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050023#include "shill/mock_modem_proxy.h"
24#include "shill/mock_modem_simple_proxy.h"
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040025#include "shill/mock_profile.h"
Eric Shienbrood9a245532012-03-07 14:20:39 -050026#include "shill/mock_rtnl_handler.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050027#include "shill/proxy_factory.h"
28
Eric Shienbrood9a245532012-03-07 14:20:39 -050029using base::Bind;
30using base::Unretained;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050031using std::string;
Darin Petkov9c1dcef2012-02-07 15:58:26 +010032using testing::InSequence;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050033using testing::NiceMock;
Eric Shienbrood9a245532012-03-07 14:20:39 -050034using testing::_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050035
36namespace shill {
37
Eric Shienbrood9a245532012-03-07 14:20:39 -050038MATCHER(IsSuccess, "") {
39 return arg.IsSuccess();
40}
41MATCHER(IsFailure, "") {
42 return arg.IsFailure();
43}
44
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050045class CellularCapabilityTest : public testing::Test {
46 public:
47 CellularCapabilityTest()
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070048 : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL),
Ben Chan3ecdf822012-08-06 12:29:23 -070049 create_gsm_card_proxy_from_factory_(false),
50 proxy_(new MockModemProxy()),
51 simple_proxy_(new MockModemSimpleProxy()),
52 cdma_proxy_(new MockModemCDMAProxy()),
53 gsm_card_proxy_(new MockModemGSMCardProxy()),
54 gsm_network_proxy_(new MockModemGSMNetworkProxy()),
Darin Petkovc37a9c42012-09-06 15:28:22 +020055 gobi_proxy_(new MockModemGobiProxy()),
Ben Chan3ecdf822012-08-06 12:29:23 -070056 proxy_factory_(this),
57 capability_(NULL),
58 device_adaptor_(NULL),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070059 cellular_(new Cellular(&modem_info_,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050060 "",
61 "",
62 0,
63 Cellular::kTypeGSM,
64 "",
65 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040066 "",
Thieu Lece4483e2013-01-23 15:12:03 -080067 &proxy_factory_)) {
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070068 modem_info_.metrics()->RegisterDevice(cellular_->interface_index(),
Thieu Lece4483e2013-01-23 15:12:03 -080069 Technology::kCellular);
70 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050071
72 virtual ~CellularCapabilityTest() {
73 cellular_->service_ = NULL;
74 capability_ = NULL;
75 device_adaptor_ = NULL;
76 }
77
78 virtual void SetUp() {
Eric Shienbrood9a245532012-03-07 14:20:39 -050079 static_cast<Device *>(cellular_)->rtnl_handler_ = &rtnl_handler_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040080
81 capability_ = dynamic_cast<CellularCapabilityClassic *>(
82 cellular_->capability_.get());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050083 device_adaptor_ =
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070084 dynamic_cast<DeviceMockAdaptor*>(cellular_->adaptor());
85 ASSERT_TRUE(device_adaptor_ != NULL);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050086 }
87
88 virtual void TearDown() {
89 capability_->proxy_factory_ = NULL;
90 }
91
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040092 void SetService() {
93 cellular_->service_ = new CellularService(
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070094 modem_info_.control_interface(), modem_info_.dispatcher(),
95 modem_info_.metrics(), modem_info_.manager(), cellular_);
96 }
97
98 void InitProviderDB() {
99 modem_info_.SetProviderDB(kTestMobileProviderDBPath);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400100 }
101
102 CellularCapabilityGSM *GetGsmCapability() {
103 return dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get());
104 }
105
Thieu Le3d275392012-07-20 15:32:58 -0700106 void ReleaseCapabilityProxies() {
107 capability_->ReleaseProxies();
108 }
109
Eric Shienbrood9a245532012-03-07 14:20:39 -0500110 void InvokeEnable(bool enable, Error *error,
111 const ResultCallback &callback, int timeout) {
112 callback.Run(Error());
113 }
114 void InvokeEnableFail(bool enable, Error *error,
115 const ResultCallback &callback, int timeout) {
116 callback.Run(Error(Error::kOperationFailed));
117 }
118 void InvokeDisconnect(Error *error, const ResultCallback &callback,
119 int timeout) {
120 callback.Run(Error());
121 }
Thieu Le923006b2012-04-05 16:32:58 -0700122 void InvokeDisconnectFail(Error *error, const ResultCallback &callback,
123 int timeout) {
124 callback.Run(Error(Error::kOperationFailed));
125 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500126 void InvokeGetModemStatus(Error *error,
127 const DBusPropertyMapCallback &callback,
128 int timeout) {
129 DBusPropertiesMap props;
130 props["carrier"].writer().append_string(kTestCarrier);
131 props["unknown-property"].writer().append_string("irrelevant-value");
132 callback.Run(props, Error());
133 }
134 void InvokeGetModemInfo(Error *error, const ModemInfoCallback &callback,
135 int timeout) {
136 ModemHardwareInfo info;
137 info._1 = kManufacturer;
138 info._2 = kModelID;
139 info._3 = kHWRev;
140 callback.Run(info, Error());
141 }
Darin Petkovc37a9c42012-09-06 15:28:22 +0200142 void InvokeSetCarrier(const string &carrier, Error *error,
143 const ResultCallback &callback, int timeout) {
144 callback.Run(Error());
145 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500146
147 MOCK_METHOD1(TestCallback, void(const Error &error));
148
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500149 protected:
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400150 static const char kTestMobileProviderDBPath[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500151 static const char kTestCarrier[];
Eric Shienbrood9a245532012-03-07 14:20:39 -0500152 static const char kManufacturer[];
153 static const char kModelID[];
154 static const char kHWRev[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500155
156 class TestProxyFactory : public ProxyFactory {
157 public:
158 explicit TestProxyFactory(CellularCapabilityTest *test) : test_(test) {}
159
160 virtual ModemProxyInterface *CreateModemProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500161 const string &/*path*/,
162 const string &/*service*/) {
163 return test_->proxy_.release();
164 }
165
166 virtual ModemSimpleProxyInterface *CreateModemSimpleProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500167 const string &/*path*/,
168 const string &/*service*/) {
169 return test_->simple_proxy_.release();
170 }
171
172 virtual ModemCDMAProxyInterface *CreateModemCDMAProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500173 const string &/*path*/,
174 const string &/*service*/) {
175 return test_->cdma_proxy_.release();
176 }
177
178 virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500179 const string &/*path*/,
180 const string &/*service*/) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700181 // TODO(benchan): This code conditionally returns a NULL pointer to avoid
182 // CellularCapabilityGSM::InitProperties (and thus
183 // CellularCapabilityGSM::GetIMSI) from being called during the
184 // construction. Remove this workaround after refactoring the tests.
185 return test_->create_gsm_card_proxy_from_factory_ ?
186 test_->gsm_card_proxy_.release() : NULL;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500187 }
188
189 virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500190 const string &/*path*/,
191 const string &/*service*/) {
192 return test_->gsm_network_proxy_.release();
193 }
194
Darin Petkovc37a9c42012-09-06 15:28:22 +0200195 virtual ModemGobiProxyInterface *CreateModemGobiProxy(
196 const string &/*path*/,
197 const string &/*service*/) {
198 return test_->gobi_proxy_.release();
199 }
200
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500201 private:
202 CellularCapabilityTest *test_;
203 };
204
205 void SetProxy() {
206 capability_->proxy_.reset(proxy_.release());
207 }
208
209 void SetSimpleProxy() {
210 capability_->simple_proxy_.reset(simple_proxy_.release());
211 }
212
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213 void SetGSMNetworkProxy() {
214 CellularCapabilityGSM *gsm_capability =
215 dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get());
216 gsm_capability->network_proxy_.reset(gsm_network_proxy_.release());
217 }
218
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500219 void SetCellularType(Cellular::Type type) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700220 cellular_->InitCapability(type);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400221 capability_ = dynamic_cast<CellularCapabilityClassic *>(
222 cellular_->capability_.get());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500223 }
224
Ben Chan3ecdf822012-08-06 12:29:23 -0700225 void AllowCreateGSMCardProxyFromFactory() {
226 create_gsm_card_proxy_from_factory_ = true;
227 }
228
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500229 EventDispatcher dispatcher_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700230 MockModemInfo modem_info_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500231 MockRTNLHandler rtnl_handler_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700232 bool create_gsm_card_proxy_from_factory_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500233 scoped_ptr<MockModemProxy> proxy_;
234 scoped_ptr<MockModemSimpleProxy> simple_proxy_;
235 scoped_ptr<MockModemCDMAProxy> cdma_proxy_;
236 scoped_ptr<MockModemGSMCardProxy> gsm_card_proxy_;
237 scoped_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_;
Darin Petkovc37a9c42012-09-06 15:28:22 +0200238 scoped_ptr<MockModemGobiProxy> gobi_proxy_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500239 TestProxyFactory proxy_factory_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400240 CellularCapabilityClassic *capability_; // Owned by |cellular_|.
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700241 DeviceMockAdaptor *device_adaptor_; // Owned by |cellular_|.
Ben Chan3ecdf822012-08-06 12:29:23 -0700242 CellularRefPtr cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500243};
244
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400245const char CellularCapabilityTest::kTestMobileProviderDBPath[] =
246 "provider_db_unittest.bfd";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500247const char CellularCapabilityTest::kTestCarrier[] = "The Cellular Carrier";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500248const char CellularCapabilityTest::kManufacturer[] = "Company";
249const char CellularCapabilityTest::kModelID[] = "Gobi 2000";
250const char CellularCapabilityTest::kHWRev[] = "A00B1234";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500251
252TEST_F(CellularCapabilityTest, GetModemStatus) {
253 SetCellularType(Cellular::kTypeCDMA);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500254 EXPECT_CALL(*simple_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500255 GetModemStatus(_, _, CellularCapability::kTimeoutDefault)).
256 WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemStatus));
257 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500258 SetSimpleProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500259 ResultCallback callback =
260 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
261 capability_->GetModemStatus(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500262 EXPECT_EQ(kTestCarrier, capability_->carrier_);
263 EXPECT_EQ(kTestCarrier, cellular_->home_provider_.GetName());
264}
265
266TEST_F(CellularCapabilityTest, GetModemInfo) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500267 EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
268 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemInfo));
269 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500270 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500271 ResultCallback callback =
272 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
273 capability_->GetModemInfo(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500274 EXPECT_EQ(kManufacturer, capability_->manufacturer_);
275 EXPECT_EQ(kModelID, capability_->model_id_);
276 EXPECT_EQ(kHWRev, capability_->hardware_revision_);
277}
278
279TEST_F(CellularCapabilityTest, EnableModemSucceed) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500280 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
281 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnable));
282 EXPECT_CALL(*this, TestCallback(IsSuccess()));
283 ResultCallback callback =
284 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500285 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500286 capability_->EnableModem(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500287}
288
289TEST_F(CellularCapabilityTest, EnableModemFail) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500290 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
291 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnableFail));
292 EXPECT_CALL(*this, TestCallback(IsFailure()));
293 ResultCallback callback =
294 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500295 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500296 capability_->EnableModem(callback);
297}
298
299TEST_F(CellularCapabilityTest, FinishEnable) {
300 EXPECT_CALL(*gsm_network_proxy_,
301 GetRegistrationInfo(NULL, _,
302 CellularCapability::kTimeoutDefault));
303 EXPECT_CALL(*gsm_network_proxy_,
304 GetSignalQuality(NULL, _, CellularCapability::kTimeoutDefault));
305 EXPECT_CALL(*this, TestCallback(IsSuccess()));
306 SetGSMNetworkProxy();
307 capability_->FinishEnable(
308 Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
309}
310
311TEST_F(CellularCapabilityTest, UnsupportedOperation) {
312 Error error;
313 EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(0);
314 capability_->CellularCapability::Scan(&error,
315 Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
316 EXPECT_TRUE(error.IsFailure());
317 EXPECT_EQ(Error::kNotSupported, error.type());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500318}
319
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100320TEST_F(CellularCapabilityTest, AllowRoaming) {
Jason Glasgow7b461df2012-05-01 16:38:45 -0400321 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
322 cellular_->SetAllowRoaming(false, NULL);
323 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100324
325 {
326 InSequence seq;
327 EXPECT_CALL(*device_adaptor_, EmitBoolChanged(
328 flimflam::kCellularAllowRoamingProperty, true));
329 EXPECT_CALL(*device_adaptor_, EmitBoolChanged(
330 flimflam::kCellularAllowRoamingProperty, false));
331 }
332
333 cellular_->state_ = Cellular::kStateConnected;
334 dynamic_cast<CellularCapabilityGSM *>(capability_)->registration_state_ =
335 MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING;
Jason Glasgow7b461df2012-05-01 16:38:45 -0400336 cellular_->SetAllowRoaming(true, NULL);
337 EXPECT_TRUE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100338 EXPECT_EQ(Cellular::kStateConnected, cellular_->state_);
339
Thieu Le049adb52012-11-12 17:14:51 -0800340 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Eric Shienbrood9a245532012-03-07 14:20:39 -0500341 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeDisconnect));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100342 SetProxy();
343 cellular_->state_ = Cellular::kStateConnected;
Jason Glasgow7b461df2012-05-01 16:38:45 -0400344 cellular_->SetAllowRoaming(false, NULL);
345 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100346 EXPECT_EQ(Cellular::kStateRegistered, cellular_->state_);
347}
348
Darin Petkovc37a9c42012-09-06 15:28:22 +0200349TEST_F(CellularCapabilityTest, SetCarrier) {
350 static const char kCarrier[] = "Generic UMTS";
351 EXPECT_CALL(
352 *gobi_proxy_,
353 SetCarrier(kCarrier, _, _,
354 CellularCapabilityClassic::kTimeoutSetCarrierMilliseconds))
355 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeSetCarrier));
356 EXPECT_CALL(*this, TestCallback(IsSuccess()));
357 Error error;
358 capability_->SetCarrier(kCarrier, &error,
359 Bind(&CellularCapabilityTest::TestCallback,
360 Unretained(this)));
361 EXPECT_TRUE(error.IsSuccess());
362}
363
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400364MATCHER_P(HasApn, apn, "") {
365 DBusPropertiesMap::const_iterator it = arg.find(flimflam::kApnProperty);
366 return it != arg.end() && apn == it->second.reader().get_string();
367}
368
369MATCHER(HasNoApn, "") {
370 return arg.find(flimflam::kApnProperty) == arg.end();
371}
372
373TEST_F(CellularCapabilityTest, TryApns) {
374 static const string kLastGoodApn("remembered.apn");
375 static const string kSuppliedApn("my.apn");
376 static const string kTmobileApn1("epc.tmobile.com");
377 static const string kTmobileApn2("wap.voicestream.com");
378 static const string kTmobileApn3("internet2.voicestream.com");
379 static const string kTmobileApn4("internet3.voicestream.com");
380
381 using testing::InSequence;
382 {
383 InSequence dummy;
384 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kLastGoodApn), _, _, _));
385 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kSuppliedApn), _, _, _));
386 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn1), _, _, _));
387 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn2), _, _, _));
388 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn3), _, _, _));
389 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn4), _, _, _));
390 EXPECT_CALL(*simple_proxy_, Connect(HasNoApn(), _, _, _));
391 }
392 CellularCapabilityGSM *gsm_capability = GetGsmCapability();
393 SetService();
394 gsm_capability->imsi_ = "310240123456789";
395 InitProviderDB();
396 gsm_capability->SetHomeProvider();
397 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700398 modem_info_.control_interface(), modem_info_.metrics(),
399 modem_info_.manager()));
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400400 cellular_->service()->set_profile(profile);
401
402 Error error;
403 Stringmap apn_info;
404 DBusPropertiesMap props;
405 apn_info[flimflam::kApnProperty] = kSuppliedApn;
406 cellular_->service()->SetApn(apn_info, &error);
407
408 apn_info.clear();
409 apn_info[flimflam::kApnProperty] = kLastGoodApn;
410 cellular_->service()->SetLastGoodApn(apn_info);
411
412 capability_->SetupConnectProperties(&props);
413 // We expect the list to contain the last good APN, plus
414 // the user-supplied APN, plus the 4 APNs from the mobile
415 // provider info database.
416 EXPECT_EQ(6, gsm_capability->apn_try_list_.size());
417 EXPECT_FALSE(props.find(flimflam::kApnProperty) == props.end());
418 EXPECT_EQ(kLastGoodApn, props[flimflam::kApnProperty].reader().get_string());
419
420 SetSimpleProxy();
421 capability_->Connect(props, &error, ResultCallback());
422 Error cerror(Error::kInvalidApn);
Nathan Williamsb54974f2012-04-19 11:16:30 -0400423 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400424 EXPECT_EQ(5, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400425 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400426 EXPECT_EQ(4, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400427 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400428 EXPECT_EQ(3, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400429 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400430 EXPECT_EQ(2, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400431 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400432 EXPECT_EQ(1, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400433 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400434 EXPECT_EQ(0, gsm_capability->apn_try_list_.size());
435}
436
Thieu Le923006b2012-04-05 16:32:58 -0700437TEST_F(CellularCapabilityTest, StopModemDisconnectSuccess) {
Thieu Le049adb52012-11-12 17:14:51 -0800438 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Thieu Le923006b2012-04-05 16:32:58 -0700439 .WillOnce(Invoke(this,
440 &CellularCapabilityTest::InvokeDisconnect));
Thieu Lec8d2d962012-05-15 14:31:18 -0700441 EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutEnable))
Thieu Le923006b2012-04-05 16:32:58 -0700442 .WillOnce(Invoke(this,
443 &CellularCapabilityTest::InvokeEnable));
444 EXPECT_CALL(*this, TestCallback(IsSuccess()));
445 SetProxy();
446
447 Error error;
448 capability_->StopModem(
449 &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
450 dispatcher_.DispatchPendingEvents();
451}
452
453TEST_F(CellularCapabilityTest, StopModemDisconnectFail) {
Thieu Le049adb52012-11-12 17:14:51 -0800454 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Thieu Le923006b2012-04-05 16:32:58 -0700455 .WillOnce(Invoke(this,
456 &CellularCapabilityTest::InvokeDisconnectFail));
Thieu Lec8d2d962012-05-15 14:31:18 -0700457 EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutEnable))
Thieu Le923006b2012-04-05 16:32:58 -0700458 .WillOnce(Invoke(this,
459 &CellularCapabilityTest::InvokeEnable));
460 EXPECT_CALL(*this, TestCallback(IsSuccess()));
461 SetProxy();
462
463 Error error;
464 capability_->StopModem(
465 &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
466 dispatcher_.DispatchPendingEvents();
467}
468
Thieu Le3d275392012-07-20 15:32:58 -0700469TEST_F(CellularCapabilityTest, DisconnectNoProxy) {
470 Error error;
471 ResultCallback disconnect_callback;
Thieu Le049adb52012-11-12 17:14:51 -0800472 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Thieu Le3d275392012-07-20 15:32:58 -0700473 .Times(0);
474 ReleaseCapabilityProxies();
475 capability_->Disconnect(&error, disconnect_callback);
476}
477
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500478} // namespace shill