shill: cellular: Obtain IMSI when a GSM device is constructed.

This CL modifies CellularCapabilityGSM to obtain the IMSI value at
construction instead of when the modem is enabled. The IMSI value is
checked by Chrome to determine if a SIM is present before the device can
be enabled.

BUG=chromium-os:31651
TEST=Tested the following:
1. Build and run unit tests.
2. Verify that Gobi and Icera modems can be enabled as follows:
   a. Configure the modem in GSM and insert a SIM with no PIN lock.
   b. Disable the cellular device from UI.
   c. Reboot the Chromebook.
   d. Enable the cellular device from UI and it works.
   e. Repeat with a PIN-locked SIM.
3. Verify that Gobi and Icera modems cannot be enabled as follows:
   a. Configure the modem in GSM with no SIM inserted.
   b. Disable the cellular device from UI.
   c. Reboot the Chromebook.
   d. Enable the cellular device from UI and it does not work.

Change-Id: Iff21f972661cedb383b9a6820fdc7ba3160fd01a
Reviewed-on: https://gerrit.chromium.org/gerrit/29313
Commit-Ready: Ben Chan <benchan@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
diff --git a/cellular_capability_gsm_unittest.cc b/cellular_capability_gsm_unittest.cc
index 8460003..d8f6dae 100644
--- a/cellular_capability_gsm_unittest.cc
+++ b/cellular_capability_gsm_unittest.cc
@@ -53,7 +53,16 @@
 class CellularCapabilityGSMTest : public testing::Test {
  public:
   CellularCapabilityGSMTest()
-      : cellular_(new Cellular(&control_,
+      : create_card_proxy_from_factory_(false),
+        proxy_(new MockModemProxy()),
+        simple_proxy_(new MockModemSimpleProxy()),
+        card_proxy_(new MockModemGSMCardProxy()),
+        network_proxy_(new MockModemGSMNetworkProxy()),
+        proxy_factory_(this),
+        capability_(NULL),
+        device_adaptor_(NULL),
+        provider_db_(NULL),
+        cellular_(new Cellular(&control_,
                                &dispatcher_,
                                &metrics_,
                                NULL,
@@ -64,15 +73,8 @@
                                "",
                                "",
                                "",
-                               NULL)),
-        proxy_(new MockModemProxy()),
-        simple_proxy_(new MockModemSimpleProxy()),
-        card_proxy_(new MockModemGSMCardProxy()),
-        network_proxy_(new MockModemGSMNetworkProxy()),
-        proxy_factory_(this),
-        capability_(NULL),
-        device_adaptor_(NULL),
-        provider_db_(NULL) {}
+                               NULL,
+                               &proxy_factory_)) {}
 
   virtual ~CellularCapabilityGSMTest() {
     cellular_->service_ = NULL;
@@ -219,7 +221,12 @@
     virtual ModemGSMCardProxyInterface *CreateModemGSMCardProxy(
         const string &/*path*/,
         const string &/*service*/) {
-      return test_->card_proxy_.release();
+      // TODO(benchan): This code conditionally returns a NULL pointer to avoid
+      // CellularCapabilityGSM::InitProperties (and thus
+      // CellularCapabilityGSM::GetIMSI) from being called during the
+      // construction. Remove this workaround after refactoring the tests.
+      return test_->create_card_proxy_from_factory_ ?
+          test_->card_proxy_.release() : NULL;
     }
 
     virtual ModemGSMNetworkProxyInterface *CreateModemGSMNetworkProxy(
@@ -244,10 +251,6 @@
     capability_->network_proxy_.reset(network_proxy_.release());
   }
 
-  void SetProxyFactory() {
-    capability_->proxy_factory_ = &proxy_factory_;
-  }
-
   void SetAccessTechnology(uint32 technology) {
     capability_->access_technology_ = technology;
   }
@@ -298,13 +301,18 @@
   }
 
   void InitProxies() {
+    AllowCreateCardProxyFromFactory();
     capability_->InitProxies();
   }
 
+  void AllowCreateCardProxyFromFactory() {
+    create_card_proxy_from_factory_ = true;
+  }
+
   NiceMockControl control_;
   EventDispatcher dispatcher_;
   MockMetrics metrics_;
-  CellularRefPtr cellular_;
+  bool create_card_proxy_from_factory_;
   scoped_ptr<MockModemProxy> proxy_;
   scoped_ptr<MockModemSimpleProxy> simple_proxy_;
   scoped_ptr<MockModemGSMCardProxy> card_proxy_;
@@ -313,6 +321,7 @@
   CellularCapabilityGSM *capability_;  // Owned by |cellular_|.
   NiceMock<DeviceMockAdaptor> *device_adaptor_;  // Owned by |cellular_|.
   mobile_provider_db *provider_db_;
+  CellularRefPtr cellular_;
   ScanResultsCallback scan_callback_;  // saved for testing scan operations
 };
 
@@ -901,7 +910,7 @@
   EXPECT_CALL(*card_proxy_,
               GetMSISDN(_, _, CellularCapability::kTimeoutDefault))
       .WillOnce(Invoke(this, &CellularCapabilityGSMTest::InvokeGetMSISDN));
-  SetProxyFactory();
+  AllowCreateCardProxyFromFactory();
 
   Error error;
   capability_->StartModem(
@@ -917,7 +926,7 @@
   EXPECT_CALL(*card_proxy_,
               GetMSISDN(_, _, CellularCapability::kTimeoutDefault))
       .WillOnce(Invoke(this, &CellularCapabilityGSMTest::InvokeGetMSISDN));
-  SetProxyFactory();
+  AllowCreateCardProxyFromFactory();
 
   Error error;
   capability_->StartModem(
@@ -933,7 +942,7 @@
   EXPECT_CALL(*card_proxy_,
               GetMSISDN(_, _, CellularCapability::kTimeoutDefault))
       .WillOnce(Invoke(this, &CellularCapabilityGSMTest::InvokeGetMSISDNFail));
-  SetProxyFactory();
+  AllowCreateCardProxyFromFactory();
 
   Error error;
   capability_->StartModem(
@@ -950,7 +959,6 @@
               Connect(_, _, _, CellularCapabilityGSM::kTimeoutConnect))
        .WillOnce(Invoke(this, &CellularCapabilityGSMTest::InvokeConnectFail));
   EXPECT_CALL(*this, TestCallback(IsFailure()));
-  SetProxyFactory();
   InitProxies();
   EXPECT_FALSE(capability_->cellular()->service());
   Error error;