blob: c80ae1e4b0faa4d12e36196daaf3ece837d10b7f [file] [log] [blame]
Darin Petkovac635a82012-01-10 16:51:58 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovb72cf402011-11-22 14:51:39 +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_service.h"
6
7#include <chromeos/dbus/service_constants.h>
8#include <gtest/gtest.h>
Christopher Wiley1582bdd2012-11-15 11:31:14 -08009#include <mm/mm-modem.h>
Darin Petkovb72cf402011-11-22 14:51:39 +010010
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050011#include "shill/cellular_capability.h"
Ben Chan3ecdf822012-08-06 12:29:23 -070012#include "shill/cellular_capability_cdma.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010013#include "shill/mock_adaptors.h"
Thieu Le398b1da2013-03-11 17:31:10 -070014#include "shill/mock_cellular.h"
Ben Chan96580442012-09-19 16:19:39 -070015#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080016#include "shill/mock_metrics.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070017#include "shill/mock_modem_info.h"
Thieu Le43ce4d42013-10-04 16:08:55 -070018#include "shill/mock_out_of_credits_detector.h"
Eric Shienbroodc7073302012-04-03 14:41:44 -040019#include "shill/mock_profile.h"
Ben Chan19f83972012-10-03 23:25:56 -070020#include "shill/mock_store.h"
Ben Chan3ecdf822012-08-06 12:29:23 -070021#include "shill/nice_mock_control.h"
22#include "shill/proxy_factory.h"
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070023#include "shill/service_property_change_test.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010024
Darin Petkov31332412012-01-28 01:50:02 +010025using std::string;
Darin Petkov9cb02682012-01-28 00:17:38 +010026using testing::_;
Eric Shienbroodc7073302012-04-03 14:41:44 -040027using testing::InSequence;
mukesh agrawal81599e02013-04-22 16:40:00 -070028using testing::Mock;
Darin Petkovb72cf402011-11-22 14:51:39 +010029using testing::NiceMock;
Ben Chan96580442012-09-19 16:19:39 -070030using testing::Return;
mukesh agrawala70ec272013-08-19 16:08:31 -070031using testing::SetArgumentPointee;
Darin Petkovb72cf402011-11-22 14:51:39 +010032
33namespace shill {
34
35class CellularServiceTest : public testing::Test {
36 public:
37 CellularServiceTest()
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070038 : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070039 device_(new MockCellular(&modem_info_,
Thieu Le398b1da2013-03-11 17:31:10 -070040 "usb0",
41 kAddress,
42 3,
43 Cellular::kTypeCDMA,
44 "",
45 "",
46 "",
Thieu Le398b1da2013-03-11 17:31:10 -070047 ProxyFactory::GetInstance())),
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070048 service_(new CellularService(&modem_info_, device_)),
Darin Petkovb72cf402011-11-22 14:51:39 +010049 adaptor_(NULL) {}
50
51 virtual ~CellularServiceTest() {
52 adaptor_ = NULL;
53 }
54
55 virtual void SetUp() {
56 adaptor_ =
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070057 dynamic_cast<ServiceMockAdaptor *>(service_->adaptor());
Thieu Le43ce4d42013-10-04 16:08:55 -070058 out_of_credits_detector_ =
59 new MockOutOfCreditsDetector(NULL, NULL, NULL, service_);
60 // Passes ownership.
61 service_->set_out_of_credits_detector(out_of_credits_detector_);
Darin Petkovb72cf402011-11-22 14:51:39 +010062 }
63
Ben Chan3ecdf822012-08-06 12:29:23 -070064 CellularCapabilityCDMA *GetCapabilityCDMA() {
65 return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get());
Jason Glasgow82f9ab32012-04-04 14:27:19 -040066 }
67
Darin Petkovb72cf402011-11-22 14:51:39 +010068 protected:
Darin Petkov31332412012-01-28 01:50:02 +010069 static const char kAddress[];
70
Darin Petkov457728b2013-01-09 09:49:08 +010071 string GetFriendlyName() const { return service_->friendly_name(); }
72
Ben Chan96580442012-09-19 16:19:39 -070073 EventDispatcher dispatcher_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070074 MockModemInfo modem_info_;
Thieu Le398b1da2013-03-11 17:31:10 -070075 scoped_refptr<MockCellular> device_;
Darin Petkovb72cf402011-11-22 14:51:39 +010076 CellularServiceRefPtr service_;
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070077 ServiceMockAdaptor *adaptor_; // Owned by |service_|.
Thieu Le43ce4d42013-10-04 16:08:55 -070078 MockOutOfCreditsDetector *out_of_credits_detector_; // Owned by |service_|.
Darin Petkovb72cf402011-11-22 14:51:39 +010079};
80
Darin Petkov31332412012-01-28 01:50:02 +010081const char CellularServiceTest::kAddress[] = "000102030405";
82
Thieu Le9fdf1372012-05-14 15:20:48 -070083TEST_F(CellularServiceTest, Constructor) {
84 EXPECT_TRUE(service_->connectable());
85}
86
Ben Chan96580442012-09-19 16:19:39 -070087TEST_F(CellularServiceTest, SetActivationState) {
88 {
89 InSequence call_sequence;
90 EXPECT_CALL(*adaptor_, EmitStringChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -070091 kActivationStateProperty,
92 kActivationStateNotActivated));
Ben Chan96580442012-09-19 16:19:39 -070093 EXPECT_CALL(*adaptor_, EmitBoolChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -070094 kConnectableProperty, false));
Ben Chan96580442012-09-19 16:19:39 -070095 EXPECT_CALL(*adaptor_, EmitStringChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -070096 kActivationStateProperty,
97 kActivationStateActivating));
Ben Chan96580442012-09-19 16:19:39 -070098 EXPECT_CALL(*adaptor_, EmitBoolChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -070099 kConnectableProperty, true));
Ben Chan96580442012-09-19 16:19:39 -0700100 EXPECT_CALL(*adaptor_, EmitStringChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -0700101 kActivationStateProperty,
102 kActivationStatePartiallyActivated));
Ben Chan96580442012-09-19 16:19:39 -0700103 EXPECT_CALL(*adaptor_, EmitStringChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -0700104 kActivationStateProperty,
105 kActivationStateActivated));
Ben Chan96580442012-09-19 16:19:39 -0700106 EXPECT_CALL(*adaptor_, EmitStringChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -0700107 kActivationStateProperty,
108 kActivationStateNotActivated));
Ben Chan96580442012-09-19 16:19:39 -0700109 EXPECT_CALL(*adaptor_, EmitBoolChanged(
Ben Chan7ea768e2013-09-20 15:08:40 -0700110 kConnectableProperty, false));
Ben Chan96580442012-09-19 16:19:39 -0700111 }
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700112 EXPECT_CALL(*modem_info_.mock_manager(), HasService(_))
113 .WillRepeatedly(Return(false));
Ben Chan96580442012-09-19 16:19:39 -0700114
115 EXPECT_TRUE(service_->activation_state().empty());
116 EXPECT_TRUE(service_->connectable());
117
Ben Chan7ea768e2013-09-20 15:08:40 -0700118 service_->SetActivationState(kActivationStateNotActivated);
119 EXPECT_EQ(kActivationStateNotActivated, service_->activation_state());
Ben Chan96580442012-09-19 16:19:39 -0700120 EXPECT_FALSE(service_->connectable());
121
Ben Chan7ea768e2013-09-20 15:08:40 -0700122 service_->SetActivationState(kActivationStateActivating);
123 EXPECT_EQ(kActivationStateActivating, service_->activation_state());
Ben Chan96580442012-09-19 16:19:39 -0700124 EXPECT_TRUE(service_->connectable());
125
Ben Chan7ea768e2013-09-20 15:08:40 -0700126 service_->SetActivationState(kActivationStatePartiallyActivated);
127 EXPECT_EQ(kActivationStatePartiallyActivated, service_->activation_state());
Ben Chan96580442012-09-19 16:19:39 -0700128 EXPECT_TRUE(service_->connectable());
129
Ben Chan7ea768e2013-09-20 15:08:40 -0700130 service_->SetActivationState(kActivationStateActivated);
131 EXPECT_EQ(kActivationStateActivated, service_->activation_state());
Ben Chan96580442012-09-19 16:19:39 -0700132 EXPECT_TRUE(service_->connectable());
133
Ben Chan7ea768e2013-09-20 15:08:40 -0700134 service_->SetActivationState(kActivationStateNotActivated);
135 EXPECT_EQ(kActivationStateNotActivated, service_->activation_state());
Ben Chan96580442012-09-19 16:19:39 -0700136 EXPECT_FALSE(service_->connectable());
137}
138
Darin Petkovb72cf402011-11-22 14:51:39 +0100139TEST_F(CellularServiceTest, SetNetworkTechnology) {
Ben Chan7ea768e2013-09-20 15:08:40 -0700140 EXPECT_CALL(*adaptor_, EmitStringChanged(kNetworkTechnologyProperty,
141 kNetworkTechnologyUmts));
Darin Petkovb72cf402011-11-22 14:51:39 +0100142 EXPECT_TRUE(service_->network_technology().empty());
Ben Chan7ea768e2013-09-20 15:08:40 -0700143 service_->SetNetworkTechnology(kNetworkTechnologyUmts);
144 EXPECT_EQ(kNetworkTechnologyUmts, service_->network_technology());
145 service_->SetNetworkTechnology(kNetworkTechnologyUmts);
Darin Petkovb72cf402011-11-22 14:51:39 +0100146}
147
148TEST_F(CellularServiceTest, SetRoamingState) {
Ben Chan7ea768e2013-09-20 15:08:40 -0700149 EXPECT_CALL(*adaptor_, EmitStringChanged(kRoamingStateProperty,
150 kRoamingStateHome));
Darin Petkovb72cf402011-11-22 14:51:39 +0100151 EXPECT_TRUE(service_->roaming_state().empty());
Ben Chan7ea768e2013-09-20 15:08:40 -0700152 service_->SetRoamingState(kRoamingStateHome);
153 EXPECT_EQ(kRoamingStateHome, service_->roaming_state());
154 service_->SetRoamingState(kRoamingStateHome);
Darin Petkovb72cf402011-11-22 14:51:39 +0100155}
156
Darin Petkovac635a82012-01-10 16:51:58 +0100157TEST_F(CellularServiceTest, FriendlyName) {
158 static const char kCarrier[] = "Cellular Carrier";
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -0800159 device_->set_carrier(kCarrier);
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700160 service_ = new CellularService(&modem_info_, device_);
Darin Petkov457728b2013-01-09 09:49:08 +0100161 EXPECT_EQ(kCarrier, GetFriendlyName());
Darin Petkovac635a82012-01-10 16:51:58 +0100162}
163
Darin Petkov31332412012-01-28 01:50:02 +0100164TEST_F(CellularServiceTest, SetStorageIdentifier) {
Ben Chan7ea768e2013-09-20 15:08:40 -0700165 EXPECT_EQ(string(kTypeCellular) + "_" +
Darin Petkov457728b2013-01-09 09:49:08 +0100166 kAddress + "_" + GetFriendlyName(),
Darin Petkov31332412012-01-28 01:50:02 +0100167 service_->GetStorageIdentifier());
168 service_->SetStorageIdentifier("a b c");
169 EXPECT_EQ("a_b_c", service_->GetStorageIdentifier());
170}
171
Darin Petkov9cb02682012-01-28 00:17:38 +0100172TEST_F(CellularServiceTest, SetServingOperator) {
173 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700174 EmitStringmapChanged(kServingOperatorProperty, _));
Darin Petkov9cb02682012-01-28 00:17:38 +0100175 static const char kCode[] = "123456";
176 static const char kName[] = "Some Cellular Operator";
177 Cellular::Operator oper;
178 service_->SetServingOperator(oper);
179 oper.SetCode(kCode);
180 oper.SetName(kName);
181 service_->SetServingOperator(oper);
182 EXPECT_EQ(kCode, service_->serving_operator().GetCode());
183 EXPECT_EQ(kName, service_->serving_operator().GetName());
184 service_->SetServingOperator(oper);
185}
186
Darin Petkov381928f2012-02-02 23:00:12 +0100187TEST_F(CellularServiceTest, SetOLP) {
188 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700189 EmitStringmapChanged(kPaymentPortalProperty, _));
Darin Petkov381928f2012-02-02 23:00:12 +0100190 static const char kURL[] = "payment.url";
191 static const char kMethod[] = "GET";
192 CellularService::OLP olp;
193 service_->SetOLP(olp);
194 olp.SetURL(kURL);
195 olp.SetMethod(kMethod);
196 service_->SetOLP(olp);
197 EXPECT_EQ(kURL, service_->olp().GetURL());
198 EXPECT_EQ(kMethod, service_->olp().GetMethod());
199 service_->SetOLP(olp);
200}
201
202TEST_F(CellularServiceTest, SetUsageURL) {
203 static const char kUsageURL[] = "usage.url";
Ben Chan7ea768e2013-09-20 15:08:40 -0700204 EXPECT_CALL(*adaptor_, EmitStringChanged(kUsageURLProperty,
Darin Petkov381928f2012-02-02 23:00:12 +0100205 kUsageURL));
206 EXPECT_TRUE(service_->usage_url().empty());
207 service_->SetUsageURL(kUsageURL);
208 EXPECT_EQ(kUsageURL, service_->usage_url());
209 service_->SetUsageURL(kUsageURL);
210}
211
Eric Shienbroodc7073302012-04-03 14:41:44 -0400212TEST_F(CellularServiceTest, SetApn) {
213 static const char kApn[] = "TheAPN";
214 static const char kUsername[] = "commander.data";
215 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700216 modem_info_.control_interface(), modem_info_.metrics(),
217 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400218 service_->set_profile(profile);
219 Error error;
220 Stringmap testapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700221 testapn[kApnProperty] = kApn;
222 testapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400223 {
224 InSequence seq;
225 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700226 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400227 _));
228 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700229 EmitStringmapChanged(kCellularApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400230 }
231 service_->SetApn(testapn, &error);
232 EXPECT_TRUE(error.IsSuccess());
233 Stringmap resultapn = service_->GetApn(&error);
234 EXPECT_TRUE(error.IsSuccess());
235 EXPECT_EQ(2, resultapn.size());
Ben Chan7ea768e2013-09-20 15:08:40 -0700236 Stringmap::const_iterator it = resultapn.find(kApnProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400237 EXPECT_TRUE(it != resultapn.end() && it->second == kApn);
Ben Chan7ea768e2013-09-20 15:08:40 -0700238 it = resultapn.find(kApnUsernameProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400239 EXPECT_TRUE(it != resultapn.end() && it->second == kUsername);
240 EXPECT_FALSE(service_->GetUserSpecifiedApn() == NULL);
241}
242
243TEST_F(CellularServiceTest, ClearApn) {
244 static const char kApn[] = "TheAPN";
245 static const char kUsername[] = "commander.data";
246 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700247 modem_info_.control_interface(), modem_info_.metrics(),
248 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400249 service_->set_profile(profile);
250 Error error;
251 // Set up an APN to make sure that it later gets cleared.
252 Stringmap testapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700253 testapn[kApnProperty] = kApn;
254 testapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400255 {
256 InSequence seq;
257 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700258 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400259 _));
260 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700261 EmitStringmapChanged(kCellularApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400262 }
263 service_->SetApn(testapn, &error);
264 Stringmap resultapn = service_->GetApn(&error);
265 ASSERT_TRUE(error.IsSuccess());
266 ASSERT_EQ(2, service_->GetApn(&error).size());
267
268 Stringmap emptyapn;
269 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700270 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400271 _)).Times(0);
272 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700273 EmitStringmapChanged(kCellularApnProperty, _)).Times(1);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400274 service_->SetApn(emptyapn, &error);
275 EXPECT_TRUE(error.IsSuccess());
276 resultapn = service_->GetApn(&error);
277 EXPECT_TRUE(resultapn.empty());
278 EXPECT_TRUE(service_->GetUserSpecifiedApn() == NULL);
279}
280
281TEST_F(CellularServiceTest, LastGoodApn) {
282 static const char kApn[] = "TheAPN";
283 static const char kUsername[] = "commander.data";
284 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700285 modem_info_.control_interface(), modem_info_.metrics(),
286 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400287 service_->set_profile(profile);
288 Stringmap testapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700289 testapn[kApnProperty] = kApn;
290 testapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400291 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700292 EmitStringmapChanged(kCellularLastGoodApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400293 service_->SetLastGoodApn(testapn);
294 Stringmap *resultapn = service_->GetLastGoodApn();
295 EXPECT_FALSE(resultapn == NULL);
296 EXPECT_EQ(2, resultapn->size());
Ben Chan7ea768e2013-09-20 15:08:40 -0700297 Stringmap::const_iterator it = resultapn->find(kApnProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400298 EXPECT_TRUE(it != resultapn->end() && it->second == kApn);
Ben Chan7ea768e2013-09-20 15:08:40 -0700299 it = resultapn->find(kApnUsernameProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400300 EXPECT_TRUE(it != resultapn->end() && it->second == kUsername);
301 // Now set the user-specified APN, and check that LastGoodApn got
302 // cleared.
303 Stringmap userapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700304 userapn[kApnProperty] = kApn;
305 userapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400306 {
307 InSequence seq;
308 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700309 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400310 _));
311 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700312 EmitStringmapChanged(kCellularApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400313 }
314 Error error;
315 service_->SetApn(userapn, &error);
316 EXPECT_TRUE(service_->GetLastGoodApn() == NULL);
317}
318
Ben Chan19f83972012-10-03 23:25:56 -0700319TEST_F(CellularServiceTest, IsAutoConnectable) {
320 const char *reason = NULL;
321
Thieu Le43ce4d42013-10-04 16:08:55 -0700322 ON_CALL(*out_of_credits_detector_, IsDetecting())
323 .WillByDefault(Return(false));
324
Ben Chan19f83972012-10-03 23:25:56 -0700325 // Auto-connect should be suppressed if the device is not running.
326 device_->running_ = false;
327 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
328 EXPECT_STREQ(CellularService::kAutoConnDeviceDisabled, reason);
329
330 device_->running_ = true;
331
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800332 // If we're waiting on a disconnect before an activation, don't auto-connect.
333 GetCapabilityCDMA()->activation_starting_ = true;
334 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
335
336 // If we're waiting on an activation, also don't auto-connect.
337 GetCapabilityCDMA()->activation_starting_ = false;
338 GetCapabilityCDMA()->activation_state_ =
339 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING;
340 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
341
Thieu Le398b1da2013-03-11 17:31:10 -0700342 GetCapabilityCDMA()->activation_state_ =
343 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED;
344
345 // Auto-connect should be suppressed if the we're undergoing an
346 // out-of-credits detection.
Thieu Le43ce4d42013-10-04 16:08:55 -0700347 EXPECT_CALL(*out_of_credits_detector_, IsDetecting())
348 .WillOnce(Return(true));
Thieu Le398b1da2013-03-11 17:31:10 -0700349 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
350 EXPECT_STREQ(CellularService::kAutoConnOutOfCreditsDetectionInProgress,
351 reason);
Thieu Le43ce4d42013-10-04 16:08:55 -0700352 Mock::VerifyAndClearExpectations(out_of_credits_detector_);
Thieu Le398b1da2013-03-11 17:31:10 -0700353
354 // Auto-connect should be suppressed if we're out of credits.
Thieu Le43ce4d42013-10-04 16:08:55 -0700355 EXPECT_CALL(*out_of_credits_detector_, IsDetecting())
356 .WillOnce(Return(false));
357 EXPECT_CALL(*out_of_credits_detector_, out_of_credits())
358 .WillOnce(Return(true));
Thieu Le398b1da2013-03-11 17:31:10 -0700359 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
360 EXPECT_STREQ(CellularService::kAutoConnOutOfCredits, reason);
Thieu Le43ce4d42013-10-04 16:08:55 -0700361 Mock::VerifyAndClearExpectations(out_of_credits_detector_);
Thieu Le398b1da2013-03-11 17:31:10 -0700362
Thieu Le43ce4d42013-10-04 16:08:55 -0700363 EXPECT_CALL(*out_of_credits_detector_, out_of_credits())
364 .WillRepeatedly(Return(false));
Thieu Le398b1da2013-03-11 17:31:10 -0700365
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800366 // But other activation states are fine.
367 GetCapabilityCDMA()->activation_state_ =
368 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED;
369 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
370 GetCapabilityCDMA()->activation_state_ =
371 MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED;
372 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
373 GetCapabilityCDMA()->activation_state_ =
374 MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED;
375 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
376
mukesh agrawald4773882013-08-14 17:21:14 -0700377 // A PPP authentication failure means the Service is not auto-connectable.
378 service_->SetFailure(Service::kFailurePPPAuth);
379 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
380
381 // Reset failure state, to make the Service auto-connectable again.
382 service_->SetState(Service::kStateIdle);
383 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
384
Ben Chan19f83972012-10-03 23:25:56 -0700385 // The following test cases are copied from ServiceTest.IsAutoConnectable
386
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700387 service_->SetConnectable(true);
Ben Chan19f83972012-10-03 23:25:56 -0700388 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
389
390 // We should not auto-connect to a Service that a user has
391 // deliberately disconnected.
392 Error error;
393 service_->UserInitiatedDisconnect(&error);
394 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
395 EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason);
396
397 // But if the Service is reloaded, it is eligible for auto-connect
398 // again.
399 NiceMock<MockStore> storage;
400 EXPECT_CALL(storage, ContainsGroup(service_->GetStorageIdentifier()))
401 .WillOnce(Return(true));
402 EXPECT_TRUE(service_->Load(&storage));
403 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
404
405 // A non-user initiated Disconnect doesn't change anything.
406 service_->Disconnect(&error);
407 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
408
409 // A resume also re-enables auto-connect.
410 service_->UserInitiatedDisconnect(&error);
411 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
412 service_->OnAfterResume();
413 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
414
415 service_->SetState(Service::kStateConnected);
416 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
417 EXPECT_STREQ(Service::kAutoConnConnected, reason);
418
419 service_->SetState(Service::kStateAssociating);
420 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
421 EXPECT_STREQ(Service::kAutoConnConnecting, reason);
422}
423
mukesh agrawala70ec272013-08-19 16:08:31 -0700424TEST_F(CellularServiceTest, LoadResetsPPPAuthFailure) {
425 NiceMock<MockStore> storage;
426 EXPECT_CALL(storage, ContainsGroup(_)).WillRepeatedly(Return(true));
427 EXPECT_CALL(storage, GetString(_, _, _)).WillRepeatedly(Return(true));
428
429 const string kDefaultUser;
430 const string kDefaultPass;
431 const string kNewUser("new-username");
432 const string kNewPass("new-password");
433 for (const auto change_username : { false, true }) {
434 for (const auto change_password : { false, true }) {
435 service_->ppp_username_ = kDefaultUser;
436 service_->ppp_password_ = kDefaultPass;
437 service_->SetFailure(Service::kFailurePPPAuth);
438 EXPECT_TRUE(service_->IsFailed());
439 EXPECT_EQ(Service::kFailurePPPAuth, service_->failure());
440 if (change_username) {
441 EXPECT_CALL(storage,
442 GetString(_, CellularService::kStoragePPPUsername, _))
443 .WillOnce(DoAll(SetArgumentPointee<2>(kNewUser), Return(true)))
444 .RetiresOnSaturation();
445 }
446 if (change_password) {
447 EXPECT_CALL(storage,
448 GetString(_, CellularService::kStoragePPPPassword, _))
449 .WillOnce(DoAll(SetArgumentPointee<2>(kNewPass), Return(true)))
450 .RetiresOnSaturation();
451 }
452 EXPECT_TRUE(service_->Load(&storage));
453 if (change_username || change_password) {
454 EXPECT_NE(Service::kFailurePPPAuth, service_->failure());
455 } else {
456 EXPECT_EQ(Service::kFailurePPPAuth, service_->failure());
457 }
458 }
459 }
460}
461
mukesh agrawal81599e02013-04-22 16:40:00 -0700462// Some of these tests duplicate signals tested above. However, it's
463// convenient to have all the property change notifications documented
464// (and tested) in one place.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700465TEST_F(CellularServiceTest, PropertyChanges) {
466 TestCommonPropertyChanges(service_, adaptor_);
467 TestAutoConnectPropertyChange(service_, adaptor_);
mukesh agrawal81599e02013-04-22 16:40:00 -0700468
469 bool activate_over_non_cellular =
470 service_->activate_over_non_cellular_network();
471 EXPECT_CALL(*adaptor_,
472 EmitBoolChanged(kActivateOverNonCellularNetworkProperty, _));
473 service_->SetActivateOverNonCellularNetwork(!activate_over_non_cellular);
474 Mock::VerifyAndClearExpectations(adaptor_);
475
Ben Chan7ea768e2013-09-20 15:08:40 -0700476 EXPECT_NE(kActivationStateNotActivated, service_->activation_state());
477 EXPECT_CALL(*adaptor_, EmitStringChanged(kActivationStateProperty, _));
478 service_->SetActivationState(kActivationStateNotActivated);
mukesh agrawal81599e02013-04-22 16:40:00 -0700479 Mock::VerifyAndClearExpectations(adaptor_);
480
481 string network_technology = service_->network_technology();
Ben Chan7ea768e2013-09-20 15:08:40 -0700482 EXPECT_CALL(*adaptor_, EmitStringChanged(kNetworkTechnologyProperty, _));
mukesh agrawal81599e02013-04-22 16:40:00 -0700483 service_->SetNetworkTechnology(network_technology + "and some new stuff");
484 Mock::VerifyAndClearExpectations(adaptor_);
485
Thieu Le43ce4d42013-10-04 16:08:55 -0700486 bool out_of_credits = true;
487 EXPECT_CALL(*adaptor_,
488 EmitBoolChanged(kOutOfCreditsProperty, out_of_credits));
489 service_->SignalOutOfCreditsChanged(out_of_credits);
mukesh agrawal81599e02013-04-22 16:40:00 -0700490 Mock::VerifyAndClearExpectations(adaptor_);
491
492 string roaming_state = service_->roaming_state();
Ben Chan7ea768e2013-09-20 15:08:40 -0700493 EXPECT_CALL(*adaptor_, EmitStringChanged(kRoamingStateProperty, _));
mukesh agrawal81599e02013-04-22 16:40:00 -0700494 service_->SetRoamingState(roaming_state + "and some new stuff");
495 Mock::VerifyAndClearExpectations(adaptor_);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700496}
497
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700498// Custom property setters should return false, and make no changes, if
499// the new value is the same as the old value.
500TEST_F(CellularServiceTest, CustomSetterNoopChange) {
501 // Test that we didn't break any setters provided by the base class.
502 TestCustomSetterNoopChange(service_, modem_info_.mock_manager());
503
504 // Test the new setter we added.
505 // First set up our environment...
506 static const char kApn[] = "TheAPN";
507 static const char kUsername[] = "commander.data";
508 Error error;
509 Stringmap testapn;
510 ProfileRefPtr profile(new NiceMock<MockProfile>(nullptr, nullptr, nullptr));
511 service_->set_profile(profile);
Ben Chan7ea768e2013-09-20 15:08:40 -0700512 testapn[kApnProperty] = kApn;
513 testapn[kApnUsernameProperty] = kUsername;
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700514 // ... then set to a known value ...
515 EXPECT_TRUE(service_->SetApn(testapn, &error));
516 EXPECT_TRUE(error.IsSuccess());
517 // ... then set to same value.
518 EXPECT_FALSE(service_->SetApn(testapn, &error));
519 EXPECT_TRUE(error.IsSuccess());
520}
521
Darin Petkovb72cf402011-11-22 14:51:39 +0100522} // namespace shill