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.
4
5 #include "chrome/browser/ui/ash/network/mobile_data_notifications.h"
6
7 #include <gtest/gtest.h>
8 #include <memory>
9 #include <string>
10 #include <utility>
11
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/run_loop.h"
15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
16 #include "chrome/browser/chromeos/profiles/profile_helper.h"
17 #include "chrome/browser/notifications/notification_display_service_tester.h"
18 #include "chrome/browser/notifications/system_notification_helper.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile_manager.h"
23 #include "chromeos/dbus/dbus_thread_manager.h"
24 #include "chromeos/dbus/shill/shill_device_client.h"
25 #include "chromeos/dbus/shill/shill_service_client.h"
26 #include "chromeos/network/network_connect.h"
27 #include "chromeos/network/network_state_handler.h"
28 #include "components/prefs/pref_service.h"
29 #include "components/session_manager/core/session_manager.h"
30 #include "components/session_manager/session_manager_types.h"
31 #include "components/user_manager/scoped_user_manager.h"
32 #include "content/public/test/browser_task_environment.h"
33 #include "third_party/cros_system_api/dbus/service_constants.h"
34
35 using chromeos::DBusThreadManager;
36 using chromeos::LoginState;
37
38 namespace {
39
40 const char kCellularDevicePath[] = "/device/stub_cellular_device1";
41 const char kCellularServicePath[] = "/service/cellular1";
42 const char kCellularGuid[] = "cellular1_guid";
43
44 const char kNotificationId[] = "chrome://settings/internet/mobile_data";
45 const char kTestUserName[] = "test-user@example.com";
46
47 class NetworkConnectTestDelegate : public chromeos::NetworkConnect::Delegate {
48 public:
NetworkConnectTestDelegate()49 NetworkConnectTestDelegate() {}
~NetworkConnectTestDelegate()50 ~NetworkConnectTestDelegate() override {}
51
ShowNetworkConfigure(const std::string & network_id)52 void ShowNetworkConfigure(const std::string& network_id) override {}
ShowNetworkSettings(const std::string & network_id)53 void ShowNetworkSettings(const std::string& network_id) override {}
ShowEnrollNetwork(const std::string & network_id)54 bool ShowEnrollNetwork(const std::string& network_id) override {
55 return false;
56 }
ShowMobileSetupDialog(const std::string & network_id)57 void ShowMobileSetupDialog(const std::string& network_id) override {}
ShowNetworkConnectError(const std::string & error_name,const std::string & network_id)58 void ShowNetworkConnectError(const std::string& error_name,
59 const std::string& network_id) override {}
ShowMobileActivationError(const std::string & network_id)60 void ShowMobileActivationError(const std::string& network_id) override {}
61
62 private:
63 DISALLOW_COPY_AND_ASSIGN(NetworkConnectTestDelegate);
64 };
65
66 class MobileDataNotificationsTest : public testing::Test {
67 public:
MobileDataNotificationsTest()68 MobileDataNotificationsTest() {}
~MobileDataNotificationsTest()69 ~MobileDataNotificationsTest() override {}
70
SetUp()71 void SetUp() override {
72 session_manager_.SetSessionState(session_manager::SessionState::ACTIVE);
73 testing::Test::SetUp();
74 DBusThreadManager::Initialize();
75 SetupUserManagerAndProfileManager();
76 SetupSystemNotifications();
77 AddUserAndSetActive(kTestUserName);
78 chromeos::NetworkHandler::Initialize();
79 SetupNetworkShillState();
80 base::RunLoop().RunUntilIdle();
81 network_connect_delegate_ = std::make_unique<NetworkConnectTestDelegate>();
82 chromeos::NetworkConnect::Initialize(network_connect_delegate_.get());
83 mobile_data_notifications_ = std::make_unique<MobileDataNotifications>();
84 }
85
TearDown()86 void TearDown() override {
87 mobile_data_notifications_.reset();
88 chromeos::NetworkConnect::Shutdown();
89 network_connect_delegate_.reset();
90 chromeos::NetworkHandler::Shutdown();
91 profile_manager_.reset();
92 user_manager_enabler_.reset();
93 DBusThreadManager::Shutdown();
94 testing::Test::TearDown();
95 }
96
97 protected:
SetupSystemNotifications()98 void SetupSystemNotifications() {
99 TestingBrowserProcess::GetGlobal()->SetSystemNotificationHelper(
100 std::make_unique<SystemNotificationHelper>());
101 // Passing nullptr sets up |display_service_| with system notifications.
102 display_service_ = std::make_unique<NotificationDisplayServiceTester>(
103 nullptr /* profile */);
104 }
SetupUserManagerAndProfileManager()105 void SetupUserManagerAndProfileManager() {
106 user_manager_ = new chromeos::FakeChromeUserManager;
107 user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
108 base::WrapUnique(user_manager_));
109
110 profile_manager_.reset(
111 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
112 ASSERT_TRUE(profile_manager_->SetUp());
113 }
114
SetupNetworkShillState()115 void SetupNetworkShillState() {
116 base::RunLoop().RunUntilIdle();
117
118 // Create a cellular device with provider.
119 chromeos::ShillDeviceClient::TestInterface* device_test =
120 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
121 device_test->ClearDevices();
122 device_test->AddDevice(kCellularDevicePath, shill::kTypeCellular,
123 "stub_cellular_device1");
124 base::DictionaryValue home_provider;
125 home_provider.SetString("name", "Cellular1_Provider");
126 home_provider.SetString("country", "us");
127 device_test->SetDeviceProperty(kCellularDevicePath,
128 shill::kHomeProviderProperty, home_provider,
129 /*notify_changed=*/true);
130
131 // Create a cellular network and activate it.
132 chromeos::ShillServiceClient::TestInterface* service_test =
133 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
134 service_test->ClearServices();
135 service_test->AddService(kCellularServicePath, kCellularGuid,
136 "cellular1" /* name */, shill::kTypeCellular,
137 shill::kStateIdle, true /* visible */);
138 service_test->SetServiceProperty(
139 kCellularServicePath, shill::kActivationStateProperty,
140 base::Value(shill::kActivationStateActivated));
141 service_test->SetServiceProperty(
142 kCellularServicePath, shill::kConnectableProperty, base::Value(true));
143 }
144
pref_service()145 PrefService* pref_service() {
146 return ProfileManager::GetActiveUserProfile()->GetPrefs();
147 }
148
AddUserAndSetActive(std::string email)149 void AddUserAndSetActive(std::string email) {
150 const AccountId test_account_id(AccountId::FromUserEmail(email));
151 TestingProfile* profile =
152 profile_manager_->CreateTestingProfile(test_account_id.GetUserEmail());
153 user_manager_->AddUser(test_account_id);
154 user_manager_->LoginUser(test_account_id);
155 user_manager_->SwitchActiveUser(test_account_id);
156 ASSERT_TRUE(ProfileManager::GetActiveUserProfile() == profile);
157 }
158
159 content::BrowserTaskEnvironment task_environment_;
160 session_manager::SessionManager session_manager_;
161 std::unique_ptr<MobileDataNotifications> mobile_data_notifications_;
162 std::unique_ptr<NetworkConnectTestDelegate> network_connect_delegate_;
163 std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
164
165 chromeos::FakeChromeUserManager* user_manager_;
166 std::unique_ptr<TestingProfileManager> profile_manager_;
167 std::unique_ptr<NotificationDisplayServiceTester> display_service_;
168
169 DISALLOW_COPY_AND_ASSIGN(MobileDataNotificationsTest);
170 };
171
172 // Verify that basic network setup does not trigger notification.
TEST_F(MobileDataNotificationsTest,SimpleSetup)173 TEST_F(MobileDataNotificationsTest, SimpleSetup) {
174 pref_service()->SetBoolean(prefs::kShowMobileDataNotification, true);
175 EXPECT_FALSE(display_service_->GetNotification(kNotificationId));
176 }
177
178 // Verify that switching to cellular whiile pref is false does not display a
179 // notification.
TEST_F(MobileDataNotificationsTest,NotificationAlreadyShown)180 TEST_F(MobileDataNotificationsTest, NotificationAlreadyShown) {
181 pref_service()->SetBoolean(prefs::kShowMobileDataNotification, false);
182
183 chromeos::NetworkConnect::Get()->ConnectToNetworkId(kCellularGuid);
184 // Wait for async ConnectToNetworkId to take effect.
185 base::RunLoop().RunUntilIdle();
186
187 EXPECT_FALSE(display_service_->GetNotification(kNotificationId));
188 }
189
190 // Verify that switching to cellular while pref is true displays notification.
TEST_F(MobileDataNotificationsTest,DisplayNotification)191 TEST_F(MobileDataNotificationsTest, DisplayNotification) {
192 pref_service()->SetBoolean(prefs::kShowMobileDataNotification, true);
193
194 chromeos::NetworkConnect::Get()->ConnectToNetworkId(kCellularGuid);
195 // Wait for async ConnectToNetworkId to take effect.
196 base::RunLoop().RunUntilIdle();
197
198 EXPECT_TRUE(display_service_->GetNotification(kNotificationId));
199 }
200
201 // Verify that displaying the notification toggles the profile pref.
TEST_F(MobileDataNotificationsTest,TogglesPref)202 TEST_F(MobileDataNotificationsTest, TogglesPref) {
203 pref_service()->SetBoolean(prefs::kShowMobileDataNotification, true);
204
205 chromeos::NetworkConnect::Get()->ConnectToNetworkId(kCellularGuid);
206 // Wait for async ConnectToNetworkId to take effect.
207 base::RunLoop().RunUntilIdle();
208
209 EXPECT_FALSE(pref_service()->GetBoolean(prefs::kShowMobileDataNotification));
210 }
211
212 // Verify that session changes display the notification if cellular is
213 // connected.
TEST_F(MobileDataNotificationsTest,SessionUpdateDisplayNotification)214 TEST_F(MobileDataNotificationsTest, SessionUpdateDisplayNotification) {
215 // Set up cellular network, don't trigger notification.
216 chromeos::NetworkConnect::Get()->ConnectToNetworkId(kCellularGuid);
217 pref_service()->SetBoolean(prefs::kShowMobileDataNotification, false);
218 // Process network observer update.
219 base::RunLoop().RunUntilIdle();
220 // Make sure notification hasn't been triggered.
221 EXPECT_FALSE(pref_service()->GetBoolean(prefs::kShowMobileDataNotification));
222
223 AddUserAndSetActive("other-user@example.com");
224
225 EXPECT_TRUE(display_service_->GetNotification(kNotificationId));
226 }
227
228 // Verify that session changes does not dispalay the notification if celluar is
229 // not connected.
TEST_F(MobileDataNotificationsTest,SessionUpdateNoNotification)230 TEST_F(MobileDataNotificationsTest, SessionUpdateNoNotification) {
231 pref_service()->SetBoolean(prefs::kShowMobileDataNotification, true);
232
233 AddUserAndSetActive("other-user@example.com");
234
235 EXPECT_FALSE(display_service_->GetNotification(kNotificationId));
236 }
237
238 } // namespace
239