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