Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | #include <glib.h> |
| 5 | |
| 6 | #include <base/callback_old.h> |
| 7 | #include <base/logging.h> |
| 8 | #include <base/memory/ref_counted.h> |
| 9 | #include <base/message_loop.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | #include <gmock/gmock.h> |
| 12 | |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 13 | #include "shill/mock_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 14 | #include "shill/manager.h" |
| 15 | #include "shill/mock_device.h" |
| 16 | #include "shill/mock_service.h" |
| 17 | |
| 18 | namespace shill { |
| 19 | using ::testing::Test; |
| 20 | using ::testing::_; |
| 21 | using ::testing::NiceMock; |
| 22 | using ::testing::Return; |
| 23 | using std::vector; |
| 24 | |
| 25 | class ManagerTest : public Test { |
| 26 | public: |
| 27 | ManagerTest() |
| 28 | : manager_(&control_, &dispatcher_), |
| 29 | factory_(this) { |
| 30 | } |
| 31 | |
| 32 | bool IsDeviceRegistered(Device *device, Device::Technology tech) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 33 | vector<DeviceRefPtr> devices; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 34 | manager_.FilterByTechnology(tech, &devices); |
| 35 | return (devices.size() == 1 && devices[0].get() == device); |
| 36 | } |
| 37 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 38 | protected: |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 39 | MockControl control_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 40 | Manager manager_; |
| 41 | EventDispatcher dispatcher_; |
| 42 | ScopedRunnableMethodFactory<ManagerTest> factory_; |
| 43 | }; |
| 44 | |
| 45 | TEST_F(ManagerTest, DeviceRegistration) { |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 46 | scoped_refptr<MockDevice> mock_device(new NiceMock<MockDevice>(&control_, |
| 47 | &dispatcher_, |
| 48 | &manager_, |
| 49 | "null0", |
| 50 | -1)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 51 | ON_CALL(*mock_device.get(), TechnologyIs(Device::kEthernet)) |
| 52 | .WillByDefault(Return(true)); |
| 53 | |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 54 | scoped_refptr<MockDevice> mock_device2(new NiceMock<MockDevice>(&control_, |
| 55 | &dispatcher_, |
| 56 | &manager_, |
| 57 | "null1", |
| 58 | -1)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 59 | ON_CALL(*mock_device2.get(), TechnologyIs(Device::kWifi)) |
| 60 | .WillByDefault(Return(true)); |
| 61 | |
| 62 | manager_.RegisterDevice(mock_device.get()); |
| 63 | manager_.RegisterDevice(mock_device2.get()); |
| 64 | |
| 65 | EXPECT_TRUE(IsDeviceRegistered(mock_device.get(), Device::kEthernet)); |
| 66 | EXPECT_TRUE(IsDeviceRegistered(mock_device2.get(), Device::kWifi)); |
| 67 | } |
| 68 | |
| 69 | TEST_F(ManagerTest, DeviceDeregistration) { |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 70 | scoped_refptr<MockDevice> mock_device(new NiceMock<MockDevice>(&control_, |
| 71 | &dispatcher_, |
| 72 | &manager_, |
| 73 | "null2", |
| 74 | -1)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 75 | ON_CALL(*mock_device.get(), TechnologyIs(Device::kEthernet)) |
| 76 | .WillByDefault(Return(true)); |
| 77 | |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 78 | scoped_refptr<MockDevice> mock_device2(new NiceMock<MockDevice>(&control_, |
| 79 | &dispatcher_, |
| 80 | &manager_, |
| 81 | "null2", |
| 82 | -1)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 83 | ON_CALL(*mock_device2.get(), TechnologyIs(Device::kWifi)) |
| 84 | .WillByDefault(Return(true)); |
| 85 | |
| 86 | manager_.RegisterDevice(mock_device.get()); |
| 87 | manager_.RegisterDevice(mock_device2.get()); |
| 88 | |
| 89 | ASSERT_TRUE(IsDeviceRegistered(mock_device.get(), Device::kEthernet)); |
| 90 | ASSERT_TRUE(IsDeviceRegistered(mock_device2.get(), Device::kWifi)); |
| 91 | |
| 92 | manager_.DeregisterDevice(mock_device.get()); |
| 93 | EXPECT_FALSE(IsDeviceRegistered(mock_device.get(), Device::kEthernet)); |
| 94 | |
| 95 | manager_.DeregisterDevice(mock_device2.get()); |
| 96 | EXPECT_FALSE(IsDeviceRegistered(mock_device2.get(), Device::kWifi)); |
| 97 | } |
| 98 | |
| 99 | TEST_F(ManagerTest, ServiceRegistration) { |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 100 | scoped_refptr<MockDevice> device(new MockDevice(&control_, |
| 101 | &dispatcher_, |
| 102 | &manager_, |
| 103 | "null3", |
| 104 | -1)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 105 | const char kService1[] = "service1"; |
| 106 | const char kService2[] = "wifi_service2"; |
| 107 | scoped_refptr<MockService> mock_service( |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 108 | new NiceMock<MockService>(&control_, |
| 109 | &dispatcher_, |
| 110 | device.get(), |
| 111 | kService1)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 112 | |
| 113 | scoped_refptr<MockService> mock_service2( |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 114 | new NiceMock<MockService>(&control_, |
| 115 | &dispatcher_, |
| 116 | device.get(), |
| 117 | kService2)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 118 | |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 119 | manager_.RegisterService(mock_service); |
| 120 | manager_.RegisterService(mock_service2); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 121 | |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(manager_.FindService(kService1).get() != NULL); |
| 123 | EXPECT_TRUE(manager_.FindService(kService2).get() != NULL); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace shill |