1// Copyright (c) 2012 The Chromium 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 "ash/system/chromeos/network/network_state_notifier.h"
6
7#include "ash/root_window_controller.h"
8#include "ash/shelf/shelf_widget.h"
9#include "ash/shell.h"
10#include "ash/system/chromeos/network/network_connect.h"
11#include "ash/system/status_area_widget.h"
12#include "ash/system/tray/system_tray.h"
13#include "ash/test/ash_test_base.h"
14#include "chromeos/dbus/dbus_thread_manager.h"
15#include "chromeos/dbus/shill_device_client.h"
16#include "chromeos/dbus/shill_service_client.h"
17#include "chromeos/login/login_state.h"
18#include "chromeos/network/network_handler.h"
19#include "third_party/cros_system_api/dbus/service_constants.h"
20#include "ui/message_center/message_center.h"
21
22namespace {
23
24ash::SystemTray* GetSystemTray() {
25  return ash::Shell::GetPrimaryRootWindowController()->shelf()->
26      status_area_widget()->system_tray();
27}
28
29}  // namespace
30
31using chromeos::DBusThreadManager;
32using chromeos::ShillDeviceClient;
33using chromeos::ShillServiceClient;
34
35namespace ash {
36namespace test {
37
38class NetworkStateNotifierTest : public AshTestBase {
39 public:
40  NetworkStateNotifierTest() {}
41  virtual ~NetworkStateNotifierTest() {}
42
43  virtual void SetUp() OVERRIDE {
44    DBusThreadManager::Initialize();
45    chromeos::LoginState::Initialize();
46    SetupDefaultShillState();
47    chromeos::NetworkHandler::Initialize();
48    RunAllPendingInMessageLoop();
49    AshTestBase::SetUp();
50  }
51
52  virtual void TearDown() OVERRIDE {
53    AshTestBase::TearDown();
54    chromeos::LoginState::Shutdown();
55    chromeos::NetworkHandler::Shutdown();
56    DBusThreadManager::Shutdown();
57  }
58
59 protected:
60  void SetupDefaultShillState() {
61    RunAllPendingInMessageLoop();
62    ShillDeviceClient::TestInterface* device_test =
63        DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
64    device_test->ClearDevices();
65    device_test->AddDevice("/device/stub_wifi_device1",
66                           shill::kTypeWifi, "stub_wifi_device1");
67    device_test->AddDevice("/device/stub_cellular_device1",
68                           shill::kTypeCellular, "stub_cellular_device1");
69
70    ShillServiceClient::TestInterface* service_test =
71        DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
72    service_test->ClearServices();
73    const bool add_to_visible = true;
74    // Create a wifi network and set to online.
75    service_test->AddService("/service/wifi1", "wifi1_guid", "wifi1",
76                             shill::kTypeWifi, shill::kStateIdle,
77                             add_to_visible);
78    service_test->SetServiceProperty("wifi1",
79                                     shill::kSecurityProperty,
80                                     base::StringValue(shill::kSecurityWep));
81    service_test->SetServiceProperty("wifi1",
82                                     shill::kConnectableProperty,
83                                     base::FundamentalValue(true));
84    service_test->SetServiceProperty("wifi1",
85                                     shill::kPassphraseProperty,
86                                     base::StringValue("failure"));
87    RunAllPendingInMessageLoop();
88  }
89
90 private:
91  DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifierTest);
92};
93
94TEST_F(NetworkStateNotifierTest, ConnectionFailure) {
95  EXPECT_FALSE(GetSystemTray()->HasNotificationBubble());
96  ash::network_connect::ConnectToNetwork("wifi1");
97  RunAllPendingInMessageLoop();
98  // Failure should spawn a notification.
99  message_center::MessageCenter* message_center =
100      message_center::MessageCenter::Get();
101  EXPECT_TRUE(message_center->FindVisibleNotificationById(
102      network_connect::kNetworkConnectNotificationId));
103}
104
105}  // namespace test
106}  // namespace ash
107