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