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 "chromeos/network/network_handler.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
20 #include "ui/message_center/message_center.h"
24 ash::SystemTray
* GetSystemTray() {
25 return ash::Shell::GetPrimaryRootWindowController()->shelf()->
26 status_area_widget()->system_tray();
31 using chromeos::DBusThreadManager
;
32 using chromeos::ShillDeviceClient
;
33 using chromeos::ShillServiceClient
;
38 class NetworkStateNotifierTest
: public AshTestBase
{
40 NetworkStateNotifierTest() {}
41 virtual ~NetworkStateNotifierTest() {}
43 virtual void SetUp() OVERRIDE
{
44 DBusThreadManager::Initialize();
45 chromeos::LoginState::Initialize();
46 SetupDefaultShillState();
47 chromeos::NetworkHandler::Initialize();
48 RunAllPendingInMessageLoop();
52 virtual void TearDown() OVERRIDE
{
53 AshTestBase::TearDown();
54 chromeos::LoginState::Shutdown();
55 chromeos::NetworkHandler::Shutdown();
56 DBusThreadManager::Shutdown();
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");
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
,
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();
91 DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifierTest
);
94 TEST_F(NetworkStateNotifierTest
, ConnectionFailure
) {
95 EXPECT_FALSE(GetSystemTray()->HasNotificationBubble());
96 ash::network_connect::ConnectToNetwork("wifi1", NULL
/* owning_window */);
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
));