blob: 462c557b4a7f2cfdb726ed30e0e9e3ce461a4cc0 [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
Paul Stewartb50f0b92011-05-16 16:31:42 -070013#include "shill/dbus_control.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070014#include "shill/device_info.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070015#include "shill/manager.h"
Chris Masone46eaaf52011-05-24 13:08:30 -070016#include "shill/mock_control.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070017#include "shill/rtnl_handler.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070018
19namespace shill {
20using ::testing::Test;
21using ::testing::_;
Paul Stewartb50f0b92011-05-16 16:31:42 -070022using ::testing::NiceMock;
Chris Masone9be4a9d2011-05-16 15:44:09 -070023
24class DeviceInfoTest : public Test {
25 public:
26 DeviceInfoTest()
Paul Stewarta3c56f92011-05-26 07:08:52 -070027 : manager_(&control_interface_, &dispatcher_),
28 device_info_(&control_interface_, &dispatcher_, &manager_),
29 factory_(this) {
Chris Masone9be4a9d2011-05-16 15:44:09 -070030 }
Paul Stewartb50f0b92011-05-16 16:31:42 -070031 base::hash_map<int, scoped_refptr<Device> > *DeviceInfoDevices() {
32 return &device_info_.devices_;
33 }
Paul Stewarta3c56f92011-05-26 07:08:52 -070034 protected:
Chris Masone46eaaf52011-05-24 13:08:30 -070035 MockControl control_interface_;
Paul Stewartb50f0b92011-05-16 16:31:42 -070036 Manager manager_;
Chris Masone9be4a9d2011-05-16 15:44:09 -070037 DeviceInfo device_info_;
38 EventDispatcher dispatcher_;
39 ScopedRunnableMethodFactory<DeviceInfoTest> factory_;
40};
41
42TEST_F(DeviceInfoTest, DeviceEnumeration) {
43 // TODO(cmasone): Make this unit test use message loop primitives.
44
Paul Stewarta3c56f92011-05-26 07:08:52 -070045 // Start our own private device_info
Chris Masone9be4a9d2011-05-16 15:44:09 -070046 device_info_.Start();
Paul Stewarta3c56f92011-05-26 07:08:52 -070047 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 Masone9be4a9d2011-05-16 15:44:09 -070051
52 // Crank the glib main loop a few times
53 for (int main_loop_count = 0;
Paul Stewarta3c56f92011-05-26 07:08:52 -070054 main_loop_count < 6 && device_map->size() == 0;
Chris Masone9be4a9d2011-05-16 15:44:09 -070055 ++main_loop_count)
56 g_main_context_iteration(NULL, TRUE);
57
Paul Stewartb50f0b92011-05-16 16:31:42 -070058 // The test machine must have a device or two...
Paul Stewartb50f0b92011-05-16 16:31:42 -070059 EXPECT_GT(device_map->size(), 0);
60
Paul Stewarta3c56f92011-05-26 07:08:52 -070061 device_info_.Stop();
62 RTNLHandler::GetInstance()->Stop();
63 // TODO(pstew): Create fake devices (simulators?) so we can do hard tests
64}
65
66TEST_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 Stewartb50f0b92011-05-16 16:31:42 -070087 // TODO(pstew): Create fake devices (simulators?) so we can do hard tests
Chris Masone9be4a9d2011-05-16 15:44:09 -070088}
89
90} // namespace shill