shill: diags: Ignore disconnect events in transitional power states.

BUG=chromium-os:37141
TEST=unit tests; tested on device with suspend and shutdown

Change-Id: I62bed5180c54c92166323755892611c01a65fd8e
Reviewed-on: https://gerrit.chromium.org/gerrit/39645
Tested-by: Darin Petkov <petkov@chromium.org>
Commit-Ready: Darin Petkov <petkov@chromium.org>
Reviewed-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
diff --git a/service_unittest.cc b/service_unittest.cc
index 38be8c7..7852537 100644
--- a/service_unittest.cc
+++ b/service_unittest.cc
@@ -26,7 +26,9 @@
 #include "shill/mock_event_dispatcher.h"
 #include "shill/mock_log.h"
 #include "shill/mock_manager.h"
+#include "shill/mock_power_manager.h"
 #include "shill/mock_profile.h"
+#include "shill/mock_proxy_factory.h"
 #include "shill/mock_store.h"
 #include "shill/mock_time.h"
 #include "shill/property_store_inspector.h"
@@ -63,9 +65,12 @@
                                       dispatcher(),
                                       metrics(),
                                       &mock_manager_)),
-        storage_id_(ServiceUnderTest::kStorageId) {
+        storage_id_(ServiceUnderTest::kStorageId),
+        power_manager_(new MockPowerManager(NULL, &proxy_factory_)) {
     service_->time_ = &time_;
     service_->diagnostics_reporter_ = &diagnostics_reporter_;
+    mock_manager_.running_ = true;
+    mock_manager_.set_power_manager(power_manager_);  // Passes ownership.
   }
 
   virtual ~ServiceTest() {}
@@ -75,6 +80,25 @@
  protected:
   typedef scoped_refptr<MockProfile> MockProfileRefPtr;
 
+  class TestProxyFactory : public ProxyFactory {
+   public:
+    TestProxyFactory() {}
+
+    virtual PowerManagerProxyInterface *CreatePowerManagerProxy(
+        PowerManagerProxyDelegate *delegate) {
+      return NULL;
+    }
+
+   private:
+    DISALLOW_COPY_AND_ASSIGN(TestProxyFactory);
+  };
+
+  void SetManagerRunning(bool running) { mock_manager_.running_ = running; }
+
+  void SetPowerState(PowerManager::SuspendState state) {
+    power_manager_->power_state_ = state;
+  }
+
   void SetExplicitlyDisconnected(bool explicitly) {
     service_->explicitly_disconnected_ = explicitly;
   }
@@ -113,6 +137,8 @@
   MockTime time_;
   scoped_refptr<ServiceUnderTest> service_;
   string storage_id_;
+  TestProxyFactory proxy_factory_;
+  MockPowerManager *power_manager_;  // Owned by |mock_manager_|.
 };
 
 class AllMockServiceTest : public testing::Test {
@@ -1018,6 +1044,20 @@
   NoteDisconnectEvent();
   EXPECT_TRUE(GetDisconnects()->empty());
   EXPECT_TRUE(GetMisconnects()->empty());
+
+  // Disconnect while manager is stopped is a non-event.
+  SetStateField(Service::kStateOnline);
+  SetManagerRunning(false);
+  NoteDisconnectEvent();
+  EXPECT_TRUE(GetDisconnects()->empty());
+  EXPECT_TRUE(GetMisconnects()->empty());
+
+  // Disconnect while suspending is a non-event.
+  SetManagerRunning(true);
+  SetPowerState(PowerManager::kSuspending);
+  NoteDisconnectEvent();
+  EXPECT_TRUE(GetDisconnects()->empty());
+  EXPECT_TRUE(GetMisconnects()->empty());
 }
 
 TEST_F(ServiceTest, NoteDisconnectEventDisconnectOnce) {