Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 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_service.h" |
| 6 | |
| 7 | #include <chromeos/dbus/service_constants.h> |
| 8 | #include <gtest/gtest.h> |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 9 | #include <mm/mm-modem.h> |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 10 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 11 | #include "shill/cellular_capability.h" |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 12 | #include "shill/cellular_capability_cdma.h" |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 13 | #include "shill/mock_adaptors.h" |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 14 | #include "shill/mock_cellular.h" |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 15 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 16 | #include "shill/mock_metrics.h" |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 17 | #include "shill/mock_modem_info.h" |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 18 | #include "shill/mock_profile.h" |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 19 | #include "shill/mock_store.h" |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 20 | #include "shill/nice_mock_control.h" |
| 21 | #include "shill/proxy_factory.h" |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 22 | #include "shill/service_property_change_test.h" |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 23 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 24 | using std::string; |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 25 | using testing::_; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 26 | using testing::InSequence; |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 27 | using testing::Mock; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 28 | using testing::NiceMock; |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 29 | using testing::Return; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 30 | |
| 31 | namespace shill { |
| 32 | |
| 33 | class CellularServiceTest : public testing::Test { |
| 34 | public: |
| 35 | CellularServiceTest() |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 36 | : modem_info_(NULL, &dispatcher_, NULL, NULL, NULL), |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 37 | device_(new MockCellular(&modem_info_, |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 38 | "usb0", |
| 39 | kAddress, |
| 40 | 3, |
| 41 | Cellular::kTypeCDMA, |
| 42 | "", |
| 43 | "", |
| 44 | "", |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 45 | ProxyFactory::GetInstance())), |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 46 | service_(new CellularService(&modem_info_, device_)), |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 47 | adaptor_(NULL) {} |
| 48 | |
| 49 | virtual ~CellularServiceTest() { |
| 50 | adaptor_ = NULL; |
| 51 | } |
| 52 | |
| 53 | virtual void SetUp() { |
| 54 | adaptor_ = |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 55 | dynamic_cast<ServiceMockAdaptor *>(service_->adaptor()); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 58 | CellularCapabilityCDMA *GetCapabilityCDMA() { |
| 59 | return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get()); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 62 | protected: |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 63 | static const char kAddress[]; |
| 64 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 65 | string GetFriendlyName() const { return service_->friendly_name(); } |
| 66 | |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 67 | EventDispatcher dispatcher_; |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 68 | MockModemInfo modem_info_; |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 69 | scoped_refptr<MockCellular> device_; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 70 | CellularServiceRefPtr service_; |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 71 | ServiceMockAdaptor *adaptor_; // Owned by |service_|. |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 74 | const char CellularServiceTest::kAddress[] = "000102030405"; |
| 75 | |
Thieu Le | 9fdf137 | 2012-05-14 15:20:48 -0700 | [diff] [blame] | 76 | TEST_F(CellularServiceTest, Constructor) { |
| 77 | EXPECT_TRUE(service_->connectable()); |
| 78 | } |
| 79 | |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 80 | TEST_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 Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 105 | EXPECT_CALL(*modem_info_.mock_manager(), HasService(_)) |
| 106 | .WillRepeatedly(Return(false)); |
Ben Chan | 9658044 | 2012-09-19 16:19:39 -0700 | [diff] [blame] | 107 | |
| 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 Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 135 | TEST_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 | |
| 144 | TEST_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 Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 153 | TEST_F(CellularServiceTest, FriendlyName) { |
| 154 | static const char kCarrier[] = "Cellular Carrier"; |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 155 | GetCapabilityCDMA()->carrier_ = kCarrier; |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 156 | service_ = new CellularService(&modem_info_, device_); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 157 | EXPECT_EQ(kCarrier, GetFriendlyName()); |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 160 | TEST_F(CellularServiceTest, SetStorageIdentifier) { |
Darin Petkov | dd3e866 | 2012-02-03 13:16:20 +0100 | [diff] [blame] | 161 | EXPECT_EQ(string(flimflam::kTypeCellular) + "_" + |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 162 | kAddress + "_" + GetFriendlyName(), |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 163 | service_->GetStorageIdentifier()); |
| 164 | service_->SetStorageIdentifier("a b c"); |
| 165 | EXPECT_EQ("a_b_c", service_->GetStorageIdentifier()); |
| 166 | } |
| 167 | |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 168 | TEST_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 Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 183 | TEST_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 | |
| 198 | TEST_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 Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 208 | TEST_F(CellularServiceTest, SetApn) { |
| 209 | static const char kApn[] = "TheAPN"; |
| 210 | static const char kUsername[] = "commander.data"; |
| 211 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 212 | modem_info_.control_interface(), modem_info_.metrics(), |
| 213 | modem_info_.manager())); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 214 | 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 | |
| 239 | TEST_F(CellularServiceTest, ClearApn) { |
| 240 | static const char kApn[] = "TheAPN"; |
| 241 | static const char kUsername[] = "commander.data"; |
| 242 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 243 | modem_info_.control_interface(), modem_info_.metrics(), |
| 244 | modem_info_.manager())); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 245 | 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 | |
| 277 | TEST_F(CellularServiceTest, LastGoodApn) { |
| 278 | static const char kApn[] = "TheAPN"; |
| 279 | static const char kUsername[] = "commander.data"; |
| 280 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 281 | modem_info_.control_interface(), modem_info_.metrics(), |
| 282 | modem_info_.manager())); |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 283 | 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 Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 315 | TEST_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 Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 325 | // 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 Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 335 | 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 Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 353 | // 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 Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 364 | // The following test cases are copied from ServiceTest.IsAutoConnectable |
| 365 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 366 | service_->SetConnectable(true); |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 367 | 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 Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 403 | TEST_F(CellularServiceTest, OutOfCreditsDetected) { |
| 404 | service_->set_enforce_out_of_credits_detection(true); |
| 405 | EXPECT_CALL(*device_, Connect(_)).Times(3); |
| 406 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 407 | service_->Connect(&error, "in test"); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 408 | 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 Le | 99dc56d | 2013-04-01 14:22:21 -0700 | [diff] [blame] | 422 | TEST_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 agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 431 | service_->Connect(&error, "in test"); |
Thieu Le | 99dc56d | 2013-04-01 14:22:21 -0700 | [diff] [blame] | 432 | 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 | |
| 446 | TEST_F(CellularServiceTest, OutOfCreditsDetectionSkippedAfterResume) { |
| 447 | service_->set_enforce_out_of_credits_detection(true); |
| 448 | service_->OnAfterResume(); |
| 449 | EXPECT_CALL(*device_, Connect(_)); |
| 450 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 451 | service_->Connect(&error, "in test"); |
Thieu Le | 99dc56d | 2013-04-01 14:22:21 -0700 | [diff] [blame] | 452 | 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 Le | 3a8683d | 2013-04-17 13:57:24 -0700 | [diff] [blame] | 461 | TEST_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 Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 475 | TEST_F(CellularServiceTest, OutOfCreditsDetectionSkippedExplicitDisconnect) { |
| 476 | service_->set_enforce_out_of_credits_detection(true); |
| 477 | EXPECT_CALL(*device_, Connect(_)); |
| 478 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 479 | service_->Connect(&error, "in test"); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 480 | 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 | |
| 490 | TEST_F(CellularServiceTest, OutOfCreditsNotDetectedConnectionNotDropped) { |
| 491 | service_->set_enforce_out_of_credits_detection(true); |
| 492 | EXPECT_CALL(*device_, Connect(_)); |
| 493 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 494 | service_->Connect(&error, "in test"); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 495 | 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 | |
| 505 | TEST_F(CellularServiceTest, OutOfCreditsNotDetectedIntermittentNetwork) { |
| 506 | service_->set_enforce_out_of_credits_detection(true); |
| 507 | EXPECT_CALL(*device_, Connect(_)); |
| 508 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 509 | service_->Connect(&error, "in test"); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 510 | 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 | |
| 523 | TEST_F(CellularServiceTest, OutOfCreditsNotEnforced) { |
| 524 | EXPECT_CALL(*device_, Connect(_)); |
| 525 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 526 | service_->Connect(&error, "in test"); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 527 | 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 agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 536 | // 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 agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 539 | TEST_F(CellularServiceTest, PropertyChanges) { |
| 540 | TestCommonPropertyChanges(service_, adaptor_); |
| 541 | TestAutoConnectPropertyChange(service_, adaptor_); |
mukesh agrawal | 81599e0 | 2013-04-22 16:40:00 -0700 | [diff] [blame] | 542 | |
| 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 agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 573 | } |
| 574 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 575 | // Custom property setters should return false, and make no changes, if |
| 576 | // the new value is the same as the old value. |
| 577 | TEST_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 Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 599 | } // namespace shill |