blob: 969d84572dad4b2d57da371d09aa8a63002e9a6b [file] [log] [blame]
mukesh agrawalab87ea42011-05-18 11:44:49 -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
5#include <iostream>
6#include <string>
7
8#include <base/at_exit.h>
9#include <base/command_line.h>
10#include <gtest/gtest.h>
11
12#include <glib.h>
13#include <dbus-c++/dbus.h>
14#include <dbus-c++/glib-integration.h>
15
16#include "shill/dbus_control.h"
17#include "shill/device_info.h"
18#include "shill/wifi.h"
19
20namespace switches {
21// wi-fi device name
22static const char kDeviceName[] = "device-name";
23// Flag that causes shill to show the help message and exit.
24static const char kHelp[] = "help";
25// The help message shown if help flag is passed to the program.
26static const char kHelpMessage[] = "\n"
27 "Switches for " __FILE__ "\n"
28 " --device-name\n"
29 " name of wi-fi device (e.g. wlan0).\n";
30} // namespace switches
31
32namespace shill {
33using ::testing::Test;
34
35const int kInterfaceIndexUnknown = -1;
36const unsigned int kScanTimeoutSecs = 60;
37const char kDefaultDeviceName[] = "wlan0";
38DBus::Glib::BusDispatcher dbus_glib_dispatcher;
39std::string device_name;
40
41class WiFiTest : public Test {
42 public:
43 WiFiTest() : timed_out_(false) {
44 wifi_ = new WiFi(&dbus_control_, NULL, NULL, device_name,
45 kInterfaceIndexUnknown);
46 }
47
48 bool ScanPending() {
49 return wifi_->scan_pending_;
50 }
51
52 void TimeOut() {
53 timed_out_ = true;
54 }
55
56 ~WiFiTest() {
57 wifi_->Release();
58 }
59
60 static gboolean TimeoutHandler(void *test_instance) {
61 static_cast<WiFiTest *>(test_instance)->TimeOut();
62 return false;
63 }
64
65 protected:
66 DBusControl dbus_control_;
67 WiFi *wifi_;
68 bool timed_out_;
69};
70
71
72TEST_F(WiFiTest, SSIDScanning) {
73 wifi_->Start();
74 g_timeout_add_seconds(10, WiFiTest::TimeoutHandler, this);
75
76 // Crank the glib main loop
77 while (ScanPending() && !timed_out_) {
78 LOG(INFO) << "cranking event loop";
79 g_main_context_iteration(NULL, TRUE);
80 }
81
82 ASSERT_FALSE(timed_out_);
83}
84
85} // namespace shill
86
87int main(int argc, char **argv) {
88 base::AtExitManager exit_manager;
89 ::testing::InitGoogleTest(&argc, argv);
90 CommandLine::Init(argc, argv);
91 CommandLine *cl = CommandLine::ForCurrentProcess();
92
93 if (cl->HasSwitch(switches::kHelp)) {
94 // NB(quiche): google test prints test framework help message
95 // at InitGoogleTest, above.
96 std::cout << switches::kHelpMessage;
97 return 0;
98 }
99
100 if (cl->HasSwitch(switches::kDeviceName)) {
101 shill::device_name =
102 cl->GetSwitchValueASCII(switches::kDeviceName);
103 } else {
104 shill::device_name = shill::kDefaultDeviceName;
105 }
106
107 shill::dbus_glib_dispatcher.attach(NULL); // NULL => default context
108 DBus::default_dispatcher = &shill::dbus_glib_dispatcher;
109 return RUN_ALL_TESTS();
110}