blob: 8678e18f5e665ef94ffb911c8801b0364a396a13 [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"
Eric Shienbrood9a245532012-03-07 14:20:39 -050018#include "shill/mock_glib.h"
19#include "shill/mock_manager.h"
20#include "shill/mock_metrics.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050021#include "shill/mock_modem_cdma_proxy.h"
Darin Petkovc37a9c42012-09-06 15:28:22 +020022#include "shill/mock_modem_gobi_proxy.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050023#include "shill/mock_modem_gsm_card_proxy.h"
24#include "shill/mock_modem_gsm_network_proxy.h"
25#include "shill/mock_modem_proxy.h"
26#include "shill/mock_modem_simple_proxy.h"
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040027#include "shill/mock_profile.h"
Eric Shienbrood9a245532012-03-07 14:20:39 -050028#include "shill/mock_rtnl_handler.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050029#include "shill/nice_mock_control.h"
30#include "shill/proxy_factory.h"
31
Eric Shienbrood9a245532012-03-07 14:20:39 -050032using base::Bind;
33using base::Unretained;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050034using std::string;
Darin Petkov9c1dcef2012-02-07 15:58:26 +010035using testing::InSequence;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050036using testing::NiceMock;
Eric Shienbrood9a245532012-03-07 14:20:39 -050037using testing::_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050038
39namespace shill {
40
Eric Shienbrood9a245532012-03-07 14:20:39 -050041MATCHER(IsSuccess, "") {
42 return arg.IsSuccess();
43}
44MATCHER(IsFailure, "") {
45 return arg.IsFailure();
46}
47
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050048class CellularCapabilityTest : public testing::Test {
49 public:
50 CellularCapabilityTest()
Eric Shienbrood9a245532012-03-07 14:20:39 -050051 : manager_(&control_, &dispatcher_, &metrics_, &glib_),
Ben Chan3ecdf822012-08-06 12:29:23 -070052 create_gsm_card_proxy_from_factory_(false),
53 proxy_(new MockModemProxy()),
54 simple_proxy_(new MockModemSimpleProxy()),
55 cdma_proxy_(new MockModemCDMAProxy()),
56 gsm_card_proxy_(new MockModemGSMCardProxy()),
57 gsm_network_proxy_(new MockModemGSMNetworkProxy()),
Darin Petkovc37a9c42012-09-06 15:28:22 +020058 gobi_proxy_(new MockModemGobiProxy()),
Ben Chan3ecdf822012-08-06 12:29:23 -070059 proxy_factory_(this),
60 capability_(NULL),
61 device_adaptor_(NULL),
62 provider_db_(NULL),
Eric Shienbrood9a245532012-03-07 14:20:39 -050063 cellular_(new Cellular(&control_,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050064 &dispatcher_,
65 NULL,
Eric Shienbrood9a245532012-03-07 14:20:39 -050066 &manager_,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050067 "",
68 "",
69 0,
70 Cellular::kTypeGSM,
71 "",
72 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040073 "",
Ben Chan3ecdf822012-08-06 12:29:23 -070074 NULL,
75 &proxy_factory_)) {}
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050076
77 virtual ~CellularCapabilityTest() {
78 cellular_->service_ = NULL;
79 capability_ = NULL;
80 device_adaptor_ = NULL;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040081 mobile_provider_close_db(provider_db_);
82 provider_db_ = NULL;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050083 }
84
85 virtual void SetUp() {
Eric Shienbrood9a245532012-03-07 14:20:39 -050086 static_cast<Device *>(cellular_)->rtnl_handler_ = &rtnl_handler_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040087
88 capability_ = dynamic_cast<CellularCapabilityClassic *>(
89 cellular_->capability_.get());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050090 device_adaptor_ =
91 dynamic_cast<NiceMock<DeviceMockAdaptor> *>(cellular_->adaptor());
92 }
93
94 virtual void TearDown() {
95 capability_->proxy_factory_ = NULL;
96 }
97
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040098 void InitProviderDB() {
99 provider_db_ = mobile_provider_open_db(kTestMobileProviderDBPath);
100 ASSERT_TRUE(provider_db_);
101 cellular_->provider_db_ = provider_db_;
102 }
103
104 void SetService() {
105 cellular_->service_ = new CellularService(
106 &control_, &dispatcher_, &metrics_, NULL, cellular_);
107 }
108
109 CellularCapabilityGSM *GetGsmCapability() {
110 return dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get());
111 }
112
Thieu Le3d275392012-07-20 15:32:58 -0700113 void ReleaseCapabilityProxies() {
114 capability_->ReleaseProxies();
115 }
116
Eric Shienbrood9a245532012-03-07 14:20:39 -0500117 void InvokeEnable(bool enable, Error *error,
118 const ResultCallback &callback, int timeout) {
119 callback.Run(Error());
120 }
121 void InvokeEnableFail(bool enable, Error *error,
122 const ResultCallback &callback, int timeout) {
123 callback.Run(Error(Error::kOperationFailed));
124 }
125 void InvokeDisconnect(Error *error, const ResultCallback &callback,
126 int timeout) {
127 callback.Run(Error());
128 }
Thieu Le923006b2012-04-05 16:32:58 -0700129 void InvokeDisconnectFail(Error *error, const ResultCallback &callback,
130 int timeout) {
131 callback.Run(Error(Error::kOperationFailed));
132 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500133 void InvokeGetModemStatus(Error *error,
134 const DBusPropertyMapCallback &callback,
135 int timeout) {
136 DBusPropertiesMap props;
137 props["carrier"].writer().append_string(kTestCarrier);
138 props["unknown-property"].writer().append_string("irrelevant-value");
139 callback.Run(props, Error());
140 }
141 void InvokeGetModemInfo(Error *error, const ModemInfoCallback &callback,
142 int timeout) {
143 ModemHardwareInfo info;
144 info._1 = kManufacturer;
145 info._2 = kModelID;
146 info._3 = kHWRev;
147 callback.Run(info, Error());
148 }
Darin Petkovc37a9c42012-09-06 15:28:22 +0200149 void InvokeSetCarrier(const string &carrier, Error *error,
150 const ResultCallback &callback, int timeout) {
151 callback.Run(Error());
152 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500153
154 MOCK_METHOD1(TestCallback, void(const Error &error));
155
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500156 protected:
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400157 static const char kTestMobileProviderDBPath[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500158 static const char kTestCarrier[];
Eric Shienbrood9a245532012-03-07 14:20:39 -0500159 static const char kManufacturer[];
160 static const char kModelID[];
161 static const char kHWRev[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500162
163 class TestProxyFactory : public ProxyFactory {
164 public:
165 explicit TestProxyFactory(CellularCapabilityTest *test) : test_(test) {}
166
167 virtual ModemProxyInterface *CreateModemProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500168 const string &/*path*/,
169 const string &/*service*/) {
170 return test_->proxy_.release();
171 }
172
173 virtual ModemSimpleProxyInterface *CreateModemSimpleProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500174 const string &/*path*/,
175 const string &/*service*/) {
176 return test_->simple_proxy_.release();
177 }
178
179 virtual ModemCDMAProxyInterface *CreateModemCDMAProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500180 const string &/*path*/,
181 const string &/*service*/) {
182 return test_->cdma_proxy_.release();
183 }
184
185 virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500186 const string &/*path*/,
187 const string &/*service*/) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700188 // TODO(benchan): This code conditionally returns a NULL pointer to avoid
189 // CellularCapabilityGSM::InitProperties (and thus
190 // CellularCapabilityGSM::GetIMSI) from being called during the
191 // construction. Remove this workaround after refactoring the tests.
192 return test_->create_gsm_card_proxy_from_factory_ ?
193 test_->gsm_card_proxy_.release() : NULL;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500194 }
195
196 virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500197 const string &/*path*/,
198 const string &/*service*/) {
199 return test_->gsm_network_proxy_.release();
200 }
201
Darin Petkovc37a9c42012-09-06 15:28:22 +0200202 virtual ModemGobiProxyInterface *CreateModemGobiProxy(
203 const string &/*path*/,
204 const string &/*service*/) {
205 return test_->gobi_proxy_.release();
206 }
207
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500208 private:
209 CellularCapabilityTest *test_;
210 };
211
212 void SetProxy() {
213 capability_->proxy_.reset(proxy_.release());
214 }
215
216 void SetSimpleProxy() {
217 capability_->simple_proxy_.reset(simple_proxy_.release());
218 }
219
Eric Shienbrood9a245532012-03-07 14:20:39 -0500220 void SetGSMNetworkProxy() {
221 CellularCapabilityGSM *gsm_capability =
222 dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get());
223 gsm_capability->network_proxy_.reset(gsm_network_proxy_.release());
224 }
225
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500226 void SetCellularType(Cellular::Type type) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700227 cellular_->InitCapability(type);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400228 capability_ = dynamic_cast<CellularCapabilityClassic *>(
229 cellular_->capability_.get());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500230 }
231
Ben Chan3ecdf822012-08-06 12:29:23 -0700232 void AllowCreateGSMCardProxyFromFactory() {
233 create_gsm_card_proxy_from_factory_ = true;
234 }
235
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500236 NiceMockControl control_;
237 EventDispatcher dispatcher_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500238 MockMetrics metrics_;
239 MockGLib glib_;
240 MockManager manager_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500241 MockRTNLHandler rtnl_handler_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700242 bool create_gsm_card_proxy_from_factory_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500243 scoped_ptr<MockModemProxy> proxy_;
244 scoped_ptr<MockModemSimpleProxy> simple_proxy_;
245 scoped_ptr<MockModemCDMAProxy> cdma_proxy_;
246 scoped_ptr<MockModemGSMCardProxy> gsm_card_proxy_;
247 scoped_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_;
Darin Petkovc37a9c42012-09-06 15:28:22 +0200248 scoped_ptr<MockModemGobiProxy> gobi_proxy_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500249 TestProxyFactory proxy_factory_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400250 CellularCapabilityClassic *capability_; // Owned by |cellular_|.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500251 NiceMock<DeviceMockAdaptor> *device_adaptor_; // Owned by |cellular_|.
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400252 mobile_provider_db *provider_db_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700253 CellularRefPtr cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500254};
255
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400256const char CellularCapabilityTest::kTestMobileProviderDBPath[] =
257 "provider_db_unittest.bfd";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500258const char CellularCapabilityTest::kTestCarrier[] = "The Cellular Carrier";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500259const char CellularCapabilityTest::kManufacturer[] = "Company";
260const char CellularCapabilityTest::kModelID[] = "Gobi 2000";
261const char CellularCapabilityTest::kHWRev[] = "A00B1234";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500262
263TEST_F(CellularCapabilityTest, GetModemStatus) {
264 SetCellularType(Cellular::kTypeCDMA);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500265 EXPECT_CALL(*simple_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500266 GetModemStatus(_, _, CellularCapability::kTimeoutDefault)).
267 WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemStatus));
268 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500269 SetSimpleProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500270 ResultCallback callback =
271 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
272 capability_->GetModemStatus(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500273 EXPECT_EQ(kTestCarrier, capability_->carrier_);
274 EXPECT_EQ(kTestCarrier, cellular_->home_provider_.GetName());
275}
276
277TEST_F(CellularCapabilityTest, GetModemInfo) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500278 EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
279 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemInfo));
280 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500281 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500282 ResultCallback callback =
283 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
284 capability_->GetModemInfo(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500285 EXPECT_EQ(kManufacturer, capability_->manufacturer_);
286 EXPECT_EQ(kModelID, capability_->model_id_);
287 EXPECT_EQ(kHWRev, capability_->hardware_revision_);
288}
289
290TEST_F(CellularCapabilityTest, EnableModemSucceed) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500291 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
292 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnable));
293 EXPECT_CALL(*this, TestCallback(IsSuccess()));
294 ResultCallback callback =
295 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500296 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500297 capability_->EnableModem(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500298}
299
300TEST_F(CellularCapabilityTest, EnableModemFail) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500301 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
302 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnableFail));
303 EXPECT_CALL(*this, TestCallback(IsFailure()));
304 ResultCallback callback =
305 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500306 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500307 capability_->EnableModem(callback);
308}
309
310TEST_F(CellularCapabilityTest, FinishEnable) {
311 EXPECT_CALL(*gsm_network_proxy_,
312 GetRegistrationInfo(NULL, _,
313 CellularCapability::kTimeoutDefault));
314 EXPECT_CALL(*gsm_network_proxy_,
315 GetSignalQuality(NULL, _, CellularCapability::kTimeoutDefault));
316 EXPECT_CALL(*this, TestCallback(IsSuccess()));
317 SetGSMNetworkProxy();
318 capability_->FinishEnable(
319 Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
320}
321
322TEST_F(CellularCapabilityTest, UnsupportedOperation) {
323 Error error;
324 EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(0);
325 capability_->CellularCapability::Scan(&error,
326 Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
327 EXPECT_TRUE(error.IsFailure());
328 EXPECT_EQ(Error::kNotSupported, error.type());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500329}
330
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100331TEST_F(CellularCapabilityTest, AllowRoaming) {
Jason Glasgow7b461df2012-05-01 16:38:45 -0400332 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
333 cellular_->SetAllowRoaming(false, NULL);
334 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100335
336 {
337 InSequence seq;
338 EXPECT_CALL(*device_adaptor_, EmitBoolChanged(
339 flimflam::kCellularAllowRoamingProperty, true));
340 EXPECT_CALL(*device_adaptor_, EmitBoolChanged(
341 flimflam::kCellularAllowRoamingProperty, false));
342 }
343
344 cellular_->state_ = Cellular::kStateConnected;
345 dynamic_cast<CellularCapabilityGSM *>(capability_)->registration_state_ =
346 MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING;
Jason Glasgow7b461df2012-05-01 16:38:45 -0400347 cellular_->SetAllowRoaming(true, NULL);
348 EXPECT_TRUE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100349 EXPECT_EQ(Cellular::kStateConnected, cellular_->state_);
350
Eric Shienbrood9a245532012-03-07 14:20:39 -0500351 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault))
352 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeDisconnect));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100353 SetProxy();
354 cellular_->state_ = Cellular::kStateConnected;
Jason Glasgow7b461df2012-05-01 16:38:45 -0400355 cellular_->SetAllowRoaming(false, NULL);
356 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100357 EXPECT_EQ(Cellular::kStateRegistered, cellular_->state_);
358}
359
Darin Petkovc37a9c42012-09-06 15:28:22 +0200360TEST_F(CellularCapabilityTest, SetCarrier) {
361 static const char kCarrier[] = "Generic UMTS";
362 EXPECT_CALL(
363 *gobi_proxy_,
364 SetCarrier(kCarrier, _, _,
365 CellularCapabilityClassic::kTimeoutSetCarrierMilliseconds))
366 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeSetCarrier));
367 EXPECT_CALL(*this, TestCallback(IsSuccess()));
368 Error error;
369 capability_->SetCarrier(kCarrier, &error,
370 Bind(&CellularCapabilityTest::TestCallback,
371 Unretained(this)));
372 EXPECT_TRUE(error.IsSuccess());
373}
374
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400375MATCHER_P(HasApn, apn, "") {
376 DBusPropertiesMap::const_iterator it = arg.find(flimflam::kApnProperty);
377 return it != arg.end() && apn == it->second.reader().get_string();
378}
379
380MATCHER(HasNoApn, "") {
381 return arg.find(flimflam::kApnProperty) == arg.end();
382}
383
384TEST_F(CellularCapabilityTest, TryApns) {
385 static const string kLastGoodApn("remembered.apn");
386 static const string kSuppliedApn("my.apn");
387 static const string kTmobileApn1("epc.tmobile.com");
388 static const string kTmobileApn2("wap.voicestream.com");
389 static const string kTmobileApn3("internet2.voicestream.com");
390 static const string kTmobileApn4("internet3.voicestream.com");
391
392 using testing::InSequence;
393 {
394 InSequence dummy;
395 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kLastGoodApn), _, _, _));
396 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kSuppliedApn), _, _, _));
397 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn1), _, _, _));
398 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn2), _, _, _));
399 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn3), _, _, _));
400 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn4), _, _, _));
401 EXPECT_CALL(*simple_proxy_, Connect(HasNoApn(), _, _, _));
402 }
403 CellularCapabilityGSM *gsm_capability = GetGsmCapability();
404 SetService();
405 gsm_capability->imsi_ = "310240123456789";
406 InitProviderDB();
407 gsm_capability->SetHomeProvider();
408 ProfileRefPtr profile(new NiceMock<MockProfile>(
409 &control_, reinterpret_cast<Manager *>(NULL)));
410 cellular_->service()->set_profile(profile);
411
412 Error error;
413 Stringmap apn_info;
414 DBusPropertiesMap props;
415 apn_info[flimflam::kApnProperty] = kSuppliedApn;
416 cellular_->service()->SetApn(apn_info, &error);
417
418 apn_info.clear();
419 apn_info[flimflam::kApnProperty] = kLastGoodApn;
420 cellular_->service()->SetLastGoodApn(apn_info);
421
422 capability_->SetupConnectProperties(&props);
423 // We expect the list to contain the last good APN, plus
424 // the user-supplied APN, plus the 4 APNs from the mobile
425 // provider info database.
426 EXPECT_EQ(6, gsm_capability->apn_try_list_.size());
427 EXPECT_FALSE(props.find(flimflam::kApnProperty) == props.end());
428 EXPECT_EQ(kLastGoodApn, props[flimflam::kApnProperty].reader().get_string());
429
430 SetSimpleProxy();
431 capability_->Connect(props, &error, ResultCallback());
432 Error cerror(Error::kInvalidApn);
Nathan Williamsb54974f2012-04-19 11:16:30 -0400433 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400434 EXPECT_EQ(5, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400435 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400436 EXPECT_EQ(4, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400437 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400438 EXPECT_EQ(3, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400439 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400440 EXPECT_EQ(2, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400441 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400442 EXPECT_EQ(1, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400443 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400444 EXPECT_EQ(0, gsm_capability->apn_try_list_.size());
445}
446
Thieu Le923006b2012-04-05 16:32:58 -0700447TEST_F(CellularCapabilityTest, StopModemDisconnectSuccess) {
448 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault))
449 .WillOnce(Invoke(this,
450 &CellularCapabilityTest::InvokeDisconnect));
Thieu Lec8d2d962012-05-15 14:31:18 -0700451 EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutEnable))
Thieu Le923006b2012-04-05 16:32:58 -0700452 .WillOnce(Invoke(this,
453 &CellularCapabilityTest::InvokeEnable));
454 EXPECT_CALL(*this, TestCallback(IsSuccess()));
455 SetProxy();
456
457 Error error;
458 capability_->StopModem(
459 &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
460 dispatcher_.DispatchPendingEvents();
461}
462
463TEST_F(CellularCapabilityTest, StopModemDisconnectFail) {
464 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault))
465 .WillOnce(Invoke(this,
466 &CellularCapabilityTest::InvokeDisconnectFail));
Thieu Lec8d2d962012-05-15 14:31:18 -0700467 EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutEnable))
Thieu Le923006b2012-04-05 16:32:58 -0700468 .WillOnce(Invoke(this,
469 &CellularCapabilityTest::InvokeEnable));
470 EXPECT_CALL(*this, TestCallback(IsSuccess()));
471 SetProxy();
472
473 Error error;
474 capability_->StopModem(
475 &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
476 dispatcher_.DispatchPendingEvents();
477}
478
Thieu Le3d275392012-07-20 15:32:58 -0700479TEST_F(CellularCapabilityTest, DisconnectNoProxy) {
480 Error error;
481 ResultCallback disconnect_callback;
482 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault))
483 .Times(0);
484 ReleaseCapabilityProxies();
485 capability_->Disconnect(&error, disconnect_callback);
486}
487
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500488} // namespace shill