Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/cellular_capability_gsm.h" |
| 6 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 7 | #include <base/bind.h> |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 8 | #include <chromeos/dbus/service_constants.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include <mm/mm-modem.h> |
| 11 | #include <mobile_provider.h> |
| 12 | |
| 13 | #include "shill/cellular.h" |
| 14 | #include "shill/cellular_service.h" |
| 15 | #include "shill/error.h" |
| 16 | #include "shill/event_dispatcher.h" |
| 17 | #include "shill/mock_adaptors.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 18 | #include "shill/mock_glib.h" |
| 19 | #include "shill/mock_manager.h" |
| 20 | #include "shill/mock_metrics.h" |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 21 | #include "shill/mock_modem_cdma_proxy.h" |
| 22 | #include "shill/mock_modem_gsm_card_proxy.h" |
| 23 | #include "shill/mock_modem_gsm_network_proxy.h" |
| 24 | #include "shill/mock_modem_proxy.h" |
| 25 | #include "shill/mock_modem_simple_proxy.h" |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 26 | #include "shill/mock_profile.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 27 | #include "shill/mock_rtnl_handler.h" |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 28 | #include "shill/nice_mock_control.h" |
| 29 | #include "shill/proxy_factory.h" |
| 30 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 31 | using base::Bind; |
| 32 | using base::Unretained; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 33 | using std::string; |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 34 | using testing::InSequence; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 35 | using testing::NiceMock; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 36 | using testing::_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 37 | |
| 38 | namespace shill { |
| 39 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 40 | MATCHER(IsSuccess, "") { |
| 41 | return arg.IsSuccess(); |
| 42 | } |
| 43 | MATCHER(IsFailure, "") { |
| 44 | return arg.IsFailure(); |
| 45 | } |
| 46 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 47 | class CellularCapabilityTest : public testing::Test { |
| 48 | public: |
| 49 | CellularCapabilityTest() |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 50 | : manager_(&control_, &dispatcher_, &metrics_, &glib_), |
| 51 | cellular_(new Cellular(&control_, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 52 | &dispatcher_, |
| 53 | NULL, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 54 | &manager_, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 55 | "", |
| 56 | "", |
| 57 | 0, |
| 58 | Cellular::kTypeGSM, |
| 59 | "", |
| 60 | "", |
| 61 | NULL)), |
| 62 | proxy_(new MockModemProxy()), |
| 63 | simple_proxy_(new MockModemSimpleProxy()), |
| 64 | cdma_proxy_(new MockModemCDMAProxy()), |
| 65 | gsm_card_proxy_(new MockModemGSMCardProxy()), |
| 66 | gsm_network_proxy_(new MockModemGSMNetworkProxy()), |
| 67 | proxy_factory_(this), |
| 68 | capability_(NULL), |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 69 | device_adaptor_(NULL), |
| 70 | provider_db_(NULL) {} |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 71 | |
| 72 | virtual ~CellularCapabilityTest() { |
| 73 | cellular_->service_ = NULL; |
| 74 | capability_ = NULL; |
| 75 | device_adaptor_ = NULL; |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 76 | mobile_provider_close_db(provider_db_); |
| 77 | provider_db_ = NULL; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | virtual void SetUp() { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 81 | static_cast<Device *>(cellular_)->rtnl_handler_ = &rtnl_handler_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 82 | capability_ = cellular_->capability_.get(); |
| 83 | device_adaptor_ = |
| 84 | dynamic_cast<NiceMock<DeviceMockAdaptor> *>(cellular_->adaptor()); |
| 85 | } |
| 86 | |
| 87 | virtual void TearDown() { |
| 88 | capability_->proxy_factory_ = NULL; |
| 89 | } |
| 90 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 91 | void InitProviderDB() { |
| 92 | provider_db_ = mobile_provider_open_db(kTestMobileProviderDBPath); |
| 93 | ASSERT_TRUE(provider_db_); |
| 94 | cellular_->provider_db_ = provider_db_; |
| 95 | } |
| 96 | |
| 97 | void SetService() { |
| 98 | cellular_->service_ = new CellularService( |
| 99 | &control_, &dispatcher_, &metrics_, NULL, cellular_); |
| 100 | } |
| 101 | |
| 102 | CellularCapabilityGSM *GetGsmCapability() { |
| 103 | return dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get()); |
| 104 | } |
| 105 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 106 | void InvokeEnable(bool enable, Error *error, |
| 107 | const ResultCallback &callback, int timeout) { |
| 108 | callback.Run(Error()); |
| 109 | } |
| 110 | void InvokeEnableFail(bool enable, Error *error, |
| 111 | const ResultCallback &callback, int timeout) { |
| 112 | callback.Run(Error(Error::kOperationFailed)); |
| 113 | } |
| 114 | void InvokeDisconnect(Error *error, const ResultCallback &callback, |
| 115 | int timeout) { |
| 116 | callback.Run(Error()); |
| 117 | } |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 118 | void InvokeDisconnectFail(Error *error, const ResultCallback &callback, |
| 119 | int timeout) { |
| 120 | callback.Run(Error(Error::kOperationFailed)); |
| 121 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 122 | void InvokeGetModemStatus(Error *error, |
| 123 | const DBusPropertyMapCallback &callback, |
| 124 | int timeout) { |
| 125 | DBusPropertiesMap props; |
| 126 | props["carrier"].writer().append_string(kTestCarrier); |
| 127 | props["unknown-property"].writer().append_string("irrelevant-value"); |
| 128 | callback.Run(props, Error()); |
| 129 | } |
| 130 | void InvokeGetModemInfo(Error *error, const ModemInfoCallback &callback, |
| 131 | int timeout) { |
| 132 | ModemHardwareInfo info; |
| 133 | info._1 = kManufacturer; |
| 134 | info._2 = kModelID; |
| 135 | info._3 = kHWRev; |
| 136 | callback.Run(info, Error()); |
| 137 | } |
| 138 | |
| 139 | MOCK_METHOD1(TestCallback, void(const Error &error)); |
| 140 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 141 | protected: |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 142 | static const char kTestMobileProviderDBPath[]; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 143 | static const char kTestCarrier[]; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 144 | static const char kManufacturer[]; |
| 145 | static const char kModelID[]; |
| 146 | static const char kHWRev[]; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 147 | |
| 148 | class TestProxyFactory : public ProxyFactory { |
| 149 | public: |
| 150 | explicit TestProxyFactory(CellularCapabilityTest *test) : test_(test) {} |
| 151 | |
| 152 | virtual ModemProxyInterface *CreateModemProxy( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 153 | const string &/*path*/, |
| 154 | const string &/*service*/) { |
| 155 | return test_->proxy_.release(); |
| 156 | } |
| 157 | |
| 158 | virtual ModemSimpleProxyInterface *CreateModemSimpleProxy( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 159 | const string &/*path*/, |
| 160 | const string &/*service*/) { |
| 161 | return test_->simple_proxy_.release(); |
| 162 | } |
| 163 | |
| 164 | virtual ModemCDMAProxyInterface *CreateModemCDMAProxy( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 165 | const string &/*path*/, |
| 166 | const string &/*service*/) { |
| 167 | return test_->cdma_proxy_.release(); |
| 168 | } |
| 169 | |
| 170 | virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 171 | const string &/*path*/, |
| 172 | const string &/*service*/) { |
| 173 | return test_->gsm_card_proxy_.release(); |
| 174 | } |
| 175 | |
| 176 | virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 177 | const string &/*path*/, |
| 178 | const string &/*service*/) { |
| 179 | return test_->gsm_network_proxy_.release(); |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | CellularCapabilityTest *test_; |
| 184 | }; |
| 185 | |
| 186 | void SetProxy() { |
| 187 | capability_->proxy_.reset(proxy_.release()); |
| 188 | } |
| 189 | |
| 190 | void SetSimpleProxy() { |
| 191 | capability_->simple_proxy_.reset(simple_proxy_.release()); |
| 192 | } |
| 193 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 194 | void SetGSMNetworkProxy() { |
| 195 | CellularCapabilityGSM *gsm_capability = |
| 196 | dynamic_cast<CellularCapabilityGSM *>(cellular_->capability_.get()); |
| 197 | gsm_capability->network_proxy_.reset(gsm_network_proxy_.release()); |
| 198 | } |
| 199 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 200 | void SetCellularType(Cellular::Type type) { |
| 201 | cellular_->InitCapability(type, &proxy_factory_); |
| 202 | capability_ = cellular_->capability_.get(); |
| 203 | } |
| 204 | |
| 205 | NiceMockControl control_; |
| 206 | EventDispatcher dispatcher_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 207 | MockMetrics metrics_; |
| 208 | MockGLib glib_; |
| 209 | MockManager manager_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 210 | CellularRefPtr cellular_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 211 | MockRTNLHandler rtnl_handler_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 212 | scoped_ptr<MockModemProxy> proxy_; |
| 213 | scoped_ptr<MockModemSimpleProxy> simple_proxy_; |
| 214 | scoped_ptr<MockModemCDMAProxy> cdma_proxy_; |
| 215 | scoped_ptr<MockModemGSMCardProxy> gsm_card_proxy_; |
| 216 | scoped_ptr<MockModemGSMNetworkProxy> gsm_network_proxy_; |
| 217 | TestProxyFactory proxy_factory_; |
| 218 | CellularCapability *capability_; // Owned by |cellular_|. |
| 219 | NiceMock<DeviceMockAdaptor> *device_adaptor_; // Owned by |cellular_|. |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 220 | mobile_provider_db *provider_db_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 221 | }; |
| 222 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 223 | const char CellularCapabilityTest::kTestMobileProviderDBPath[] = |
| 224 | "provider_db_unittest.bfd"; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 225 | const char CellularCapabilityTest::kTestCarrier[] = "The Cellular Carrier"; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 226 | const char CellularCapabilityTest::kManufacturer[] = "Company"; |
| 227 | const char CellularCapabilityTest::kModelID[] = "Gobi 2000"; |
| 228 | const char CellularCapabilityTest::kHWRev[] = "A00B1234"; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 229 | |
| 230 | TEST_F(CellularCapabilityTest, GetModemStatus) { |
| 231 | SetCellularType(Cellular::kTypeCDMA); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 232 | EXPECT_CALL(*simple_proxy_, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 233 | GetModemStatus(_, _, CellularCapability::kTimeoutDefault)). |
| 234 | WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemStatus)); |
| 235 | EXPECT_CALL(*this, TestCallback(IsSuccess())); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 236 | SetSimpleProxy(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 237 | ResultCallback callback = |
| 238 | Bind(&CellularCapabilityTest::TestCallback, Unretained(this)); |
| 239 | capability_->GetModemStatus(callback); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 240 | EXPECT_EQ(kTestCarrier, capability_->carrier_); |
| 241 | EXPECT_EQ(kTestCarrier, cellular_->home_provider_.GetName()); |
| 242 | } |
| 243 | |
| 244 | TEST_F(CellularCapabilityTest, GetModemInfo) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 245 | EXPECT_CALL(*proxy_, GetModemInfo(_, _, CellularCapability::kTimeoutDefault)) |
| 246 | .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeGetModemInfo)); |
| 247 | EXPECT_CALL(*this, TestCallback(IsSuccess())); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 248 | SetProxy(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 249 | ResultCallback callback = |
| 250 | Bind(&CellularCapabilityTest::TestCallback, Unretained(this)); |
| 251 | capability_->GetModemInfo(callback); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 252 | EXPECT_EQ(kManufacturer, capability_->manufacturer_); |
| 253 | EXPECT_EQ(kModelID, capability_->model_id_); |
| 254 | EXPECT_EQ(kHWRev, capability_->hardware_revision_); |
| 255 | } |
| 256 | |
| 257 | TEST_F(CellularCapabilityTest, EnableModemSucceed) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 258 | EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable)) |
| 259 | .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnable)); |
| 260 | EXPECT_CALL(*this, TestCallback(IsSuccess())); |
| 261 | ResultCallback callback = |
| 262 | Bind(&CellularCapabilityTest::TestCallback, Unretained(this)); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 263 | SetProxy(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 264 | capability_->EnableModem(callback); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | TEST_F(CellularCapabilityTest, EnableModemFail) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 268 | EXPECT_CALL(*proxy_, Enable(true, _, _, CellularCapability::kTimeoutEnable)) |
| 269 | .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeEnableFail)); |
| 270 | EXPECT_CALL(*this, TestCallback(IsFailure())); |
| 271 | ResultCallback callback = |
| 272 | Bind(&CellularCapabilityTest::TestCallback, Unretained(this)); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 273 | SetProxy(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 274 | capability_->EnableModem(callback); |
| 275 | } |
| 276 | |
| 277 | TEST_F(CellularCapabilityTest, FinishEnable) { |
| 278 | EXPECT_CALL(*gsm_network_proxy_, |
| 279 | GetRegistrationInfo(NULL, _, |
| 280 | CellularCapability::kTimeoutDefault)); |
| 281 | EXPECT_CALL(*gsm_network_proxy_, |
| 282 | GetSignalQuality(NULL, _, CellularCapability::kTimeoutDefault)); |
| 283 | EXPECT_CALL(*this, TestCallback(IsSuccess())); |
| 284 | SetGSMNetworkProxy(); |
| 285 | capability_->FinishEnable( |
| 286 | Bind(&CellularCapabilityTest::TestCallback, Unretained(this))); |
| 287 | } |
| 288 | |
| 289 | TEST_F(CellularCapabilityTest, UnsupportedOperation) { |
| 290 | Error error; |
| 291 | EXPECT_CALL(*this, TestCallback(IsSuccess())).Times(0); |
| 292 | capability_->CellularCapability::Scan(&error, |
| 293 | Bind(&CellularCapabilityTest::TestCallback, Unretained(this))); |
| 294 | EXPECT_TRUE(error.IsFailure()); |
| 295 | EXPECT_EQ(Error::kNotSupported, error.type()); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 298 | TEST_F(CellularCapabilityTest, AllowRoaming) { |
| 299 | EXPECT_FALSE(capability_->GetAllowRoaming(NULL)); |
| 300 | capability_->SetAllowRoaming(false, NULL); |
| 301 | EXPECT_FALSE(capability_->GetAllowRoaming(NULL)); |
| 302 | |
| 303 | { |
| 304 | InSequence seq; |
| 305 | EXPECT_CALL(*device_adaptor_, EmitBoolChanged( |
| 306 | flimflam::kCellularAllowRoamingProperty, true)); |
| 307 | EXPECT_CALL(*device_adaptor_, EmitBoolChanged( |
| 308 | flimflam::kCellularAllowRoamingProperty, false)); |
| 309 | } |
| 310 | |
| 311 | cellular_->state_ = Cellular::kStateConnected; |
| 312 | dynamic_cast<CellularCapabilityGSM *>(capability_)->registration_state_ = |
| 313 | MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING; |
| 314 | capability_->SetAllowRoaming(true, NULL); |
| 315 | EXPECT_TRUE(capability_->GetAllowRoaming(NULL)); |
| 316 | EXPECT_EQ(Cellular::kStateConnected, cellular_->state_); |
| 317 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 318 | EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault)) |
| 319 | .WillOnce(Invoke(this, &CellularCapabilityTest::InvokeDisconnect)); |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 320 | SetProxy(); |
| 321 | cellular_->state_ = Cellular::kStateConnected; |
| 322 | capability_->SetAllowRoaming(false, NULL); |
| 323 | EXPECT_FALSE(capability_->GetAllowRoaming(NULL)); |
| 324 | EXPECT_EQ(Cellular::kStateRegistered, cellular_->state_); |
| 325 | } |
| 326 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 327 | MATCHER_P(HasApn, apn, "") { |
| 328 | DBusPropertiesMap::const_iterator it = arg.find(flimflam::kApnProperty); |
| 329 | return it != arg.end() && apn == it->second.reader().get_string(); |
| 330 | } |
| 331 | |
| 332 | MATCHER(HasNoApn, "") { |
| 333 | return arg.find(flimflam::kApnProperty) == arg.end(); |
| 334 | } |
| 335 | |
| 336 | TEST_F(CellularCapabilityTest, TryApns) { |
| 337 | static const string kLastGoodApn("remembered.apn"); |
| 338 | static const string kSuppliedApn("my.apn"); |
| 339 | static const string kTmobileApn1("epc.tmobile.com"); |
| 340 | static const string kTmobileApn2("wap.voicestream.com"); |
| 341 | static const string kTmobileApn3("internet2.voicestream.com"); |
| 342 | static const string kTmobileApn4("internet3.voicestream.com"); |
| 343 | |
| 344 | using testing::InSequence; |
| 345 | { |
| 346 | InSequence dummy; |
| 347 | EXPECT_CALL(*simple_proxy_, Connect(HasApn(kLastGoodApn), _, _, _)); |
| 348 | EXPECT_CALL(*simple_proxy_, Connect(HasApn(kSuppliedApn), _, _, _)); |
| 349 | EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn1), _, _, _)); |
| 350 | EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn2), _, _, _)); |
| 351 | EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn3), _, _, _)); |
| 352 | EXPECT_CALL(*simple_proxy_, Connect(HasApn(kTmobileApn4), _, _, _)); |
| 353 | EXPECT_CALL(*simple_proxy_, Connect(HasNoApn(), _, _, _)); |
| 354 | } |
| 355 | CellularCapabilityGSM *gsm_capability = GetGsmCapability(); |
| 356 | SetService(); |
| 357 | gsm_capability->imsi_ = "310240123456789"; |
| 358 | InitProviderDB(); |
| 359 | gsm_capability->SetHomeProvider(); |
| 360 | ProfileRefPtr profile(new NiceMock<MockProfile>( |
| 361 | &control_, reinterpret_cast<Manager *>(NULL))); |
| 362 | cellular_->service()->set_profile(profile); |
| 363 | |
| 364 | Error error; |
| 365 | Stringmap apn_info; |
| 366 | DBusPropertiesMap props; |
| 367 | apn_info[flimflam::kApnProperty] = kSuppliedApn; |
| 368 | cellular_->service()->SetApn(apn_info, &error); |
| 369 | |
| 370 | apn_info.clear(); |
| 371 | apn_info[flimflam::kApnProperty] = kLastGoodApn; |
| 372 | cellular_->service()->SetLastGoodApn(apn_info); |
| 373 | |
| 374 | capability_->SetupConnectProperties(&props); |
| 375 | // We expect the list to contain the last good APN, plus |
| 376 | // the user-supplied APN, plus the 4 APNs from the mobile |
| 377 | // provider info database. |
| 378 | EXPECT_EQ(6, gsm_capability->apn_try_list_.size()); |
| 379 | EXPECT_FALSE(props.find(flimflam::kApnProperty) == props.end()); |
| 380 | EXPECT_EQ(kLastGoodApn, props[flimflam::kApnProperty].reader().get_string()); |
| 381 | |
| 382 | SetSimpleProxy(); |
| 383 | capability_->Connect(props, &error, ResultCallback()); |
| 384 | Error cerror(Error::kInvalidApn); |
| 385 | capability_->OnConnectReply(ResultCallback(), cerror); |
| 386 | EXPECT_EQ(5, gsm_capability->apn_try_list_.size()); |
| 387 | capability_->OnConnectReply(ResultCallback(), cerror); |
| 388 | EXPECT_EQ(4, gsm_capability->apn_try_list_.size()); |
| 389 | capability_->OnConnectReply(ResultCallback(), cerror); |
| 390 | EXPECT_EQ(3, gsm_capability->apn_try_list_.size()); |
| 391 | capability_->OnConnectReply(ResultCallback(), cerror); |
| 392 | EXPECT_EQ(2, gsm_capability->apn_try_list_.size()); |
| 393 | capability_->OnConnectReply(ResultCallback(), cerror); |
| 394 | EXPECT_EQ(1, gsm_capability->apn_try_list_.size()); |
| 395 | capability_->OnConnectReply(ResultCallback(), cerror); |
| 396 | EXPECT_EQ(0, gsm_capability->apn_try_list_.size()); |
| 397 | } |
| 398 | |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 399 | TEST_F(CellularCapabilityTest, StopModemDisconnectSuccess) { |
| 400 | EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault)) |
| 401 | .WillOnce(Invoke(this, |
| 402 | &CellularCapabilityTest::InvokeDisconnect)); |
| 403 | EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutDefault)) |
| 404 | .WillOnce(Invoke(this, |
| 405 | &CellularCapabilityTest::InvokeEnable)); |
| 406 | EXPECT_CALL(*this, TestCallback(IsSuccess())); |
| 407 | SetProxy(); |
| 408 | |
| 409 | Error error; |
| 410 | capability_->StopModem( |
| 411 | &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this))); |
| 412 | dispatcher_.DispatchPendingEvents(); |
| 413 | } |
| 414 | |
| 415 | TEST_F(CellularCapabilityTest, StopModemDisconnectFail) { |
| 416 | EXPECT_CALL(*proxy_, Disconnect(_, _, CellularCapability::kTimeoutDefault)) |
| 417 | .WillOnce(Invoke(this, |
| 418 | &CellularCapabilityTest::InvokeDisconnectFail)); |
| 419 | EXPECT_CALL(*proxy_, Enable(_, _, _, CellularCapability::kTimeoutDefault)) |
| 420 | .WillOnce(Invoke(this, |
| 421 | &CellularCapabilityTest::InvokeEnable)); |
| 422 | EXPECT_CALL(*this, TestCallback(IsSuccess())); |
| 423 | SetProxy(); |
| 424 | |
| 425 | Error error; |
| 426 | capability_->StopModem( |
| 427 | &error, Bind(&CellularCapabilityTest::TestCallback, Unretained(this))); |
| 428 | dispatcher_.DispatchPendingEvents(); |
| 429 | } |
| 430 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 431 | } // namespace shill |