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.
5 #include "ash/system/chromeos/network/network_state_notifier.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_widget.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 "third_party/cros_system_api/dbus/service_constants.h"
19 #include "ui/message_center/message_center.h"
23 ash::SystemTray* GetSystemTray() {
24 return ash::Shell::GetPrimaryRootWindowController()->shelf()->
25 status_area_widget()->system_tray();
30 using chromeos::DBusThreadManager;
31 using chromeos::ShillDeviceClient;
32 using chromeos::ShillServiceClient;
37 class NetworkStateNotifierTest : public AshTestBase {
39 NetworkStateNotifierTest() {}
40 virtual ~NetworkStateNotifierTest() {}
42 virtual void SetUp() OVERRIDE {
43 DBusThreadManager::InitializeWithStub();
44 chromeos::LoginState::Initialize();
45 SetupDefaultShillState();
46 RunAllPendingInMessageLoop();
50 virtual void TearDown() OVERRIDE {
51 AshTestBase::TearDown();
52 chromeos::LoginState::Shutdown();
53 DBusThreadManager::Shutdown();
57 void SetupDefaultShillState() {
58 RunAllPendingInMessageLoop();
59 ShillDeviceClient::TestInterface* device_test =
60 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
61 device_test->ClearDevices();
62 device_test->AddDevice("/device/stub_wifi_device1",
63 shill::kTypeWifi, "stub_wifi_device1");
64 device_test->AddDevice("/device/stub_cellular_device1",
65 shill::kTypeCellular, "stub_cellular_device1");
67 ShillServiceClient::TestInterface* service_test =
68 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
69 service_test->ClearServices();
70 const bool add_to_watchlist = true;
71 const bool add_to_visible = true;
72 // Create wifi and cellular networks and set to online.
73 service_test->AddService("wifi1", "wifi1",
74 shill::kTypeWifi, shill::kStateIdle,
75 add_to_visible, add_to_watchlist);
76 service_test->SetServiceProperty("wifi1",
77 shill::kSecurityProperty,
78 base::StringValue(shill::kSecurityWep));
79 service_test->SetServiceProperty("wifi1",
80 shill::kConnectableProperty,
81 base::FundamentalValue(true));
82 service_test->SetServiceProperty("wifi1",
83 shill::kPassphraseProperty,
84 base::StringValue("failure"));
85 RunAllPendingInMessageLoop();
89 DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifierTest);
92 TEST_F(NetworkStateNotifierTest, ConnectionFailure) {
93 EXPECT_FALSE(GetSystemTray()->HasNotificationBubble());
94 ash::network_connect::ConnectToNetwork("wifi1", NULL /* owning_window */);
95 RunAllPendingInMessageLoop();
96 // Failure should spawn a notification.
97 message_center::MessageCenter* message_center =
98 message_center::MessageCenter::Get();
99 EXPECT_TRUE(message_center->HasNotification(
100 network_connect::kNetworkConnectNotificationId));