blob: f444dcb8d5902f922217c51715991e9e2239e0cc [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,
Ben Chan62028b22012-11-05 11:20:02 -080075 NULL,
Ben Chan3ecdf822012-08-06 12:29:23 -070076 &proxy_factory_)) {}
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050077
78 virtual ~CellularCapabilityTest() {
79 cellular_->service_ = NULL;
80 capability_ = NULL;
81 device_adaptor_ = NULL;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040082 mobile_provider_close_db(provider_db_);
83 provider_db_ = NULL;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050084 }
85
86 virtual void SetUp() {
Eric Shienbrood9a245532012-03-07 14:20:39 -050087 static_cast<Device *>(cellular_)->rtnl_handler_ = &rtnl_handler_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040088
89 capability_ = dynamic_cast<CellularCapabilityClassic *>(
90 cellular_->capability_.get());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050091 device_adaptor_ =
92 dynamic_cast<NiceMock<DeviceMockAdaptor> *>(cellular_->adaptor());
93 }
94
95 virtual void TearDown() {
96 capability_->proxy_factory_ = NULL;
97 }
98
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040099 void InitProviderDB() {
100 provider_db_ = mobile_provider_open_db(kTestMobileProviderDBPath);
101 ASSERT_TRUE(provider_db_);
102 cellular_->provider_db_ = provider_db_;
103 }
104
105 void SetService() {
106 cellular_->service_ = new CellularService(
107 &control_, &dispatcher_, &metrics_, NULL, cellular_);
108 }
109
110 CellularCapabilityGSM *GetGsmCapability() {
111 return dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get());
112 }
113
Thieu Le3d275392012-07-20 15:32:58 -0700114 void ReleaseCapabilityProxies() {
115 capability_->ReleaseProxies();
116 }
117
Eric Shienbrood9a245532012-03-07 14:20:39 -0500118 void InvokeEnable(bool enable, Error *error,
119 const ResultCallback &callback, int timeout) {
120 callback.Run(Error());
121 }
122 void InvokeEnableFail(bool enable, Error *error,
123 const ResultCallback &callback, int timeout) {
124 callback.Run(Error(Error::kOperationFailed));
125 }
126 void InvokeDisconnect(Error *error, const ResultCallback &callback,
127 int timeout) {
128 callback.Run(Error());
129 }
Thieu Le923006b2012-04-05 16:32:58 -0700130 void InvokeDisconnectFail(Error *error, const ResultCallback &callback,
131 int timeout) {
132 callback.Run(Error(Error::kOperationFailed));
133 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500134 void InvokeGetModemStatus(Error *error,
135 const DBusPropertyMapCallback &callback,
136 int timeout) {
137 DBusPropertiesMap props;
138 props["carrier"].writer().append_string(kTestCarrier);
139 props["unknown-property"].writer().append_string("irrelevant-value");
140 callback.Run(props, Error());
141 }
142 void InvokeGetModemInfo(Error *error, const ModemInfoCallback &callback,
143 int timeout) {
144 ModemHardwareInfo info;
145 info._1 = kManufacturer;
146 info._2 = kModelID;
147 info._3 = kHWRev;
148 callback.Run(info, Error());
149 }
Darin Petkovc37a9c42012-09-06 15:28:22 +0200150 void InvokeSetCarrier(const string &carrier, Error *error,
151 const ResultCallback &callback, int timeout) {
152 callback.Run(Error());
153 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500154
155 MOCK_METHOD1(TestCallback, void(const Error &error));
156
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500157 protected:
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400158 static const char kTestMobileProviderDBPath[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500159 static const char kTestCarrier[];
Eric Shienbrood9a245532012-03-07 14:20:39 -0500160 static const char kManufacturer[];
161 static const char kModelID[];
162 static const char kHWRev[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500163
164 class TestProxyFactory : public ProxyFactory {
165 public:
166 explicit TestProxyFactory(CellularCapabilityTest *test) : test_(test) {}
167
168 virtual ModemProxyInterface *CreateModemProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500169 const string &/*path*/,
170 const string &/*service*/) {
171 return test_->proxy_.release();
172 }
173
174 virtual ModemSimpleProxyInterface *CreateModemSimpleProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500175 const string &/*path*/,
176 const string &/*service*/) {
177 return test_->simple_proxy_.release();
178 }
179
180 virtual ModemCDMAProxyInterface *CreateModemCDMAProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500181 const string &/*path*/,
182 const string &/*service*/) {
183 return test_->cdma_proxy_.release();
184 }
185
186 virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500187 const string &/*path*/,
188 const string &/*service*/) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700189 // TODO(benchan): This code conditionally returns a NULL pointer to avoid
190 // CellularCapabilityGSM::InitProperties (and thus
191 // CellularCapabilityGSM::GetIMSI) from being called during the
192 // construction. Remove this workaround after refactoring the tests.
193 return test_->create_gsm_card_proxy_from_factory_ ?
194 test_->gsm_card_proxy_.release() : NULL;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500195 }
196
197 virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500198 const string &/*path*/,
199 const string &/*service*/) {
200 return test_->gsm_network_proxy_.release();
201 }
202
Darin Petkovc37a9c42012-09-06 15:28:22 +0200203 virtual ModemGobiProxyInterface *CreateModemGobiProxy(
204 const string &/*path*/,
205 const string &/*service*/) {
206 return test_->gobi_proxy_.release();
207 }
208
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500209 private:
210 CellularCapabilityTest *test_;
211 };
212
213 void SetProxy() {
214 capability_->proxy_.reset(proxy_.release());
215 }
216
217 void SetSimpleProxy() {
218 capability_->simple_proxy_.reset(simple_proxy_.release());
219 }
220
Eric Shienbrood9a245532012-03-07 14:20:39 -0500221 void SetGSMNetworkProxy() {
222 CellularCapabilityGSM *gsm_capability =
223 dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get());
224 gsm_capability->network_proxy_.reset(gsm_network_proxy_.release());
225 }
226
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500227 void SetCellularType(Cellular::Type type) {
Ben Chan3ecdf822012-08-06 12:29:23 -0700228 cellular_->InitCapability(type);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400229 capability_ = dynamic_cast<CellularCapabilityClassic *>(
230 cellular_->capability_.get());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500231 }
232
Ben Chan3ecdf822012-08-06 12:29:23 -0700233 void AllowCreateGSMCardProxyFromFactory() {
234 create_gsm_card_proxy_from_factory_ = true;
235 }
236
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500237 NiceMockControl control_;
238 EventDispatcher dispatcher_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500239 MockMetrics metrics_;
240 MockGLib glib_;
241 MockManager manager_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500242 MockRTNLHandler rtnl_handler_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700243 bool create_gsm_card_proxy_from_factory_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500244 scoped_ptr<MockModemProxy> proxy_;
245 scoped_ptr<MockModemSimpleProxy> simple_proxy_;
246 scoped_ptr<MockModemCDMAProxy> cdma_proxy_;
247 scoped_ptr<MockModemGSMCardProxy> gsm_card_proxy_;
248 scoped_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_;
Darin Petkovc37a9c42012-09-06 15:28:22 +0200249 scoped_ptr<MockModemGobiProxy> gobi_proxy_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500250 TestProxyFactory proxy_factory_;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400251 CellularCapabilityClassic *capability_; // Owned by |cellular_|.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500252 NiceMock<DeviceMockAdaptor> *device_adaptor_; // Owned by |cellular_|.
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400253 mobile_provider_db *provider_db_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700254 CellularRefPtr cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500255};
256
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400257const char CellularCapabilityTest::kTestMobileProviderDBPath[] =
258 "provider_db_unittest.bfd";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500259const char CellularCapabilityTest::kTestCarrier[] = "The Cellular Carrier";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500260const char CellularCapabilityTest::kManufacturer[] = "Company";
261const char CellularCapabilityTest::kModelID[] = "Gobi 2000";
262const char CellularCapabilityTest::kHWRev[] = "A00B1234";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500263
264TEST_F(CellularCapabilityTest, GetModemStatus) {
265 SetCellularType(Cellular::kTypeCDMA);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500266 EXPECT_CALL(*simple_proxy_,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500267 GetModemStatus(_, _, CellularCapability::kTimeoutDefault)).
268 WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemStatus));
269 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500270 SetSimpleProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500271 ResultCallback callback =
272 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
273 capability_->GetModemStatus(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500274 EXPECT_EQ(kTestCarrier, capability_->carrier_);
275 EXPECT_EQ(kTestCarrier, cellular_->home_provider_.GetName());
276}
277
278TEST_F(CellularCapabilityTest, GetModemInfo) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500279 EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault))
280 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemInfo));
281 EXPECT_CALL(*this, TestCallback(IsSuccess()));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500282 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500283 ResultCallback callback =
284 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
285 capability_->GetModemInfo(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500286 EXPECT_EQ(kManufacturer, capability_->manufacturer_);
287 EXPECT_EQ(kModelID, capability_->model_id_);
288 EXPECT_EQ(kHWRev, capability_->hardware_revision_);
289}
290
291TEST_F(CellularCapabilityTest, EnableModemSucceed) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500292 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
293 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnable));
294 EXPECT_CALL(*this, TestCallback(IsSuccess()));
295 ResultCallback callback =
296 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500297 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500298 capability_->EnableModem(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500299}
300
301TEST_F(CellularCapabilityTest, EnableModemFail) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500302 EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable))
303 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnableFail));
304 EXPECT_CALL(*this, TestCallback(IsFailure()));
305 ResultCallback callback =
306 Bind(&CellularCapabilityTest::TestCallback, Unretained(this));
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500307 SetProxy();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500308 capability_->EnableModem(callback);
309}
310
311TEST_F(CellularCapabilityTest, FinishEnable) {
312 EXPECT_CALL(*gsm_network_proxy_,
313 GetRegistrationInfo(NULL, _,
314 CellularCapability::kTimeoutDefault));
315 EXPECT_CALL(*gsm_network_proxy_,
316 GetSignalQuality(NULL, _, CellularCapability::kTimeoutDefault));
317 EXPECT_CALL(*this, TestCallback(IsSuccess()));
318 SetGSMNetworkProxy();
319 capability_->FinishEnable(
320 Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
321}
322
323TEST_F(CellularCapabilityTest, UnsupportedOperation) {
324 Error error;
325 EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(0);
326 capability_->CellularCapability::Scan(&error,
327 Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
328 EXPECT_TRUE(error.IsFailure());
329 EXPECT_EQ(Error::kNotSupported, error.type());
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500330}
331
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100332TEST_F(CellularCapabilityTest, AllowRoaming) {
Jason Glasgow7b461df2012-05-01 16:38:45 -0400333 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
334 cellular_->SetAllowRoaming(false, NULL);
335 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100336
337 {
338 InSequence seq;
339 EXPECT_CALL(*device_adaptor_, EmitBoolChanged(
340 flimflam::kCellularAllowRoamingProperty, true));
341 EXPECT_CALL(*device_adaptor_, EmitBoolChanged(
342 flimflam::kCellularAllowRoamingProperty, false));
343 }
344
345 cellular_->state_ = Cellular::kStateConnected;
346 dynamic_cast<CellularCapabilityGSM *>(capability_)->registration_state_ =
347 MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING;
Jason Glasgow7b461df2012-05-01 16:38:45 -0400348 cellular_->SetAllowRoaming(true, NULL);
349 EXPECT_TRUE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100350 EXPECT_EQ(Cellular::kStateConnected, cellular_->state_);
351
Thieu Le049adb52012-11-12 17:14:51 -0800352 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Eric Shienbrood9a245532012-03-07 14:20:39 -0500353 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeDisconnect));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100354 SetProxy();
355 cellular_->state_ = Cellular::kStateConnected;
Jason Glasgow7b461df2012-05-01 16:38:45 -0400356 cellular_->SetAllowRoaming(false, NULL);
357 EXPECT_FALSE(cellular_->GetAllowRoaming(NULL));
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100358 EXPECT_EQ(Cellular::kStateRegistered, cellular_->state_);
359}
360
Darin Petkovc37a9c42012-09-06 15:28:22 +0200361TEST_F(CellularCapabilityTest, SetCarrier) {
362 static const char kCarrier[] = "Generic UMTS";
363 EXPECT_CALL(
364 *gobi_proxy_,
365 SetCarrier(kCarrier, _, _,
366 CellularCapabilityClassic::kTimeoutSetCarrierMilliseconds))
367 .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeSetCarrier));
368 EXPECT_CALL(*this, TestCallback(IsSuccess()));
369 Error error;
370 capability_->SetCarrier(kCarrier, &error,
371 Bind(&CellularCapabilityTest::TestCallback,
372 Unretained(this)));
373 EXPECT_TRUE(error.IsSuccess());
374}
375
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400376MATCHER_P(HasApn, apn, "") {
377 DBusPropertiesMap::const_iterator it = arg.find(flimflam::kApnProperty);
378 return it != arg.end() && apn == it->second.reader().get_string();
379}
380
381MATCHER(HasNoApn, "") {
382 return arg.find(flimflam::kApnProperty) == arg.end();
383}
384
385TEST_F(CellularCapabilityTest, TryApns) {
386 static const string kLastGoodApn("remembered.apn");
387 static const string kSuppliedApn("my.apn");
388 static const string kTmobileApn1("epc.tmobile.com");
389 static const string kTmobileApn2("wap.voicestream.com");
390 static const string kTmobileApn3("internet2.voicestream.com");
391 static const string kTmobileApn4("internet3.voicestream.com");
392
393 using testing::InSequence;
394 {
395 InSequence dummy;
396 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kLastGoodApn), _, _, _));
397 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kSuppliedApn), _, _, _));
398 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn1), _, _, _));
399 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn2), _, _, _));
400 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn3), _, _, _));
401 EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn4), _, _, _));
402 EXPECT_CALL(*simple_proxy_, Connect(HasNoApn(), _, _, _));
403 }
404 CellularCapabilityGSM *gsm_capability = GetGsmCapability();
405 SetService();
406 gsm_capability->imsi_ = "310240123456789";
407 InitProviderDB();
408 gsm_capability->SetHomeProvider();
409 ProfileRefPtr profile(new NiceMock<MockProfile>(
410 &control_, reinterpret_cast<Manager *>(NULL)));
411 cellular_->service()->set_profile(profile);
412
413 Error error;
414 Stringmap apn_info;
415 DBusPropertiesMap props;
416 apn_info[flimflam::kApnProperty] = kSuppliedApn;
417 cellular_->service()->SetApn(apn_info, &error);
418
419 apn_info.clear();
420 apn_info[flimflam::kApnProperty] = kLastGoodApn;
421 cellular_->service()->SetLastGoodApn(apn_info);
422
423 capability_->SetupConnectProperties(&props);
424 // We expect the list to contain the last good APN, plus
425 // the user-supplied APN, plus the 4 APNs from the mobile
426 // provider info database.
427 EXPECT_EQ(6, gsm_capability->apn_try_list_.size());
428 EXPECT_FALSE(props.find(flimflam::kApnProperty) == props.end());
429 EXPECT_EQ(kLastGoodApn, props[flimflam::kApnProperty].reader().get_string());
430
431 SetSimpleProxy();
432 capability_->Connect(props, &error, ResultCallback());
433 Error cerror(Error::kInvalidApn);
Nathan Williamsb54974f2012-04-19 11:16:30 -0400434 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400435 EXPECT_EQ(5, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400436 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400437 EXPECT_EQ(4, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400438 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400439 EXPECT_EQ(3, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400440 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400441 EXPECT_EQ(2, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400442 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400443 EXPECT_EQ(1, gsm_capability->apn_try_list_.size());
Nathan Williamsb54974f2012-04-19 11:16:30 -0400444 gsm_capability->OnConnectReply(ResultCallback(), cerror);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400445 EXPECT_EQ(0, gsm_capability->apn_try_list_.size());
446}
447
Thieu Le923006b2012-04-05 16:32:58 -0700448TEST_F(CellularCapabilityTest, StopModemDisconnectSuccess) {
Thieu Le049adb52012-11-12 17:14:51 -0800449 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Thieu Le923006b2012-04-05 16:32:58 -0700450 .WillOnce(Invoke(this,
451 &CellularCapabilityTest::InvokeDisconnect));
Thieu Lec8d2d962012-05-15 14:31:18 -0700452 EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutEnable))
Thieu Le923006b2012-04-05 16:32:58 -0700453 .WillOnce(Invoke(this,
454 &CellularCapabilityTest::InvokeEnable));
455 EXPECT_CALL(*this, TestCallback(IsSuccess()));
456 SetProxy();
457
458 Error error;
459 capability_->StopModem(
460 &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
461 dispatcher_.DispatchPendingEvents();
462}
463
464TEST_F(CellularCapabilityTest, StopModemDisconnectFail) {
Thieu Le049adb52012-11-12 17:14:51 -0800465 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Thieu Le923006b2012-04-05 16:32:58 -0700466 .WillOnce(Invoke(this,
467 &CellularCapabilityTest::InvokeDisconnectFail));
Thieu Lec8d2d962012-05-15 14:31:18 -0700468 EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutEnable))
Thieu Le923006b2012-04-05 16:32:58 -0700469 .WillOnce(Invoke(this,
470 &CellularCapabilityTest::InvokeEnable));
471 EXPECT_CALL(*this, TestCallback(IsSuccess()));
472 SetProxy();
473
474 Error error;
475 capability_->StopModem(
476 &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this)));
477 dispatcher_.DispatchPendingEvents();
478}
479
Thieu Le3d275392012-07-20 15:32:58 -0700480TEST_F(CellularCapabilityTest, DisconnectNoProxy) {
481 Error error;
482 ResultCallback disconnect_callback;
Thieu Le049adb52012-11-12 17:14:51 -0800483 EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDisconnect))
Thieu Le3d275392012-07-20 15:32:58 -0700484 .Times(0);
485 ReleaseCapabilityProxies();
486 capability_->Disconnect(&error, disconnect_callback);
487}
488
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500489} // namespace shill