blob: 95770159bb694ef600eee4ebace58b6ac85a8780 [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"
Eric Shienbroodc7073302012-04-03 14:41:44 -040018#include "shill/mock_profile.h"
Ben Chan19f83972012-10-03 23:25:56 -070019#include "shill/mock_store.h"
Ben Chan3ecdf822012-08-06 12:29:23 -070020#include "shill/nice_mock_control.h"
21#include "shill/proxy_factory.h"
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070022#include "shill/service_property_change_test.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010023
Darin Petkov31332412012-01-28 01:50:02 +010024using std::string;
Darin Petkov9cb02682012-01-28 00:17:38 +010025using testing::_;
Eric Shienbroodc7073302012-04-03 14:41:44 -040026using testing::InSequence;
mukesh agrawal81599e02013-04-22 16:40:00 -070027using testing::Mock;
Darin Petkovb72cf402011-11-22 14:51:39 +010028using testing::NiceMock;
Ben Chan96580442012-09-19 16:19:39 -070029using testing::Return;
Darin Petkovb72cf402011-11-22 14:51:39 +010030
31namespace shill {
32
33class CellularServiceTest : public testing::Test {
34 public:
35 CellularServiceTest()
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070036 : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070037 device_(new MockCellular(&modem_info_,
Thieu Le398b1da2013-03-11 17:31:10 -070038 "usb0",
39 kAddress,
40 3,
41 Cellular::kTypeCDMA,
42 "",
43 "",
44 "",
Thieu Le398b1da2013-03-11 17:31:10 -070045 ProxyFactory::GetInstance())),
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070046 service_(new CellularService(&modem_info_, device_)),
Darin Petkovb72cf402011-11-22 14:51:39 +010047 adaptor_(NULL) {}
48
49 virtual ~CellularServiceTest() {
50 adaptor_ = NULL;
51 }
52
53 virtual void SetUp() {
54 adaptor_ =
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070055 dynamic_cast<ServiceMockAdaptor *>(service_->adaptor());
Darin Petkovb72cf402011-11-22 14:51:39 +010056 }
57
Ben Chan3ecdf822012-08-06 12:29:23 -070058 CellularCapabilityCDMA *GetCapabilityCDMA() {
59 return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get());
Jason Glasgow82f9ab32012-04-04 14:27:19 -040060 }
61
Darin Petkovb72cf402011-11-22 14:51:39 +010062 protected:
Darin Petkov31332412012-01-28 01:50:02 +010063 static const char kAddress[];
64
Darin Petkov457728b2013-01-09 09:49:08 +010065 string GetFriendlyName() const { return service_->friendly_name(); }
66
Ben Chan96580442012-09-19 16:19:39 -070067 EventDispatcher dispatcher_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070068 MockModemInfo modem_info_;
Thieu Le398b1da2013-03-11 17:31:10 -070069 scoped_refptr<MockCellular> device_;
Darin Petkovb72cf402011-11-22 14:51:39 +010070 CellularServiceRefPtr service_;
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070071 ServiceMockAdaptor *adaptor_; // Owned by |service_|.
Darin Petkovb72cf402011-11-22 14:51:39 +010072};
73
Darin Petkov31332412012-01-28 01:50:02 +010074const char CellularServiceTest::kAddress[] = "000102030405";
75
Thieu Le9fdf1372012-05-14 15:20:48 -070076TEST_F(CellularServiceTest, Constructor) {
77 EXPECT_TRUE(service_->connectable());
78}
79
Ben Chan96580442012-09-19 16:19:39 -070080TEST_F(CellularServiceTest, SetActivationState) {
81 {
82 InSequence call_sequence;
83 EXPECT_CALL(*adaptor_, EmitStringChanged(
84 flimflam::kActivationStateProperty,
85 flimflam::kActivationStateNotActivated));
86 EXPECT_CALL(*adaptor_, EmitBoolChanged(
87 flimflam::kConnectableProperty, false));
88 EXPECT_CALL(*adaptor_, EmitStringChanged(
89 flimflam::kActivationStateProperty,
90 flimflam::kActivationStateActivating));
91 EXPECT_CALL(*adaptor_, EmitBoolChanged(
92 flimflam::kConnectableProperty, true));
93 EXPECT_CALL(*adaptor_, EmitStringChanged(
94 flimflam::kActivationStateProperty,
95 flimflam::kActivationStatePartiallyActivated));
96 EXPECT_CALL(*adaptor_, EmitStringChanged(
97 flimflam::kActivationStateProperty,
98 flimflam::kActivationStateActivated));
99 EXPECT_CALL(*adaptor_, EmitStringChanged(
100 flimflam::kActivationStateProperty,
101 flimflam::kActivationStateNotActivated));
102 EXPECT_CALL(*adaptor_, EmitBoolChanged(
103 flimflam::kConnectableProperty, false));
104 }
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700105 EXPECT_CALL(*modem_info_.mock_manager(), HasService(_))
106 .WillRepeatedly(Return(false));
Ben Chan96580442012-09-19 16:19:39 -0700107
108 EXPECT_TRUE(service_->activation_state().empty());
109 EXPECT_TRUE(service_->connectable());
110
111 service_->SetActivationState(flimflam::kActivationStateNotActivated);
112 EXPECT_EQ(flimflam::kActivationStateNotActivated,
113 service_->activation_state());
114 EXPECT_FALSE(service_->connectable());
115
116 service_->SetActivationState(flimflam::kActivationStateActivating);
117 EXPECT_EQ(flimflam::kActivationStateActivating, service_->activation_state());
118 EXPECT_TRUE(service_->connectable());
119
120 service_->SetActivationState(flimflam::kActivationStatePartiallyActivated);
121 EXPECT_EQ(flimflam::kActivationStatePartiallyActivated,
122 service_->activation_state());
123 EXPECT_TRUE(service_->connectable());
124
125 service_->SetActivationState(flimflam::kActivationStateActivated);
126 EXPECT_EQ(flimflam::kActivationStateActivated, service_->activation_state());
127 EXPECT_TRUE(service_->connectable());
128
129 service_->SetActivationState(flimflam::kActivationStateNotActivated);
130 EXPECT_EQ(flimflam::kActivationStateNotActivated,
131 service_->activation_state());
132 EXPECT_FALSE(service_->connectable());
133}
134
Darin Petkovb72cf402011-11-22 14:51:39 +0100135TEST_F(CellularServiceTest, SetNetworkTechnology) {
136 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kNetworkTechnologyProperty,
137 flimflam::kNetworkTechnologyUmts));
138 EXPECT_TRUE(service_->network_technology().empty());
139 service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts);
140 EXPECT_EQ(flimflam::kNetworkTechnologyUmts, service_->network_technology());
141 service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts);
142}
143
144TEST_F(CellularServiceTest, SetRoamingState) {
145 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kRoamingStateProperty,
146 flimflam::kRoamingStateHome));
147 EXPECT_TRUE(service_->roaming_state().empty());
148 service_->SetRoamingState(flimflam::kRoamingStateHome);
149 EXPECT_EQ(flimflam::kRoamingStateHome, service_->roaming_state());
150 service_->SetRoamingState(flimflam::kRoamingStateHome);
151}
152
Darin Petkovac635a82012-01-10 16:51:58 +0100153TEST_F(CellularServiceTest, FriendlyName) {
154 static const char kCarrier[] = "Cellular Carrier";
Ben Chan3ecdf822012-08-06 12:29:23 -0700155 GetCapabilityCDMA()->carrier_ = kCarrier;
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700156 service_ = new CellularService(&modem_info_, device_);
Darin Petkov457728b2013-01-09 09:49:08 +0100157 EXPECT_EQ(kCarrier, GetFriendlyName());
Darin Petkovac635a82012-01-10 16:51:58 +0100158}
159
Darin Petkov31332412012-01-28 01:50:02 +0100160TEST_F(CellularServiceTest, SetStorageIdentifier) {
Darin Petkovdd3e8662012-02-03 13:16:20 +0100161 EXPECT_EQ(string(flimflam::kTypeCellular) + "_" +
Darin Petkov457728b2013-01-09 09:49:08 +0100162 kAddress + "_" + GetFriendlyName(),
Darin Petkov31332412012-01-28 01:50:02 +0100163 service_->GetStorageIdentifier());
164 service_->SetStorageIdentifier("a b c");
165 EXPECT_EQ("a_b_c", service_->GetStorageIdentifier());
166}
167
Darin Petkov9cb02682012-01-28 00:17:38 +0100168TEST_F(CellularServiceTest, SetServingOperator) {
169 EXPECT_CALL(*adaptor_,
170 EmitStringmapChanged(flimflam::kServingOperatorProperty, _));
171 static const char kCode[] = "123456";
172 static const char kName[] = "Some Cellular Operator";
173 Cellular::Operator oper;
174 service_->SetServingOperator(oper);
175 oper.SetCode(kCode);
176 oper.SetName(kName);
177 service_->SetServingOperator(oper);
178 EXPECT_EQ(kCode, service_->serving_operator().GetCode());
179 EXPECT_EQ(kName, service_->serving_operator().GetName());
180 service_->SetServingOperator(oper);
181}
182
Darin Petkov381928f2012-02-02 23:00:12 +0100183TEST_F(CellularServiceTest, SetOLP) {
184 EXPECT_CALL(*adaptor_,
185 EmitStringmapChanged(flimflam::kPaymentPortalProperty, _));
186 static const char kURL[] = "payment.url";
187 static const char kMethod[] = "GET";
188 CellularService::OLP olp;
189 service_->SetOLP(olp);
190 olp.SetURL(kURL);
191 olp.SetMethod(kMethod);
192 service_->SetOLP(olp);
193 EXPECT_EQ(kURL, service_->olp().GetURL());
194 EXPECT_EQ(kMethod, service_->olp().GetMethod());
195 service_->SetOLP(olp);
196}
197
198TEST_F(CellularServiceTest, SetUsageURL) {
199 static const char kUsageURL[] = "usage.url";
200 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kUsageURLProperty,
201 kUsageURL));
202 EXPECT_TRUE(service_->usage_url().empty());
203 service_->SetUsageURL(kUsageURL);
204 EXPECT_EQ(kUsageURL, service_->usage_url());
205 service_->SetUsageURL(kUsageURL);
206}
207
Eric Shienbroodc7073302012-04-03 14:41:44 -0400208TEST_F(CellularServiceTest, SetApn) {
209 static const char kApn[] = "TheAPN";
210 static const char kUsername[] = "commander.data";
211 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700212 modem_info_.control_interface(), modem_info_.metrics(),
213 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400214 service_->set_profile(profile);
215 Error error;
216 Stringmap testapn;
217 testapn[flimflam::kApnProperty] = kApn;
218 testapn[flimflam::kApnUsernameProperty] = kUsername;
219 {
220 InSequence seq;
221 EXPECT_CALL(*adaptor_,
222 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
223 _));
224 EXPECT_CALL(*adaptor_,
225 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
226 }
227 service_->SetApn(testapn, &error);
228 EXPECT_TRUE(error.IsSuccess());
229 Stringmap resultapn = service_->GetApn(&error);
230 EXPECT_TRUE(error.IsSuccess());
231 EXPECT_EQ(2, resultapn.size());
232 Stringmap::const_iterator it = resultapn.find(flimflam::kApnProperty);
233 EXPECT_TRUE(it != resultapn.end() && it->second == kApn);
234 it = resultapn.find(flimflam::kApnUsernameProperty);
235 EXPECT_TRUE(it != resultapn.end() && it->second == kUsername);
236 EXPECT_FALSE(service_->GetUserSpecifiedApn() == NULL);
237}
238
239TEST_F(CellularServiceTest, ClearApn) {
240 static const char kApn[] = "TheAPN";
241 static const char kUsername[] = "commander.data";
242 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700243 modem_info_.control_interface(), modem_info_.metrics(),
244 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400245 service_->set_profile(profile);
246 Error error;
247 // Set up an APN to make sure that it later gets cleared.
248 Stringmap testapn;
249 testapn[flimflam::kApnProperty] = kApn;
250 testapn[flimflam::kApnUsernameProperty] = kUsername;
251 {
252 InSequence seq;
253 EXPECT_CALL(*adaptor_,
254 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
255 _));
256 EXPECT_CALL(*adaptor_,
257 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
258 }
259 service_->SetApn(testapn, &error);
260 Stringmap resultapn = service_->GetApn(&error);
261 ASSERT_TRUE(error.IsSuccess());
262 ASSERT_EQ(2, service_->GetApn(&error).size());
263
264 Stringmap emptyapn;
265 EXPECT_CALL(*adaptor_,
266 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
267 _)).Times(0);
268 EXPECT_CALL(*adaptor_,
269 EmitStringmapChanged(flimflam::kCellularApnProperty, _)).Times(1);
270 service_->SetApn(emptyapn, &error);
271 EXPECT_TRUE(error.IsSuccess());
272 resultapn = service_->GetApn(&error);
273 EXPECT_TRUE(resultapn.empty());
274 EXPECT_TRUE(service_->GetUserSpecifiedApn() == NULL);
275}
276
277TEST_F(CellularServiceTest, LastGoodApn) {
278 static const char kApn[] = "TheAPN";
279 static const char kUsername[] = "commander.data";
280 ProfileRefPtr profile(new NiceMock<MockProfile>(
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700281 modem_info_.control_interface(), modem_info_.metrics(),
282 modem_info_.manager()));
Eric Shienbroodc7073302012-04-03 14:41:44 -0400283 service_->set_profile(profile);
284 Stringmap testapn;
285 testapn[flimflam::kApnProperty] = kApn;
286 testapn[flimflam::kApnUsernameProperty] = kUsername;
287 EXPECT_CALL(*adaptor_,
288 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, _));
289 service_->SetLastGoodApn(testapn);
290 Stringmap *resultapn = service_->GetLastGoodApn();
291 EXPECT_FALSE(resultapn == NULL);
292 EXPECT_EQ(2, resultapn->size());
293 Stringmap::const_iterator it = resultapn->find(flimflam::kApnProperty);
294 EXPECT_TRUE(it != resultapn->end() && it->second == kApn);
295 it = resultapn->find(flimflam::kApnUsernameProperty);
296 EXPECT_TRUE(it != resultapn->end() && it->second == kUsername);
297 // Now set the user-specified APN, and check that LastGoodApn got
298 // cleared.
299 Stringmap userapn;
300 userapn[flimflam::kApnProperty] = kApn;
301 userapn[flimflam::kApnUsernameProperty] = kUsername;
302 {
303 InSequence seq;
304 EXPECT_CALL(*adaptor_,
305 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
306 _));
307 EXPECT_CALL(*adaptor_,
308 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
309 }
310 Error error;
311 service_->SetApn(userapn, &error);
312 EXPECT_TRUE(service_->GetLastGoodApn() == NULL);
313}
314
Ben Chan19f83972012-10-03 23:25:56 -0700315TEST_F(CellularServiceTest, IsAutoConnectable) {
316 const char *reason = NULL;
317
318 // Auto-connect should be suppressed if the device is not running.
319 device_->running_ = false;
320 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
321 EXPECT_STREQ(CellularService::kAutoConnDeviceDisabled, reason);
322
323 device_->running_ = true;
324
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800325 // If we're waiting on a disconnect before an activation, don't auto-connect.
326 GetCapabilityCDMA()->activation_starting_ = true;
327 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
328
329 // If we're waiting on an activation, also don't auto-connect.
330 GetCapabilityCDMA()->activation_starting_ = false;
331 GetCapabilityCDMA()->activation_state_ =
332 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING;
333 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
334
Thieu Le398b1da2013-03-11 17:31:10 -0700335 GetCapabilityCDMA()->activation_state_ =
336 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED;
337
338 // Auto-connect should be suppressed if the we're undergoing an
339 // out-of-credits detection.
340 service_->out_of_credits_detection_in_progress_ = true;
341 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
342 EXPECT_STREQ(CellularService::kAutoConnOutOfCreditsDetectionInProgress,
343 reason);
344
345 // Auto-connect should be suppressed if we're out of credits.
346 service_->out_of_credits_detection_in_progress_ = false;
347 service_->out_of_credits_ = true;
348 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
349 EXPECT_STREQ(CellularService::kAutoConnOutOfCredits, reason);
350
351 service_->out_of_credits_ = false;
352
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800353 // But other activation states are fine.
354 GetCapabilityCDMA()->activation_state_ =
355 MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED;
356 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
357 GetCapabilityCDMA()->activation_state_ =
358 MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED;
359 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
360 GetCapabilityCDMA()->activation_state_ =
361 MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED;
362 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
363
Ben Chan19f83972012-10-03 23:25:56 -0700364 // The following test cases are copied from ServiceTest.IsAutoConnectable
365
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700366 service_->SetConnectable(true);
Ben Chan19f83972012-10-03 23:25:56 -0700367 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
368
369 // We should not auto-connect to a Service that a user has
370 // deliberately disconnected.
371 Error error;
372 service_->UserInitiatedDisconnect(&error);
373 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
374 EXPECT_STREQ(Service::kAutoConnExplicitDisconnect, reason);
375
376 // But if the Service is reloaded, it is eligible for auto-connect
377 // again.
378 NiceMock<MockStore> storage;
379 EXPECT_CALL(storage, ContainsGroup(service_->GetStorageIdentifier()))
380 .WillOnce(Return(true));
381 EXPECT_TRUE(service_->Load(&storage));
382 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
383
384 // A non-user initiated Disconnect doesn't change anything.
385 service_->Disconnect(&error);
386 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
387
388 // A resume also re-enables auto-connect.
389 service_->UserInitiatedDisconnect(&error);
390 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
391 service_->OnAfterResume();
392 EXPECT_TRUE(service_->IsAutoConnectable(&reason));
393
394 service_->SetState(Service::kStateConnected);
395 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
396 EXPECT_STREQ(Service::kAutoConnConnected, reason);
397
398 service_->SetState(Service::kStateAssociating);
399 EXPECT_FALSE(service_->IsAutoConnectable(&reason));
400 EXPECT_STREQ(Service::kAutoConnConnecting, reason);
401}
402
Thieu Le398b1da2013-03-11 17:31:10 -0700403TEST_F(CellularServiceTest, OutOfCreditsDetected) {
404 service_->set_enforce_out_of_credits_detection(true);
405 EXPECT_CALL(*device_, Connect(_)).Times(3);
406 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700407 service_->Connect(&error, "in test");
Thieu Le398b1da2013-03-11 17:31:10 -0700408 service_->SetState(Service::kStateAssociating);
409 service_->SetState(Service::kStateFailure);
410 EXPECT_TRUE(service_->out_of_credits_detection_in_progress_);
411 dispatcher_.DispatchPendingEvents();
412 service_->SetState(Service::kStateConfiguring);
413 service_->SetState(Service::kStateIdle);
414 EXPECT_TRUE(service_->out_of_credits_detection_in_progress_);
415 dispatcher_.DispatchPendingEvents();
416 service_->SetState(Service::kStateConnected);
417 service_->SetState(Service::kStateIdle);
418 EXPECT_TRUE(service_->out_of_credits_);
419 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
420}
421
Thieu Le99dc56d2013-04-01 14:22:21 -0700422TEST_F(CellularServiceTest, OutOfCreditsDetectionNotSkippedAfterSlowResume) {
423 service_->set_enforce_out_of_credits_detection(true);
424 service_->OnAfterResume();
425 service_->resume_start_time_ =
426 base::Time::Now() -
427 base::TimeDelta::FromSeconds(
428 CellularService::kOutOfCreditsResumeIgnoreSeconds + 1);
429 EXPECT_CALL(*device_, Connect(_)).Times(3);
430 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700431 service_->Connect(&error, "in test");
Thieu Le99dc56d2013-04-01 14:22:21 -0700432 service_->SetState(Service::kStateAssociating);
433 service_->SetState(Service::kStateFailure);
434 EXPECT_TRUE(service_->out_of_credits_detection_in_progress_);
435 dispatcher_.DispatchPendingEvents();
436 service_->SetState(Service::kStateConfiguring);
437 service_->SetState(Service::kStateIdle);
438 EXPECT_TRUE(service_->out_of_credits_detection_in_progress_);
439 dispatcher_.DispatchPendingEvents();
440 service_->SetState(Service::kStateConnected);
441 service_->SetState(Service::kStateIdle);
442 EXPECT_TRUE(service_->out_of_credits_);
443 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
444}
445
446TEST_F(CellularServiceTest, OutOfCreditsDetectionSkippedAfterResume) {
447 service_->set_enforce_out_of_credits_detection(true);
448 service_->OnAfterResume();
449 EXPECT_CALL(*device_, Connect(_));
450 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700451 service_->Connect(&error, "in test");
Thieu Le99dc56d2013-04-01 14:22:21 -0700452 service_->SetState(Service::kStateConnected);
453 service_->SetState(Service::kStateIdle);
454 EXPECT_FALSE(service_->out_of_credits_);
455 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
456 // There should not be any pending connect requests but dispatch pending
457 // events anyway to be sure.
458 dispatcher_.DispatchPendingEvents();
459}
460
Thieu Le3a8683d2013-04-17 13:57:24 -0700461TEST_F(CellularServiceTest, OutOfCreditsDetectionSkippedAlreadyOutOfCredits) {
462 service_->set_enforce_out_of_credits_detection(true);
463 EXPECT_CALL(*device_, Connect(_));
464 Error error;
465 service_->Connect(&error, "in test");
466 service_->out_of_credits_ = true;
467 service_->SetState(Service::kStateConnected);
468 service_->SetState(Service::kStateIdle);
469 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
470 // There should not be any pending connect requests but dispatch pending
471 // events anyway to be sure.
472 dispatcher_.DispatchPendingEvents();
473}
474
Thieu Le398b1da2013-03-11 17:31:10 -0700475TEST_F(CellularServiceTest, OutOfCreditsDetectionSkippedExplicitDisconnect) {
476 service_->set_enforce_out_of_credits_detection(true);
477 EXPECT_CALL(*device_, Connect(_));
478 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700479 service_->Connect(&error, "in test");
Thieu Le398b1da2013-03-11 17:31:10 -0700480 service_->SetState(Service::kStateConnected);
481 service_->UserInitiatedDisconnect(&error);
482 service_->SetState(Service::kStateIdle);
483 EXPECT_FALSE(service_->out_of_credits_);
484 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
485 // There should not be any pending connect requests but dispatch pending
486 // events anyway to be sure.
487 dispatcher_.DispatchPendingEvents();
488}
489
490TEST_F(CellularServiceTest, OutOfCreditsNotDetectedConnectionNotDropped) {
491 service_->set_enforce_out_of_credits_detection(true);
492 EXPECT_CALL(*device_, Connect(_));
493 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700494 service_->Connect(&error, "in test");
Thieu Le398b1da2013-03-11 17:31:10 -0700495 service_->SetState(Service::kStateAssociating);
496 service_->SetState(Service::kStateConfiguring);
497 service_->SetState(Service::kStateConnected);
498 EXPECT_FALSE(service_->out_of_credits_);
499 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
500 // There should not be any pending connect requests but dispatch pending
501 // events anyway to be sure.
502 dispatcher_.DispatchPendingEvents();
503}
504
505TEST_F(CellularServiceTest, OutOfCreditsNotDetectedIntermittentNetwork) {
506 service_->set_enforce_out_of_credits_detection(true);
507 EXPECT_CALL(*device_, Connect(_));
508 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700509 service_->Connect(&error, "in test");
Thieu Le398b1da2013-03-11 17:31:10 -0700510 service_->SetState(Service::kStateConnected);
511 service_->connect_start_time_ =
512 base::Time::Now() -
513 base::TimeDelta::FromSeconds(
514 CellularService::kOutOfCreditsConnectionDropSeconds + 1);
515 service_->SetState(Service::kStateIdle);
516 EXPECT_FALSE(service_->out_of_credits_);
517 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
518 // There should not be any pending connect requests but dispatch pending
519 // events anyway to be sure.
520 dispatcher_.DispatchPendingEvents();
521}
522
523TEST_F(CellularServiceTest, OutOfCreditsNotEnforced) {
524 EXPECT_CALL(*device_, Connect(_));
525 Error error;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700526 service_->Connect(&error, "in test");
Thieu Le398b1da2013-03-11 17:31:10 -0700527 service_->SetState(Service::kStateConnected);
528 service_->SetState(Service::kStateIdle);
529 EXPECT_FALSE(service_->out_of_credits_);
530 EXPECT_FALSE(service_->out_of_credits_detection_in_progress_);
531 // There should not be any pending connect requests but dispatch pending
532 // events anyway to be sure.
533 dispatcher_.DispatchPendingEvents();
534}
535
mukesh agrawal81599e02013-04-22 16:40:00 -0700536// Some of these tests duplicate signals tested above. However, it's
537// convenient to have all the property change notifications documented
538// (and tested) in one place.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700539TEST_F(CellularServiceTest, PropertyChanges) {
540 TestCommonPropertyChanges(service_, adaptor_);
541 TestAutoConnectPropertyChange(service_, adaptor_);
mukesh agrawal81599e02013-04-22 16:40:00 -0700542
543 bool activate_over_non_cellular =
544 service_->activate_over_non_cellular_network();
545 EXPECT_CALL(*adaptor_,
546 EmitBoolChanged(kActivateOverNonCellularNetworkProperty, _));
547 service_->SetActivateOverNonCellularNetwork(!activate_over_non_cellular);
548 Mock::VerifyAndClearExpectations(adaptor_);
549
550 EXPECT_NE(flimflam::kActivationStateNotActivated,
551 service_->activation_state());
552 EXPECT_CALL(*adaptor_,
553 EmitStringChanged(flimflam::kActivationStateProperty, _));
554 service_->SetActivationState(flimflam::kActivationStateNotActivated);
555 Mock::VerifyAndClearExpectations(adaptor_);
556
557 string network_technology = service_->network_technology();
558 EXPECT_CALL(*adaptor_,
559 EmitStringChanged(flimflam::kNetworkTechnologyProperty, _));
560 service_->SetNetworkTechnology(network_technology + "and some new stuff");
561 Mock::VerifyAndClearExpectations(adaptor_);
562
563 bool out_of_credits = service_->out_of_credits();
564 EXPECT_CALL(*adaptor_, EmitBoolChanged(kOutOfCreditsProperty, _));
565 service_->SetOutOfCredits(!out_of_credits);
566 Mock::VerifyAndClearExpectations(adaptor_);
567
568 string roaming_state = service_->roaming_state();
569 EXPECT_CALL(*adaptor_,
570 EmitStringChanged(flimflam::kRoamingStateProperty, _));
571 service_->SetRoamingState(roaming_state + "and some new stuff");
572 Mock::VerifyAndClearExpectations(adaptor_);
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700573}
574
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700575// Custom property setters should return false, and make no changes, if
576// the new value is the same as the old value.
577TEST_F(CellularServiceTest, CustomSetterNoopChange) {
578 // Test that we didn't break any setters provided by the base class.
579 TestCustomSetterNoopChange(service_, modem_info_.mock_manager());
580
581 // Test the new setter we added.
582 // First set up our environment...
583 static const char kApn[] = "TheAPN";
584 static const char kUsername[] = "commander.data";
585 Error error;
586 Stringmap testapn;
587 ProfileRefPtr profile(new NiceMock<MockProfile>(nullptr, nullptr, nullptr));
588 service_->set_profile(profile);
589 testapn[flimflam::kApnProperty] = kApn;
590 testapn[flimflam::kApnUsernameProperty] = kUsername;
591 // ... then set to a known value ...
592 EXPECT_TRUE(service_->SetApn(testapn, &error));
593 EXPECT_TRUE(error.IsSuccess());
594 // ... then set to same value.
595 EXPECT_FALSE(service_->SetApn(testapn, &error));
596 EXPECT_TRUE(error.IsSuccess());
597}
598
Darin Petkovb72cf402011-11-22 14:51:39 +0100599} // namespace shill