blob: d8d05ec787a78dd7ff9b71c07ba8db5be19d1920 [file] [log] [blame]
Arman Ugurayf4c61812013-01-10 18:58:39 -08001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
Jason Glasgowef965562012-04-10 16:12:35 -04002// 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_universal.h"
6
Nathan Williams4b7c2a82012-04-13 15:19:47 -04007#include <string>
8#include <vector>
9
Jason Glasgowef965562012-04-10 16:12:35 -040010#include <base/bind.h>
Darin Petkova4ca3c32012-08-17 16:05:24 +020011#include <base/stringprintf.h>
Arman Ugurayc9533572013-01-22 17:34:20 -080012#include <base/string_util.h>
Jason Glasgowef965562012-04-10 16:12:35 -040013#include <chromeos/dbus/service_constants.h>
Arman Uguray1361c032013-02-11 17:53:39 -080014#include <gmock/gmock.h>
Jason Glasgowef965562012-04-10 16:12:35 -040015#include <gtest/gtest.h>
16#include <mobile_provider.h>
Ben Chan5c853ef2012-10-05 00:05:37 -070017#include <ModemManager/ModemManager.h>
Jason Glasgowef965562012-04-10 16:12:35 -040018
19#include "shill/cellular.h"
20#include "shill/cellular_service.h"
Jason Glasgowaf583282012-04-18 15:18:22 -040021#include "shill/dbus_adaptor.h"
Jason Glasgowef965562012-04-10 16:12:35 -040022#include "shill/error.h"
23#include "shill/event_dispatcher.h"
24#include "shill/mock_adaptors.h"
Ben Chan15786032012-11-04 21:28:02 -080025#include "shill/mock_cellular_operator_info.h"
Jason Glasgow14521872012-05-07 19:12:15 -040026#include "shill/mock_cellular_service.h"
Jason Glasgowaf583282012-04-18 15:18:22 -040027#include "shill/mock_dbus_properties_proxy.h"
Arman Uguray1361c032013-02-11 17:53:39 -080028#include "shill/mock_event_dispatcher.h"
Arman Uguray6e5639f2012-11-15 20:30:19 -080029#include "shill/mock_mm1_bearer_proxy.h"
Jason Glasgowef965562012-04-10 16:12:35 -040030#include "shill/mock_mm1_modem_modem3gpp_proxy.h"
31#include "shill/mock_mm1_modem_modemcdma_proxy.h"
32#include "shill/mock_mm1_modem_proxy.h"
33#include "shill/mock_mm1_modem_simple_proxy.h"
34#include "shill/mock_mm1_sim_proxy.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070035#include "shill/mock_modem_info.h"
Arman Uguray41cc6342013-03-29 16:34:39 -070036#include "shill/mock_pending_activation_store.h"
Jason Glasgowef965562012-04-10 16:12:35 -040037#include "shill/mock_profile.h"
38#include "shill/mock_rtnl_handler.h"
Jason Glasgowef965562012-04-10 16:12:35 -040039#include "shill/proxy_factory.h"
40
41using base::Bind;
Darin Petkova4ca3c32012-08-17 16:05:24 +020042using base::StringPrintf;
Jason Glasgowef965562012-04-10 16:12:35 -040043using base::Unretained;
44using std::string;
Nathan Williams4b7c2a82012-04-13 15:19:47 -040045using std::vector;
Jason Glasgowef965562012-04-10 16:12:35 -040046using testing::InSequence;
Ben Chan6d0d1e72012-11-06 21:19:28 -080047using testing::Invoke;
Arman Uguray1361c032013-02-11 17:53:39 -080048using testing::InvokeWithoutArgs;
Gary Moraine285a842012-08-15 08:23:57 -070049using testing::Mock;
Jason Glasgowef965562012-04-10 16:12:35 -040050using testing::NiceMock;
51using testing::Return;
Jason Glasgowcd0349c2012-05-03 23:32:15 -040052using testing::SaveArg;
Jason Glasgowef965562012-04-10 16:12:35 -040053using testing::_;
54
55namespace shill {
56
57MATCHER(IsSuccess, "") {
58 return arg.IsSuccess();
59}
60MATCHER(IsFailure, "") {
61 return arg.IsFailure();
62}
Jason Glasgow14521872012-05-07 19:12:15 -040063MATCHER_P(HasApn, expected_apn, "") {
64 string apn;
65 return (DBusProperties::GetString(arg,
66 CellularCapabilityUniversal::kConnectApn,
67 &apn) &&
68 apn == expected_apn);
69}
Jason Glasgowef965562012-04-10 16:12:35 -040070
Arman Uguray1361c032013-02-11 17:53:39 -080071class CellularCapabilityUniversalTest : public testing::TestWithParam<string> {
Jason Glasgowef965562012-04-10 16:12:35 -040072 public:
Arman Uguray1361c032013-02-11 17:53:39 -080073 CellularCapabilityUniversalTest(EventDispatcher *dispatcher)
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070074 : modem_info_(NULL, dispatcher, NULL, NULL, NULL),
Arman Uguray6e5639f2012-11-15 20:30:19 -080075 bearer_proxy_(new mm1::MockBearerProxy()),
Ben Chan3ecdf822012-08-06 12:29:23 -070076 modem_3gpp_proxy_(new mm1::MockModemModem3gppProxy()),
77 modem_cdma_proxy_(new mm1::MockModemModemCdmaProxy()),
78 modem_proxy_(new mm1::MockModemProxy()),
79 modem_simple_proxy_(new mm1::MockModemSimpleProxy()),
80 sim_proxy_(new mm1::MockSimProxy()),
81 properties_proxy_(new MockDBusPropertiesProxy()),
82 proxy_factory_(this),
83 capability_(NULL),
84 device_adaptor_(NULL),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070085 cellular_(new Cellular(&modem_info_,
Jason Glasgowef965562012-04-10 16:12:35 -040086 "",
Arman Ugurayc9533572013-01-22 17:34:20 -080087 kMachineAddress,
Jason Glasgowef965562012-04-10 16:12:35 -040088 0,
89 Cellular::kTypeUniversal,
90 "",
91 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040092 "",
Ben Chan3ecdf822012-08-06 12:29:23 -070093 &proxy_factory_)),
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070094 service_(new MockCellularService(&modem_info_, cellular_)) {
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070095 modem_info_.metrics()->RegisterDevice(cellular_->interface_index(),
96 Technology::kCellular);
Thieu Lece4483e2013-01-23 15:12:03 -080097 }
Jason Glasgowef965562012-04-10 16:12:35 -040098
99 virtual ~CellularCapabilityUniversalTest() {
100 cellular_->service_ = NULL;
101 capability_ = NULL;
102 device_adaptor_ = NULL;
103 }
104
105 virtual void SetUp() {
106 capability_ = dynamic_cast<CellularCapabilityUniversal *>(
107 cellular_->capability_.get());
Jason Glasgowef965562012-04-10 16:12:35 -0400108 device_adaptor_ =
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700109 dynamic_cast<DeviceMockAdaptor *>(cellular_->adaptor());
Jason Glasgow14521872012-05-07 19:12:15 -0400110 cellular_->service_ = service_;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700111
112 // kStateUnknown leads to minimal extra work in maintaining
113 // activation state.
Arman Uguray41cc6342013-03-29 16:34:39 -0700114 ON_CALL(*modem_info_.mock_pending_activation_store(),
115 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
116 .WillByDefault(Return(PendingActivationStore::kStateUnknown));
Jason Glasgowef965562012-04-10 16:12:35 -0400117 }
118
119 virtual void TearDown() {
120 capability_->proxy_factory_ = NULL;
121 }
122
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700123 void InitProviderDB() {
124 modem_info_.SetProviderDB(kTestMobileProviderDBPath);
125 }
126
Darin Petkova4ca3c32012-08-17 16:05:24 +0200127 void SetService() {
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -0700128 cellular_->service_ = new CellularService(&modem_info_, cellular_);
Jason Glasgow4380f0d2012-05-03 18:05:04 -0400129 }
130
Jason Glasgowef965562012-04-10 16:12:35 -0400131 void InvokeEnable(bool enable, Error *error,
132 const ResultCallback &callback, int timeout) {
133 callback.Run(Error());
134 }
135 void InvokeEnableFail(bool enable, Error *error,
136 const ResultCallback &callback, int timeout) {
137 callback.Run(Error(Error::kOperationFailed));
138 }
Jason Glasgowaf583282012-04-18 15:18:22 -0400139 void InvokeRegister(const string &operator_id, Error *error,
140 const ResultCallback &callback, int timeout) {
141 callback.Run(Error());
142 }
143
Gary Morainceba6aa2012-05-03 10:28:26 -0700144 void InvokeScan(Error *error, const DBusPropertyMapsCallback &callback,
145 int timeout) {
146 callback.Run(CellularCapabilityUniversal::ScanResults(), Error());
147 }
Jason Glasgowcd0349c2012-05-03 23:32:15 -0400148 void ScanError(Error *error, const DBusPropertyMapsCallback &callback,
149 int timeout) {
150 error->Populate(Error::kOperationFailed);
151 }
Gary Morainceba6aa2012-05-03 10:28:26 -0700152
Arman Uguray1361c032013-02-11 17:53:39 -0800153 bool InvokeScanningOrSearchingTimeout() {
154 capability_->OnScanningOrSearchingTimeout();
155 return true;
156 }
157
Gary Morainceba6aa2012-05-03 10:28:26 -0700158 void Set3gppProxy() {
159 capability_->modem_3gpp_proxy_.reset(modem_3gpp_proxy_.release());
160 }
161
Jason Glasgow14521872012-05-07 19:12:15 -0400162 void SetSimpleProxy() {
163 capability_->modem_simple_proxy_.reset(modem_simple_proxy_.release());
164 }
165
Thieu Le3d275392012-07-20 15:32:58 -0700166 void ReleaseCapabilityProxies() {
167 capability_->ReleaseProxies();
168 }
169
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700170 void SetRegistrationDroppedUpdateTimeout(int64 timeout_milliseconds) {
171 capability_->registration_dropped_update_timeout_milliseconds_ =
172 timeout_milliseconds;
173 }
174
Jason Glasgowef965562012-04-10 16:12:35 -0400175 MOCK_METHOD1(TestCallback, void(const Error &error));
176
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700177 MOCK_METHOD0(DummyCallback, void(void));
178
179 void SetMockRegistrationDroppedUpdateCallback() {
180 capability_->registration_dropped_update_callback_.Reset(
181 Bind(&CellularCapabilityUniversalTest::DummyCallback,
182 Unretained(this)));
183 }
184
Jason Glasgowef965562012-04-10 16:12:35 -0400185 protected:
Arman Uguray6e5639f2012-11-15 20:30:19 -0800186 static const char kActiveBearerPathPrefix[];
Jason Glasgowef965562012-04-10 16:12:35 -0400187 static const char kImei[];
Arman Uguray6e5639f2012-11-15 20:30:19 -0800188 static const char kInactiveBearerPathPrefix[];
Arman Ugurayc9533572013-01-22 17:34:20 -0800189 static const char kMachineAddress[];
Jason Glasgowaf583282012-04-18 15:18:22 -0400190 static const char kSimPath[];
191 static const uint32 kAccessTechnologies;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700192 static const char kTestMobileProviderDBPath[];
Jason Glasgowef965562012-04-10 16:12:35 -0400193
194 class TestProxyFactory : public ProxyFactory {
195 public:
196 explicit TestProxyFactory(CellularCapabilityUniversalTest *test) :
mukesh agrawal9da07772013-05-15 14:15:17 -0700197 test_(test) {
198 ::DBus::Variant ip_method_dhcp;
199 ip_method_dhcp.writer().append_uint32(MM_BEARER_IP_METHOD_DHCP);
200 bearer_ip4config_dhcp_[
201 CellularCapabilityUniversal::kIpConfigPropertyMethod] =
202 ip_method_dhcp;
203 }
Jason Glasgowef965562012-04-10 16:12:35 -0400204
Arman Uguray6e5639f2012-11-15 20:30:19 -0800205 virtual mm1::BearerProxyInterface *CreateBearerProxy(
206 const std::string &path,
207 const std::string &/*service*/) {
208 mm1::MockBearerProxy *bearer_proxy = test_->bearer_proxy_.release();
209 if (path.find(kActiveBearerPathPrefix) != std::string::npos)
210 ON_CALL(*bearer_proxy, Connected()).WillByDefault(Return(true));
211 else
212 ON_CALL(*bearer_proxy, Connected()).WillByDefault(Return(false));
mukesh agrawal9da07772013-05-15 14:15:17 -0700213 ON_CALL(*bearer_proxy, Ip4Config()).WillByDefault(Return(
214 bearer_ip4config_dhcp_));
Arman Uguray6e5639f2012-11-15 20:30:19 -0800215 test_->bearer_proxy_.reset(new mm1::MockBearerProxy());
216 return bearer_proxy;
217 }
218
Jason Glasgowef965562012-04-10 16:12:35 -0400219 virtual mm1::ModemModem3gppProxyInterface *CreateMM1ModemModem3gppProxy(
Arman Uguray6e5639f2012-11-15 20:30:19 -0800220 const std::string &/*path*/,
221 const std::string &/*service*/) {
Jason Glasgowef965562012-04-10 16:12:35 -0400222 return test_->modem_3gpp_proxy_.release();
223 }
224
225 virtual mm1::ModemModemCdmaProxyInterface *CreateMM1ModemModemCdmaProxy(
Arman Uguray6e5639f2012-11-15 20:30:19 -0800226 const std::string &/*path*/,
227 const std::string &/*service*/) {
Jason Glasgowef965562012-04-10 16:12:35 -0400228 return test_->modem_cdma_proxy_.release();
229 }
230
231 virtual mm1::ModemProxyInterface *CreateMM1ModemProxy(
Arman Uguray6e5639f2012-11-15 20:30:19 -0800232 const std::string &/*path*/,
233 const std::string &/*service*/) {
Jason Glasgowef965562012-04-10 16:12:35 -0400234 return test_->modem_proxy_.release();
235 }
236
237 virtual mm1::ModemSimpleProxyInterface *CreateMM1ModemSimpleProxy(
Arman Uguray6e5639f2012-11-15 20:30:19 -0800238 const std::string &/*path*/,
239 const std::string &/*service*/) {
Jason Glasgowef965562012-04-10 16:12:35 -0400240 return test_->modem_simple_proxy_.release();
241 }
242
243 virtual mm1::SimProxyInterface *CreateSimProxy(
Arman Uguray6e5639f2012-11-15 20:30:19 -0800244 const std::string &/*path*/,
245 const std::string &/*service*/) {
Arman Uguray6552f8c2013-02-12 15:33:18 -0800246 mm1::MockSimProxy *sim_proxy = test_->sim_proxy_.release();
247 test_->sim_proxy_.reset(new mm1::MockSimProxy());
248 return sim_proxy;
Jason Glasgowef965562012-04-10 16:12:35 -0400249 }
Jason Glasgowaf583282012-04-18 15:18:22 -0400250 virtual DBusPropertiesProxyInterface *CreateDBusPropertiesProxy(
Arman Uguray6e5639f2012-11-15 20:30:19 -0800251 const std::string &/*path*/,
252 const std::string &/*service*/) {
Arman Uguray6552f8c2013-02-12 15:33:18 -0800253 MockDBusPropertiesProxy *properties_proxy =
254 test_->properties_proxy_.release();
255 test_->properties_proxy_.reset(new MockDBusPropertiesProxy());
256 return properties_proxy;
Jason Glasgowaf583282012-04-18 15:18:22 -0400257 }
Jason Glasgowef965562012-04-10 16:12:35 -0400258
259 private:
260 CellularCapabilityUniversalTest *test_;
mukesh agrawal9da07772013-05-15 14:15:17 -0700261 DBusPropertiesMap bearer_ip4config_dhcp_;
Jason Glasgowef965562012-04-10 16:12:35 -0400262 };
263
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700264 MockModemInfo modem_info_;
Arman Uguray6e5639f2012-11-15 20:30:19 -0800265 scoped_ptr<mm1::MockBearerProxy> bearer_proxy_;
Jason Glasgowef965562012-04-10 16:12:35 -0400266 scoped_ptr<mm1::MockModemModem3gppProxy> modem_3gpp_proxy_;
267 scoped_ptr<mm1::MockModemModemCdmaProxy> modem_cdma_proxy_;
268 scoped_ptr<mm1::MockModemProxy> modem_proxy_;
269 scoped_ptr<mm1::MockModemSimpleProxy> modem_simple_proxy_;
270 scoped_ptr<mm1::MockSimProxy> sim_proxy_;
Jason Glasgowaf583282012-04-18 15:18:22 -0400271 scoped_ptr<MockDBusPropertiesProxy> properties_proxy_;
Jason Glasgowef965562012-04-10 16:12:35 -0400272 TestProxyFactory proxy_factory_;
273 CellularCapabilityUniversal *capability_; // Owned by |cellular_|.
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700274 DeviceMockAdaptor *device_adaptor_; // Owned by |cellular_|.
Ben Chan3ecdf822012-08-06 12:29:23 -0700275 CellularRefPtr cellular_;
276 MockCellularService *service_; // owned by cellular_
Jason Glasgowcd0349c2012-05-03 23:32:15 -0400277 DBusPropertyMapsCallback scan_callback_; // saved for testing scan operations
Jason Glasgow14521872012-05-07 19:12:15 -0400278 DBusPathCallback connect_callback_; // saved for testing connect operations
Jason Glasgowef965562012-04-10 16:12:35 -0400279};
280
Arman Uguray1361c032013-02-11 17:53:39 -0800281// Most of our tests involve using a real EventDispatcher object.
282class CellularCapabilityUniversalMainTest
283 : public CellularCapabilityUniversalTest {
284 public:
285 CellularCapabilityUniversalMainTest() :
286 CellularCapabilityUniversalTest(&dispatcher_) {}
287
288 protected:
289 EventDispatcher dispatcher_;
290};
291
292// Tests that involve timers will (or may) use a mock of the event dispatcher
293// instead of a real one.
294class CellularCapabilityUniversalTimerTest
295 : public CellularCapabilityUniversalTest {
296 public:
297 CellularCapabilityUniversalTimerTest()
298 : CellularCapabilityUniversalTest(&mock_dispatcher_) {}
299
300 protected:
301 ::testing::StrictMock<MockEventDispatcher> mock_dispatcher_;
302};
303
Arman Uguray6e5639f2012-11-15 20:30:19 -0800304const char CellularCapabilityUniversalTest::kActiveBearerPathPrefix[] =
305 "/bearer/active";
Jason Glasgowef965562012-04-10 16:12:35 -0400306const char CellularCapabilityUniversalTest::kImei[] = "999911110000";
Arman Uguray6e5639f2012-11-15 20:30:19 -0800307const char CellularCapabilityUniversalTest::kInactiveBearerPathPrefix[] =
308 "/bearer/inactive";
Arman Ugurayc9533572013-01-22 17:34:20 -0800309const char CellularCapabilityUniversalTest::kMachineAddress[] =
310 "TestMachineAddress";
Jason Glasgowaf583282012-04-18 15:18:22 -0400311const char CellularCapabilityUniversalTest::kSimPath[] = "/foo/sim";
312const uint32 CellularCapabilityUniversalTest::kAccessTechnologies =
313 MM_MODEM_ACCESS_TECHNOLOGY_LTE |
314 MM_MODEM_ACCESS_TECHNOLOGY_HSPA_PLUS;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700315const char CellularCapabilityUniversalTest::kTestMobileProviderDBPath[] =
316 "provider_db_unittest.bfd";
Jason Glasgowef965562012-04-10 16:12:35 -0400317
Arman Uguray1361c032013-02-11 17:53:39 -0800318TEST_F(CellularCapabilityUniversalMainTest, StartModem) {
Jason Glasgowaf583282012-04-18 15:18:22 -0400319 // Set up mock modem properties
320 DBusPropertiesMap modem_properties;
321 string operator_name = "TestOperator";
322 string operator_code = "001400";
323
324 modem_properties[MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES].
325 writer().append_uint32(kAccessTechnologies);
326
327 ::DBus::Variant v;
328 ::DBus::MessageIter writer = v.writer();
Jason Glasgowef965562012-04-10 16:12:35 -0400329 ::DBus::Struct< uint32_t, bool > quality;
330 quality._1 = 90;
331 quality._2 = true;
Jason Glasgowaf583282012-04-18 15:18:22 -0400332 writer << quality;
333 modem_properties[MM_MODEM_PROPERTY_SIGNALQUALITY] = v;
334
335 // Set up mock modem 3gpp properties
336 DBusPropertiesMap modem3gpp_properties;
337 modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_ENABLEDFACILITYLOCKS].
338 writer().append_uint32(0);
339 modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_IMEI].
340 writer().append_string(kImei);
341
342 EXPECT_CALL(*modem_proxy_,
343 Enable(true, _, _, CellularCapability::kTimeoutEnable))
344 .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeEnable));
345 EXPECT_CALL(*properties_proxy_,
346 GetAll(MM_DBUS_INTERFACE_MODEM))
347 .WillOnce(Return(modem_properties));
348 EXPECT_CALL(*properties_proxy_,
349 GetAll(MM_DBUS_INTERFACE_MODEM_MODEM3GPP))
350 .WillOnce(Return(modem3gpp_properties));
Jason Glasgowef965562012-04-10 16:12:35 -0400351
Gary Moraine285a842012-08-15 08:23:57 -0700352 // Let the modem report that it is initializing. StartModem() should defer
353 // enabling the modem until its state changes to disabled.
354 EXPECT_CALL(*modem_proxy_, State())
355 .WillOnce(Return(Cellular::kModemStateInitializing));
356
Jason Glasgowef965562012-04-10 16:12:35 -0400357 // After setup we lose pointers to the proxies, so it is hard to set
358 // expectations.
359 SetUp();
360
361 Error error;
Gary Moraine285a842012-08-15 08:23:57 -0700362 EXPECT_CALL(*this, TestCallback(_)).Times(0);
Jason Glasgowef965562012-04-10 16:12:35 -0400363 ResultCallback callback =
364 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
365 capability_->StartModem(&error, callback);
Gary Moraine285a842012-08-15 08:23:57 -0700366
Arman Uguray6e5639f2012-11-15 20:30:19 -0800367 // Verify that the modem has not been enabled.
Gary Moraine285a842012-08-15 08:23:57 -0700368 EXPECT_TRUE(capability_->imei_.empty());
369 EXPECT_EQ(0, capability_->access_technologies_);
370 Mock::VerifyAndClearExpectations(this);
371
372 // Change the state to kModemStateDisabling and verify that it still has not
Arman Uguray6e5639f2012-11-15 20:30:19 -0800373 // been enabled.
Gary Moraine285a842012-08-15 08:23:57 -0700374 EXPECT_CALL(*this, TestCallback(_)).Times(0);
375 capability_->OnModemStateChangedSignal(Cellular::kModemStateInitializing,
376 Cellular::kModemStateDisabling, 0);
377 EXPECT_TRUE(capability_->imei_.empty());
378 EXPECT_EQ(0, capability_->access_technologies_);
379 Mock::VerifyAndClearExpectations(this);
380
381 // Change the state of the modem to disabled and verify that it gets enabled.
382 EXPECT_CALL(*this, TestCallback(IsSuccess()));
383 capability_->OnModemStateChangedSignal(Cellular::kModemStateDisabling,
384 Cellular::kModemStateDisabled, 0);
Jason Glasgowef965562012-04-10 16:12:35 -0400385 EXPECT_TRUE(error.IsSuccess());
Jason Glasgowaf583282012-04-18 15:18:22 -0400386 EXPECT_EQ(kImei, capability_->imei_);
387 EXPECT_EQ(kAccessTechnologies, capability_->access_technologies_);
Jason Glasgowef965562012-04-10 16:12:35 -0400388}
389
Arman Uguray1361c032013-02-11 17:53:39 -0800390TEST_F(CellularCapabilityUniversalMainTest, StartModemFail) {
Gary Moraine285a842012-08-15 08:23:57 -0700391 EXPECT_CALL(*modem_proxy_, State())
392 .WillOnce(Return(Cellular::kModemStateDisabled));
Jason Glasgowef965562012-04-10 16:12:35 -0400393 EXPECT_CALL(*modem_proxy_,
394 Enable(true, _, _, CellularCapability::kTimeoutEnable))
395 .WillOnce(
396 Invoke(this, &CellularCapabilityUniversalTest::InvokeEnableFail));
397 EXPECT_CALL(*this, TestCallback(IsFailure()));
398 ResultCallback callback =
399 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
400 SetUp();
401
402 Error error;
403 capability_->StartModem(&error, callback);
Thieu Lee3b36592012-08-30 17:50:26 -0700404 EXPECT_TRUE(error.IsOngoing());
Jason Glasgowef965562012-04-10 16:12:35 -0400405}
406
Thieu Leb9c05e02013-03-04 14:09:32 -0800407TEST_F(CellularCapabilityUniversalMainTest, StartModemAlreadyEnabled) {
408 EXPECT_CALL(*modem_proxy_, State())
409 .WillOnce(Return(Cellular::kModemStateEnabled));
410 SetUp();
411 capability_->cellular()->modem_state_ = Cellular::kModemStateConnected;
412
413 // Make sure the call to StartModem() doesn't attempt to complete the
414 // request synchronously, else it will crash DBus-C++.
415 Error error(Error::kOperationInitiated);
416 capability_->StartModem(&error, ResultCallback());
417 EXPECT_TRUE(error.IsOngoing());
418}
419
Arman Uguray1361c032013-02-11 17:53:39 -0800420TEST_F(CellularCapabilityUniversalMainTest, StopModem) {
Jason Glasgow02401cc2012-05-16 10:35:37 -0400421 // Save pointers to proxies before they are lost by the call to InitProxies
422 mm1::MockModemProxy *modem_proxy = modem_proxy_.get();
423 SetUp();
424 EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
425 capability_->InitProxies();
426
427 Error error;
428 ResultCallback callback =
429 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
430 capability_->StopModem(&error, callback);
431 EXPECT_TRUE(error.IsSuccess());
432
433 ResultCallback disable_callback;
434 EXPECT_CALL(*modem_proxy,
435 Enable(false, _, _, CellularCapability::kTimeoutEnable))
436 .WillOnce(SaveArg<2>(&disable_callback));
437 dispatcher_.DispatchPendingEvents();
438
Arman Ugurayee464d32013-02-13 17:14:36 -0800439 ResultCallback set_power_state_callback;
Thieu Le2cac2942013-03-05 18:41:08 -0800440 EXPECT_CALL(
441 *modem_proxy,
442 SetPowerState(
443 MM_MODEM_POWER_STATE_LOW, _, _,
444 CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
Arman Ugurayee464d32013-02-13 17:14:36 -0800445 .WillOnce(SaveArg<2>(&set_power_state_callback));
Jason Glasgow02401cc2012-05-16 10:35:37 -0400446 disable_callback.Run(Error(Error::kSuccess));
Arman Ugurayee464d32013-02-13 17:14:36 -0800447
448 EXPECT_CALL(*this, TestCallback(IsSuccess()));
449 set_power_state_callback.Run(Error(Error::kSuccess));
Prathmesh Prabhub5fde532013-03-19 17:36:09 -0700450 Mock::VerifyAndClearExpectations(this);
Arman Ugurayee464d32013-02-13 17:14:36 -0800451
452 // TestCallback should get called with success even if the power state
453 // callback gets called with an error
454 EXPECT_CALL(*this, TestCallback(IsSuccess()));
455 set_power_state_callback.Run(Error(Error::kOperationFailed));
Jason Glasgow02401cc2012-05-16 10:35:37 -0400456}
457
Arman Uguray1361c032013-02-11 17:53:39 -0800458TEST_F(CellularCapabilityUniversalMainTest, StopModemConnected) {
Jason Glasgow02401cc2012-05-16 10:35:37 -0400459 // Save pointers to proxies before they are lost by the call to InitProxies
460 mm1::MockModemProxy *modem_proxy = modem_proxy_.get();
461 mm1::MockModemSimpleProxy *modem_simple_proxy = modem_simple_proxy_.get();
462 SetUp();
463 EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
464 capability_->InitProxies();
465
466 ResultCallback disconnect_callback;
467 Error error;
468 ResultCallback callback =
469 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
470 EXPECT_CALL(*modem_simple_proxy,
471 Disconnect(::DBus::Path("/"), _, _,
Thieu Le049adb52012-11-12 17:14:51 -0800472 CellularCapability::kTimeoutDisconnect))
Jason Glasgow02401cc2012-05-16 10:35:37 -0400473 .WillOnce(SaveArg<2>(&disconnect_callback));
Thieu Led0012052012-07-25 16:09:09 -0700474 capability_->cellular()->modem_state_ = Cellular::kModemStateConnected;
Jason Glasgow02401cc2012-05-16 10:35:37 -0400475 capability_->StopModem(&error, callback);
476 EXPECT_TRUE(error.IsSuccess());
477
478 ResultCallback disable_callback;
479 EXPECT_CALL(*modem_proxy,
480 Enable(false, _, _, CellularCapability::kTimeoutEnable))
481 .WillOnce(SaveArg<2>(&disable_callback));
482 disconnect_callback.Run(Error(Error::kSuccess));
483
Arman Ugurayee464d32013-02-13 17:14:36 -0800484 ResultCallback set_power_state_callback;
Thieu Le2cac2942013-03-05 18:41:08 -0800485 EXPECT_CALL(
486 *modem_proxy,
487 SetPowerState(
488 MM_MODEM_POWER_STATE_LOW, _, _,
489 CellularCapabilityUniversal::kSetPowerStateTimeoutMilliseconds))
Arman Ugurayee464d32013-02-13 17:14:36 -0800490 .WillOnce(SaveArg<2>(&set_power_state_callback));
491
Jason Glasgow02401cc2012-05-16 10:35:37 -0400492 disable_callback.Run(Error(Error::kSuccess));
Arman Ugurayee464d32013-02-13 17:14:36 -0800493
494 EXPECT_CALL(*this, TestCallback(IsSuccess()));
495 set_power_state_callback.Run(Error(Error::kSuccess));
Jason Glasgow02401cc2012-05-16 10:35:37 -0400496}
497
Arman Uguray1361c032013-02-11 17:53:39 -0800498TEST_F(CellularCapabilityUniversalMainTest, DisconnectModemNoBearer) {
Thieu Le5d6864a2012-07-20 11:43:51 -0700499 Error error;
500 ResultCallback disconnect_callback;
Thieu Le3d275392012-07-20 15:32:58 -0700501 EXPECT_CALL(*modem_simple_proxy_,
Thieu Le049adb52012-11-12 17:14:51 -0800502 Disconnect(_, _, _, CellularCapability::kTimeoutDisconnect))
Thieu Le5d6864a2012-07-20 11:43:51 -0700503 .Times(0);
504 capability_->Disconnect(&error, disconnect_callback);
505}
506
Arman Uguray1361c032013-02-11 17:53:39 -0800507TEST_F(CellularCapabilityUniversalMainTest, DisconnectNoProxy) {
Thieu Le3d275392012-07-20 15:32:58 -0700508 Error error;
509 ResultCallback disconnect_callback;
510 capability_->bearer_path_ = "/foo";
511 EXPECT_CALL(*modem_simple_proxy_,
Thieu Le049adb52012-11-12 17:14:51 -0800512 Disconnect(_, _, _, CellularCapability::kTimeoutDisconnect))
Thieu Le3d275392012-07-20 15:32:58 -0700513 .Times(0);
514 ReleaseCapabilityProxies();
515 capability_->Disconnect(&error, disconnect_callback);
516}
517
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700518TEST_F(CellularCapabilityUniversalMainTest, DisconnectWithDeferredCallback) {
519 Error error;
520 ResultCallback disconnect_callback;
521 capability_->bearer_path_ = "/foo";
522 EXPECT_CALL(*modem_simple_proxy_,
523 Disconnect(_, _, _, CellularCapability::kTimeoutDisconnect));
524 SetSimpleProxy();
525 SetMockRegistrationDroppedUpdateCallback();
526 EXPECT_CALL(*this, DummyCallback());
527 capability_->Disconnect(&error, disconnect_callback);
528}
529
Arman Uguray1361c032013-02-11 17:53:39 -0800530TEST_F(CellularCapabilityUniversalMainTest, SimLockStatusChanged) {
Arman Ugurayab9364e2012-12-19 20:45:25 -0800531 // Set up mock SIM properties
532 const char kImsi[] = "310100000001";
533 const char kSimIdentifier[] = "9999888";
534 const char kOperatorIdentifier[] = "310240";
535 const char kOperatorName[] = "Custom SPN";
536 DBusPropertiesMap sim_properties;
537 sim_properties[MM_SIM_PROPERTY_IMSI].writer().append_string(kImsi);
538 sim_properties[MM_SIM_PROPERTY_SIMIDENTIFIER].writer()
539 .append_string(kSimIdentifier);
540 sim_properties[MM_SIM_PROPERTY_OPERATORIDENTIFIER].writer()
541 .append_string(kOperatorIdentifier);
542 sim_properties[MM_SIM_PROPERTY_OPERATORNAME].writer()
543 .append_string(kOperatorName);
544
545 EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
546 .WillOnce(Return(sim_properties));
Arman Uguray41cc6342013-03-29 16:34:39 -0700547 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
548 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700549 .Times(1);
Arman Ugurayab9364e2012-12-19 20:45:25 -0800550
551 SetUp();
552 InitProviderDB();
553
554 EXPECT_FALSE(capability_->sim_present_);
555 EXPECT_TRUE(capability_->sim_proxy_ == NULL);
556
557 capability_->OnSimPathChanged(kSimPath);
558 EXPECT_TRUE(capability_->sim_present_);
559 EXPECT_TRUE(capability_->sim_proxy_ != NULL);
560 EXPECT_EQ(kSimPath, capability_->sim_path_);
561
562 capability_->imsi_ = "";
563 capability_->sim_identifier_ = "";
564 capability_->operator_id_ = "";
565 capability_->spn_ = "";
566
567 // SIM is locked.
Arman Ugurayc7e63af2013-06-13 17:07:32 -0700568 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
Arman Ugurayab9364e2012-12-19 20:45:25 -0800569 capability_->OnSimLockStatusChanged();
Arman Uguray41cc6342013-03-29 16:34:39 -0700570 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Ugurayab9364e2012-12-19 20:45:25 -0800571
572 EXPECT_EQ("", capability_->imsi_);
573 EXPECT_EQ("", capability_->sim_identifier_);
574 EXPECT_EQ("", capability_->operator_id_);
575 EXPECT_EQ("", capability_->spn_);
576
577 // SIM is unlocked.
578 properties_proxy_.reset(new MockDBusPropertiesProxy());
579 EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
580 .WillOnce(Return(sim_properties));
Arman Uguray41cc6342013-03-29 16:34:39 -0700581 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
582 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700583 .Times(1);
Arman Ugurayab9364e2012-12-19 20:45:25 -0800584
Arman Ugurayc7e63af2013-06-13 17:07:32 -0700585 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_NONE;
Arman Ugurayab9364e2012-12-19 20:45:25 -0800586 capability_->OnSimLockStatusChanged();
Arman Uguray41cc6342013-03-29 16:34:39 -0700587 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Ugurayab9364e2012-12-19 20:45:25 -0800588
589 EXPECT_EQ(kImsi, capability_->imsi_);
590 EXPECT_EQ(kSimIdentifier, capability_->sim_identifier_);
591 EXPECT_EQ(kOperatorIdentifier, capability_->operator_id_);
592 EXPECT_EQ(kOperatorName, capability_->spn_);
593}
594
Arman Uguray1361c032013-02-11 17:53:39 -0800595TEST_F(CellularCapabilityUniversalMainTest, PropertiesChanged) {
Jason Glasgowaf583282012-04-18 15:18:22 -0400596 // Set up mock modem properties
597 DBusPropertiesMap modem_properties;
598 modem_properties[MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES].
599 writer().append_uint32(kAccessTechnologies);
600 modem_properties[MM_MODEM_PROPERTY_SIM].
601 writer().append_path(kSimPath);
602
603 // Set up mock modem 3gpp properties
604 DBusPropertiesMap modem3gpp_properties;
605 modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_ENABLEDFACILITYLOCKS].
606 writer().append_uint32(0);
607 modem3gpp_properties[MM_MODEM_MODEM3GPP_PROPERTY_IMEI].
608 writer().append_string(kImei);
609
610 // Set up mock modem sim properties
611 DBusPropertiesMap sim_properties;
612
613 // After setup we lose pointers to the proxies, so it is hard to set
614 // expectations.
615 EXPECT_CALL(*properties_proxy_,
616 GetAll(MM_DBUS_INTERFACE_SIM))
617 .WillOnce(Return(sim_properties));
618
619 SetUp();
620
621 EXPECT_EQ("", capability_->imei_);
622 EXPECT_EQ(MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN,
623 capability_->access_technologies_);
624 EXPECT_FALSE(capability_->sim_proxy_.get());
Jason Glasgowbad114b2012-05-21 15:24:16 -0400625 EXPECT_CALL(*device_adaptor_, EmitStringChanged(
626 flimflam::kTechnologyFamilyProperty, flimflam::kTechnologyFamilyGsm));
Jason Glasgowaf583282012-04-18 15:18:22 -0400627 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
628 modem_properties, vector<string>());
629 EXPECT_EQ(kAccessTechnologies, capability_->access_technologies_);
630 EXPECT_EQ(kSimPath, capability_->sim_path_);
631 EXPECT_TRUE(capability_->sim_proxy_.get());
632
633 // Changing properties on wrong interface will not have an effect
634 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
635 modem3gpp_properties,
636 vector<string>());
637 EXPECT_EQ("", capability_->imei_);
638
639 // Changing properties on the right interface gets reflected in the
640 // capabilities object
641 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM_MODEM3GPP,
642 modem3gpp_properties,
643 vector<string>());
644 EXPECT_EQ(kImei, capability_->imei_);
Prathmesh Prabhub5fde532013-03-19 17:36:09 -0700645 Mock::VerifyAndClearExpectations(device_adaptor_);
Jason Glasgowbad114b2012-05-21 15:24:16 -0400646
647 // Expect to see changes when the family changes
648 modem_properties.clear();
649 modem_properties[MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES].
650 writer().append_uint32(MM_MODEM_ACCESS_TECHNOLOGY_1XRTT);
651 EXPECT_CALL(*device_adaptor_, EmitStringChanged(
652 flimflam::kTechnologyFamilyProperty, flimflam::kTechnologyFamilyCdma)).
653 Times(1);
654 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
655 modem_properties,
656 vector<string>());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -0700657 Mock::VerifyAndClearExpectations(device_adaptor_);
658
Jason Glasgowbad114b2012-05-21 15:24:16 -0400659 // Back to LTE
660 modem_properties.clear();
661 modem_properties[MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES].
662 writer().append_uint32(MM_MODEM_ACCESS_TECHNOLOGY_LTE);
663 EXPECT_CALL(*device_adaptor_, EmitStringChanged(
664 flimflam::kTechnologyFamilyProperty, flimflam::kTechnologyFamilyGsm)).
665 Times(1);
666 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
667 modem_properties,
668 vector<string>());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -0700669 Mock::VerifyAndClearExpectations(device_adaptor_);
Jason Glasgowbad114b2012-05-21 15:24:16 -0400670
671 // LTE & CDMA - the device adaptor should not be called!
672 modem_properties.clear();
673 modem_properties[MM_MODEM_PROPERTY_ACCESSTECHNOLOGIES].
674 writer().append_uint32(MM_MODEM_ACCESS_TECHNOLOGY_LTE |
675 MM_MODEM_ACCESS_TECHNOLOGY_1XRTT);
676 EXPECT_CALL(*device_adaptor_, EmitStringChanged(_, _)).Times(0);
677 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
678 modem_properties,
679 vector<string>());
Jason Glasgowaf583282012-04-18 15:18:22 -0400680}
681
Arman Uguray1361c032013-02-11 17:53:39 -0800682TEST_F(CellularCapabilityUniversalMainTest, UpdateServiceName) {
Arman Uguray2717a102013-01-29 23:36:06 -0800683 ::DBus::Struct<uint32_t, bool> data;
684 data._1 = 100;
685 data._2 = true;
686 EXPECT_CALL(*modem_proxy_, SignalQuality()).WillRepeatedly(Return(data));
687
688 SetUp();
689 InitProviderDB();
690 capability_->InitProxies();
Arman Uguray2717a102013-01-29 23:36:06 -0800691
692 SetService();
693
694 size_t len = strlen(CellularCapabilityUniversal::kGenericServiceNamePrefix);
695 EXPECT_EQ(CellularCapabilityUniversal::kGenericServiceNamePrefix,
696 cellular_->service_->friendly_name().substr(0, len));
697
698 capability_->imsi_ = "310240123456789";
699 capability_->SetHomeProvider();
700 EXPECT_EQ("", capability_->spn_);
701 EXPECT_EQ("T-Mobile", cellular_->home_provider().GetName());
702 EXPECT_EQ(CellularCapabilityUniversal::kGenericServiceNamePrefix,
703 cellular_->service_->friendly_name().substr(0, len));
704
705 capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
706 capability_->SetHomeProvider();
707 EXPECT_EQ("", capability_->spn_);
708 EXPECT_EQ("T-Mobile", cellular_->home_provider().GetName());
709 EXPECT_EQ("T-Mobile", cellular_->service_->friendly_name());
710
711 capability_->spn_ = "Test Home Provider";
712 capability_->SetHomeProvider();
713 EXPECT_EQ("Test Home Provider", cellular_->home_provider().GetName());
714 EXPECT_EQ("Test Home Provider", cellular_->service_->friendly_name());
715
716 capability_->On3GPPRegistrationChanged(
717 MM_MODEM_3GPP_REGISTRATION_STATE_HOME, "", "OTA Name");
718 EXPECT_EQ("OTA Name", cellular_->service_->friendly_name());
719
720 capability_->On3GPPRegistrationChanged(
721 MM_MODEM_3GPP_REGISTRATION_STATE_HOME, "123", "OTA Name 2");
722 EXPECT_EQ("OTA Name 2", cellular_->service_->friendly_name());
723
724 capability_->On3GPPRegistrationChanged(
725 MM_MODEM_3GPP_REGISTRATION_STATE_HOME, "123", "");
726 EXPECT_EQ("Test Home Provider", cellular_->service_->friendly_name());
727}
728
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700729TEST_F(CellularCapabilityUniversalMainTest, UpdateRegistrationState) {
730 SetUp();
731 InitProviderDB();
732 capability_->InitProxies();
733
734 SetService();
735 capability_->imsi_ = "310240123456789";
736 capability_->SetHomeProvider();
737 cellular_->set_modem_state(Cellular::kModemStateConnected);
738 SetRegistrationDroppedUpdateTimeout(0);
739
740 string home_provider = cellular_->home_provider().GetName();
741 string ota_name = cellular_->service_->friendly_name();
742
743 // Home --> Roaming should be effective immediately.
744 capability_->On3GPPRegistrationChanged(
745 MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
746 home_provider,
747 ota_name);
748 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
749 capability_->registration_state_);
750 capability_->On3GPPRegistrationChanged(
751 MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
752 home_provider,
753 ota_name);
754 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
755 capability_->registration_state_);
756
757 // Idle --> Roaming should be effective immediately.
758 capability_->On3GPPRegistrationChanged(
759 MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
760 home_provider,
761 ota_name);
762 dispatcher_.DispatchPendingEvents();
763 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
764 capability_->registration_state_);
765 capability_->On3GPPRegistrationChanged(
766 MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
767 home_provider,
768 ota_name);
769 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING,
770 capability_->registration_state_);
771
772 // Idle --> Searching should be effective immediately.
773 capability_->On3GPPRegistrationChanged(
774 MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
775 home_provider,
776 ota_name);
777 dispatcher_.DispatchPendingEvents();
778 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_IDLE,
779 capability_->registration_state_);
780 capability_->On3GPPRegistrationChanged(
781 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
782 home_provider,
783 ota_name);
784 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
785 capability_->registration_state_);
786
787 // Home --> Searching --> Home should never see Searching.
Prathmesh Prabhu08757aa2013-05-15 17:17:33 -0700788 EXPECT_CALL(*(modem_info_.mock_metrics()),
789 Notify3GPPRegistrationDelayedDropPosted());
790 EXPECT_CALL(*(modem_info_.mock_metrics()),
791 Notify3GPPRegistrationDelayedDropCanceled());
792
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700793 capability_->On3GPPRegistrationChanged(
794 MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
795 home_provider,
796 ota_name);
797 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
798 capability_->registration_state_);
799 capability_->On3GPPRegistrationChanged(
800 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
801 home_provider,
802 ota_name);
803 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
804 capability_->registration_state_);
805 capability_->On3GPPRegistrationChanged(
806 MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
807 home_provider,
808 ota_name);
809 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
810 capability_->registration_state_);
811 dispatcher_.DispatchPendingEvents();
812 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
813 capability_->registration_state_);
Prathmesh Prabhu08757aa2013-05-15 17:17:33 -0700814 Mock::VerifyAndClearExpectations(modem_info_.mock_metrics());
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700815
816 // Home --> Searching --> wait till dispatch should see Searching
Prathmesh Prabhu08757aa2013-05-15 17:17:33 -0700817 EXPECT_CALL(*(modem_info_.mock_metrics()),
818 Notify3GPPRegistrationDelayedDropPosted());
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700819 capability_->On3GPPRegistrationChanged(
820 MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
821 home_provider,
822 ota_name);
823 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
824 capability_->registration_state_);
825 capability_->On3GPPRegistrationChanged(
826 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
827 home_provider,
828 ota_name);
829 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
830 capability_->registration_state_);
831 dispatcher_.DispatchPendingEvents();
832 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
833 capability_->registration_state_);
Prathmesh Prabhu08757aa2013-05-15 17:17:33 -0700834 Mock::VerifyAndClearExpectations(modem_info_.mock_metrics());
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700835
836 // Home --> Searching --> Searching --> wait till dispatch should see
837 // Searching *and* the first callback should be cancelled.
838 EXPECT_CALL(*this, DummyCallback()).Times(0);
Prathmesh Prabhu08757aa2013-05-15 17:17:33 -0700839 EXPECT_CALL(*(modem_info_.mock_metrics()),
840 Notify3GPPRegistrationDelayedDropPosted());
841
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700842 capability_->On3GPPRegistrationChanged(
843 MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
844 home_provider,
845 ota_name);
846 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
847 capability_->registration_state_);
848 capability_->On3GPPRegistrationChanged(
849 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
850 home_provider,
851 ota_name);
852 SetMockRegistrationDroppedUpdateCallback();
853 capability_->On3GPPRegistrationChanged(
854 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
855 home_provider,
856 ota_name);
857 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
858 capability_->registration_state_);
859 dispatcher_.DispatchPendingEvents();
860 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
861 capability_->registration_state_);
Prathmesh Prabhu8f6479f2013-05-15 12:56:13 -0700862}
863
864TEST_F(CellularCapabilityUniversalMainTest,
865 UpdateRegistrationStateModemNotConnected) {
866 SetUp();
867 InitProviderDB();
868 capability_->InitProxies();
869 SetService();
870
871 capability_->imsi_ = "310240123456789";
872 capability_->SetHomeProvider();
873 cellular_->set_modem_state(Cellular::kModemStateRegistered);
874 SetRegistrationDroppedUpdateTimeout(0);
875
876 string home_provider = cellular_->home_provider().GetName();
877 string ota_name = cellular_->service_->friendly_name();
878
879 // Home --> Searching should be effective immediately.
880 capability_->On3GPPRegistrationChanged(
881 MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
882 home_provider,
883 ota_name);
884 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_HOME,
885 capability_->registration_state_);
886 capability_->On3GPPRegistrationChanged(
887 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
888 home_provider,
889 ota_name);
890 EXPECT_EQ(MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING,
891 capability_->registration_state_);
892}
893
Arman Uguray6552f8c2013-02-12 15:33:18 -0800894TEST_F(CellularCapabilityUniversalMainTest, IsValidSimPath) {
895 // Invalid paths
896 EXPECT_FALSE(capability_->IsValidSimPath(""));
897 EXPECT_FALSE(capability_->IsValidSimPath("/"));
898
899 // A valid path
900 EXPECT_TRUE(capability_->IsValidSimPath(
901 "/org/freedesktop/ModemManager1/SIM/0"));
902
903 // Note that any string that is not one of the above invalid paths is
904 // currently regarded as valid, since the ModemManager spec doesn't impose
905 // a strict format on the path. The validity of this is subject to change.
906 EXPECT_TRUE(capability_->IsValidSimPath("path"));
907}
908
Ben Chand7592522013-02-13 16:02:01 -0800909TEST_F(CellularCapabilityUniversalMainTest, NormalizeMdn) {
910 EXPECT_EQ("", capability_->NormalizeMdn(""));
911 EXPECT_EQ("12345678901", capability_->NormalizeMdn("12345678901"));
912 EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1 234 567 8901"));
913 EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1-234-567-8901"));
914 EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1 (234) 567-8901"));
915 EXPECT_EQ("12345678901", capability_->NormalizeMdn("1 234 567 8901 "));
916 EXPECT_EQ("2345678901", capability_->NormalizeMdn("(234) 567-8901"));
917}
918
Arman Uguray1361c032013-02-11 17:53:39 -0800919TEST_F(CellularCapabilityUniversalMainTest, SimPathChanged) {
Ben Chanbd3aee82012-10-16 23:52:04 -0700920 // Set up mock modem SIM properties
921 const char kImsi[] = "310100000001";
922 const char kSimIdentifier[] = "9999888";
923 const char kOperatorIdentifier[] = "310240";
924 const char kOperatorName[] = "Custom SPN";
925 DBusPropertiesMap sim_properties;
926 sim_properties[MM_SIM_PROPERTY_IMSI].writer().append_string(kImsi);
927 sim_properties[MM_SIM_PROPERTY_SIMIDENTIFIER].writer()
928 .append_string(kSimIdentifier);
929 sim_properties[MM_SIM_PROPERTY_OPERATORIDENTIFIER].writer()
930 .append_string(kOperatorIdentifier);
931 sim_properties[MM_SIM_PROPERTY_OPERATORNAME].writer()
932 .append_string(kOperatorName);
933
934 EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
935 .Times(1).WillOnce(Return(sim_properties));
Arman Uguray41cc6342013-03-29 16:34:39 -0700936 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
937 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700938 .Times(1);
Ben Chanbd3aee82012-10-16 23:52:04 -0700939
940 EXPECT_FALSE(capability_->sim_present_);
941 EXPECT_TRUE(capability_->sim_proxy_ == NULL);
942 EXPECT_EQ("", capability_->sim_path_);
943 EXPECT_EQ("", capability_->imsi_);
944 EXPECT_EQ("", capability_->sim_identifier_);
945 EXPECT_EQ("", capability_->operator_id_);
946 EXPECT_EQ("", capability_->spn_);
947
948 capability_->OnSimPathChanged(kSimPath);
949 EXPECT_TRUE(capability_->sim_present_);
950 EXPECT_TRUE(capability_->sim_proxy_ != NULL);
951 EXPECT_EQ(kSimPath, capability_->sim_path_);
952 EXPECT_EQ(kImsi, capability_->imsi_);
953 EXPECT_EQ(kSimIdentifier, capability_->sim_identifier_);
954 EXPECT_EQ(kOperatorIdentifier, capability_->operator_id_);
955 EXPECT_EQ(kOperatorName, capability_->spn_);
956
957 // Changing to the same SIM path should be a no-op.
958 capability_->OnSimPathChanged(kSimPath);
959 EXPECT_TRUE(capability_->sim_present_);
960 EXPECT_TRUE(capability_->sim_proxy_ != NULL);
961 EXPECT_EQ(kSimPath, capability_->sim_path_);
962 EXPECT_EQ(kImsi, capability_->imsi_);
963 EXPECT_EQ(kSimIdentifier, capability_->sim_identifier_);
964 EXPECT_EQ(kOperatorIdentifier, capability_->operator_id_);
965 EXPECT_EQ(kOperatorName, capability_->spn_);
966
967 capability_->OnSimPathChanged("");
Arman Uguray41cc6342013-03-29 16:34:39 -0700968 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -0700969 Mock::VerifyAndClearExpectations(properties_proxy_.get());
Ben Chanbd3aee82012-10-16 23:52:04 -0700970 EXPECT_FALSE(capability_->sim_present_);
971 EXPECT_TRUE(capability_->sim_proxy_ == NULL);
972 EXPECT_EQ("", capability_->sim_path_);
973 EXPECT_EQ("", capability_->imsi_);
974 EXPECT_EQ("", capability_->sim_identifier_);
975 EXPECT_EQ("", capability_->operator_id_);
976 EXPECT_EQ("", capability_->spn_);
Arman Uguray6552f8c2013-02-12 15:33:18 -0800977
978 EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
979 .Times(1).WillOnce(Return(sim_properties));
Arman Uguray41cc6342013-03-29 16:34:39 -0700980 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
981 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700982 .Times(1);
Arman Uguray6552f8c2013-02-12 15:33:18 -0800983
984 capability_->OnSimPathChanged(kSimPath);
985 EXPECT_TRUE(capability_->sim_present_);
986 EXPECT_TRUE(capability_->sim_proxy_ != NULL);
987 EXPECT_EQ(kSimPath, capability_->sim_path_);
988 EXPECT_EQ(kImsi, capability_->imsi_);
989 EXPECT_EQ(kSimIdentifier, capability_->sim_identifier_);
990 EXPECT_EQ(kOperatorIdentifier, capability_->operator_id_);
991 EXPECT_EQ(kOperatorName, capability_->spn_);
992
993 capability_->OnSimPathChanged("/");
994 EXPECT_FALSE(capability_->sim_present_);
995 EXPECT_TRUE(capability_->sim_proxy_ == NULL);
996 EXPECT_EQ("/", capability_->sim_path_);
997 EXPECT_EQ("", capability_->imsi_);
998 EXPECT_EQ("", capability_->sim_identifier_);
999 EXPECT_EQ("", capability_->operator_id_);
1000 EXPECT_EQ("", capability_->spn_);
Ben Chanbd3aee82012-10-16 23:52:04 -07001001}
1002
Arman Uguray1361c032013-02-11 17:53:39 -08001003TEST_F(CellularCapabilityUniversalMainTest, SimPropertiesChanged) {
Jason Glasgowaf583282012-04-18 15:18:22 -04001004 // Set up mock modem properties
1005 DBusPropertiesMap modem_properties;
1006 modem_properties[MM_MODEM_PROPERTY_SIM].writer().append_path(kSimPath);
1007
1008 // Set up mock modem sim properties
1009 const char kImsi[] = "310100000001";
1010 DBusPropertiesMap sim_properties;
1011 sim_properties[MM_SIM_PROPERTY_IMSI].writer().append_string(kImsi);
1012
1013 EXPECT_CALL(*properties_proxy_, GetAll(MM_DBUS_INTERFACE_SIM))
1014 .WillOnce(Return(sim_properties));
Arman Uguray41cc6342013-03-29 16:34:39 -07001015 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1016 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -07001017 .Times(0);
Jason Glasgowaf583282012-04-18 15:18:22 -04001018 // After setup we lose pointers to the proxies, so it is hard to set
1019 // expectations.
1020 SetUp();
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001021 InitProviderDB();
Jason Glasgowaf583282012-04-18 15:18:22 -04001022
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001023 EXPECT_TRUE(cellular_->home_provider().GetName().empty());
1024 EXPECT_TRUE(cellular_->home_provider().GetCountry().empty());
1025 EXPECT_TRUE(cellular_->home_provider().GetCode().empty());
Jason Glasgowaf583282012-04-18 15:18:22 -04001026 EXPECT_FALSE(capability_->sim_proxy_.get());
1027 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_MODEM,
1028 modem_properties, vector<string>());
1029 EXPECT_EQ(kSimPath, capability_->sim_path_);
1030 EXPECT_TRUE(capability_->sim_proxy_.get());
1031 EXPECT_EQ(kImsi, capability_->imsi_);
Arman Uguray41cc6342013-03-29 16:34:39 -07001032 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Jason Glasgowaf583282012-04-18 15:18:22 -04001033
1034 // Updating the SIM
Arman Uguray41cc6342013-03-29 16:34:39 -07001035 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1036 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -07001037 .Times(2);
Jason Glasgowaf583282012-04-18 15:18:22 -04001038 DBusPropertiesMap new_properties;
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001039 const char kCountry[] = "us";
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001040 const char kNewImsi[] = "310240123456789";
Jason Glasgowaf583282012-04-18 15:18:22 -04001041 const char kSimIdentifier[] = "9999888";
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001042 const char kOperatorIdentifier[] = "310240";
1043 const char kOperatorName[] = "Custom SPN";
Jason Glasgowaf583282012-04-18 15:18:22 -04001044 new_properties[MM_SIM_PROPERTY_IMSI].writer().append_string(kNewImsi);
1045 new_properties[MM_SIM_PROPERTY_SIMIDENTIFIER].writer().
1046 append_string(kSimIdentifier);
1047 new_properties[MM_SIM_PROPERTY_OPERATORIDENTIFIER].writer().
1048 append_string(kOperatorIdentifier);
Jason Glasgowaf583282012-04-18 15:18:22 -04001049 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_SIM,
1050 new_properties,
1051 vector<string>());
1052 EXPECT_EQ(kNewImsi, capability_->imsi_);
1053 EXPECT_EQ(kSimIdentifier, capability_->sim_identifier_);
1054 EXPECT_EQ(kOperatorIdentifier, capability_->operator_id_);
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001055 EXPECT_EQ("", capability_->spn_);
1056 EXPECT_EQ("T-Mobile", cellular_->home_provider().GetName());
1057 EXPECT_EQ(kCountry, cellular_->home_provider().GetCountry());
Arman Ugurayd73783f2013-01-31 16:11:21 -08001058 EXPECT_EQ(kOperatorIdentifier, cellular_->home_provider().GetCode());
Jason Glasgow4380f0d2012-05-03 18:05:04 -04001059 EXPECT_EQ(4, capability_->apn_list_.size());
1060
1061 new_properties[MM_SIM_PROPERTY_OPERATORNAME].writer().
1062 append_string(kOperatorName);
1063 capability_->OnDBusPropertiesChanged(MM_DBUS_INTERFACE_SIM,
1064 new_properties,
1065 vector<string>());
1066 EXPECT_EQ(kOperatorName, cellular_->home_provider().GetName());
Jason Glasgowaf583282012-04-18 15:18:22 -04001067 EXPECT_EQ(kOperatorName, capability_->spn_);
1068}
1069
Gary Morainceba6aa2012-05-03 10:28:26 -07001070MATCHER_P(SizeIs, value, "") {
1071 return static_cast<size_t>(value) == arg.size();
1072}
1073
Arman Uguray1361c032013-02-11 17:53:39 -08001074TEST_F(CellularCapabilityUniversalMainTest, Reset) {
Ben Chan5d0d32c2013-01-08 02:05:29 -08001075 // Save pointers to proxies before they are lost by the call to InitProxies
1076 mm1::MockModemProxy *modem_proxy = modem_proxy_.get();
1077 SetUp();
1078 EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
1079 capability_->InitProxies();
1080
1081 Error error;
1082 ResultCallback reset_callback;
1083
1084 EXPECT_CALL(*modem_proxy, Reset(_, _, CellularCapability::kTimeoutReset))
1085 .WillOnce(SaveArg<1>(&reset_callback));
1086
1087 capability_->Reset(&error, ResultCallback());
1088 EXPECT_TRUE(capability_->resetting_);
1089 reset_callback.Run(error);
1090 EXPECT_FALSE(capability_->resetting_);
1091}
1092
Gary Morainceba6aa2012-05-03 10:28:26 -07001093// Validates that OnScanReply does not crash with a null callback.
Arman Uguray1361c032013-02-11 17:53:39 -08001094TEST_F(CellularCapabilityUniversalMainTest, ScanWithNullCallback) {
Gary Morainceba6aa2012-05-03 10:28:26 -07001095 Error error;
1096 EXPECT_CALL(*modem_3gpp_proxy_, Scan(_, _, CellularCapability::kTimeoutScan))
1097 .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeScan));
1098 EXPECT_CALL(*device_adaptor_,
1099 EmitStringmapsChanged(flimflam::kFoundNetworksProperty,
1100 SizeIs(0)));
1101 Set3gppProxy();
1102 capability_->Scan(&error, ResultCallback());
1103 EXPECT_TRUE(error.IsSuccess());
1104}
1105
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001106// Validates that the scanning property is updated
Arman Uguray1361c032013-02-11 17:53:39 -08001107TEST_F(CellularCapabilityUniversalMainTest, Scan) {
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001108 Error error;
1109
1110 EXPECT_CALL(*modem_3gpp_proxy_, Scan(_, _, CellularCapability::kTimeoutScan))
1111 .WillRepeatedly(SaveArg<1>(&scan_callback_));
1112 EXPECT_CALL(*device_adaptor_,
1113 EmitBoolChanged(flimflam::kScanningProperty, true));
1114 Set3gppProxy();
1115 capability_->Scan(&error, ResultCallback());
1116 EXPECT_TRUE(capability_->scanning_);
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001117 Mock::VerifyAndClearExpectations(device_adaptor_);
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001118
1119 // Simulate the completion of the scan with 2 networks in the results.
1120 EXPECT_CALL(*device_adaptor_,
1121 EmitBoolChanged(flimflam::kScanningProperty, false));
1122 EXPECT_CALL(*device_adaptor_,
1123 EmitStringmapsChanged(flimflam::kFoundNetworksProperty,
1124 SizeIs(2)));
1125 vector<DBusPropertiesMap> results;
1126 const char kScanID0[] = "testID0";
1127 const char kScanID1[] = "testID1";
1128 results.push_back(DBusPropertiesMap());
1129 results[0][CellularCapabilityUniversal::kOperatorLongProperty].
1130 writer().append_string(kScanID0);
1131 results.push_back(DBusPropertiesMap());
1132 results[1][CellularCapabilityUniversal::kOperatorLongProperty].
1133 writer().append_string(kScanID1);
1134 scan_callback_.Run(results, error);
1135 EXPECT_FALSE(capability_->scanning_);
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001136 Mock::VerifyAndClearExpectations(device_adaptor_);
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001137
1138 // Simulate the completion of the scan with no networks in the results.
1139 EXPECT_CALL(*device_adaptor_,
1140 EmitBoolChanged(flimflam::kScanningProperty, true));
1141 capability_->Scan(&error, ResultCallback());
1142 EXPECT_TRUE(capability_->scanning_);
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001143 Mock::VerifyAndClearExpectations(device_adaptor_);
1144
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001145 EXPECT_CALL(*device_adaptor_,
1146 EmitBoolChanged(flimflam::kScanningProperty, false));
1147 EXPECT_CALL(*device_adaptor_,
1148 EmitStringmapsChanged(flimflam::kFoundNetworksProperty,
1149 SizeIs(0)));
1150 scan_callback_.Run(vector<DBusPropertiesMap>(), Error());
1151 EXPECT_FALSE(capability_->scanning_);
1152}
1153
1154// Validates expected property updates when scan fails
Arman Uguray1361c032013-02-11 17:53:39 -08001155TEST_F(CellularCapabilityUniversalMainTest, ScanFailure) {
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001156 Error error;
1157
1158 // Test immediate error
1159 {
1160 InSequence seq;
1161 EXPECT_CALL(*modem_3gpp_proxy_,
1162 Scan(_, _, CellularCapability::kTimeoutScan))
1163 .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::ScanError));
1164 EXPECT_CALL(*modem_3gpp_proxy_,
1165 Scan(_, _, CellularCapability::kTimeoutScan))
1166 .WillOnce(SaveArg<1>(&scan_callback_));
1167 }
1168 Set3gppProxy();
1169 capability_->Scan(&error, ResultCallback());
1170 EXPECT_FALSE(capability_->scanning_);
1171 EXPECT_TRUE(error.IsFailure());
1172
1173 // Initiate a scan
1174 error.Populate(Error::kSuccess);
1175 EXPECT_CALL(*device_adaptor_,
1176 EmitBoolChanged(flimflam::kScanningProperty, true));
1177 capability_->Scan(&error, ResultCallback());
1178 EXPECT_TRUE(capability_->scanning_);
1179 EXPECT_TRUE(error.IsSuccess());
1180
1181 // Validate that error is returned if Scan is called while already scanning.
1182 capability_->Scan(&error, ResultCallback());
1183 EXPECT_TRUE(capability_->scanning_);
1184 EXPECT_TRUE(error.IsFailure());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001185 Mock::VerifyAndClearExpectations(device_adaptor_);
Jason Glasgowcd0349c2012-05-03 23:32:15 -04001186
1187 // Validate that signals are emitted even if an error is reported.
1188 capability_->found_networks_.clear();
1189 capability_->found_networks_.push_back(Stringmap());
1190 EXPECT_CALL(*device_adaptor_,
1191 EmitBoolChanged(flimflam::kScanningProperty, false));
1192 EXPECT_CALL(*device_adaptor_,
1193 EmitStringmapsChanged(flimflam::kFoundNetworksProperty,
1194 SizeIs(0)));
1195 vector<DBusPropertiesMap> results;
1196 scan_callback_.Run(results, Error(Error::kOperationFailed));
1197 EXPECT_FALSE(capability_->scanning_);
1198}
1199
Arman Uguray6e5639f2012-11-15 20:30:19 -08001200// Validates expected behavior of OnListBearersReply function
Arman Uguray1361c032013-02-11 17:53:39 -08001201TEST_F(CellularCapabilityUniversalMainTest, OnListBearersReply) {
Arman Uguray6e5639f2012-11-15 20:30:19 -08001202 // Check that bearer_path_ is set correctly when an active bearer
1203 // is returned.
1204 const size_t kPathCount = 3;
1205 DBus::Path active_paths[kPathCount], inactive_paths[kPathCount];
1206 for (size_t i = 0; i < kPathCount; ++i) {
1207 active_paths[i] =
1208 DBus::Path(base::StringPrintf("%s/%zu", kActiveBearerPathPrefix, i));
1209 inactive_paths[i] =
1210 DBus::Path(base::StringPrintf("%s/%zu", kInactiveBearerPathPrefix, i));
1211 }
1212
1213 std::vector<DBus::Path> paths;
1214 paths.push_back(inactive_paths[0]);
1215 paths.push_back(inactive_paths[1]);
1216 paths.push_back(active_paths[2]);
1217 paths.push_back(inactive_paths[1]);
1218 paths.push_back(inactive_paths[2]);
1219
1220 Error error;
1221 capability_->OnListBearersReply(paths, error);
1222 EXPECT_STREQ(capability_->bearer_path_.c_str(), active_paths[2].c_str());
1223
1224 paths.clear();
1225
1226 // Check that bearer_path_ is empty if no active bearers are returned.
1227 paths.push_back(inactive_paths[0]);
1228 paths.push_back(inactive_paths[1]);
1229 paths.push_back(inactive_paths[2]);
1230 paths.push_back(inactive_paths[1]);
1231
1232 capability_->OnListBearersReply(paths, error);
1233 EXPECT_TRUE(capability_->bearer_path_.empty());
1234
1235 // Check that returning multiple bearers causes death.
1236 paths.push_back(active_paths[0]);
1237 paths.push_back(inactive_paths[1]);
1238 paths.push_back(inactive_paths[2]);
1239 paths.push_back(active_paths[1]);
1240 paths.push_back(inactive_paths[1]);
1241
1242 EXPECT_DEATH(capability_->OnListBearersReply(paths, error),
1243 "Found more than one active bearer.");
1244}
1245
Jason Glasgow14521872012-05-07 19:12:15 -04001246// Validates expected behavior of Connect function
Arman Uguray1361c032013-02-11 17:53:39 -08001247TEST_F(CellularCapabilityUniversalMainTest, Connect) {
Jason Glasgow14521872012-05-07 19:12:15 -04001248 mm1::MockModemSimpleProxy *modem_simple_proxy = modem_simple_proxy_.get();
1249 SetSimpleProxy();
1250 Error error;
1251 DBusPropertiesMap properties;
1252 capability_->apn_try_list_.clear();
1253 ResultCallback callback =
1254 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
1255 DBus::Path bearer("/foo");
1256
1257 // Test connect failures
1258 EXPECT_CALL(*modem_simple_proxy, Connect(_, _, _, _))
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001259 .WillRepeatedly(SaveArg<2>(&connect_callback_));
Jason Glasgow14521872012-05-07 19:12:15 -04001260 capability_->Connect(properties, &error, callback);
1261 EXPECT_TRUE(error.IsSuccess());
1262 EXPECT_CALL(*this, TestCallback(IsFailure()));
1263 EXPECT_CALL(*service_, ClearLastGoodApn());
1264 connect_callback_.Run(bearer, Error(Error::kOperationFailed));
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001265 Mock::VerifyAndClearExpectations(this);
Jason Glasgow14521872012-05-07 19:12:15 -04001266
1267 // Test connect success
Jason Glasgow14521872012-05-07 19:12:15 -04001268 capability_->Connect(properties, &error, callback);
1269 EXPECT_TRUE(error.IsSuccess());
1270 EXPECT_CALL(*this, TestCallback(IsSuccess()));
1271 connect_callback_.Run(bearer, Error(Error::kSuccess));
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001272 Mock::VerifyAndClearExpectations(this);
Jason Glasgow7234ec32012-05-23 16:01:21 -04001273
1274 // Test connect failures without a service. Make sure that shill
1275 // does not crash if the connect failed and there is no
1276 // CellularService object. This can happen if the modem is enabled
1277 // and then quickly disabled.
1278 cellular_->service_ = NULL;
1279 EXPECT_FALSE(capability_->cellular()->service());
Jason Glasgow7234ec32012-05-23 16:01:21 -04001280 capability_->Connect(properties, &error, callback);
1281 EXPECT_TRUE(error.IsSuccess());
1282 EXPECT_CALL(*this, TestCallback(IsFailure()));
1283 connect_callback_.Run(bearer, Error(Error::kOperationFailed));
Jason Glasgow14521872012-05-07 19:12:15 -04001284}
1285
1286// Validates Connect iterates over APNs
Arman Uguray1361c032013-02-11 17:53:39 -08001287TEST_F(CellularCapabilityUniversalMainTest, ConnectApns) {
Jason Glasgow14521872012-05-07 19:12:15 -04001288 mm1::MockModemSimpleProxy *modem_simple_proxy = modem_simple_proxy_.get();
1289 SetSimpleProxy();
1290 Error error;
1291 DBusPropertiesMap properties;
1292 capability_->apn_try_list_.clear();
1293 ResultCallback callback =
1294 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
1295 DBus::Path bearer("/bearer0");
1296
1297 const char apn_name_foo[] = "foo";
1298 const char apn_name_bar[] = "bar";
1299 EXPECT_CALL(*modem_simple_proxy, Connect(HasApn(apn_name_foo), _, _, _))
1300 .WillOnce(SaveArg<2>(&connect_callback_));
1301 Stringmap apn1;
1302 apn1[flimflam::kApnProperty] = apn_name_foo;
1303 capability_->apn_try_list_.push_back(apn1);
1304 Stringmap apn2;
1305 apn2[flimflam::kApnProperty] = apn_name_bar;
1306 capability_->apn_try_list_.push_back(apn2);
1307 capability_->FillConnectPropertyMap(&properties);
1308 capability_->Connect(properties, &error, callback);
1309 EXPECT_TRUE(error.IsSuccess());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001310 Mock::VerifyAndClearExpectations(modem_simple_proxy);
Jason Glasgow14521872012-05-07 19:12:15 -04001311
1312 EXPECT_CALL(*modem_simple_proxy, Connect(HasApn(apn_name_bar), _, _, _))
1313 .WillOnce(SaveArg<2>(&connect_callback_));
1314 EXPECT_CALL(*service_, ClearLastGoodApn());
1315 connect_callback_.Run(bearer, Error(Error::kInvalidApn));
1316
1317 EXPECT_CALL(*service_, SetLastGoodApn(apn2));
1318 EXPECT_CALL(*this, TestCallback(IsSuccess()));
1319 connect_callback_.Run(bearer, Error(Error::kSuccess));
1320}
1321
Jason Glasgow9f09aef2012-05-08 16:26:55 -04001322// Validates GetTypeString and AccessTechnologyToTechnologyFamily
Arman Uguray1361c032013-02-11 17:53:39 -08001323TEST_F(CellularCapabilityUniversalMainTest, GetTypeString) {
Jason Glasgow9f09aef2012-05-08 16:26:55 -04001324 const int gsm_technologies[] = {
1325 MM_MODEM_ACCESS_TECHNOLOGY_LTE,
1326 MM_MODEM_ACCESS_TECHNOLOGY_HSPA_PLUS,
1327 MM_MODEM_ACCESS_TECHNOLOGY_HSPA,
1328 MM_MODEM_ACCESS_TECHNOLOGY_HSUPA,
1329 MM_MODEM_ACCESS_TECHNOLOGY_HSDPA,
1330 MM_MODEM_ACCESS_TECHNOLOGY_UMTS,
1331 MM_MODEM_ACCESS_TECHNOLOGY_EDGE,
1332 MM_MODEM_ACCESS_TECHNOLOGY_GPRS,
1333 MM_MODEM_ACCESS_TECHNOLOGY_GSM_COMPACT,
1334 MM_MODEM_ACCESS_TECHNOLOGY_GSM,
1335 MM_MODEM_ACCESS_TECHNOLOGY_LTE | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1336 MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1337 MM_MODEM_ACCESS_TECHNOLOGY_LTE | MM_MODEM_ACCESS_TECHNOLOGY_EVDOA,
1338 MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_EVDOA,
1339 MM_MODEM_ACCESS_TECHNOLOGY_LTE | MM_MODEM_ACCESS_TECHNOLOGY_EVDOB,
1340 MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_EVDOB,
1341 MM_MODEM_ACCESS_TECHNOLOGY_GSM | MM_MODEM_ACCESS_TECHNOLOGY_1XRTT,
1342 };
Ben Chan62028b22012-11-05 11:20:02 -08001343 for (size_t i = 0; i < arraysize(gsm_technologies); ++i) {
Jason Glasgow9f09aef2012-05-08 16:26:55 -04001344 capability_->access_technologies_ = gsm_technologies[i];
1345 ASSERT_EQ(capability_->GetTypeString(), flimflam::kTechnologyFamilyGsm);
1346 }
1347 const int cdma_technologies[] = {
1348 MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1349 MM_MODEM_ACCESS_TECHNOLOGY_EVDOA,
1350 MM_MODEM_ACCESS_TECHNOLOGY_EVDOA | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1351 MM_MODEM_ACCESS_TECHNOLOGY_EVDOB,
1352 MM_MODEM_ACCESS_TECHNOLOGY_EVDOB | MM_MODEM_ACCESS_TECHNOLOGY_EVDO0,
1353 MM_MODEM_ACCESS_TECHNOLOGY_1XRTT,
1354 };
Ben Chan62028b22012-11-05 11:20:02 -08001355 for (size_t i = 0; i < arraysize(cdma_technologies); ++i) {
Jason Glasgow9f09aef2012-05-08 16:26:55 -04001356 capability_->access_technologies_ = cdma_technologies[i];
1357 ASSERT_EQ(capability_->GetTypeString(), flimflam::kTechnologyFamilyCdma);
1358 }
1359 capability_->access_technologies_ = MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN;
1360 ASSERT_EQ(capability_->GetTypeString(), "");
1361}
1362
Arman Uguray1361c032013-02-11 17:53:39 -08001363TEST_F(CellularCapabilityUniversalMainTest, AllowRoaming) {
Darin Petkovf508c822012-09-21 13:43:17 +02001364 EXPECT_FALSE(cellular_->allow_roaming_);
1365 EXPECT_FALSE(capability_->provider_requires_roaming_);
1366 EXPECT_FALSE(capability_->AllowRoaming());
1367 capability_->provider_requires_roaming_ = true;
1368 EXPECT_TRUE(capability_->AllowRoaming());
1369 capability_->provider_requires_roaming_ = false;
1370 cellular_->allow_roaming_ = true;
1371 EXPECT_TRUE(capability_->AllowRoaming());
1372}
1373
Arman Uguray1361c032013-02-11 17:53:39 -08001374TEST_F(CellularCapabilityUniversalMainTest, SetHomeProvider) {
Darin Petkovb4fccd22012-08-10 11:59:26 +02001375 static const char kTestCarrier[] = "The Cellular Carrier";
1376 static const char kCountry[] = "us";
1377 static const char kCode[] = "310160";
Darin Petkovb4fccd22012-08-10 11:59:26 +02001378
Darin Petkovf508c822012-09-21 13:43:17 +02001379 EXPECT_FALSE(capability_->home_provider_);
1380 EXPECT_FALSE(capability_->provider_requires_roaming_);
1381
Arman Ugurayd73783f2013-01-31 16:11:21 -08001382 // No mobile provider DB available.
1383 capability_->SetHomeProvider();
Darin Petkovb4fccd22012-08-10 11:59:26 +02001384 EXPECT_TRUE(cellular_->home_provider().GetName().empty());
1385 EXPECT_TRUE(cellular_->home_provider().GetCountry().empty());
1386 EXPECT_TRUE(cellular_->home_provider().GetCode().empty());
Darin Petkovf508c822012-09-21 13:43:17 +02001387 EXPECT_FALSE(capability_->provider_requires_roaming_);
Darin Petkovb4fccd22012-08-10 11:59:26 +02001388
1389 InitProviderDB();
Arman Ugurayd73783f2013-01-31 16:11:21 -08001390
1391 // IMSI and Operator Code not available.
1392 capability_->SetHomeProvider();
1393 EXPECT_TRUE(cellular_->home_provider().GetName().empty());
1394 EXPECT_TRUE(cellular_->home_provider().GetCountry().empty());
1395 EXPECT_TRUE(cellular_->home_provider().GetCode().empty());
1396 EXPECT_FALSE(capability_->provider_requires_roaming_);
1397
1398 // Operator Code available.
1399 capability_->operator_id_ = "310240";
1400 capability_->SetHomeProvider();
1401 EXPECT_EQ("T-Mobile", cellular_->home_provider().GetName());
1402 EXPECT_EQ(kCountry, cellular_->home_provider().GetCountry());
1403 EXPECT_EQ("310240", cellular_->home_provider().GetCode());
1404 EXPECT_EQ(4, capability_->apn_list_.size());
1405 ASSERT_TRUE(capability_->home_provider_);
1406 EXPECT_FALSE(capability_->provider_requires_roaming_);
1407
1408 cellular_->home_provider_.SetName("");
1409 cellular_->home_provider_.SetCountry("");
1410 cellular_->home_provider_.SetCode("");
1411
1412 // IMSI available
1413 capability_->imsi_ = "310240123456789";
1414 capability_->operator_id_.clear();
Darin Petkovb4fccd22012-08-10 11:59:26 +02001415 capability_->SetHomeProvider();
1416 EXPECT_EQ("T-Mobile", cellular_->home_provider().GetName());
1417 EXPECT_EQ(kCountry, cellular_->home_provider().GetCountry());
1418 EXPECT_EQ(kCode, cellular_->home_provider().GetCode());
1419 EXPECT_EQ(4, capability_->apn_list_.size());
1420 ASSERT_TRUE(capability_->home_provider_);
Darin Petkovf508c822012-09-21 13:43:17 +02001421 EXPECT_FALSE(capability_->provider_requires_roaming_);
Darin Petkovb4fccd22012-08-10 11:59:26 +02001422
1423 Cellular::Operator oper;
1424 cellular_->set_home_provider(oper);
1425 capability_->spn_ = kTestCarrier;
1426 capability_->SetHomeProvider();
1427 EXPECT_EQ(kTestCarrier, cellular_->home_provider().GetName());
1428 EXPECT_EQ(kCountry, cellular_->home_provider().GetCountry());
1429 EXPECT_EQ(kCode, cellular_->home_provider().GetCode());
Darin Petkovf508c822012-09-21 13:43:17 +02001430 EXPECT_FALSE(capability_->provider_requires_roaming_);
Darin Petkovb4fccd22012-08-10 11:59:26 +02001431
1432 static const char kCubic[] = "Cubic";
1433 capability_->spn_ = kCubic;
1434 capability_->SetHomeProvider();
1435 EXPECT_EQ(kCubic, cellular_->home_provider().GetName());
1436 EXPECT_EQ("", cellular_->home_provider().GetCode());
1437 ASSERT_TRUE(capability_->home_provider_);
Darin Petkovf508c822012-09-21 13:43:17 +02001438 EXPECT_TRUE(capability_->provider_requires_roaming_);
Darin Petkovb4fccd22012-08-10 11:59:26 +02001439
1440 static const char kCUBIC[] = "CUBIC";
1441 capability_->spn_ = kCUBIC;
1442 capability_->home_provider_ = NULL;
1443 capability_->SetHomeProvider();
1444 EXPECT_EQ(kCUBIC, cellular_->home_provider().GetName());
1445 EXPECT_EQ("", cellular_->home_provider().GetCode());
1446 ASSERT_TRUE(capability_->home_provider_);
Darin Petkovf508c822012-09-21 13:43:17 +02001447 EXPECT_TRUE(capability_->provider_requires_roaming_);
Darin Petkovb4fccd22012-08-10 11:59:26 +02001448}
1449
Arman Uguray1361c032013-02-11 17:53:39 -08001450TEST_F(CellularCapabilityUniversalMainTest, UpdateScanningProperty) {
Ben Chan8a2c01e2013-01-23 10:09:14 -08001451 // Save pointers to proxies before they are lost by the call to InitProxies
1452 // mm1::MockModemProxy *modem_proxy = modem_proxy_.get();
1453 SetUp();
1454 //EXPECT_CALL(*modem_proxy, set_state_changed_callback(_));
1455 capability_->InitProxies();
1456
1457 EXPECT_FALSE(capability_->scanning_or_searching_);
1458 capability_->UpdateScanningProperty();
1459 EXPECT_FALSE(capability_->scanning_or_searching_);
1460
1461 capability_->scanning_ = true;
1462 capability_->UpdateScanningProperty();
1463 EXPECT_TRUE(capability_->scanning_or_searching_);
1464
1465 capability_->scanning_ = false;
1466 capability_->cellular()->modem_state_ = Cellular::kModemStateInitializing;
1467 capability_->UpdateScanningProperty();
1468 EXPECT_FALSE(capability_->scanning_or_searching_);
1469 capability_->cellular()->modem_state_ = Cellular::kModemStateLocked;
1470 capability_->UpdateScanningProperty();
1471 EXPECT_FALSE(capability_->scanning_or_searching_);
1472 capability_->cellular()->modem_state_ = Cellular::kModemStateDisabled;
1473 capability_->UpdateScanningProperty();
1474 EXPECT_FALSE(capability_->scanning_or_searching_);
1475 capability_->cellular()->modem_state_ = Cellular::kModemStateEnabling;
1476 capability_->UpdateScanningProperty();
1477 EXPECT_TRUE(capability_->scanning_or_searching_);
1478 capability_->cellular()->modem_state_ = Cellular::kModemStateEnabled;
1479 capability_->UpdateScanningProperty();
1480 EXPECT_TRUE(capability_->scanning_or_searching_);
1481 capability_->cellular()->modem_state_ = Cellular::kModemStateSearching;
1482 capability_->UpdateScanningProperty();
1483 EXPECT_TRUE(capability_->scanning_or_searching_);
1484 capability_->cellular()->modem_state_ = Cellular::kModemStateRegistered;
1485 capability_->UpdateScanningProperty();
1486 EXPECT_FALSE(capability_->scanning_or_searching_);
1487 capability_->cellular()->modem_state_ = Cellular::kModemStateConnecting;
1488 capability_->UpdateScanningProperty();
1489 EXPECT_FALSE(capability_->scanning_or_searching_);
1490 capability_->cellular()->modem_state_ = Cellular::kModemStateConnected;
1491 capability_->UpdateScanningProperty();
1492 EXPECT_FALSE(capability_->scanning_or_searching_);
1493 capability_->cellular()->modem_state_ = Cellular::kModemStateDisconnecting;
1494 capability_->UpdateScanningProperty();
1495 EXPECT_FALSE(capability_->scanning_or_searching_);
Ben Chane1e1e562013-01-26 00:39:01 -08001496
Ben Chan40a2f862013-02-13 17:44:38 -08001497 // Modem with an unactivated service in the 'enabled' or 'searching' state
1498 capability_->cellular()->modem_state_ = Cellular::kModemStateEnabled;
Ben Chane1e1e562013-01-26 00:39:01 -08001499 capability_->mdn_ = "0000000000";
Ben Chane1e1e562013-01-26 00:39:01 -08001500 CellularService::OLP olp;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001501 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(), GetOLPByMCCMNC(_))
Ben Chan40a2f862013-02-13 17:44:38 -08001502 .WillRepeatedly(Return(&olp));
1503 capability_->UpdateScanningProperty();
1504 EXPECT_FALSE(capability_->scanning_or_searching_);
1505
1506 capability_->cellular()->modem_state_ = Cellular::kModemStateSearching;
Ben Chane1e1e562013-01-26 00:39:01 -08001507 capability_->UpdateScanningProperty();
1508 EXPECT_FALSE(capability_->scanning_or_searching_);
Ben Chan8a2c01e2013-01-23 10:09:14 -08001509}
1510
Arman Uguray1361c032013-02-11 17:53:39 -08001511TEST_F(CellularCapabilityUniversalTimerTest, UpdateScanningPropertyTimeout) {
1512 SetUp();
1513 capability_->InitProxies();
1514
1515 EXPECT_FALSE(capability_->scanning_or_searching_);
1516 EXPECT_TRUE(
1517 capability_->scanning_or_searching_timeout_callback_.IsCancelled());
1518 capability_->UpdateScanningProperty();
1519 EXPECT_FALSE(capability_->scanning_or_searching_);
1520 EXPECT_TRUE(
1521 capability_->scanning_or_searching_timeout_callback_.IsCancelled());
1522
1523 EXPECT_CALL(mock_dispatcher_,
1524 PostDelayedTask(
1525 _,
1526 CellularCapabilityUniversal::
1527 kDefaultScanningOrSearchingTimeoutMilliseconds));
1528
1529 capability_->scanning_ = true;
1530 capability_->UpdateScanningProperty();
1531 EXPECT_FALSE(
1532 capability_->scanning_or_searching_timeout_callback_.IsCancelled());
1533 EXPECT_TRUE(capability_->scanning_or_searching_);
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001534 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
Arman Uguray1361c032013-02-11 17:53:39 -08001535
1536 EXPECT_CALL(mock_dispatcher_,
1537 PostDelayedTask(
1538 _,
1539 CellularCapabilityUniversal::
1540 kDefaultScanningOrSearchingTimeoutMilliseconds))
1541 .Times(0);
1542
1543 capability_->scanning_ = false;
1544 capability_->UpdateScanningProperty();
1545 EXPECT_TRUE(
1546 capability_->scanning_or_searching_timeout_callback_.IsCancelled());
1547 EXPECT_FALSE(capability_->scanning_or_searching_);
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001548 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
Arman Uguray1361c032013-02-11 17:53:39 -08001549
1550 EXPECT_CALL(mock_dispatcher_,
1551 PostDelayedTask(
1552 _,
1553 CellularCapabilityUniversal::
1554 kDefaultScanningOrSearchingTimeoutMilliseconds))
1555 .WillOnce(InvokeWithoutArgs(
1556 this,
1557 &CellularCapabilityUniversalTest::InvokeScanningOrSearchingTimeout));
1558
1559 capability_->scanning_ = true;
1560 capability_->UpdateScanningProperty();
1561 // The callback has been scheduled
1562 EXPECT_FALSE(
1563 capability_->scanning_or_searching_timeout_callback_.IsCancelled());
1564 // Our mock invocation worked
1565 EXPECT_FALSE(capability_->scanning_or_searching_);
1566}
1567
1568TEST_F(CellularCapabilityUniversalMainTest, UpdateStorageIdentifier) {
Arman Ugurayc9533572013-01-22 17:34:20 -08001569 CellularOperatorInfo::CellularOperator provider;
Arman Ugurayc9533572013-01-22 17:34:20 -08001570
1571 SetService();
1572
1573 const string prefix = "cellular_" + string(kMachineAddress) + "_";
Arman Uguray2717a102013-01-29 23:36:06 -08001574 string default_identifier_pattern =
1575 prefix + string(CellularCapabilityUniversal::kGenericServiceNamePrefix);
1576 std::replace_if(default_identifier_pattern.begin(),
1577 default_identifier_pattern.end(),
1578 &Service::IllegalChar, '_');
1579 default_identifier_pattern += "*";
Arman Ugurayc9533572013-01-22 17:34:20 -08001580
1581 // |capability_->operator_id_| is "".
1582 capability_->UpdateStorageIdentifier();
1583 EXPECT_TRUE(::MatchPattern(cellular_->service()->storage_identifier_,
1584 default_identifier_pattern));
1585
1586 // GetCellularOperatorByMCCMNC returns NULL.
1587 capability_->operator_id_ = "1";
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001588 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(),
Arman Ugurayc9533572013-01-22 17:34:20 -08001589 GetCellularOperatorByMCCMNC(capability_->operator_id_))
1590 .WillOnce(
1591 Return((const CellularOperatorInfo::CellularOperator *)NULL));
1592
1593 capability_->UpdateStorageIdentifier();
1594 EXPECT_TRUE(::MatchPattern(cellular_->service()->storage_identifier_,
1595 default_identifier_pattern));
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001596 Mock::VerifyAndClearExpectations(modem_info_.mock_cellular_operator_info());
Arman Ugurayc9533572013-01-22 17:34:20 -08001597
1598 // |capability_->imsi_| is not ""
1599 capability_->imsi_ = "TESTIMSI";
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001600 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(),
Arman Ugurayc9533572013-01-22 17:34:20 -08001601 GetCellularOperatorByMCCMNC(capability_->operator_id_))
1602 .WillOnce(
1603 Return((const CellularOperatorInfo::CellularOperator *)NULL));
1604
1605 capability_->UpdateStorageIdentifier();
1606 EXPECT_EQ(prefix + "TESTIMSI", cellular_->service()->storage_identifier_);
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001607 Mock::VerifyAndClearExpectations(modem_info_.mock_cellular_operator_info());
Arman Ugurayc9533572013-01-22 17:34:20 -08001608
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001609 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(),
Arman Ugurayc9533572013-01-22 17:34:20 -08001610 GetCellularOperatorByMCCMNC(capability_->operator_id_))
1611 .Times(2)
1612 .WillRepeatedly(Return(&provider));
1613
1614 // |provider.identifier_| is "".
1615 capability_->UpdateStorageIdentifier();
1616 EXPECT_EQ(prefix + "TESTIMSI", cellular_->service()->storage_identifier_);
1617
1618 // Success.
1619 provider.identifier_ = "testidentifier";
1620 capability_->UpdateStorageIdentifier();
1621 EXPECT_EQ(prefix + "testidentifier",
1622 cellular_->service()->storage_identifier_);
1623}
1624
Arman Uguray1361c032013-02-11 17:53:39 -08001625TEST_F(CellularCapabilityUniversalMainTest, UpdateOLP) {
Ben Chan6d0d1e72012-11-06 21:19:28 -08001626 CellularService::OLP test_olp;
1627 test_olp.SetURL("http://testurl");
1628 test_olp.SetMethod("POST");
Arman Uguray72fab6a2013-01-10 19:32:42 -08001629 test_olp.SetPostData("imei=${imei}&imsi=${imsi}&mdn=${mdn}&"
1630 "min=${min}&iccid=${iccid}");
Ben Chan6d0d1e72012-11-06 21:19:28 -08001631
Ben Chan6d0d1e72012-11-06 21:19:28 -08001632 capability_->imei_ = "1";
1633 capability_->imsi_ = "2";
1634 capability_->mdn_ = "3";
Ben Chan6d0d1e72012-11-06 21:19:28 -08001635 capability_->min_ = "5";
1636 capability_->sim_identifier_ = "6";
1637 capability_->operator_id_ = "123456";
Ben Chan6d0d1e72012-11-06 21:19:28 -08001638
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001639 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(),
Arman Ugurayf4c61812013-01-10 18:58:39 -08001640 GetOLPByMCCMNC(capability_->operator_id_))
1641 .WillRepeatedly(Return(&test_olp));
Ben Chan6d0d1e72012-11-06 21:19:28 -08001642
1643 SetService();
1644 capability_->UpdateOLP();
1645 const CellularService::OLP &olp = cellular_->service()->olp();
1646 EXPECT_EQ("http://testurl", olp.GetURL());
1647 EXPECT_EQ("POST", olp.GetMethod());
Arman Uguray72fab6a2013-01-10 19:32:42 -08001648 EXPECT_EQ("imei=1&imsi=2&mdn=3&min=5&iccid=6",
Ben Chan6d0d1e72012-11-06 21:19:28 -08001649 olp.GetPostData());
1650}
1651
Arman Ugurayc7b15602013-02-16 00:56:18 -08001652TEST_F(CellularCapabilityUniversalMainTest, IsMdnValid) {
1653 capability_->mdn_.clear();
1654 EXPECT_FALSE(capability_->IsMdnValid());
1655 capability_->mdn_ = "0000000";
1656 EXPECT_FALSE(capability_->IsMdnValid());
1657 capability_->mdn_ = "0000001";
1658 EXPECT_TRUE(capability_->IsMdnValid());
1659 capability_->mdn_ = "1231223";
1660 EXPECT_TRUE(capability_->IsMdnValid());
1661}
1662
Arman Uguraya14941d2013-04-12 16:58:26 -07001663TEST_F(CellularCapabilityUniversalTimerTest, CompleteActivation) {
Arman Ugurayefea6e02013-02-21 13:28:04 -08001664 const char kIccid[] = "1234567";
1665
1666 capability_->mdn_.clear();
1667 capability_->sim_identifier_.clear();
Arman Ugurayefea6e02013-02-21 13:28:04 -08001668
Arman Ugurayefea6e02013-02-21 13:28:04 -08001669 EXPECT_CALL(*service_,
1670 SetActivationState(flimflam::kActivationStateActivating))
1671 .Times(0);
Arman Uguray41cc6342013-03-29 16:34:39 -07001672 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1673 SetActivationState(
1674 PendingActivationStore::kIdentifierICCID, _, _))
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001675 .Times(0);
Arman Uguraya14941d2013-04-12 16:58:26 -07001676 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(0);
Arman Ugurayefea6e02013-02-21 13:28:04 -08001677 Error error;
1678 capability_->CompleteActivation(&error);
Arman Uguray41cc6342013-03-29 16:34:39 -07001679 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001680 Mock::VerifyAndClearExpectations(service_);
Arman Uguraya14941d2013-04-12 16:58:26 -07001681 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
1682 EXPECT_TRUE(
1683 capability_->activation_wait_for_registration_callback_.IsCancelled());
Arman Ugurayefea6e02013-02-21 13:28:04 -08001684
1685 capability_->sim_identifier_ = kIccid;
Arman Uguray41cc6342013-03-29 16:34:39 -07001686 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1687 SetActivationState(PendingActivationStore::kIdentifierICCID,
1688 kIccid,
1689 PendingActivationStore::kStatePending))
Arman Ugurayefea6e02013-02-21 13:28:04 -08001690 .Times(1);
1691 EXPECT_CALL(*service_,
1692 SetActivationState(flimflam::kActivationStateActivating))
1693 .Times(1);
Arman Uguraya14941d2013-04-12 16:58:26 -07001694 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(1);
Arman Ugurayefea6e02013-02-21 13:28:04 -08001695 capability_->CompleteActivation(&error);
Arman Uguray41cc6342013-03-29 16:34:39 -07001696 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001697 Mock::VerifyAndClearExpectations(service_);
Arman Uguraya14941d2013-04-12 16:58:26 -07001698 Mock::VerifyAndClearExpectations(&mock_dispatcher_);
1699 EXPECT_FALSE(
1700 capability_->activation_wait_for_registration_callback_.IsCancelled());
Arman Ugurayefea6e02013-02-21 13:28:04 -08001701
Arman Uguray41cc6342013-03-29 16:34:39 -07001702 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1703 SetActivationState(PendingActivationStore::kIdentifierICCID,
1704 kIccid,
1705 PendingActivationStore::kStatePending))
Arman Ugurayefea6e02013-02-21 13:28:04 -08001706 .Times(0);
1707 EXPECT_CALL(*service_,
1708 SetActivationState(flimflam::kActivationStateActivating))
1709 .Times(0);
Arman Uguraya14941d2013-04-12 16:58:26 -07001710 EXPECT_CALL(mock_dispatcher_, PostDelayedTask(_, _)).Times(0);
Arman Ugurayefea6e02013-02-21 13:28:04 -08001711 capability_->mdn_ = "1231231212";
1712 capability_->CompleteActivation(&error);
1713}
1714
1715TEST_F(CellularCapabilityUniversalMainTest, UpdateServiceActivationState) {
1716 const char kIccid[] = "1234567";
Arman Ugurayefea6e02013-02-21 13:28:04 -08001717 capability_->sim_identifier_.clear();
1718 capability_->mdn_ = "0000000000";
Arman Ugurayefea6e02013-02-21 13:28:04 -08001719 CellularService::OLP olp;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001720 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(), GetOLPByMCCMNC(_))
Arman Ugurayefea6e02013-02-21 13:28:04 -08001721 .WillRepeatedly(Return(&olp));
1722
Arman Uguray6bb252d2013-05-15 14:29:53 -07001723 service_->SetAutoConnect(false);
Arman Ugurayefea6e02013-02-21 13:28:04 -08001724 EXPECT_CALL(*service_,
1725 SetActivationState(flimflam::kActivationStateNotActivated))
1726 .Times(1);
1727 capability_->UpdateServiceActivationState();
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001728 Mock::VerifyAndClearExpectations(service_);
Arman Uguray6bb252d2013-05-15 14:29:53 -07001729 EXPECT_FALSE(service_->auto_connect());
Arman Ugurayefea6e02013-02-21 13:28:04 -08001730
1731 capability_->mdn_ = "1231231122";
1732 EXPECT_CALL(*service_,
1733 SetActivationState(flimflam::kActivationStateActivated))
1734 .Times(1);
1735 capability_->UpdateServiceActivationState();
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001736 Mock::VerifyAndClearExpectations(service_);
Arman Uguray6bb252d2013-05-15 14:29:53 -07001737 EXPECT_TRUE(service_->auto_connect());
Arman Ugurayefea6e02013-02-21 13:28:04 -08001738
Arman Uguray6bb252d2013-05-15 14:29:53 -07001739 service_->SetAutoConnect(false);
Arman Ugurayefea6e02013-02-21 13:28:04 -08001740 capability_->mdn_ = "0000000000";
1741 capability_->sim_identifier_ = kIccid;
Arman Uguray41cc6342013-03-29 16:34:39 -07001742 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1743 GetActivationState(PendingActivationStore::kIdentifierICCID,
1744 kIccid))
Arman Ugurayefea6e02013-02-21 13:28:04 -08001745 .Times(2)
Arman Uguray41cc6342013-03-29 16:34:39 -07001746 .WillOnce(Return(PendingActivationStore::kStatePending))
1747 .WillOnce(Return(PendingActivationStore::kStatePendingTimeout));
Arman Ugurayefea6e02013-02-21 13:28:04 -08001748 EXPECT_CALL(*service_,
1749 SetActivationState(flimflam::kActivationStateActivating))
1750 .Times(1);
1751 capability_->UpdateServiceActivationState();
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001752 Mock::VerifyAndClearExpectations(service_);
Arman Uguray41cc6342013-03-29 16:34:39 -07001753 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Uguray6bb252d2013-05-15 14:29:53 -07001754 EXPECT_FALSE(service_->auto_connect());
Arman Ugurayefea6e02013-02-21 13:28:04 -08001755
Arman Uguray41cc6342013-03-29 16:34:39 -07001756 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1757 GetActivationState(PendingActivationStore::kIdentifierICCID,
1758 kIccid))
Arman Ugurayefea6e02013-02-21 13:28:04 -08001759 .Times(2)
Arman Uguray41cc6342013-03-29 16:34:39 -07001760 .WillRepeatedly(Return(PendingActivationStore::kStateActivated));
Arman Ugurayefea6e02013-02-21 13:28:04 -08001761 EXPECT_CALL(*service_,
1762 SetActivationState(flimflam::kActivationStateActivated))
1763 .Times(1);
1764 capability_->UpdateServiceActivationState();
Arman Uguray6bb252d2013-05-15 14:29:53 -07001765 Mock::VerifyAndClearExpectations(service_);
1766 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
1767 EXPECT_TRUE(service_->auto_connect());
Arman Ugurayefea6e02013-02-21 13:28:04 -08001768}
1769
Arman Uguraya14941d2013-04-12 16:58:26 -07001770TEST_F(CellularCapabilityUniversalMainTest, ActivationWaitForRegisterTimeout) {
1771 const char kIccid[] = "1234567";
1772
1773 mm1::MockModemProxy *modem_proxy = modem_proxy_.get();
1774 capability_->InitProxies();
Arman Uguray41cc6342013-03-29 16:34:39 -07001775 EXPECT_CALL(*modem_proxy, Reset(_, _, _)).Times(0);
1776 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1777 SetActivationState(_, _, _))
Arman Uguraya14941d2013-04-12 16:58:26 -07001778 .Times(0);
1779
1780 // No ICCID, no MDN
1781 capability_->sim_identifier_.clear();
1782 capability_->mdn_.clear();
1783 capability_->reset_done_ = false;
1784 capability_->OnActivationWaitForRegisterTimeout();
1785
1786 // State is not activated.
1787 capability_->sim_identifier_ = kIccid;
Arman Uguray41cc6342013-03-29 16:34:39 -07001788 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1789 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1790 .WillOnce(Return(PendingActivationStore::kStateActivated))
1791 .WillRepeatedly(Return(PendingActivationStore::kStatePending));
Arman Uguraya14941d2013-04-12 16:58:26 -07001792 capability_->OnActivationWaitForRegisterTimeout();
1793
1794 // Valid MDN.
1795 capability_->mdn_ = "0000000001";
1796 capability_->OnActivationWaitForRegisterTimeout();
1797
1798 // Invalid MDN, reset done.
1799 capability_->mdn_ = "0000000000";
1800 capability_->reset_done_ = true;
1801 capability_->OnActivationWaitForRegisterTimeout();
1802
1803 Mock::VerifyAndClearExpectations(modem_proxy);
Arman Uguray41cc6342013-03-29 16:34:39 -07001804 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Uguraya14941d2013-04-12 16:58:26 -07001805
1806 // Reset not done.
1807 capability_->reset_done_ = false;
1808 EXPECT_CALL(*modem_proxy, Reset(_,_,_)).Times(1);
Arman Uguray41cc6342013-03-29 16:34:39 -07001809 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1810 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
1811 .WillOnce(Return(PendingActivationStore::kStatePending));
1812 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1813 SetActivationState(PendingActivationStore::kIdentifierICCID,
1814 kIccid,
1815 PendingActivationStore::kStatePendingTimeout))
Arman Uguraya14941d2013-04-12 16:58:26 -07001816 .Times(1);
1817 capability_->OnActivationWaitForRegisterTimeout();
1818}
1819
Arman Uguray0a3e2792013-01-17 16:31:50 -08001820TEST_F(CellularCapabilityUniversalMainTest, UpdatePendingActivationState) {
Arman Ugurayc7b15602013-02-16 00:56:18 -08001821 const char kIccid[] = "1234567";
1822
1823 mm1::MockModemProxy *modem_proxy = modem_proxy_.get();
Arman Ugurayc7b15602013-02-16 00:56:18 -08001824 capability_->InitProxies();
1825 capability_->registration_state_ =
1826 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING;
1827
1828 // No MDN, no ICCID.
1829 capability_->mdn_ = "0000000";
1830 capability_->sim_identifier_.clear();
Arman Uguray41cc6342013-03-29 16:34:39 -07001831 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1832 GetActivationState(PendingActivationStore::kIdentifierICCID, _))
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001833 .Times(0);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001834 capability_->UpdatePendingActivationState();
Arman Uguray41cc6342013-03-29 16:34:39 -07001835 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Ugurayc7b15602013-02-16 00:56:18 -08001836
1837 // ICCID known.
1838 capability_->sim_identifier_ = kIccid;
1839
1840 // After the modem has reset.
1841 capability_->reset_done_ = true;
Arman Uguray41cc6342013-03-29 16:34:39 -07001842 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1843 GetActivationState(PendingActivationStore::kIdentifierICCID,
1844 kIccid))
1845 .Times(1).WillOnce(Return(PendingActivationStore::kStatePending));
1846 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1847 SetActivationState(PendingActivationStore::kIdentifierICCID,
1848 kIccid,
1849 PendingActivationStore::kStateActivated))
Arman Ugurayc7b15602013-02-16 00:56:18 -08001850 .Times(1);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001851 capability_->UpdatePendingActivationState();
Arman Uguray41cc6342013-03-29 16:34:39 -07001852 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Ugurayc7b15602013-02-16 00:56:18 -08001853
1854 // Before reset, not registered.
1855 capability_->reset_done_ = false;
Arman Uguray41cc6342013-03-29 16:34:39 -07001856 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1857 GetActivationState(PendingActivationStore::kIdentifierICCID,
1858 kIccid))
1859 .Times(2).WillRepeatedly(Return(PendingActivationStore::kStatePending));
Arman Ugurayefea6e02013-02-21 13:28:04 -08001860 EXPECT_CALL(*service_,
1861 SetActivationState(flimflam::kActivationStateActivating))
1862 .Times(2);
Arman Ugurayc7b15602013-02-16 00:56:18 -08001863 EXPECT_CALL(*modem_proxy, Reset(_, _, _)).Times(0);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001864 capability_->UpdatePendingActivationState();
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001865 Mock::VerifyAndClearExpectations(modem_proxy);
Arman Ugurayc7b15602013-02-16 00:56:18 -08001866
1867 // Before reset, registered.
1868 capability_->registration_state_ =
1869 MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
1870 EXPECT_CALL(*modem_proxy, Reset(_, _, _)).Times(1);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001871 capability_->UpdatePendingActivationState();
Arman Uguray41cc6342013-03-29 16:34:39 -07001872 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Ugurayc7b15602013-02-16 00:56:18 -08001873
1874 // Not registered.
1875 capability_->registration_state_ =
1876 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING;
Arman Uguray41cc6342013-03-29 16:34:39 -07001877 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1878 GetActivationState(PendingActivationStore::kIdentifierICCID,
1879 kIccid))
1880 .Times(2).WillRepeatedly(Return(PendingActivationStore::kStateActivated));
Arman Ugurayc7b15602013-02-16 00:56:18 -08001881 EXPECT_CALL(*service_, AutoConnect()).Times(0);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001882 capability_->UpdatePendingActivationState();
Prathmesh Prabhub5fde532013-03-19 17:36:09 -07001883 Mock::VerifyAndClearExpectations(service_);
Arman Ugurayc7b15602013-02-16 00:56:18 -08001884
1885 // Service, registered.
1886 capability_->registration_state_ =
1887 MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
1888 EXPECT_CALL(*service_, AutoConnect()).Times(1);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001889 capability_->UpdatePendingActivationState();
Arman Ugurayc7b15602013-02-16 00:56:18 -08001890
1891 cellular_->service_->activation_state_ =
1892 flimflam::kActivationStateNotActivated;
1893
Arman Uguray41cc6342013-03-29 16:34:39 -07001894 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Uguraya14941d2013-04-12 16:58:26 -07001895
Arman Ugurayc7b15602013-02-16 00:56:18 -08001896 // Device is connected.
1897 cellular_->state_ = Cellular::kStateConnected;
1898 EXPECT_CALL(*service_,
1899 SetActivationState(flimflam::kActivationStateActivated))
1900 .Times(3);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001901 capability_->UpdatePendingActivationState();
Arman Ugurayc7b15602013-02-16 00:56:18 -08001902
1903 // Device is linked.
1904 cellular_->state_ = Cellular::kStateLinked;
Arman Uguray0a3e2792013-01-17 16:31:50 -08001905 capability_->UpdatePendingActivationState();
Arman Ugurayc7b15602013-02-16 00:56:18 -08001906
1907 // Got valid MDN.
1908 cellular_->state_ = Cellular::kStateRegistered;
1909 capability_->mdn_ = "1231223";
Arman Uguray41cc6342013-03-29 16:34:39 -07001910 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1911 RemoveEntry(PendingActivationStore::kIdentifierICCID, kIccid))
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07001912 .Times(1);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001913 capability_->UpdatePendingActivationState();
Arman Uguraya14941d2013-04-12 16:58:26 -07001914
1915 Mock::VerifyAndClearExpectations(service_);
Arman Uguray41cc6342013-03-29 16:34:39 -07001916 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Uguraya14941d2013-04-12 16:58:26 -07001917
1918 // Timed out, not registered.
1919 capability_->mdn_.clear();
Arman Uguray41cc6342013-03-29 16:34:39 -07001920 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1921 GetActivationState(PendingActivationStore::kIdentifierICCID,
1922 kIccid))
Arman Uguraya14941d2013-04-12 16:58:26 -07001923 .Times(1)
Arman Uguray41cc6342013-03-29 16:34:39 -07001924 .WillOnce(Return(PendingActivationStore::kStatePendingTimeout));
Arman Uguraya14941d2013-04-12 16:58:26 -07001925 capability_->registration_state_ =
1926 MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING;
Arman Uguray41cc6342013-03-29 16:34:39 -07001927 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1928 SetActivationState(_, _, _))
Arman Uguraya14941d2013-04-12 16:58:26 -07001929 .Times(0);
1930 EXPECT_CALL(*service_, SetActivationState(_)).Times(0);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001931 capability_->UpdatePendingActivationState();
Arman Uguraya14941d2013-04-12 16:58:26 -07001932 Mock::VerifyAndClearExpectations(service_);
Arman Uguray41cc6342013-03-29 16:34:39 -07001933 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Uguraya14941d2013-04-12 16:58:26 -07001934
1935 // Timed out, registered.
Arman Uguray41cc6342013-03-29 16:34:39 -07001936 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1937 GetActivationState(PendingActivationStore::kIdentifierICCID,
1938 kIccid))
Arman Uguraya14941d2013-04-12 16:58:26 -07001939 .Times(1)
Arman Uguray41cc6342013-03-29 16:34:39 -07001940 .WillOnce(Return(PendingActivationStore::kStatePendingTimeout));
Arman Uguraya14941d2013-04-12 16:58:26 -07001941 capability_->registration_state_ =
1942 MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
Arman Uguray41cc6342013-03-29 16:34:39 -07001943 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
1944 SetActivationState(PendingActivationStore::kIdentifierICCID,
1945 kIccid,
1946 PendingActivationStore::kStateActivated))
Arman Uguraya14941d2013-04-12 16:58:26 -07001947 .Times(1);
1948 EXPECT_CALL(*service_,
1949 SetActivationState(flimflam::kActivationStateActivated))
1950 .Times(1);
Arman Uguray0a3e2792013-01-17 16:31:50 -08001951 capability_->UpdatePendingActivationState();
Arman Uguraya14941d2013-04-12 16:58:26 -07001952 Mock::VerifyAndClearExpectations(service_);
Arman Uguray41cc6342013-03-29 16:34:39 -07001953 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Arman Ugurayc7b15602013-02-16 00:56:18 -08001954}
1955
Arman Uguray1361c032013-02-11 17:53:39 -08001956TEST_F(CellularCapabilityUniversalMainTest, UpdateOperatorInfo) {
Darin Petkova4ca3c32012-08-17 16:05:24 +02001957 static const char kOperatorName[] = "Swisscom";
1958 InitProviderDB();
1959 capability_->serving_operator_.SetCode("22801");
1960 SetService();
1961 capability_->UpdateOperatorInfo();
1962 EXPECT_EQ(kOperatorName, capability_->serving_operator_.GetName());
1963 EXPECT_EQ("ch", capability_->serving_operator_.GetCountry());
1964 EXPECT_EQ(kOperatorName, cellular_->service()->serving_operator().GetName());
1965
1966 static const char kTestOperator[] = "Testcom";
1967 capability_->serving_operator_.SetName(kTestOperator);
1968 capability_->serving_operator_.SetCountry("");
1969 capability_->UpdateOperatorInfo();
1970 EXPECT_EQ(kTestOperator, capability_->serving_operator_.GetName());
1971 EXPECT_EQ("ch", capability_->serving_operator_.GetCountry());
1972 EXPECT_EQ(kTestOperator, cellular_->service()->serving_operator().GetName());
1973}
1974
Arman Uguray1361c032013-02-11 17:53:39 -08001975TEST_F(CellularCapabilityUniversalMainTest, UpdateOperatorInfoViaOperatorId) {
Ben Chan092b12b2012-11-07 22:04:05 -08001976 static const char kOperatorName[] = "Swisscom";
1977 static const char kOperatorId[] = "22801";
1978 InitProviderDB();
1979 capability_->serving_operator_.SetCode("");
1980 SetService();
1981 capability_->UpdateOperatorInfo();
1982 EXPECT_EQ("", capability_->serving_operator_.GetName());
1983 EXPECT_EQ("", capability_->serving_operator_.GetCountry());
1984 EXPECT_EQ("", cellular_->service()->serving_operator().GetName());
1985
1986 capability_->operator_id_ = kOperatorId;
1987
Ben Chan092b12b2012-11-07 22:04:05 -08001988 capability_->UpdateOperatorInfo();
1989 EXPECT_EQ(kOperatorId, capability_->serving_operator_.GetCode());
1990 EXPECT_EQ(kOperatorName, capability_->serving_operator_.GetName());
1991 EXPECT_EQ("ch", capability_->serving_operator_.GetCountry());
1992 EXPECT_EQ(kOperatorName, cellular_->service()->serving_operator().GetName());
1993}
1994
Arman Uguray1361c032013-02-11 17:53:39 -08001995TEST_F(CellularCapabilityUniversalMainTest, CreateFriendlyServiceName) {
Darin Petkova4ca3c32012-08-17 16:05:24 +02001996 CellularCapabilityUniversal::friendly_service_name_id_ = 0;
Arman Uguray2717a102013-01-29 23:36:06 -08001997 EXPECT_EQ("Mobile Network 0", capability_->CreateFriendlyServiceName());
1998 EXPECT_EQ("Mobile Network 1", capability_->CreateFriendlyServiceName());
Darin Petkova4ca3c32012-08-17 16:05:24 +02001999
Ben Chan092b12b2012-11-07 22:04:05 -08002000 capability_->operator_id_ = "0123";
Ben Chan092b12b2012-11-07 22:04:05 -08002001 EXPECT_EQ("cellular_0123", capability_->CreateFriendlyServiceName());
2002 EXPECT_EQ("0123", capability_->serving_operator_.GetCode());
2003
Darin Petkova4ca3c32012-08-17 16:05:24 +02002004 capability_->serving_operator_.SetCode("1234");
2005 EXPECT_EQ("cellular_1234", capability_->CreateFriendlyServiceName());
2006
2007 static const char kHomeProvider[] = "The GSM Home Provider";
2008 cellular_->home_provider_.SetName(kHomeProvider);
2009 EXPECT_EQ("cellular_1234", capability_->CreateFriendlyServiceName());
2010 capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_HOME;
2011 EXPECT_EQ(kHomeProvider, capability_->CreateFriendlyServiceName());
2012
2013 static const char kTestOperator[] = "A GSM Operator";
2014 capability_->serving_operator_.SetName(kTestOperator);
2015 EXPECT_EQ(kTestOperator, capability_->CreateFriendlyServiceName());
2016
2017 capability_->registration_state_ = MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING;
2018 EXPECT_EQ(StringPrintf("%s | %s", kHomeProvider, kTestOperator),
2019 capability_->CreateFriendlyServiceName());
2020}
2021
Arman Uguray1361c032013-02-11 17:53:39 -08002022TEST_F(CellularCapabilityUniversalMainTest, IsServiceActivationRequired) {
Ben Chan15786032012-11-04 21:28:02 -08002023 capability_->mdn_ = "0000000000";
Ben Chan15786032012-11-04 21:28:02 -08002024 EXPECT_FALSE(capability_->IsServiceActivationRequired());
2025
Arman Ugurayf4c61812013-01-10 18:58:39 -08002026 CellularService::OLP olp;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -07002027 EXPECT_CALL(*modem_info_.mock_cellular_operator_info(), GetOLPByMCCMNC(_))
Arman Ugurayf4c61812013-01-10 18:58:39 -08002028 .WillOnce(Return((const CellularService::OLP *)NULL))
2029 .WillRepeatedly(Return(&olp));
Ben Chan15786032012-11-04 21:28:02 -08002030 EXPECT_FALSE(capability_->IsServiceActivationRequired());
2031
2032 capability_->mdn_ = "";
2033 EXPECT_FALSE(capability_->IsServiceActivationRequired());
2034 capability_->mdn_ = "1234567890";
2035 EXPECT_FALSE(capability_->IsServiceActivationRequired());
Ben Chan15786032012-11-04 21:28:02 -08002036 capability_->mdn_ = "0000000000";
2037 EXPECT_TRUE(capability_->IsServiceActivationRequired());
Arman Ugurayefea6e02013-02-21 13:28:04 -08002038
2039 const char kIccid[] = "1234567890";
2040 capability_->sim_identifier_ = kIccid;
Arman Uguray41cc6342013-03-29 16:34:39 -07002041 EXPECT_CALL(*modem_info_.mock_pending_activation_store(),
2042 GetActivationState(PendingActivationStore::kIdentifierICCID,
2043 kIccid))
2044 .WillOnce(Return(PendingActivationStore::kStateActivated))
2045 .WillOnce(Return(PendingActivationStore::kStatePending))
2046 .WillOnce(Return(PendingActivationStore::kStatePendingTimeout))
2047 .WillOnce(Return(PendingActivationStore::kStateUnknown));
Arman Ugurayefea6e02013-02-21 13:28:04 -08002048 EXPECT_FALSE(capability_->IsServiceActivationRequired());
Arman Uguraya14941d2013-04-12 16:58:26 -07002049 EXPECT_FALSE(capability_->IsServiceActivationRequired());
2050 EXPECT_FALSE(capability_->IsServiceActivationRequired());
2051 EXPECT_TRUE(capability_->IsServiceActivationRequired());
Arman Uguray41cc6342013-03-29 16:34:39 -07002052 Mock::VerifyAndClearExpectations(modem_info_.mock_pending_activation_store());
Ben Chan15786032012-11-04 21:28:02 -08002053}
2054
Arman Uguray1361c032013-02-11 17:53:39 -08002055TEST_F(CellularCapabilityUniversalMainTest, OnModemCurrentCapabilitiesChanged) {
Ben Chanfcca27b2013-01-22 15:03:44 -08002056 EXPECT_FALSE(capability_->scanning_supported_);
2057 capability_->OnModemCurrentCapabilitiesChanged(MM_MODEM_CAPABILITY_LTE);
2058 EXPECT_FALSE(capability_->scanning_supported_);
2059 capability_->OnModemCurrentCapabilitiesChanged(MM_MODEM_CAPABILITY_CDMA_EVDO);
2060 EXPECT_FALSE(capability_->scanning_supported_);
2061 capability_->OnModemCurrentCapabilitiesChanged(MM_MODEM_CAPABILITY_GSM_UMTS);
2062 EXPECT_TRUE(capability_->scanning_supported_);
2063 capability_->OnModemCurrentCapabilitiesChanged(
2064 MM_MODEM_CAPABILITY_GSM_UMTS | MM_MODEM_CAPABILITY_CDMA_EVDO);
2065 EXPECT_TRUE(capability_->scanning_supported_);
2066}
2067
Arman Uguray7af0fac2013-03-18 17:35:35 -07002068TEST_F(CellularCapabilityUniversalMainTest, GetNetworkTechnologyStringOnE362) {
2069 capability_->model_id_.clear();
2070 capability_->access_technologies_ = 0;
2071 EXPECT_TRUE(capability_->GetNetworkTechnologyString().empty());
2072
2073 capability_->model_id_ = CellularCapabilityUniversal::kE362ModelId;
2074 EXPECT_EQ(flimflam::kNetworkTechnologyLte,
2075 capability_->GetNetworkTechnologyString());
2076
2077 capability_->access_technologies_ = MM_MODEM_ACCESS_TECHNOLOGY_GPRS;
2078 EXPECT_EQ(flimflam::kNetworkTechnologyLte,
2079 capability_->GetNetworkTechnologyString());
2080
2081 capability_->model_id_.clear();
2082 EXPECT_EQ(flimflam::kNetworkTechnologyGprs,
2083 capability_->GetNetworkTechnologyString());
2084}
2085
Arman Ugurayf84a4242013-04-09 20:01:07 -07002086TEST_F(CellularCapabilityUniversalMainTest, ShouldDetectOutOfCredit) {
Arman Ugurayd42d8ec2013-04-08 19:28:21 -07002087 capability_->model_id_.clear();
Arman Ugurayf84a4242013-04-09 20:01:07 -07002088 EXPECT_FALSE(capability_->ShouldDetectOutOfCredit());
Arman Ugurayd42d8ec2013-04-08 19:28:21 -07002089 capability_->model_id_ = CellularCapabilityUniversal::kE362ModelId;
Arman Ugurayf84a4242013-04-09 20:01:07 -07002090 EXPECT_TRUE(capability_->ShouldDetectOutOfCredit());
Arman Ugurayd42d8ec2013-04-08 19:28:21 -07002091}
2092
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002093TEST_F(CellularCapabilityUniversalMainTest, SimLockStatusToProperty) {
2094 Error error;
2095 KeyValueStore store = capability_->SimLockStatusToProperty(&error);
2096 EXPECT_FALSE(store.GetBool(flimflam::kSIMLockEnabledProperty));
2097 EXPECT_TRUE(store.GetString(flimflam::kSIMLockTypeProperty).empty());
2098 EXPECT_EQ(0, store.GetUint(flimflam::kSIMLockRetriesLeftProperty));
2099
2100 capability_->sim_lock_status_.enabled = true;
2101 capability_->sim_lock_status_.retries_left = 3;
2102 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
2103 store = capability_->SimLockStatusToProperty(&error);
2104 EXPECT_TRUE(store.GetBool(flimflam::kSIMLockEnabledProperty));
2105 EXPECT_EQ("sim-pin", store.GetString(flimflam::kSIMLockTypeProperty));
2106 EXPECT_EQ(3, store.GetUint(flimflam::kSIMLockRetriesLeftProperty));
2107
2108 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK;
2109 store = capability_->SimLockStatusToProperty(&error);
2110 EXPECT_EQ("sim-puk", store.GetString(flimflam::kSIMLockTypeProperty));
2111
2112 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN2;
2113 store = capability_->SimLockStatusToProperty(&error);
2114 EXPECT_TRUE(store.GetString(flimflam::kSIMLockTypeProperty).empty());
2115
2116 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK2;
2117 store = capability_->SimLockStatusToProperty(&error);
2118 EXPECT_TRUE(store.GetString(flimflam::kSIMLockTypeProperty).empty());
2119}
2120
2121TEST_F(CellularCapabilityUniversalMainTest, OnLockRetriesChanged) {
2122 CellularCapabilityUniversal::LockRetryData data;
2123 const uint32 kDefaultRetries = 999;
2124
2125 capability_->OnLockRetriesChanged(data);
2126 EXPECT_EQ(kDefaultRetries, capability_->sim_lock_status_.retries_left);
2127
2128 data[MM_MODEM_LOCK_SIM_PIN] = 3;
Arman Ugurayea5ff272013-06-25 10:28:02 -07002129 data[MM_MODEM_LOCK_SIM_PUK] = 10;
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002130 capability_->OnLockRetriesChanged(data);
Arman Uguray192ad822013-06-13 19:49:16 -07002131 EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002132
Arman Ugurayea5ff272013-06-25 10:28:02 -07002133 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PUK;
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002134 capability_->OnLockRetriesChanged(data);
Arman Ugurayea5ff272013-06-25 10:28:02 -07002135 EXPECT_EQ(10, capability_->sim_lock_status_.retries_left);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002136
2137 capability_->sim_lock_status_.lock_type = MM_MODEM_LOCK_SIM_PIN;
2138 capability_->OnLockRetriesChanged(data);
2139 EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
Arman Uguray192ad822013-06-13 19:49:16 -07002140
2141 data.clear();
2142 capability_->OnLockRetriesChanged(data);
2143 EXPECT_EQ(kDefaultRetries, capability_->sim_lock_status_.retries_left);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002144}
2145
2146TEST_F(CellularCapabilityUniversalMainTest, OnLockTypeChanged) {
2147 EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
2148
2149 capability_->OnLockTypeChanged(MM_MODEM_LOCK_NONE);
2150 EXPECT_EQ(MM_MODEM_LOCK_NONE, capability_->sim_lock_status_.lock_type);
Arman Uguray4ef02fd2013-06-13 20:14:20 -07002151 EXPECT_FALSE(capability_->sim_lock_status_.enabled);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002152
2153 capability_->OnLockTypeChanged(MM_MODEM_LOCK_SIM_PIN);
2154 EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
Arman Uguray4ef02fd2013-06-13 20:14:20 -07002155 EXPECT_TRUE(capability_->sim_lock_status_.enabled);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002156
Arman Uguray4ef02fd2013-06-13 20:14:20 -07002157 capability_->sim_lock_status_.enabled = false;
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002158 capability_->OnLockTypeChanged(MM_MODEM_LOCK_SIM_PUK);
2159 EXPECT_EQ(MM_MODEM_LOCK_SIM_PUK, capability_->sim_lock_status_.lock_type);
Arman Uguray4ef02fd2013-06-13 20:14:20 -07002160 EXPECT_TRUE(capability_->sim_lock_status_.enabled);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002161}
2162
2163TEST_F(CellularCapabilityUniversalMainTest, OnSimLockPropertiesChanged) {
2164 EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
2165 EXPECT_EQ(0, capability_->sim_lock_status_.retries_left);
2166
2167 DBusPropertiesMap changed;
2168 vector<string> invalidated;
2169
2170 capability_->OnModemPropertiesChanged(changed, invalidated);
2171 EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
2172 EXPECT_EQ(0, capability_->sim_lock_status_.retries_left);
2173
2174 ::DBus::Variant variant;
2175 ::DBus::MessageIter writer = variant.writer();
2176
2177 // Unlock retries changed, but the SIM wasn't locked.
2178 CellularCapabilityUniversal::LockRetryData retry_data;
2179 retry_data[MM_MODEM_LOCK_SIM_PIN] = 3;
2180 writer << retry_data;
2181 changed[MM_MODEM_PROPERTY_UNLOCKRETRIES] = variant;
2182
2183 capability_->OnModemPropertiesChanged(changed, invalidated);
2184 EXPECT_EQ(MM_MODEM_LOCK_UNKNOWN, capability_->sim_lock_status_.lock_type);
Arman Uguray192ad822013-06-13 19:49:16 -07002185 EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002186
2187 // Unlock retries changed and the SIM got locked.
2188 variant.clear();
2189 writer = variant.writer();
2190 writer << static_cast<uint32>(MM_MODEM_LOCK_SIM_PIN);
2191 changed[MM_MODEM_PROPERTY_UNLOCKREQUIRED] = variant;
2192 capability_->OnModemPropertiesChanged(changed, invalidated);
2193 EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
2194 EXPECT_EQ(3, capability_->sim_lock_status_.retries_left);
2195
2196 // Only unlock retries changed.
2197 changed.erase(MM_MODEM_PROPERTY_UNLOCKREQUIRED);
2198 retry_data[MM_MODEM_LOCK_SIM_PIN] = 2;
2199 variant.clear();
2200 writer = variant.writer();
2201 writer << retry_data;
2202 changed[MM_MODEM_PROPERTY_UNLOCKRETRIES] = variant;
2203 capability_->OnModemPropertiesChanged(changed, invalidated);
2204 EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
2205 EXPECT_EQ(2, capability_->sim_lock_status_.retries_left);
Arman Uguray192ad822013-06-13 19:49:16 -07002206
2207 // Unlock retries changed with a value that doesn't match the current
Arman Ugurayea5ff272013-06-25 10:28:02 -07002208 // lock type. Default to whatever count is available.
Arman Uguray192ad822013-06-13 19:49:16 -07002209 retry_data.clear();
2210 retry_data[MM_MODEM_LOCK_SIM_PIN2] = 2;
2211 variant.clear();
2212 writer = variant.writer();
2213 writer << retry_data;
2214 changed[MM_MODEM_PROPERTY_UNLOCKRETRIES] = variant;
2215 capability_->OnModemPropertiesChanged(changed, invalidated);
2216 EXPECT_EQ(MM_MODEM_LOCK_SIM_PIN, capability_->sim_lock_status_.lock_type);
Arman Ugurayea5ff272013-06-25 10:28:02 -07002217 EXPECT_EQ(2, capability_->sim_lock_status_.retries_left);
Arman Ugurayc7e63af2013-06-13 17:07:32 -07002218}
2219
Jason Glasgowef965562012-04-10 16:12:35 -04002220} // namespace shill