OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/network/network_device_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/values.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/shill_device_client.h" |
| 13 #include "chromeos/dbus/shill_manager_client.h" |
| 14 #include "dbus/object_path.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class TestObserver : public NetworkDeviceHandler::Observer { |
| 23 public: |
| 24 TestObserver() : device_updates_(0) {} |
| 25 |
| 26 virtual void NetworkDevicesUpdated(const DeviceMap& devices) { |
| 27 ++device_updates_; |
| 28 devices_ = devices; |
| 29 } |
| 30 |
| 31 int device_updates() { return device_updates_; } |
| 32 const DeviceMap& devices() { return devices_; } |
| 33 |
| 34 private: |
| 35 int device_updates_; |
| 36 DeviceMap devices_; |
| 37 }; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 class NetworkDeviceHandlerTest : public testing::Test { |
| 42 public: |
| 43 NetworkDeviceHandlerTest() |
| 44 : manager_test_(NULL), |
| 45 device_test_(NULL) { |
| 46 } |
| 47 virtual ~NetworkDeviceHandlerTest() { |
| 48 } |
| 49 |
| 50 virtual void SetUp() OVERRIDE { |
| 51 // Initialize DBusThreadManager with a stub implementation. |
| 52 DBusThreadManager::InitializeWithStub(); |
| 53 // Get the test interface for manager / device. |
| 54 manager_test_ = |
| 55 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
| 56 ASSERT_TRUE(manager_test_); |
| 57 device_test_ = |
| 58 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); |
| 59 ASSERT_TRUE(device_test_); |
| 60 } |
| 61 |
| 62 virtual void TearDown() OVERRIDE { |
| 63 device_handler_->RemoveObserver(observer_.get()); |
| 64 observer_.reset(); |
| 65 device_handler_.reset(); |
| 66 DBusThreadManager::Shutdown(); |
| 67 } |
| 68 |
| 69 void AddDevice(const std::string& type, const std::string& id) { |
| 70 manager_test_->AddDevice(id); |
| 71 device_test_->AddDevice(id, type, std::string("/device/" + id), "/stub"); |
| 72 } |
| 73 |
| 74 void RemoveDevice(const std::string& id) { |
| 75 manager_test_->RemoveDevice(id); |
| 76 device_test_->RemoveDevice(id); |
| 77 } |
| 78 |
| 79 // Call this after any initial Shill client setup |
| 80 void SetupNetworkDeviceHandler() { |
| 81 device_handler_.reset(new NetworkDeviceHandler()); |
| 82 device_handler_->Init(); |
| 83 observer_.reset(new TestObserver()); |
| 84 device_handler_->AddObserver(observer_.get()); |
| 85 } |
| 86 |
| 87 protected: |
| 88 MessageLoopForUI message_loop_; |
| 89 scoped_ptr<TestObserver> observer_; |
| 90 scoped_ptr<NetworkDeviceHandler> device_handler_; |
| 91 ShillManagerClient::TestInterface* manager_test_; |
| 92 ShillDeviceClient::TestInterface* device_test_; |
| 93 |
| 94 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandlerTest); |
| 96 }; |
| 97 |
| 98 TEST_F(NetworkDeviceHandlerTest, NetworkDeviceHandlerStub) { |
| 99 SetupNetworkDeviceHandler(); |
| 100 EXPECT_FALSE(device_handler_->devices_ready()); |
| 101 |
| 102 message_loop_.RunUntilIdle(); |
| 103 EXPECT_EQ(1, observer_->device_updates()); |
| 104 EXPECT_TRUE(device_handler_->devices_ready()); |
| 105 // ShillManagerClient default stub entries are in shill_manager_client.cc. |
| 106 // TODO(stevenjb): Eliminate default stub entries and add them explicitly. |
| 107 const size_t kNumShillManagerClientStubImplDevices = 2; |
| 108 EXPECT_EQ(kNumShillManagerClientStubImplDevices, |
| 109 device_handler_->devices().size()); |
| 110 } |
| 111 |
| 112 TEST_F(NetworkDeviceHandlerTest, NetworkDeviceHandlerPropertyChanged) { |
| 113 // This relies on the stub dbus implementations for ShillManagerClient, |
| 114 SetupNetworkDeviceHandler(); |
| 115 |
| 116 message_loop_.RunUntilIdle(); |
| 117 EXPECT_EQ(1, observer_->device_updates()); |
| 118 const size_t kNumShillManagerClientStubImplDevices = 2; |
| 119 EXPECT_EQ(kNumShillManagerClientStubImplDevices, observer_->devices().size()); |
| 120 |
| 121 // Add a device. |
| 122 const std::string kTestDevicePath("test_wifi_device1"); |
| 123 AddDevice(flimflam::kTypeWifi, kTestDevicePath); |
| 124 message_loop_.RunUntilIdle(); |
| 125 EXPECT_EQ(2, observer_->device_updates()); |
| 126 EXPECT_EQ(kNumShillManagerClientStubImplDevices + 1, |
| 127 observer_->devices().size()); |
| 128 |
| 129 // Remove a device |
| 130 RemoveDevice(kTestDevicePath); |
| 131 message_loop_.RunUntilIdle(); |
| 132 EXPECT_EQ(3, observer_->device_updates()); |
| 133 EXPECT_EQ(kNumShillManagerClientStubImplDevices, observer_->devices().size()); |
| 134 } |
| 135 |
| 136 } // namespace chromeos |
OLD | NEW |