1 // Copyright (c) 2015 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 "chrome/browser/chromeos/status/data_promo_notification.h"
7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
10 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chrome/test/base/testing_profile_manager.h"
13 #include "chromeos/chromeos_switches.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_state_handler.h"
19 #include "testing/platform_test.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
21 #include "ui/chromeos/network/network_connect.h"
22 #include "ui/message_center/message_center.h"
26 const char kCellularDevicePath
[] = "/device/stub_cellular_device1";
27 const char kCellularServicePath
[] = "/service/cellular1";
28 const char kNotificationId
[] = "chrome://settings/internet/data_saver";
29 const char kTestUserName
[] = "test-user@example.com";
31 class NetworkConnectTestDelegate
: public ui::NetworkConnect::Delegate
{
33 NetworkConnectTestDelegate() {}
34 ~NetworkConnectTestDelegate() override
{}
36 void ShowNetworkConfigure(const std::string
& network_id
) override
{}
37 void ShowNetworkSettingsForGuid(const std::string
& network_id
) override
{}
38 bool ShowEnrollNetwork(const std::string
& network_id
) override
{
41 void ShowMobileSimDialog() override
{}
42 void ShowMobileSetupDialog(const std::string
& service_path
) override
{}
45 DISALLOW_COPY_AND_ASSIGN(NetworkConnectTestDelegate
);
53 class DataPromoNotificationTest
: public testing::Test
{
55 DataPromoNotificationTest() {}
56 ~DataPromoNotificationTest() override
{}
58 void SetUp() override
{
59 testing::Test::SetUp();
60 base::CommandLine::ForCurrentProcess()->AppendSwitch(
61 switches::kEnableDataSaverPrompt
);
62 DBusThreadManager::Initialize();
63 NetworkHandler::Initialize();
64 data_promo_notification_
.reset(new DataPromoNotification
);
66 SetupNetworkShillState();
67 message_center::MessageCenter::Initialize();
68 base::RunLoop().RunUntilIdle();
69 network_connect_delegate_
.reset(new NetworkConnectTestDelegate
);
70 ui::NetworkConnect::Initialize(network_connect_delegate_
.get());
73 void TearDown() override
{
74 ui::NetworkConnect::Shutdown();
75 network_connect_delegate_
.reset();
76 message_center::MessageCenter::Shutdown();
77 LoginState::Shutdown();
78 profile_manager_
.reset();
79 user_manager_enabler_
.reset();
80 data_promo_notification_
.reset();
81 NetworkHandler::Shutdown();
82 DBusThreadManager::Shutdown();
83 testing::Test::TearDown();
88 scoped_ptr
<FakeChromeUserManager
> user_manager(new FakeChromeUserManager());
89 user_manager
->AddUser(kTestUserName
);
90 user_manager
->LoginUser(kTestUserName
);
91 user_manager_enabler_
.reset(
92 new ScopedUserManagerEnabler(user_manager
.release()));
94 profile_manager_
.reset(
95 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
96 ASSERT_TRUE(profile_manager_
->SetUp());
97 profile_manager_
->SetLoggedIn(true);
98 ASSERT_TRUE(user_manager::UserManager::Get()->GetPrimaryUser());
100 LoginState::Initialize();
101 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE
,
102 LoginState::LOGGED_IN_USER_REGULAR
);
105 void SetupNetworkShillState() {
106 base::RunLoop().RunUntilIdle();
108 // Create a cellular device with provider.
109 ShillDeviceClient::TestInterface
* device_test
=
110 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
111 device_test
->ClearDevices();
112 device_test
->AddDevice(kCellularDevicePath
, shill::kTypeCellular
,
113 "stub_cellular_device1");
114 base::DictionaryValue home_provider
;
115 home_provider
.SetString("name", "Cellular1_Provider");
116 home_provider
.SetString("country", "us");
117 device_test
->SetDeviceProperty(kCellularDevicePath
,
118 shill::kHomeProviderProperty
, home_provider
);
120 // Create a cellular network and activate it.
121 ShillServiceClient::TestInterface
* service_test
=
122 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
123 service_test
->ClearServices();
124 service_test
->AddService(kCellularServicePath
, "cellular1_guid",
125 "cellular1" /* name */, shill::kTypeCellular
,
126 "activated", true /* visible */);
127 service_test
->SetServiceProperty(
128 kCellularServicePath
, shill::kActivationStateProperty
,
129 base::StringValue(shill::kActivationStateActivated
));
130 service_test
->SetServiceProperty(kCellularServicePath
,
131 shill::kConnectableProperty
,
132 base::FundamentalValue(true));
135 scoped_ptr
<DataPromoNotification
> data_promo_notification_
;
136 scoped_ptr
<NetworkConnectTestDelegate
> network_connect_delegate_
;
137 scoped_ptr
<ScopedUserManagerEnabler
> user_manager_enabler_
;
138 scoped_ptr
<TestingProfileManager
> profile_manager_
;
139 base::MessageLoop message_loop_
;
142 DISALLOW_COPY_AND_ASSIGN(DataPromoNotificationTest
);
145 TEST_F(DataPromoNotificationTest
, DataSaverNotification
) {
146 message_center::MessageCenter
* message_center
=
147 message_center::MessageCenter::Get();
149 // Network setup shouldn't be enough to activate notification.
150 EXPECT_FALSE(message_center
->FindVisibleNotificationById(kNotificationId
));
152 ui::NetworkConnect::Get()->ConnectToNetwork(kCellularServicePath
);
153 base::RunLoop().RunUntilIdle();
154 // Connecting to cellular network (which here makes it the default network)
155 // should trigger the Data Saver notification.
156 EXPECT_TRUE(message_center
->FindVisibleNotificationById(kNotificationId
));
160 } // namespace chromeos