Revert "[M120 Migration]Fix for crash during chrome exit"
[platform/framework/web/chromium-efl.git] / ash / policy / policy_recommendation_restorer_unittest.cc
1 // Copyright 2018 The Chromium Authors
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 "ash/policy/policy_recommendation_restorer.h"
6
7 #include "ash/constants/ash_pref_names.h"
8 #include "ash/public/cpp/ash_prefs.h"
9 #include "ash/session/session_controller_impl.h"
10 #include "ash/session/test_session_controller_client.h"
11 #include "ash/shell.h"
12 #include "ash/test/ash_test_base.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/memory/raw_ptr.h"
15 #include "components/prefs/pref_notifier_impl.h"
16 #include "components/prefs/testing_pref_store.h"
17 #include "components/sync_preferences/testing_pref_service_syncable.h"
18 #include "ui/base/user_activity/user_activity_detector.h"
19
20 namespace ash {
21
22 class PolicyRecommendationRestorerTest : public NoSessionAshTestBase {
23  public:
24   PolicyRecommendationRestorerTest(const PolicyRecommendationRestorerTest&) =
25       delete;
26   PolicyRecommendationRestorerTest& operator=(
27       const PolicyRecommendationRestorerTest&) = delete;
28
29  protected:
30   PolicyRecommendationRestorerTest()
31       : recommended_prefs_(new TestingPrefStore),
32         prefs_(new sync_preferences::TestingPrefServiceSyncable(
33             /*managed_prefs=*/new TestingPrefStore,
34             /*supervised_user_prefs=*/new TestingPrefStore,
35             /*extension_prefs=*/new TestingPrefStore,
36             /*standalone_browser_prefs=*/new TestingPrefStore,
37             /*user_prefs=*/new TestingPrefStore,
38             recommended_prefs_,
39             new user_prefs::PrefRegistrySyncable,
40             new PrefNotifierImpl)) {}
41   ~PolicyRecommendationRestorerTest() override = default;
42
43   // NoSessionAshTestBase override:
44   void SetUp() override {
45     TestSessionControllerClient::DisableAutomaticallyProvideSigninPref();
46     NoSessionAshTestBase::SetUp();
47
48     // Register sigin prefs but not connected to pref service yet. This allows
49     // us set pref values before ash connects to pref service for testing.
50     RegisterSigninProfilePrefs(prefs_->registry(), /*country=*/"",
51                                /*for_test=*/true);
52
53     restorer_ = Shell::Get()->policy_recommendation_restorer();
54   }
55
56   void ConnectToSigninPrefService() {
57     GetSessionControllerClient()->SetSigninScreenPrefService(
58         base::WrapUnique(prefs_.get()));
59     ASSERT_EQ(Shell::Get()->session_controller()->GetSigninScreenPrefService(),
60               prefs_);
61     // Manually trigger a user activity, so that the delay is not skipped due to
62     // no user input since a pref is started observing recommended value. See
63     // PolicyRecommendationRestorer::Restore() for the information.
64     ui::UserActivityDetector::Get()->HandleExternalUserActivity();
65   }
66
67   void SetRecommendedValues() {
68     recommended_prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled,
69                                    false);
70     recommended_prefs_->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled,
71                                    false);
72     recommended_prefs_->SetBoolean(prefs::kAccessibilityHighContrastEnabled,
73                                    false);
74     recommended_prefs_->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled,
75                                    false);
76     recommended_prefs_->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled,
77                                    false);
78   }
79
80   void SetUserSettings() {
81     prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled, true);
82     prefs_->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled, true);
83     prefs_->SetBoolean(prefs::kAccessibilityHighContrastEnabled, true);
84     prefs_->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled, true);
85     prefs_->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled, true);
86   }
87
88   void VerifyPrefFollowsUser(const std::string& pref_name,
89                              const base::Value& expected_value) const {
90     const sync_preferences::PrefServiceSyncable::Preference* pref =
91         prefs_->FindPreference(pref_name);
92     ASSERT_TRUE(pref);
93     EXPECT_TRUE(pref->HasUserSetting());
94     const base::Value* value = pref->GetValue();
95     ASSERT_TRUE(value);
96     EXPECT_EQ(expected_value, *value);
97   }
98
99   void VerifyPrefsFollowUser() const {
100     VerifyPrefFollowsUser(prefs::kAccessibilityLargeCursorEnabled,
101                           base::Value(true));
102     VerifyPrefFollowsUser(prefs::kAccessibilitySpokenFeedbackEnabled,
103                           base::Value(true));
104     VerifyPrefFollowsUser(prefs::kAccessibilityHighContrastEnabled,
105                           base::Value(true));
106     VerifyPrefFollowsUser(prefs::kAccessibilityScreenMagnifierEnabled,
107                           base::Value(true));
108     VerifyPrefFollowsUser(prefs::kAccessibilityVirtualKeyboardEnabled,
109                           base::Value(true));
110   }
111
112   void VerifyPrefFollowsRecommendation(
113       const std::string& pref_name,
114       const base::Value& expected_value) const {
115     const sync_preferences::PrefServiceSyncable::Preference* pref =
116         prefs_->FindPreference(pref_name);
117     ASSERT_TRUE(pref);
118     EXPECT_TRUE(pref->IsRecommended());
119     EXPECT_FALSE(pref->HasUserSetting());
120     const base::Value* value = pref->GetValue();
121     ASSERT_TRUE(value);
122     EXPECT_EQ(expected_value, *value);
123   }
124
125   void VerifyPrefsFollowRecommendation() const {
126     VerifyPrefFollowsRecommendation(prefs::kAccessibilityLargeCursorEnabled,
127                                     base::Value(false));
128     VerifyPrefFollowsRecommendation(prefs::kAccessibilitySpokenFeedbackEnabled,
129                                     base::Value(false));
130     VerifyPrefFollowsRecommendation(prefs::kAccessibilityHighContrastEnabled,
131                                     base::Value(false));
132     VerifyPrefFollowsRecommendation(prefs::kAccessibilityScreenMagnifierEnabled,
133                                     base::Value(false));
134     VerifyPrefFollowsRecommendation(prefs::kAccessibilityVirtualKeyboardEnabled,
135                                     base::Value(false));
136   }
137
138   bool RestoreTimerIsRunning() {
139     return restorer_->restore_timer_for_test()->IsRunning();
140   }
141
142   // If restore timer is running, stops it, runs its task and returns true.
143   // Otherwise, returns false.
144   [[nodiscard]] bool TriggerRestoreTimer() {
145     if (!restorer_->restore_timer_for_test()->IsRunning())
146       return false;
147
148     restorer_->restore_timer_for_test()->FireNow();
149     return true;
150   }
151
152   raw_ptr<PolicyRecommendationRestorer, DanglingUntriaged | ExperimentalAsh>
153       restorer_ = nullptr;
154
155   // Ownerships are passed to SessionController.
156   raw_ptr<TestingPrefStore, DanglingUntriaged | ExperimentalAsh>
157       recommended_prefs_;
158   raw_ptr<sync_preferences::TestingPrefServiceSyncable,
159           DanglingUntriaged | ExperimentalAsh>
160       prefs_;
161 };
162
163 // Verifies that when no recommended values have been set, |restorer_| does not
164 // clear user settings on initialization and does not start a timer that will
165 // clear user settings eventually.
166 TEST_F(PolicyRecommendationRestorerTest, NoRecommendations) {
167   SetUserSettings();
168
169   ConnectToSigninPrefService();
170   VerifyPrefsFollowUser();
171   EXPECT_FALSE(RestoreTimerIsRunning());
172 }
173
174 // Verifies that when recommended values have been set, |restorer_| clears user
175 // settings on signing pref initialization.
176 TEST_F(PolicyRecommendationRestorerTest, RestoreOnSigninPrefInitialized) {
177   SetRecommendedValues();
178   SetUserSettings();
179
180   ConnectToSigninPrefService();
181   VerifyPrefsFollowRecommendation();
182   EXPECT_FALSE(RestoreTimerIsRunning());
183 }
184
185 // Verifies that if recommended values change while the signin screen is being
186 // shown, a timer is started that will clear user settings eventually.
187 TEST_F(PolicyRecommendationRestorerTest,
188        RestoreOnRecommendationChangeOnSigninScreen) {
189   // No recommended values in observed prefs.
190   ConnectToSigninPrefService();
191   EXPECT_FALSE(RestoreTimerIsRunning());
192
193   prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled, true);
194   VerifyPrefFollowsUser(prefs::kAccessibilityLargeCursorEnabled,
195                         base::Value(true));
196   EXPECT_FALSE(RestoreTimerIsRunning());
197   recommended_prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled,
198                                  false);
199   EXPECT_TRUE(TriggerRestoreTimer());
200   VerifyPrefFollowsRecommendation(prefs::kAccessibilityLargeCursorEnabled,
201                                   base::Value(false));
202
203   prefs_->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled, true);
204   VerifyPrefFollowsUser(prefs::kAccessibilitySpokenFeedbackEnabled,
205                         base::Value(true));
206   recommended_prefs_->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled,
207                                  false);
208   EXPECT_TRUE(TriggerRestoreTimer());
209   VerifyPrefFollowsRecommendation(prefs::kAccessibilitySpokenFeedbackEnabled,
210                                   base::Value(false));
211
212   prefs_->SetBoolean(prefs::kAccessibilityHighContrastEnabled, true);
213   VerifyPrefFollowsUser(prefs::kAccessibilityHighContrastEnabled,
214                         base::Value(true));
215   recommended_prefs_->SetBoolean(prefs::kAccessibilityHighContrastEnabled,
216                                  false);
217   EXPECT_TRUE(TriggerRestoreTimer());
218   VerifyPrefFollowsRecommendation(prefs::kAccessibilityHighContrastEnabled,
219                                   base::Value(false));
220
221   prefs_->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled, true);
222   VerifyPrefFollowsUser(prefs::kAccessibilityScreenMagnifierEnabled,
223                         base::Value(true));
224   recommended_prefs_->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled,
225                                  false);
226   EXPECT_TRUE(TriggerRestoreTimer());
227   VerifyPrefFollowsRecommendation(prefs::kAccessibilityScreenMagnifierEnabled,
228                                   base::Value(false));
229
230   prefs_->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled, true);
231   VerifyPrefFollowsUser(prefs::kAccessibilityVirtualKeyboardEnabled,
232                         base::Value(true));
233   recommended_prefs_->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled,
234                                  false);
235   EXPECT_TRUE(TriggerRestoreTimer());
236   VerifyPrefFollowsRecommendation(prefs::kAccessibilityVirtualKeyboardEnabled,
237                                   base::Value(false));
238 }
239
240 // Verifies that if recommended values change while a user session is in
241 // progress, user settings are cleared immediately.
242 TEST_F(PolicyRecommendationRestorerTest,
243        RestoreOnRecommendationChangeOnUserSession) {
244   SetUserSettings();
245
246   ConnectToSigninPrefService();
247   SimulateUserLogin("user@test.com");
248
249   VerifyPrefFollowsUser(prefs::kAccessibilityLargeCursorEnabled,
250                         base::Value(true));
251   recommended_prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled,
252                                  false);
253   EXPECT_FALSE(RestoreTimerIsRunning());
254   VerifyPrefFollowsRecommendation(prefs::kAccessibilityLargeCursorEnabled,
255                                   base::Value(false));
256
257   VerifyPrefFollowsUser(prefs::kAccessibilitySpokenFeedbackEnabled,
258                         base::Value(true));
259   recommended_prefs_->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled,
260                                  false);
261   EXPECT_FALSE(RestoreTimerIsRunning());
262   VerifyPrefFollowsRecommendation(prefs::kAccessibilitySpokenFeedbackEnabled,
263                                   base::Value(false));
264
265   VerifyPrefFollowsUser(prefs::kAccessibilityHighContrastEnabled,
266                         base::Value(true));
267   recommended_prefs_->SetBoolean(prefs::kAccessibilityHighContrastEnabled,
268                                  false);
269   EXPECT_FALSE(RestoreTimerIsRunning());
270   VerifyPrefFollowsRecommendation(prefs::kAccessibilityHighContrastEnabled,
271                                   base::Value(false));
272
273   VerifyPrefFollowsUser(prefs::kAccessibilityScreenMagnifierEnabled,
274                         base::Value(true));
275   recommended_prefs_->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled,
276                                  false);
277   EXPECT_FALSE(RestoreTimerIsRunning());
278   VerifyPrefFollowsRecommendation(prefs::kAccessibilityScreenMagnifierEnabled,
279                                   base::Value(false));
280
281   VerifyPrefFollowsUser(prefs::kAccessibilityVirtualKeyboardEnabled,
282                         base::Value(true));
283   recommended_prefs_->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled,
284                                  false);
285   EXPECT_FALSE(RestoreTimerIsRunning());
286   VerifyPrefFollowsRecommendation(prefs::kAccessibilityVirtualKeyboardEnabled,
287                                   base::Value(false));
288 }
289
290 // Verifies that if no recommended values have been set and user settings
291 // change, the user settings are not cleared immediately and no timer is started
292 // that will clear the user settings eventually.
293 TEST_F(PolicyRecommendationRestorerTest, DoNothingOnUserChange) {
294   ConnectToSigninPrefService();
295
296   // a11y mono audio is not observing recommended values in production.
297   prefs_->SetBoolean(prefs::kAccessibilityMonoAudioEnabled, true);
298   VerifyPrefFollowsUser(prefs::kAccessibilityMonoAudioEnabled,
299                         base::Value(true));
300   EXPECT_FALSE(RestoreTimerIsRunning());
301 }
302
303 TEST_F(PolicyRecommendationRestorerTest, RestoreOnUserChange) {
304   SetRecommendedValues();
305   ConnectToSigninPrefService();
306
307   EXPECT_FALSE(RestoreTimerIsRunning());
308   prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled, true);
309   VerifyPrefFollowsUser(prefs::kAccessibilityLargeCursorEnabled,
310                         base::Value(true));
311   EXPECT_TRUE(TriggerRestoreTimer());
312   VerifyPrefFollowsRecommendation(prefs::kAccessibilityLargeCursorEnabled,
313                                   base::Value(false));
314
315   EXPECT_FALSE(RestoreTimerIsRunning());
316   prefs_->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled, true);
317   VerifyPrefFollowsUser(prefs::kAccessibilitySpokenFeedbackEnabled,
318                         base::Value(true));
319   EXPECT_TRUE(TriggerRestoreTimer());
320   VerifyPrefFollowsRecommendation(prefs::kAccessibilitySpokenFeedbackEnabled,
321                                   base::Value(false));
322
323   EXPECT_FALSE(RestoreTimerIsRunning());
324   prefs_->SetBoolean(prefs::kAccessibilityHighContrastEnabled, true);
325   VerifyPrefFollowsUser(prefs::kAccessibilityHighContrastEnabled,
326                         base::Value(true));
327   EXPECT_TRUE(TriggerRestoreTimer());
328   VerifyPrefFollowsRecommendation(prefs::kAccessibilityHighContrastEnabled,
329                                   base::Value(false));
330
331   EXPECT_FALSE(RestoreTimerIsRunning());
332   prefs_->SetBoolean(prefs::kAccessibilityScreenMagnifierEnabled, true);
333   VerifyPrefFollowsUser(prefs::kAccessibilityScreenMagnifierEnabled,
334                         base::Value(true));
335   EXPECT_TRUE(TriggerRestoreTimer());
336   VerifyPrefFollowsRecommendation(prefs::kAccessibilityScreenMagnifierEnabled,
337                                   base::Value(false));
338
339   EXPECT_FALSE(RestoreTimerIsRunning());
340   prefs_->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled, true);
341   VerifyPrefFollowsUser(prefs::kAccessibilityVirtualKeyboardEnabled,
342                         base::Value(true));
343   EXPECT_TRUE(TriggerRestoreTimer());
344   VerifyPrefFollowsRecommendation(prefs::kAccessibilityVirtualKeyboardEnabled,
345                                   base::Value(false));
346
347   EXPECT_FALSE(RestoreTimerIsRunning());
348 }
349
350 // Verifies that if recommended values have not been set, user settings have
351 // changed and a session is then started, the user settings are not cleared
352 // immediately.
353 TEST_F(PolicyRecommendationRestorerTest, DoNothingOnSessionStart) {
354   ConnectToSigninPrefService();
355   SetUserSettings();
356
357   SimulateUserLogin("user@test.com");
358   VerifyPrefsFollowUser();
359   EXPECT_FALSE(RestoreTimerIsRunning());
360 }
361
362 // Verifies that user activity resets the timer which clears user settings.
363 TEST_F(PolicyRecommendationRestorerTest, UserActivityResetsTimer) {
364   recommended_prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled,
365                                  false);
366   ConnectToSigninPrefService();
367
368   prefs_->SetBoolean(prefs::kAccessibilityLargeCursorEnabled, true);
369   EXPECT_TRUE(RestoreTimerIsRunning());
370
371   // Notify that there is user activity.
372   restorer_->OnUserActivity(nullptr);
373   VerifyPrefFollowsUser(prefs::kAccessibilityLargeCursorEnabled,
374                         base::Value(true));
375
376   // Trigger reset timer to timeout.
377   EXPECT_TRUE(TriggerRestoreTimer());
378   VerifyPrefFollowsRecommendation(prefs::kAccessibilityLargeCursorEnabled,
379                                   base::Value(false));
380   EXPECT_FALSE(RestoreTimerIsRunning());
381 }
382
383 }  // namespace ash