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 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 13 | #include "shill/dbus_control.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 14 | #include "shill/device_info.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 15 | #include "shill/manager.h" |
Chris Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 16 | #include "shill/mock_control.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 17 | #include "shill/rtnl_handler.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | using ::testing::Test; |
| 21 | using ::testing::_; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 22 | using ::testing::NiceMock; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 23 | |
| 24 | class DeviceInfoTest : public Test { |
| 25 | public: |
| 26 | DeviceInfoTest() |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 27 | : manager_(&control_interface_, &dispatcher_), |
| 28 | device_info_(&control_interface_, &dispatcher_, &manager_), |
| 29 | factory_(this) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 30 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 31 | base::hash_map<int, scoped_refptr<Device> > *DeviceInfoDevices() { |
| 32 | return &device_info_.devices_; |
| 33 | } |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 34 | protected: |
Chris Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 35 | MockControl control_interface_; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 36 | Manager manager_; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 37 | DeviceInfo device_info_; |
| 38 | EventDispatcher dispatcher_; |
| 39 | ScopedRunnableMethodFactory<DeviceInfoTest> factory_; |
| 40 | }; |
| 41 | |
| 42 | TEST_F(DeviceInfoTest, DeviceEnumeration) { |
| 43 | // TODO(cmasone): Make this unit test use message loop primitives. |
| 44 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 45 | // Start our own private device_info |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 46 | device_info_.Start(); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 47 | RTNLHandler::GetInstance()->Start(&dispatcher_); |
| 48 | |
| 49 | // Peek in at the map of devices |
| 50 | base::hash_map<int, scoped_refptr<Device> > *device_map = DeviceInfoDevices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 51 | |
| 52 | // Crank the glib main loop a few times |
| 53 | for (int main_loop_count = 0; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 54 | main_loop_count < 6 && device_map->size() == 0; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 55 | ++main_loop_count) |
| 56 | g_main_context_iteration(NULL, TRUE); |
| 57 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 58 | // The test machine must have a device or two... |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 59 | EXPECT_GT(device_map->size(), 0); |
| 60 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 61 | device_info_.Stop(); |
| 62 | RTNLHandler::GetInstance()->Stop(); |
| 63 | // TODO(pstew): Create fake devices (simulators?) so we can do hard tests |
| 64 | } |
| 65 | |
| 66 | TEST_F(DeviceInfoTest, DeviceEnumerationReverse) { |
| 67 | // TODO(cmasone): Make this unit test use message loop primitives. |
| 68 | |
| 69 | // Start our own private device_info _after_ RTNLHandler has been started |
| 70 | RTNLHandler::GetInstance()->Start(&dispatcher_); |
| 71 | device_info_.Start(); |
| 72 | |
| 73 | // Peek in at the map of devices |
| 74 | base::hash_map<int, scoped_refptr<Device> > *device_map = DeviceInfoDevices(); |
| 75 | |
| 76 | // Crank the glib main loop a few times |
| 77 | for (int main_loop_count = 0; |
| 78 | main_loop_count < 6 && device_map->size() == 0; |
| 79 | ++main_loop_count) |
| 80 | g_main_context_iteration(NULL, TRUE); |
| 81 | |
| 82 | // The test machine must have a device or two... |
| 83 | EXPECT_GT(device_map->size(), 0); |
| 84 | |
| 85 | RTNLHandler::GetInstance()->Stop(); |
| 86 | device_info_.Stop(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 87 | // TODO(pstew): Create fake devices (simulators?) so we can do hard tests |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace shill |