blob: e66cd3091340d276e3c590ec03c1e0f37fa55484 [file] [log] [blame]
Chris Masone9be4a9d2011-05-16 15:44:09 -07001// 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 Stewartb50f0b92011-05-16 16:31:42 -070014#include "shill/manager.h"
Chris Masone46eaaf52011-05-24 13:08:30 -070015#include "shill/mock_control.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070016#include "shill/rtnl_handler.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070017
18namespace shill {
19using ::testing::Test;
20using ::testing::_;
Paul Stewartb50f0b92011-05-16 16:31:42 -070021using ::testing::NiceMock;
Chris Masone9be4a9d2011-05-16 15:44:09 -070022
23class DeviceInfoTest : public Test {
24 public:
25 DeviceInfoTest()
Paul Stewarta3c56f92011-05-26 07:08:52 -070026 : manager_(&control_interface_, &dispatcher_),
27 device_info_(&control_interface_, &dispatcher_, &manager_),
28 factory_(this) {
Chris Masone9be4a9d2011-05-16 15:44:09 -070029 }
Chris Masonec1e50412011-06-07 13:04:53 -070030 base::hash_map<int, DeviceRefPtr> *DeviceInfoDevices() {
Paul Stewartb50f0b92011-05-16 16:31:42 -070031 return &device_info_.devices_;
32 }
Paul Stewarta3c56f92011-05-26 07:08:52 -070033 protected:
Chris Masone46eaaf52011-05-24 13:08:30 -070034 MockControl control_interface_;
Paul Stewartb50f0b92011-05-16 16:31:42 -070035 Manager manager_;
Chris Masone9be4a9d2011-05-16 15:44:09 -070036 DeviceInfo device_info_;
37 EventDispatcher dispatcher_;
38 ScopedRunnableMethodFactory<DeviceInfoTest> factory_;
39};
40
41TEST_F(DeviceInfoTest, DeviceEnumeration) {
42 // TODO(cmasone): Make this unit test use message loop primitives.
43
Paul Stewarta3c56f92011-05-26 07:08:52 -070044 // Start our own private device_info
Chris Masone9be4a9d2011-05-16 15:44:09 -070045 device_info_.Start();
Paul Stewarta3c56f92011-05-26 07:08:52 -070046 RTNLHandler::GetInstance()->Start(&dispatcher_);
47
48 // Peek in at the map of devices
Chris Masonec1e50412011-06-07 13:04:53 -070049 base::hash_map<int, DeviceRefPtr> *device_map = DeviceInfoDevices();
Chris Masone9be4a9d2011-05-16 15:44:09 -070050
51 // Crank the glib main loop a few times
52 for (int main_loop_count = 0;
Paul Stewarta3c56f92011-05-26 07:08:52 -070053 main_loop_count < 6 && device_map->size() == 0;
Chris Masone9be4a9d2011-05-16 15:44:09 -070054 ++main_loop_count)
55 g_main_context_iteration(NULL, TRUE);
56
Paul Stewartb50f0b92011-05-16 16:31:42 -070057 // The test machine must have a device or two...
Paul Stewartb50f0b92011-05-16 16:31:42 -070058 EXPECT_GT(device_map->size(), 0);
59
Paul Stewarta3c56f92011-05-26 07:08:52 -070060 device_info_.Stop();
61 RTNLHandler::GetInstance()->Stop();
62 // TODO(pstew): Create fake devices (simulators?) so we can do hard tests
63}
64
65TEST_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 Masonec1e50412011-06-07 13:04:53 -070073 base::hash_map<int, DeviceRefPtr> *device_map = DeviceInfoDevices();
Paul Stewarta3c56f92011-05-26 07:08:52 -070074
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 Stewartb50f0b92011-05-16 16:31:42 -070086 // TODO(pstew): Create fake devices (simulators?) so we can do hard tests
Chris Masone9be4a9d2011-05-16 15:44:09 -070087}
88
89} // namespace shill