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 "ui/chromeos/network/network_state_notifier.h"
7 #include "base/run_loop.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/shill_device_client.h"
10 #include "chromeos/dbus/shill_service_client.h"
11 #include "chromeos/login/login_state.h"
12 #include "chromeos/network/network_handler.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
16 #include "ui/chromeos/network/network_connect.h"
17 #include "ui/message_center/message_center.h"
19 using chromeos::DBusThreadManager
;
20 using chromeos::ShillDeviceClient
;
21 using chromeos::ShillServiceClient
;
26 class NetworkConnectTestDelegate
: public NetworkConnect::Delegate
{
28 NetworkConnectTestDelegate() {}
29 ~NetworkConnectTestDelegate() override
{}
31 // NetworkConnect::Delegate
32 void ShowNetworkConfigure(const std::string
& network_id
) override
{}
33 void ShowNetworkSettingsForGuid(const std::string
& network_id
) override
{}
34 bool ShowEnrollNetwork(const std::string
& network_id
) override
{
37 void ShowMobileSimDialog() override
{}
38 void ShowMobileSetupDialog(const std::string
& service_path
) override
{}
41 DISALLOW_COPY_AND_ASSIGN(NetworkConnectTestDelegate
);
44 class NetworkStateNotifierTest
: public testing::Test
{
46 NetworkStateNotifierTest() {}
47 ~NetworkStateNotifierTest() override
{}
49 void SetUp() override
{
50 testing::Test::SetUp();
51 DBusThreadManager::Initialize();
52 chromeos::LoginState::Initialize();
53 SetupDefaultShillState();
54 chromeos::NetworkHandler::Initialize();
55 message_center::MessageCenter::Initialize();
56 base::RunLoop().RunUntilIdle();
57 network_connect_delegate_
.reset(new NetworkConnectTestDelegate
);
58 NetworkConnect::Initialize(network_connect_delegate_
.get());
61 void TearDown() override
{
62 NetworkConnect::Shutdown();
63 network_connect_delegate_
.reset();
64 message_center::MessageCenter::Shutdown();
65 chromeos::LoginState::Shutdown();
66 chromeos::NetworkHandler::Shutdown();
67 DBusThreadManager::Shutdown();
68 testing::Test::TearDown();
72 void SetupDefaultShillState() {
73 base::RunLoop().RunUntilIdle();
74 ShillDeviceClient::TestInterface
* device_test
=
75 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
76 device_test
->ClearDevices();
77 device_test
->AddDevice("/device/stub_wifi_device1", shill::kTypeWifi
,
79 device_test
->AddDevice("/device/stub_cellular_device1",
80 shill::kTypeCellular
, "stub_cellular_device1");
82 ShillServiceClient::TestInterface
* service_test
=
83 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
84 service_test
->ClearServices();
85 const bool add_to_visible
= true;
86 // Create a wifi network and set to online.
87 service_test
->AddService("/service/wifi1", "wifi1_guid", "wifi1",
88 shill::kTypeWifi
, shill::kStateIdle
,
90 service_test
->SetServiceProperty("wifi1", shill::kSecurityClassProperty
,
91 base::StringValue(shill::kSecurityWep
));
92 service_test
->SetServiceProperty("wifi1", shill::kConnectableProperty
,
93 base::FundamentalValue(true));
94 service_test
->SetServiceProperty("wifi1", shill::kPassphraseProperty
,
95 base::StringValue("failure"));
96 base::RunLoop().RunUntilIdle();
99 scoped_ptr
<NetworkConnectTestDelegate
> network_connect_delegate_
;
100 base::MessageLoop message_loop_
;
103 DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifierTest
);
106 TEST_F(NetworkStateNotifierTest
, ConnectionFailure
) {
107 NetworkConnect::Get()->ConnectToNetwork("wifi1");
108 base::RunLoop().RunUntilIdle();
109 // Failure should spawn a notification.
110 message_center::MessageCenter
* message_center
=
111 message_center::MessageCenter::Get();
112 EXPECT_TRUE(message_center
->FindVisibleNotificationById(
113 NetworkStateNotifier::kNetworkConnectNotificationId
));