blob: 8e4ec1cb551ecf9706d3255d21eb0ee8ced7669e [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
Ben Chanc54afe52014-11-05 10:28:08 -08005#include "shill/cellular/cellular_service.h"
Darin Petkovb72cf402011-11-22 14:51:39 +01006
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
Ben Chanc54afe52014-11-05 10:28:08 -080011#include "shill/cellular/cellular_capability.h"
12#include "shill/cellular/cellular_capability_cdma.h"
13#include "shill/cellular/mock_cellular.h"
14#include "shill/cellular/mock_modem_info.h"
15#include "shill/cellular/mock_out_of_credits_detector.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010016#include "shill/mock_adaptors.h"
Ben Chan96580442012-09-19 16:19:39 -070017#include "shill/mock_manager.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080018#include "shill/mock_metrics.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()
Ben Chanea18c6c2014-09-30 13:08:26 -070038 : modem_info_(nullptr, &dispatcher_, nullptr, nullptr, nullptr),
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_)),
Ben Chanea18c6c2014-09-30 13:08:26 -070049 adaptor_(nullptr) {}
Darin Petkovb72cf402011-11-22 14:51:39 +010050
51 virtual ~CellularServiceTest() {
Ben Chanea18c6c2014-09-30 13:08:26 -070052 adaptor_ = nullptr;
Darin Petkovb72cf402011-11-22 14:51:39 +010053 }
54
55 virtual void SetUp() {
56 adaptor_ =
Paul Stewart2f6c7892015-06-16 13:13:10 -070057 dynamic_cast<ServiceMockAdaptor*>(service_->adaptor());
Thieu Le43ce4d42013-10-04 16:08:55 -070058 out_of_credits_detector_ =
Alex Vakulenko0951ccb2014-12-10 12:52:31 -080059 new MockOutOfCreditsDetector(nullptr, nullptr, nullptr, service_.get());
Thieu Le43ce4d42013-10-04 16:08:55 -070060 // Passes ownership.
61 service_->set_out_of_credits_detector(out_of_credits_detector_);
Darin Petkovb72cf402011-11-22 14:51:39 +010062 }
63
Paul Stewart2f6c7892015-06-16 13:13:10 -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_;
Paul Stewart2f6c7892015-06-16 13:13:10 -070077 ServiceMockAdaptor* adaptor_; // Owned by |service_|.
78 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 Petkov31332412012-01-28 01:50:02 +0100157TEST_F(CellularServiceTest, SetStorageIdentifier) {
Ben Chan7ea768e2013-09-20 15:08:40 -0700158 EXPECT_EQ(string(kTypeCellular) + "_" +
Darin Petkov457728b2013-01-09 09:49:08 +0100159 kAddress + "_" + GetFriendlyName(),
Darin Petkov31332412012-01-28 01:50:02 +0100160 service_->GetStorageIdentifier());
161 service_->SetStorageIdentifier("a b c");
162 EXPECT_EQ("a_b_c", service_->GetStorageIdentifier());
163}
164
Darin Petkov9cb02682012-01-28 00:17:38 +0100165TEST_F(CellularServiceTest, SetServingOperator) {
Darin Petkov9cb02682012-01-28 00:17:38 +0100166 static const char kCode[] = "123456";
167 static const char kName[] = "Some Cellular Operator";
Roshan Piusec77f592014-10-29 15:07:38 -0700168 Stringmap test_operator;
169 service_->set_serving_operator(test_operator);
170 test_operator[kOperatorCodeKey] = kCode;
171 test_operator[kOperatorNameKey] = kName;
172 EXPECT_CALL(*adaptor_,
173 EmitStringmapChanged(kServingOperatorProperty, _));
174 service_->set_serving_operator(test_operator);
Paul Stewart2f6c7892015-06-16 13:13:10 -0700175 const Stringmap& serving_operator = service_->serving_operator();
Roshan Piusec77f592014-10-29 15:07:38 -0700176 ASSERT_NE(serving_operator.end(), serving_operator.find(kOperatorCodeKey));
177 ASSERT_NE(serving_operator.end(), serving_operator.find(kOperatorNameKey));
178 EXPECT_EQ(kCode, serving_operator.find(kOperatorCodeKey)->second);
179 EXPECT_EQ(kName, serving_operator.find(kOperatorNameKey)->second);
180 Mock::VerifyAndClearExpectations(adaptor_);
181 EXPECT_CALL(*adaptor_,
182 EmitStringmapChanged(kServingOperatorProperty, _)).Times(0);
183 service_->set_serving_operator(serving_operator);
Darin Petkov9cb02682012-01-28 00:17:38 +0100184}
185
Darin Petkov381928f2012-02-02 23:00:12 +0100186TEST_F(CellularServiceTest, SetOLP) {
Prathmesh Prabhu92df6192014-04-29 18:08:08 -0700187 const char kMethod[] = "GET";
188 const char kURL[] = "payment.url";
189 const char kPostData[] = "post_man";
190 Stringmap olp;
191
192 service_->SetOLP("", "", "");
193 olp = service_->olp(); // Copy to simplify assertions below.
194 EXPECT_EQ("", olp[kPaymentPortalURL]);
195 EXPECT_EQ("", olp[kPaymentPortalMethod]);
196 EXPECT_EQ("", olp[kPaymentPortalPostData]);
197
Darin Petkov381928f2012-02-02 23:00:12 +0100198 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700199 EmitStringmapChanged(kPaymentPortalProperty, _));
Prathmesh Prabhu92df6192014-04-29 18:08:08 -0700200 service_->SetOLP(kURL, kMethod, kPostData);
201 olp = service_->olp(); // Copy to simplify assertions below.
202 EXPECT_EQ(kURL, olp[kPaymentPortalURL]);
203 EXPECT_EQ(kMethod, olp[kPaymentPortalMethod]);
204 EXPECT_EQ(kPostData, olp[kPaymentPortalPostData]);
Darin Petkov381928f2012-02-02 23:00:12 +0100205}
206
207TEST_F(CellularServiceTest, SetUsageURL) {
208 static const char kUsageURL[] = "usage.url";
Ben Chan7ea768e2013-09-20 15:08:40 -0700209 EXPECT_CALL(*adaptor_, EmitStringChanged(kUsageURLProperty,
Darin Petkov381928f2012-02-02 23:00:12 +0100210 kUsageURL));
211 EXPECT_TRUE(service_->usage_url().empty());
212 service_->SetUsageURL(kUsageURL);
213 EXPECT_EQ(kUsageURL, service_->usage_url());
214 service_->SetUsageURL(kUsageURL);
215}
216
Eric Shienbroodc7073302012-04-03 14:41:44 -0400217TEST_F(CellularServiceTest, SetApn) {
218 static const char kApn[] = "TheAPN";
219 static const char kUsername[] = "commander.data";
220 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700221 modem_info_.control_interface(), modem_info_.metrics(),
222 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400223 service_->set_profile(profile);
224 Error error;
225 Stringmap testapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700226 testapn[kApnProperty] = kApn;
227 testapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400228 {
229 InSequence seq;
230 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700231 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400232 _));
233 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700234 EmitStringmapChanged(kCellularApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400235 }
236 service_->SetApn(testapn, &error);
237 EXPECT_TRUE(error.IsSuccess());
238 Stringmap resultapn = service_->GetApn(&error);
239 EXPECT_TRUE(error.IsSuccess());
240 EXPECT_EQ(2, resultapn.size());
Ben Chan7ea768e2013-09-20 15:08:40 -0700241 Stringmap::const_iterator it = resultapn.find(kApnProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400242 EXPECT_TRUE(it != resultapn.end() && it->second == kApn);
Ben Chan7ea768e2013-09-20 15:08:40 -0700243 it = resultapn.find(kApnUsernameProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400244 EXPECT_TRUE(it != resultapn.end() && it->second == kUsername);
Ben Chanea18c6c2014-09-30 13:08:26 -0700245 EXPECT_NE(nullptr, service_->GetUserSpecifiedApn());
Eric Shienbroodc7073302012-04-03 14:41:44 -0400246}
247
248TEST_F(CellularServiceTest, ClearApn) {
249 static const char kApn[] = "TheAPN";
250 static const char kUsername[] = "commander.data";
251 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700252 modem_info_.control_interface(), modem_info_.metrics(),
253 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400254 service_->set_profile(profile);
255 Error error;
256 // Set up an APN to make sure that it later gets cleared.
257 Stringmap testapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700258 testapn[kApnProperty] = kApn;
259 testapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400260 {
261 InSequence seq;
262 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700263 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400264 _));
265 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700266 EmitStringmapChanged(kCellularApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400267 }
268 service_->SetApn(testapn, &error);
269 Stringmap resultapn = service_->GetApn(&error);
270 ASSERT_TRUE(error.IsSuccess());
271 ASSERT_EQ(2, service_->GetApn(&error).size());
272
273 Stringmap emptyapn;
274 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700275 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400276 _)).Times(0);
277 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700278 EmitStringmapChanged(kCellularApnProperty, _)).Times(1);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400279 service_->SetApn(emptyapn, &error);
280 EXPECT_TRUE(error.IsSuccess());
281 resultapn = service_->GetApn(&error);
282 EXPECT_TRUE(resultapn.empty());
Ben Chanea18c6c2014-09-30 13:08:26 -0700283 EXPECT_EQ(nullptr, service_->GetUserSpecifiedApn());;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400284}
285
286TEST_F(CellularServiceTest, LastGoodApn) {
287 static const char kApn[] = "TheAPN";
288 static const char kUsername[] = "commander.data";
289 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700290 modem_info_.control_interface(), modem_info_.metrics(),
291 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400292 service_->set_profile(profile);
293 Stringmap testapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700294 testapn[kApnProperty] = kApn;
295 testapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400296 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700297 EmitStringmapChanged(kCellularLastGoodApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400298 service_->SetLastGoodApn(testapn);
Paul Stewart2f6c7892015-06-16 13:13:10 -0700299 Stringmap* resultapn = service_->GetLastGoodApn();
Ben Chanea18c6c2014-09-30 13:08:26 -0700300 EXPECT_NE(nullptr, resultapn);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400301 EXPECT_EQ(2, resultapn->size());
Ben Chan7ea768e2013-09-20 15:08:40 -0700302 Stringmap::const_iterator it = resultapn->find(kApnProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400303 EXPECT_TRUE(it != resultapn->end() && it->second == kApn);
Ben Chan7ea768e2013-09-20 15:08:40 -0700304 it = resultapn->find(kApnUsernameProperty);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400305 EXPECT_TRUE(it != resultapn->end() && it->second == kUsername);
306 // Now set the user-specified APN, and check that LastGoodApn got
307 // cleared.
308 Stringmap userapn;
Ben Chan7ea768e2013-09-20 15:08:40 -0700309 userapn[kApnProperty] = kApn;
310 userapn[kApnUsernameProperty] = kUsername;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400311 {
312 InSequence seq;
313 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700314 EmitStringmapChanged(kCellularLastGoodApnProperty,
Eric Shienbroodc7073302012-04-03 14:41:44 -0400315 _));
316 EXPECT_CALL(*adaptor_,
Ben Chan7ea768e2013-09-20 15:08:40 -0700317 EmitStringmapChanged(kCellularApnProperty, _));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400318 }
319 Error error;
320 service_->SetApn(userapn, &error);
Ben Chanea18c6c2014-09-30 13:08:26 -0700321 EXPECT_EQ(nullptr, service_->GetLastGoodApn());;
Eric Shienbroodc7073302012-04-03 14:41:44 -0400322}
323
Ben Chan19f83972012-10-03 23:25:56 -0700324TEST_F(CellularServiceTest, IsAutoConnectable) {
Paul Stewart2f6c7892015-06-16 13:13:10 -0700325 const char* reason = nullptr;
Ben Chan19f83972012-10-03 23:25:56 -0700326
Thieu Le43ce4d42013-10-04 16:08:55 -0700327 ON_CALL(*out_of_credits_detector_, IsDetecting())
328 .WillByDefault(Return(false));
329
Ben Chan19f83972012-10-03 23:25:56 -0700330 // Auto-connect should be suppressed if the device is not running.
331 device_->running_ = false;
332 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
333 EXPECT_STREQ(CellularService::kAutoConnDeviceDisabled, reason);
334
335 device_->running_ = true;
336
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800337 // If we're waiting on a disconnect before an activation, don't auto-connect.
338 GetCapabilityCDMA()->activation_starting_ = true;
339 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
340
341 // If we're waiting on an activation, also don't auto-connect.
342 GetCapabilityCDMA()->activation_starting_ = false;
343 GetCapabilityCDMA()->activation_state_ =
344 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING;
345 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
346
Thieu Le398b1da2013-03-11 17:31:10 -0700347 GetCapabilityCDMA()->activation_state_ =
348 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED;
349
350 // Auto-connect should be suppressed if the we're undergoing an
351 // out-of-credits detection.
Thieu Le43ce4d42013-10-04 16:08:55 -0700352 EXPECT_CALL(*out_of_credits_detector_, IsDetecting())
353 .WillOnce(Return(true));
Thieu Le398b1da2013-03-11 17:31:10 -0700354 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
355 EXPECT_STREQ(CellularService::kAutoConnOutOfCreditsDetectionInProgress,
356 reason);
Thieu Le43ce4d42013-10-04 16:08:55 -0700357 Mock::VerifyAndClearExpectations(out_of_credits_detector_);
Thieu Le398b1da2013-03-11 17:31:10 -0700358
359 // Auto-connect should be suppressed if we're out of credits.
Thieu Le43ce4d42013-10-04 16:08:55 -0700360 EXPECT_CALL(*out_of_credits_detector_, IsDetecting())
361 .WillOnce(Return(false));
362 EXPECT_CALL(*out_of_credits_detector_, out_of_credits())
363 .WillOnce(Return(true));
Thieu Le398b1da2013-03-11 17:31:10 -0700364 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
365 EXPECT_STREQ(CellularService::kAutoConnOutOfCredits, reason);
Thieu Le43ce4d42013-10-04 16:08:55 -0700366 Mock::VerifyAndClearExpectations(out_of_credits_detector_);
Thieu Le398b1da2013-03-11 17:31:10 -0700367
Thieu Le43ce4d42013-10-04 16:08:55 -0700368 EXPECT_CALL(*out_of_credits_detector_, out_of_credits())
369 .WillRepeatedly(Return(false));
Thieu Le398b1da2013-03-11 17:31:10 -0700370
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800371 // But other activation states are fine.
372 GetCapabilityCDMA()->activation_state_ =
373 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED;
374 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
375 GetCapabilityCDMA()->activation_state_ =
376 MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED;
377 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
378 GetCapabilityCDMA()->activation_state_ =
379 MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED;
380 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
381
mukesh agrawald4773882013-08-14 17:21:14 -0700382 // A PPP authentication failure means the Service is not auto-connectable.
383 service_->SetFailure(Service::kFailurePPPAuth);
384 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
385
386 // Reset failure state, to make the Service auto-connectable again.
387 service_->SetState(Service::kStateIdle);
388 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
389
Ben Chan19f83972012-10-03 23:25:56 -0700390 // The following test cases are copied from ServiceTest.IsAutoConnectable
391
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700392 service_->SetConnectable(true);
Ben Chan19f83972012-10-03 23:25:56 -0700393 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
394
395 // We should not auto-connect to a Service that a user has
396 // deliberately disconnected.
397 Error error;
398 service_->UserInitiatedDisconnect(&error);
399 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
400 EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason);
401
402 // But if the Service is reloaded, it is eligible for auto-connect
403 // again.
404 NiceMock<MockStore> storage;
405 EXPECT_CALL(storage, ContainsGroup(service_->GetStorageIdentifier()))
406 .WillOnce(Return(true));
407 EXPECT_TRUE(service_->Load(&storage));
408 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
409
410 // A non-user initiated Disconnect doesn't change anything.
Samuel Tan0d061192014-07-07 15:45:15 -0700411 service_->Disconnect(&error, "in test");
Ben Chan19f83972012-10-03 23:25:56 -0700412 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
413
414 // A resume also re-enables auto-connect.
415 service_->UserInitiatedDisconnect(&error);
416 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
417 service_->OnAfterResume();
418 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
419
420 service_->SetState(Service::kStateConnected);
421 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
422 EXPECT_STREQ(Service::kAutoConnConnected, reason);
423
424 service_->SetState(Service::kStateAssociating);
425 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
426 EXPECT_STREQ(Service::kAutoConnConnecting, reason);
427}
428
mukesh agrawala70ec272013-08-19 16:08:31 -0700429TEST_F(CellularServiceTest, LoadResetsPPPAuthFailure) {
430 NiceMock<MockStore> storage;
431 EXPECT_CALL(storage, ContainsGroup(_)).WillRepeatedly(Return(true));
432 EXPECT_CALL(storage, GetString(_, _, _)).WillRepeatedly(Return(true));
433
434 const string kDefaultUser;
435 const string kDefaultPass;
436 const string kNewUser("new-username");
437 const string kNewPass("new-password");
438 for (const auto change_username : { false, true }) {
439 for (const auto change_password : { false, true }) {
440 service_->ppp_username_ = kDefaultUser;
441 service_->ppp_password_ = kDefaultPass;
442 service_->SetFailure(Service::kFailurePPPAuth);
443 EXPECT_TRUE(service_->IsFailed());
444 EXPECT_EQ(Service::kFailurePPPAuth, service_->failure());
445 if (change_username) {
446 EXPECT_CALL(storage,
447 GetString(_, CellularService::kStoragePPPUsername, _))
448 .WillOnce(DoAll(SetArgumentPointee<2>(kNewUser), Return(true)))
449 .RetiresOnSaturation();
450 }
451 if (change_password) {
452 EXPECT_CALL(storage,
453 GetString(_, CellularService::kStoragePPPPassword, _))
454 .WillOnce(DoAll(SetArgumentPointee<2>(kNewPass), Return(true)))
455 .RetiresOnSaturation();
456 }
457 EXPECT_TRUE(service_->Load(&storage));
458 if (change_username || change_password) {
459 EXPECT_NE(Service::kFailurePPPAuth, service_->failure());
460 } else {
461 EXPECT_EQ(Service::kFailurePPPAuth, service_->failure());
462 }
463 }
464 }
465}
466
mukesh agrawal81599e02013-04-22 16:40:00 -0700467// Some of these tests duplicate signals tested above. However, it's
468// convenient to have all the property change notifications documented
469// (and tested) in one place.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700470TEST_F(CellularServiceTest, PropertyChanges) {
471 TestCommonPropertyChanges(service_, adaptor_);
472 TestAutoConnectPropertyChange(service_, adaptor_);
mukesh agrawal81599e02013-04-22 16:40:00 -0700473
mukesh agrawal81599e02013-04-22 16:40:00 -0700474 EXPECT_CALL(*adaptor_,
Thieu Le71f81a62014-07-11 15:17:47 -0700475 EmitStringChanged(kActivationTypeProperty, _));
476 service_->SetActivationType(CellularService::kActivationTypeOTA);
mukesh agrawal81599e02013-04-22 16:40:00 -0700477 Mock::VerifyAndClearExpectations(adaptor_);
478
Ben Chan7ea768e2013-09-20 15:08:40 -0700479 EXPECT_NE(kActivationStateNotActivated, service_->activation_state());
480 EXPECT_CALL(*adaptor_, EmitStringChanged(kActivationStateProperty, _));
481 service_->SetActivationState(kActivationStateNotActivated);
mukesh agrawal81599e02013-04-22 16:40:00 -0700482 Mock::VerifyAndClearExpectations(adaptor_);
483
484 string network_technology = service_->network_technology();
Ben Chan7ea768e2013-09-20 15:08:40 -0700485 EXPECT_CALL(*adaptor_, EmitStringChanged(kNetworkTechnologyProperty, _));
mukesh agrawal81599e02013-04-22 16:40:00 -0700486 service_->SetNetworkTechnology(network_technology + "and some new stuff");
487 Mock::VerifyAndClearExpectations(adaptor_);
488
Thieu Le43ce4d42013-10-04 16:08:55 -0700489 bool out_of_credits = true;
490 EXPECT_CALL(*adaptor_,
491 EmitBoolChanged(kOutOfCreditsProperty, out_of_credits));
492 service_->SignalOutOfCreditsChanged(out_of_credits);
mukesh agrawal81599e02013-04-22 16:40:00 -0700493 Mock::VerifyAndClearExpectations(adaptor_);
494
495 string roaming_state = service_->roaming_state();
Ben Chan7ea768e2013-09-20 15:08:40 -0700496 EXPECT_CALL(*adaptor_, EmitStringChanged(kRoamingStateProperty, _));
mukesh agrawal81599e02013-04-22 16:40:00 -0700497 service_->SetRoamingState(roaming_state + "and some new stuff");
498 Mock::VerifyAndClearExpectations(adaptor_);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700499}
500
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700501// Custom property setters should return false, and make no changes, if
502// the new value is the same as the old value.
503TEST_F(CellularServiceTest, CustomSetterNoopChange) {
504 // Test that we didn't break any setters provided by the base class.
505 TestCustomSetterNoopChange(service_, modem_info_.mock_manager());
506
507 // Test the new setter we added.
508 // First set up our environment...
509 static const char kApn[] = "TheAPN";
510 static const char kUsername[] = "commander.data";
511 Error error;
512 Stringmap testapn;
513 ProfileRefPtr profile(new NiceMock<MockProfile>(nullptr, nullptr, nullptr));
514 service_->set_profile(profile);
Ben Chan7ea768e2013-09-20 15:08:40 -0700515 testapn[kApnProperty] = kApn;
516 testapn[kApnUsernameProperty] = kUsername;
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700517 // ... then set to a known value ...
518 EXPECT_TRUE(service_->SetApn(testapn, &error));
519 EXPECT_TRUE(error.IsSuccess());
520 // ... then set to same value.
521 EXPECT_FALSE(service_->SetApn(testapn, &error));
522 EXPECT_TRUE(error.IsSuccess());
523}
524
Darin Petkovb72cf402011-11-22 14:51:39 +0100525} // namespace shill