1 // Copyright 2017 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/chromeos/arc/arc_util.h"
6 
7 #include <memory>
8 
9 #include "base/command_line.h"
10 #include "base/macros.h"
11 #include "base/system/sys_info.h"
12 #include "base/test/icu_test_util.h"
13 #include "base/test/scoped_command_line.h"
14 #include "base/test/scoped_feature_list.h"
15 #include "base/values.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/arc/session/arc_session_manager.h"
18 #include "chrome/browser/chromeos/login/demo_mode/demo_session.h"
19 #include "chrome/browser/chromeos/login/oobe_configuration.h"
20 #include "chrome/browser/chromeos/login/ui/fake_login_display_host.h"
21 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
22 #include "chrome/browser/chromeos/login/wizard_controller.h"
23 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
24 #include "chrome/browser/chromeos/profiles/profile_helper.h"
25 #include "chrome/browser/chromeos/settings/cros_settings.h"
26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
27 #include "chrome/browser/policy/profile_policy_connector.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/ui/webui/chromeos/login/demo_preferences_screen_handler.h"
30 #include "chrome/test/base/testing_browser_process.h"
31 #include "chrome/test/base/testing_profile.h"
32 #include "chrome/test/base/testing_profile_manager.h"
33 #include "chromeos/constants/chromeos_features.h"
34 #include "chromeos/dbus/dbus_thread_manager.h"
35 #include "chromeos/dbus/fake_oobe_configuration_client.h"
36 #include "chromeos/tpm/stub_install_attributes.h"
37 #include "components/account_id/account_id.h"
38 #include "components/arc/arc_prefs.h"
39 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
40 #include "components/prefs/pref_service.h"
41 #include "components/prefs/testing_pref_service.h"
42 #include "components/sync_preferences/testing_pref_service_syncable.h"
43 #include "components/user_manager/known_user.h"
44 #include "components/user_manager/scoped_user_manager.h"
45 #include "components/user_manager/user_manager.h"
46 #include "components/user_manager/user_names.h"
47 #include "content/public/browser/notification_service.h"
48 #include "content/public/common/content_switches.h"
49 #include "content/public/test/browser_task_environment.h"
50 #include "testing/gtest/include/gtest/gtest.h"
51 
52 namespace arc {
53 namespace util {
54 
55 namespace {
56 
57 constexpr char kTestProfileName[] = "user@gmail.com";
58 constexpr char kTestGaiaId[] = "1234567890";
59 
SetProfileIsManagedForTesting(Profile * profile)60 void SetProfileIsManagedForTesting(Profile* profile) {
61   policy::ProfilePolicyConnector* const connector =
62       profile->GetProfilePolicyConnector();
63   connector->OverrideIsManagedForTesting(true);
64 }
65 
DisableDBusForProfileManager()66 void DisableDBusForProfileManager() {
67   // Prevent access to DBus. This switch is reset in case set from test SetUp
68   // due massive usage of InitFromArgv.
69   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
70   if (!command_line->HasSwitch(switches::kTestType))
71     command_line->AppendSwitch(switches::kTestType);
72 }
73 
74 class FakeUserManagerWithLocalState : public chromeos::FakeChromeUserManager {
75  public:
FakeUserManagerWithLocalState(TestingProfileManager * testing_profile_manager)76   explicit FakeUserManagerWithLocalState(
77       TestingProfileManager* testing_profile_manager)
78       : testing_profile_manager_(testing_profile_manager),
79         test_local_state_(std::make_unique<TestingPrefServiceSimple>()) {
80     RegisterPrefs(test_local_state_->registry());
81   }
82 
GetLocalState() const83   PrefService* GetLocalState() const override {
84     return test_local_state_.get();
85   }
86 
testing_profile_manager()87   TestingProfileManager* testing_profile_manager() {
88     return testing_profile_manager_;
89   }
90 
91  private:
92   // Unowned pointer.
93   TestingProfileManager* const testing_profile_manager_;
94 
95   std::unique_ptr<TestingPrefServiceSimple> test_local_state_;
96 
97   DISALLOW_COPY_AND_ASSIGN(FakeUserManagerWithLocalState);
98 };
99 
100 class ScopedLogIn {
101  public:
ScopedLogIn(FakeUserManagerWithLocalState * fake_user_manager,const AccountId & account_id,user_manager::UserType user_type=user_manager::USER_TYPE_REGULAR)102   ScopedLogIn(
103       FakeUserManagerWithLocalState* fake_user_manager,
104       const AccountId& account_id,
105       user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR)
106       : fake_user_manager_(fake_user_manager), account_id_(account_id) {
107     // Prevent access to DBus. This switch is reset in case set from test SetUp
108     // due massive usage of InitFromArgv.
109     base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
110     if (!command_line.HasSwitch(switches::kTestType))
111       command_line.AppendSwitch(switches::kTestType);
112 
113     switch (user_type) {
114       case user_manager::USER_TYPE_REGULAR:  // fallthrough
115       case user_manager::USER_TYPE_ACTIVE_DIRECTORY:
116         LogIn();
117         break;
118       case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
119         LogInAsPublicAccount();
120         break;
121       case user_manager::USER_TYPE_ARC_KIOSK_APP:
122         LogInArcKioskApp();
123         break;
124       default:
125         NOTREACHED();
126     }
127   }
128 
~ScopedLogIn()129   ~ScopedLogIn() { fake_user_manager_->RemoveUserFromList(account_id_); }
130 
131  private:
LogIn()132   void LogIn() {
133     fake_user_manager_->AddUser(account_id_);
134     fake_user_manager_->LoginUser(account_id_);
135   }
136 
LogInAsPublicAccount()137   void LogInAsPublicAccount() {
138     fake_user_manager_->AddPublicAccountUser(account_id_);
139     fake_user_manager_->LoginUser(account_id_);
140   }
141 
LogInArcKioskApp()142   void LogInArcKioskApp() {
143     fake_user_manager_->AddArcKioskAppUser(account_id_);
144     fake_user_manager_->LoginUser(account_id_);
145   }
146 
147   FakeUserManagerWithLocalState* fake_user_manager_;
148   const AccountId account_id_;
149 
150   DISALLOW_COPY_AND_ASSIGN(ScopedLogIn);
151 };
152 
IsArcAllowedForProfileOnFirstCall(const Profile * profile)153 bool IsArcAllowedForProfileOnFirstCall(const Profile* profile) {
154   ResetArcAllowedCheckForTesting(profile);
155   return IsArcAllowedForProfile(profile);
156 }
157 
158 }  // namespace
159 
160 class ChromeArcUtilTest : public testing::Test {
161  public:
162   ChromeArcUtilTest() = default;
163   ~ChromeArcUtilTest() override = default;
164 
SetUp()165   void SetUp() override {
166     command_line_ = std::make_unique<base::test::ScopedCommandLine>();
167 
168     ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
169     profile_manager_ = std::make_unique<TestingProfileManager>(
170         TestingBrowserProcess::GetGlobal());
171     ASSERT_TRUE(profile_manager_->SetUp());
172 
173     user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
174         std::make_unique<FakeUserManagerWithLocalState>(
175             profile_manager_.get()));
176 
177     profile_ = profile_manager_->CreateTestingProfile(kTestProfileName);
178   }
179 
TearDown()180   void TearDown() override {
181     // Avoid retries, let the next test start safely.
182     ResetArcAllowedCheckForTesting(profile_);
183     profile_manager_->DeleteTestingProfile(kTestProfileName);
184     profile_ = nullptr;
185     user_manager_enabler_.reset();
186     profile_manager_.reset();
187     command_line_.reset();
188   }
189 
profile()190   TestingProfile* profile() { return profile_; }
191 
GetFakeUserManager() const192   FakeUserManagerWithLocalState* GetFakeUserManager() const {
193     return static_cast<FakeUserManagerWithLocalState*>(
194         user_manager::UserManager::Get());
195   }
196 
LogIn()197   void LogIn() {
198     const auto account_id = AccountId::FromUserEmailGaiaId(
199         profile()->GetProfileUserName(), kTestGaiaId);
200     GetFakeUserManager()->AddUser(account_id);
201     GetFakeUserManager()->LoginUser(account_id);
202   }
203 
204  private:
205   std::unique_ptr<base::test::ScopedCommandLine> command_line_;
206   base::test::ScopedFeatureList feature_list_;
207   content::BrowserTaskEnvironment task_environment_;
208   chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
209   base::ScopedTempDir data_dir_;
210   std::unique_ptr<TestingProfileManager> profile_manager_;
211   std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
212   // Owned by |profile_manager_|
213   TestingProfile* profile_ = nullptr;
214 
215   DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest);
216 };
217 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile)218 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) {
219   base::CommandLine::ForCurrentProcess()->InitFromArgv(
220       {"", "--arc-availability=officially-supported"});
221   ScopedLogIn login(GetFakeUserManager(),
222                     AccountId::FromUserEmailGaiaId(
223                         profile()->GetProfileUserName(), kTestGaiaId));
224   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
225 
226   // false for nullptr.
227   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(nullptr));
228 
229   // false for incognito mode profile.
230   EXPECT_FALSE(
231       IsArcAllowedForProfileOnFirstCall(profile()->GetPrimaryOTRProfile()));
232 
233   // false for Legacy supervised user.
234   profile()->SetSupervisedUserId("foo");
235   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
236 }
237 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfileLegacy)238 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfileLegacy) {
239   base::CommandLine::ForCurrentProcess()->InitFromArgv({"", "--enable-arc"});
240   ScopedLogIn login(GetFakeUserManager(),
241                     AccountId::FromUserEmailGaiaId(
242                         profile()->GetProfileUserName(), kTestGaiaId));
243   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
244 
245   // false for nullptr.
246   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(nullptr));
247 
248   // false for incognito mode profile.
249   EXPECT_FALSE(
250       IsArcAllowedForProfileOnFirstCall(profile()->GetPrimaryOTRProfile()));
251 
252   // false for Legacy supervised user.
253   profile()->SetSupervisedUserId("foo");
254   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
255 }
256 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_DisableArc)257 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_DisableArc) {
258   base::CommandLine::ForCurrentProcess()->InitFromArgv({""});
259   ScopedLogIn login(GetFakeUserManager(),
260                     AccountId::FromUserEmailGaiaId(
261                         profile()->GetProfileUserName(), kTestGaiaId));
262   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
263 }
264 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_NonPrimaryProfile)265 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_NonPrimaryProfile) {
266   base::CommandLine::ForCurrentProcess()->InitFromArgv(
267       {"", "--arc-availability=officially-supported"});
268   ScopedLogIn login2(
269       GetFakeUserManager(),
270       AccountId::FromUserEmailGaiaId("user2@gmail.com", "0123456789"));
271   ScopedLogIn login(GetFakeUserManager(),
272                     AccountId::FromUserEmailGaiaId(
273                         profile()->GetProfileUserName(), kTestGaiaId));
274   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
275 }
276 
277 // User without GAIA account.
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_PublicAccount)278 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_PublicAccount) {
279   base::CommandLine::ForCurrentProcess()->InitFromArgv(
280       {"", "--arc-availability=officially-supported"});
281   ScopedLogIn login(GetFakeUserManager(),
282                     AccountId::FromUserEmail("public_user@gmail.com"),
283                     user_manager::USER_TYPE_PUBLIC_ACCOUNT);
284   EXPECT_TRUE(IsArcAllowedForProfile(profile()));
285 }
286 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_ActiveDirectoryEnabled)287 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_ActiveDirectoryEnabled) {
288   base::CommandLine::ForCurrentProcess()->InitFromArgv(
289       {"", "--arc-availability=officially-supported"});
290   ScopedLogIn login(
291       GetFakeUserManager(),
292       AccountId::AdFromObjGuid("f04557de-5da2-40ce-ae9d-b8874d8da96e"),
293       user_manager::USER_TYPE_ACTIVE_DIRECTORY);
294   EXPECT_FALSE(chromeos::ProfileHelper::Get()
295                    ->GetUserByProfile(profile())
296                    ->HasGaiaAccount());
297   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
298 }
299 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_ActiveDirectoryDisabled)300 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_ActiveDirectoryDisabled) {
301   base::CommandLine::ForCurrentProcess()->InitFromArgv({""});
302   ScopedLogIn login(
303       GetFakeUserManager(),
304       AccountId::AdFromObjGuid("f04557de-5da2-40ce-ae9d-b8874d8da96e"),
305       user_manager::USER_TYPE_ACTIVE_DIRECTORY);
306   EXPECT_FALSE(chromeos::ProfileHelper::Get()
307                    ->GetUserByProfile(profile())
308                    ->HasGaiaAccount());
309   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
310 }
311 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_KioskArcNotAvailable)312 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_KioskArcNotAvailable) {
313   base::CommandLine::ForCurrentProcess()->InitFromArgv({""});
314   ScopedLogIn login(GetFakeUserManager(),
315                     AccountId::FromUserEmail(profile()->GetProfileUserName()),
316                     user_manager::USER_TYPE_ARC_KIOSK_APP);
317   EXPECT_FALSE(chromeos::ProfileHelper::Get()
318                    ->GetUserByProfile(profile())
319                    ->HasGaiaAccount());
320   EXPECT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
321 }
322 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_KioskArcInstalled)323 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_KioskArcInstalled) {
324   base::CommandLine::ForCurrentProcess()->InitFromArgv(
325       {"", "--arc-availability=installed"});
326   ScopedLogIn login(GetFakeUserManager(),
327                     AccountId::FromUserEmail(profile()->GetProfileUserName()),
328                     user_manager::USER_TYPE_ARC_KIOSK_APP);
329   EXPECT_FALSE(chromeos::ProfileHelper::Get()
330                    ->GetUserByProfile(profile())
331                    ->HasGaiaAccount());
332   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
333 }
334 
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_KioskArcSupported)335 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_KioskArcSupported) {
336   base::CommandLine::ForCurrentProcess()->InitFromArgv(
337       {"", "--arc-availability=officially-supported"});
338   ScopedLogIn login(GetFakeUserManager(),
339                     AccountId::FromUserEmail(profile()->GetProfileUserName()),
340                     user_manager::USER_TYPE_ARC_KIOSK_APP);
341   EXPECT_FALSE(chromeos::ProfileHelper::Get()
342                    ->GetUserByProfile(profile())
343                    ->HasGaiaAccount());
344   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
345 }
346 
347 // Guest account is interpreted as EphemeralDataUser.
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_GuestAccount)348 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_GuestAccount) {
349   base::CommandLine::ForCurrentProcess()->InitFromArgv(
350       {"", "--arc-availability=officially-supported"});
351   ScopedLogIn login(GetFakeUserManager(),
352                     GetFakeUserManager()->GetGuestAccountId());
353   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
354 }
355 
356 // Demo account is interpreted as EphemeralDataUser.
TEST_F(ChromeArcUtilTest,IsArcAllowedForProfile_DemoAccount)357 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_DemoAccount) {
358   base::CommandLine::ForCurrentProcess()->InitFromArgv(
359       {"", "--arc-availability=officially-supported"});
360   ScopedLogIn login(GetFakeUserManager(), user_manager::DemoAccountId());
361   EXPECT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
362 }
363 
TEST_F(ChromeArcUtilTest,IsArcBlockedDueToIncompatibleFileSystem)364 TEST_F(ChromeArcUtilTest, IsArcBlockedDueToIncompatibleFileSystem) {
365   base::CommandLine::ForCurrentProcess()->InitFromArgv(
366       {"", "--arc-availability=officially-supported"});
367   SetArcBlockedDueToIncompatibleFileSystemForTesting(true);
368 
369   const AccountId user_id(AccountId::FromUserEmailGaiaId(
370       profile()->GetProfileUserName(), kTestGaiaId));
371   const AccountId robot_id(
372       AccountId::FromUserEmail(profile()->GetProfileUserName()));
373 
374   // Blocked for a regular user.
375   {
376     ScopedLogIn login(GetFakeUserManager(), user_id,
377                       user_manager::USER_TYPE_REGULAR);
378     EXPECT_TRUE(IsArcBlockedDueToIncompatibleFileSystem(profile()));
379   }
380 
381   // Never blocked for an ARC kiosk.
382   {
383     ScopedLogIn login(GetFakeUserManager(), robot_id,
384                       user_manager::USER_TYPE_ARC_KIOSK_APP);
385     EXPECT_FALSE(IsArcBlockedDueToIncompatibleFileSystem(profile()));
386   }
387 
388   // Never blocked for a public session.
389   {
390     ScopedLogIn login(GetFakeUserManager(), robot_id,
391                       user_manager::USER_TYPE_PUBLIC_ACCOUNT);
392     EXPECT_FALSE(IsArcBlockedDueToIncompatibleFileSystem(profile()));
393   }
394 }
395 
TEST_F(ChromeArcUtilTest,IsArcCompatibleFileSystemUsedForProfile)396 TEST_F(ChromeArcUtilTest, IsArcCompatibleFileSystemUsedForProfile) {
397   base::CommandLine::ForCurrentProcess()->InitFromArgv(
398       {"", "--arc-availability=officially-supported"});
399 
400   const AccountId id(AccountId::FromUserEmailGaiaId(
401       profile()->GetProfileUserName(), kTestGaiaId));
402   ScopedLogIn login(GetFakeUserManager(), id);
403   const user_manager::User* user =
404       chromeos::ProfileHelper::Get()->GetUserByProfile(profile());
405 
406   // Unconfirmed
407   EXPECT_FALSE(IsArcCompatibleFileSystemUsedForUser(user));
408 
409   // Old FS
410   user_manager::known_user::SetIntegerPref(
411       id, prefs::kArcCompatibleFilesystemChosen, kFileSystemIncompatible);
412   EXPECT_FALSE(IsArcCompatibleFileSystemUsedForUser(user));
413 
414   // New FS
415   user_manager::known_user::SetIntegerPref(
416       id, prefs::kArcCompatibleFilesystemChosen, kFileSystemCompatible);
417   EXPECT_TRUE(IsArcCompatibleFileSystemUsedForUser(user));
418 
419   // New FS (User notified)
420   user_manager::known_user::SetIntegerPref(
421       id, prefs::kArcCompatibleFilesystemChosen,
422       kFileSystemCompatibleAndNotifiedDeprecated);
423   EXPECT_TRUE(IsArcCompatibleFileSystemUsedForUser(user));
424 }
425 
TEST_F(ChromeArcUtilTest,ArcPlayStoreEnabledForProfile)426 TEST_F(ChromeArcUtilTest, ArcPlayStoreEnabledForProfile) {
427   base::CommandLine::ForCurrentProcess()->InitFromArgv(
428       {"", "--arc-availability=officially-supported"});
429   // Ensure IsAllowedForProfile() true.
430   ScopedLogIn login(GetFakeUserManager(),
431                     AccountId::FromUserEmailGaiaId(
432                         profile()->GetProfileUserName(), kTestGaiaId));
433   ASSERT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
434 
435   // By default, Google Play Store is disabled.
436   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
437 
438   // Enable Google Play Store.
439   SetArcPlayStoreEnabledForProfile(profile(), true);
440   EXPECT_TRUE(IsArcPlayStoreEnabledForProfile(profile()));
441 
442   // Disable Google Play Store.
443   SetArcPlayStoreEnabledForProfile(profile(), false);
444   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
445 }
446 
TEST_F(ChromeArcUtilTest,ArcPlayStoreEnabledForProfile_NotAllowed)447 TEST_F(ChromeArcUtilTest, ArcPlayStoreEnabledForProfile_NotAllowed) {
448   base::CommandLine::ForCurrentProcess()->InitFromArgv(
449       {"", "--arc-availability=officially-supported"});
450   ASSERT_FALSE(IsArcAllowedForProfileOnFirstCall(profile()));
451 
452   // If ARC is not allowed for the profile, always return false.
453   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
454 
455   // Directly set the preference value, to avoid DCHECK in
456   // SetArcPlayStoreEnabledForProfile().
457   profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true);
458   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
459 }
460 
TEST_F(ChromeArcUtilTest,ArcPlayStoreEnabledForProfile_Managed)461 TEST_F(ChromeArcUtilTest, ArcPlayStoreEnabledForProfile_Managed) {
462   base::CommandLine::ForCurrentProcess()->InitFromArgv(
463       {"", "--arc-availability=officially-supported"});
464   // Ensure IsAllowedForProfile() true.
465   ScopedLogIn login(GetFakeUserManager(),
466                     AccountId::FromUserEmailGaiaId(
467                         profile()->GetProfileUserName(), kTestGaiaId));
468   ASSERT_TRUE(IsArcAllowedForProfileOnFirstCall(profile()));
469 
470   // By default it is not managed.
471   EXPECT_FALSE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
472   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
473 
474   // 1) Set managed preference to true, then try to set the value to false
475   // via SetArcPlayStoreEnabledForProfile().
476   profile()->GetTestingPrefService()->SetManagedPref(
477       prefs::kArcEnabled, std::make_unique<base::Value>(true));
478   EXPECT_TRUE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
479   EXPECT_TRUE(IsArcPlayStoreEnabledForProfile(profile()));
480   SetArcPlayStoreEnabledForProfile(profile(), false);
481   EXPECT_TRUE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
482   EXPECT_TRUE(IsArcPlayStoreEnabledForProfile(profile()));
483 
484   // Remove managed state.
485   profile()->GetTestingPrefService()->RemoveManagedPref(prefs::kArcEnabled);
486   EXPECT_FALSE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
487 
488   // 2) Set managed preference to false, then try to set the value to true
489   // via SetArcPlayStoreEnabledForProfile().
490   profile()->GetTestingPrefService()->SetManagedPref(
491       prefs::kArcEnabled, std::make_unique<base::Value>(false));
492   EXPECT_TRUE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
493   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
494   SetArcPlayStoreEnabledForProfile(profile(), true);
495   EXPECT_TRUE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
496   EXPECT_FALSE(IsArcPlayStoreEnabledForProfile(profile()));
497 
498   // Remove managed state.
499   profile()->GetTestingPrefService()->RemoveManagedPref(prefs::kArcEnabled);
500   EXPECT_FALSE(IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
501 }
502 
503 // Test the AreArcAllOptInPreferencesIgnorableForProfile() function.
TEST_F(ChromeArcUtilTest,AreArcAllOptInPreferencesIgnorableForProfile)504 TEST_F(ChromeArcUtilTest, AreArcAllOptInPreferencesIgnorableForProfile) {
505   base::CommandLine::ForCurrentProcess()->InitFromArgv(
506       {"", "--arc-availability=officially-supported"});
507   // OptIn prefs are unset, the function returns false.
508   EXPECT_FALSE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
509 
510   // Prefs are unused for Active Directory users.
511   {
512     ScopedLogIn login(GetFakeUserManager(),
513                       AccountId::AdFromUserEmailObjGuid(
514                           profile()->GetProfileUserName(), kTestGaiaId));
515     EXPECT_TRUE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
516   }
517 
518   // OptIn prefs are set to unmanaged/OFF values, and the function returns
519   // false.
520   profile()->GetPrefs()->SetBoolean(prefs::kArcBackupRestoreEnabled, false);
521   profile()->GetPrefs()->SetBoolean(prefs::kArcLocationServiceEnabled, false);
522   EXPECT_FALSE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
523 
524   // OptIn prefs are set to unmanaged/ON values, and the function returns false.
525   profile()->GetPrefs()->SetBoolean(prefs::kArcBackupRestoreEnabled, true);
526   profile()->GetPrefs()->SetBoolean(prefs::kArcLocationServiceEnabled, true);
527   EXPECT_FALSE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
528 
529   // Backup-restore pref is managed/OFF, while location-service is unmanaged,
530   // and the function returns false.
531   profile()->GetTestingPrefService()->SetManagedPref(
532       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
533   profile()->GetPrefs()->SetBoolean(prefs::kArcLocationServiceEnabled, false);
534   EXPECT_FALSE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
535 
536   // Location-service pref is managed/OFF, while backup-restore is unmanaged,
537   // and the function returns false.
538   profile()->GetTestingPrefService()->RemoveManagedPref(
539       prefs::kArcBackupRestoreEnabled);
540   profile()->GetTestingPrefService()->SetManagedPref(
541       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
542   EXPECT_FALSE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
543 
544   // Both OptIn prefs are set to managed/OFF values, and the function returns
545   // true.
546   profile()->GetTestingPrefService()->SetManagedPref(
547       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
548   profile()->GetTestingPrefService()->SetManagedPref(
549       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
550   EXPECT_TRUE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
551 
552   // Backup-restore pref is set to managed/ON, while location-service pref is
553   // set to managed/OFF, and the function returns true.
554   profile()->GetTestingPrefService()->SetManagedPref(
555       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(true));
556   profile()->GetTestingPrefService()->SetManagedPref(
557       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
558   EXPECT_TRUE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
559 
560   // Location-service pref is set to managed/ON, while location-service pref is
561   // set to managed/OFF, and the function returns true.
562   profile()->GetTestingPrefService()->SetManagedPref(
563       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
564   profile()->GetTestingPrefService()->SetManagedPref(
565       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(true));
566   EXPECT_TRUE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
567 
568   // Both OptIn prefs are set to managed/ON values, and the function returns
569   // true.
570   profile()->GetTestingPrefService()->SetManagedPref(
571       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(true));
572   profile()->GetTestingPrefService()->SetManagedPref(
573       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(true));
574   EXPECT_TRUE(AreArcAllOptInPreferencesIgnorableForProfile(profile()));
575 }
576 
577 // Test the IsActiveDirectoryUserForProfile() function for non-AD accounts.
TEST_F(ChromeArcUtilTest,IsActiveDirectoryUserForProfile_Gaia)578 TEST_F(ChromeArcUtilTest, IsActiveDirectoryUserForProfile_Gaia) {
579   ScopedLogIn login(GetFakeUserManager(),
580                     AccountId::FromUserEmailGaiaId(
581                         profile()->GetProfileUserName(), kTestGaiaId));
582   EXPECT_FALSE(IsActiveDirectoryUserForProfile(profile()));
583 }
584 
585 // Test the IsActiveDirectoryUserForProfile() function for AD accounts.
TEST_F(ChromeArcUtilTest,IsActiveDirectoryUserForProfile_AD)586 TEST_F(ChromeArcUtilTest, IsActiveDirectoryUserForProfile_AD) {
587   ScopedLogIn login(GetFakeUserManager(),
588                     AccountId::AdFromUserEmailObjGuid(
589                         profile()->GetProfileUserName(), kTestGaiaId));
590   EXPECT_TRUE(IsActiveDirectoryUserForProfile(profile()));
591 }
592 
TEST_F(ChromeArcUtilTest,TermsOfServiceNegotiationNeededForAlreadyAccepted)593 TEST_F(ChromeArcUtilTest, TermsOfServiceNegotiationNeededForAlreadyAccepted) {
594   base::CommandLine::ForCurrentProcess()->InitFromArgv(
595       {"", "--arc-availability=officially-supported"});
596   DisableDBusForProfileManager();
597   ScopedLogIn login(GetFakeUserManager(),
598                     AccountId::FromUserEmailGaiaId(
599                         profile()->GetProfileUserName(), kTestGaiaId));
600   EXPECT_TRUE(IsArcTermsOfServiceNegotiationNeeded(profile()));
601   EXPECT_TRUE(IsArcTermsOfServiceOobeNegotiationNeeded());
602   profile()->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true);
603   EXPECT_FALSE(IsArcTermsOfServiceNegotiationNeeded(profile()));
604   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
605 }
606 
607 // For managed user, generally no opt-in dialog is shown.
608 // For OOBE user, see TermsOfServiceOobeNegotiationNeededForManagedUser test.
TEST_F(ChromeArcUtilTest,TermsOfServiceNegotiationNeededForManagedUser)609 TEST_F(ChromeArcUtilTest, TermsOfServiceNegotiationNeededForManagedUser) {
610   base::CommandLine::ForCurrentProcess()->InitFromArgv(
611       {"", "--arc-availability=officially-supported"});
612   DisableDBusForProfileManager();
613   ScopedLogIn login(GetFakeUserManager(),
614                     AccountId::FromUserEmailGaiaId(
615                         profile()->GetProfileUserName(), kTestGaiaId));
616 
617   EXPECT_TRUE(IsArcTermsOfServiceNegotiationNeeded(profile()));
618 
619   SetProfileIsManagedForTesting(profile());
620 
621   profile()->GetTestingPrefService()->SetManagedPref(
622       prefs::kArcEnabled, std::make_unique<base::Value>(true));
623   EXPECT_TRUE(ShouldStartArcSilentlyForManagedProfile(profile()));
624   EXPECT_FALSE(IsArcTermsOfServiceNegotiationNeeded(profile()));
625 }
626 
TEST_F(ChromeArcUtilTest,TermsOfServiceOobeNegotiationNeededNoLogin)627 TEST_F(ChromeArcUtilTest, TermsOfServiceOobeNegotiationNeededNoLogin) {
628   DisableDBusForProfileManager();
629   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
630 }
631 
TEST_F(ChromeArcUtilTest,TermsOfServiceOobeNegotiationNeededNoArcAvailability)632 TEST_F(ChromeArcUtilTest,
633        TermsOfServiceOobeNegotiationNeededNoArcAvailability) {
634   DisableDBusForProfileManager();
635   ScopedLogIn login(GetFakeUserManager(),
636                     AccountId::FromUserEmailGaiaId(
637                         profile()->GetProfileUserName(), kTestGaiaId));
638   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
639 }
640 
TEST_F(ChromeArcUtilTest,TermsOfServiceOobeNegotiationNeededNoPlayStore)641 TEST_F(ChromeArcUtilTest, TermsOfServiceOobeNegotiationNeededNoPlayStore) {
642   base::CommandLine::ForCurrentProcess()->InitFromArgv(
643       {"", "--arc-availability=officially-supported",
644        "--arc-start-mode=always-start-with-no-play-store"});
645   DisableDBusForProfileManager();
646   ScopedLogIn login(GetFakeUserManager(),
647                     AccountId::FromUserEmailGaiaId(
648                         profile()->GetProfileUserName(), kTestGaiaId));
649   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
650 }
651 
TEST_F(ChromeArcUtilTest,TermsOfServiceOobeNegotiationNeededAdUser)652 TEST_F(ChromeArcUtilTest, TermsOfServiceOobeNegotiationNeededAdUser) {
653   base::CommandLine::ForCurrentProcess()->InitFromArgv(
654       {"", "--arc-availability=officially-supported"});
655   DisableDBusForProfileManager();
656   ScopedLogIn login(GetFakeUserManager(),
657                     AccountId::AdFromUserEmailObjGuid(
658                         profile()->GetProfileUserName(), kTestGaiaId));
659   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
660 }
661 
TEST_F(ChromeArcUtilTest,IsArcStatsReportingEnabled)662 TEST_F(ChromeArcUtilTest, IsArcStatsReportingEnabled) {
663   base::CommandLine::ForCurrentProcess()->InitFromArgv(
664       {"", "--arc-availability=officially-supported"});
665   ScopedLogIn login(GetFakeUserManager(),
666                     AccountId::FromUserEmailGaiaId(
667                         profile()->GetProfileUserName(), kTestGaiaId));
668   EXPECT_FALSE(IsArcStatsReportingEnabled());
669 }
670 
TEST_F(ChromeArcUtilTest,IsArcStatsReportingEnabled_PublicAccount)671 TEST_F(ChromeArcUtilTest, IsArcStatsReportingEnabled_PublicAccount) {
672   base::CommandLine::ForCurrentProcess()->InitFromArgv(
673       {"", "--arc-availability=officially-supported"});
674   ScopedLogIn login(GetFakeUserManager(),
675                     AccountId::FromUserEmail("public_user@gmail.com"),
676                     user_manager::USER_TYPE_PUBLIC_ACCOUNT);
677   EXPECT_FALSE(IsArcStatsReportingEnabled());
678 }
679 
TEST_F(ChromeArcUtilTest,ArcStartModeDefault)680 TEST_F(ChromeArcUtilTest, ArcStartModeDefault) {
681   auto* command_line = base::CommandLine::ForCurrentProcess();
682   command_line->InitFromArgv({"", "--arc-availability=installed"});
683   EXPECT_TRUE(IsPlayStoreAvailable());
684 }
685 
TEST_F(ChromeArcUtilTest,ArcStartModeDefaultPublicSession)686 TEST_F(ChromeArcUtilTest, ArcStartModeDefaultPublicSession) {
687   auto* command_line = base::CommandLine::ForCurrentProcess();
688   command_line->InitFromArgv({"", "--arc-availability=installed"});
689   ScopedLogIn login(GetFakeUserManager(),
690                     AccountId::FromUserEmail("public_user@gmail.com"),
691                     user_manager::USER_TYPE_PUBLIC_ACCOUNT);
692   EXPECT_FALSE(IsPlayStoreAvailable());
693 }
694 
TEST_F(ChromeArcUtilTest,ArcStartModeDefaultDemoMode)695 TEST_F(ChromeArcUtilTest, ArcStartModeDefaultDemoMode) {
696   auto* command_line = base::CommandLine::ForCurrentProcess();
697   command_line->InitFromArgv({"", "--arc-availability=installed"});
698   chromeos::DemoSession::SetDemoConfigForTesting(
699       chromeos::DemoSession::DemoModeConfig::kOnline);
700   ScopedLogIn login(GetFakeUserManager(),
701                     AccountId::FromUserEmail("public_user@gmail.com"),
702                     user_manager::USER_TYPE_PUBLIC_ACCOUNT);
703   EXPECT_TRUE(IsPlayStoreAvailable());
704 }
705 
706 // TODO(b/154290639): We disable Play Store if device is offline enrolled.
TEST_F(ChromeArcUtilTest,ArcStartModeDefaultOfflineDemoMode)707 TEST_F(ChromeArcUtilTest, ArcStartModeDefaultOfflineDemoMode) {
708   auto* command_line = base::CommandLine::ForCurrentProcess();
709   command_line->InitFromArgv({"", "--arc-availability=installed"});
710   chromeos::DemoSession::SetDemoConfigForTesting(
711       chromeos::DemoSession::DemoModeConfig::kOffline);
712   ScopedLogIn login(GetFakeUserManager(),
713                     AccountId::FromUserEmail("public_user@gmail.com"),
714                     user_manager::USER_TYPE_PUBLIC_ACCOUNT);
715   EXPECT_FALSE(IsPlayStoreAvailable());
716 }
717 
TEST_F(ChromeArcUtilTest,ArcStartModeDefaultDemoModeWithoutPlayStore)718 TEST_F(ChromeArcUtilTest, ArcStartModeDefaultDemoModeWithoutPlayStore) {
719   base::test::ScopedFeatureList feature_list;
720   feature_list.InitWithFeatureState(chromeos::features::kShowPlayInDemoMode,
721                                     false /* disabled */);
722   auto* command_line = base::CommandLine::ForCurrentProcess();
723   command_line->InitFromArgv({"", "--arc-availability=installed"});
724   chromeos::DemoSession::SetDemoConfigForTesting(
725       chromeos::DemoSession::DemoModeConfig::kOnline);
726   ScopedLogIn login(GetFakeUserManager(),
727                     AccountId::FromUserEmail("public_user@gmail.com"),
728                     user_manager::USER_TYPE_PUBLIC_ACCOUNT);
729   EXPECT_FALSE(IsPlayStoreAvailable());
730 }
731 
TEST_F(ChromeArcUtilTest,ArcStartModeWithoutPlayStore)732 TEST_F(ChromeArcUtilTest, ArcStartModeWithoutPlayStore) {
733   auto* command_line = base::CommandLine::ForCurrentProcess();
734   command_line->InitFromArgv(
735       {"", "--arc-availability=installed",
736        "--arc-start-mode=always-start-with-no-play-store"});
737   EXPECT_FALSE(IsPlayStoreAvailable());
738 }
739 
740 class ArcOobeTest : public ChromeArcUtilTest {
741  public:
ArcOobeTest()742   ArcOobeTest() {
743     chromeos::DBusThreadManager::GetSetterForTesting();
744     oobe_configuration_ = std::make_unique<chromeos::OobeConfiguration>();
745   }
746 
~ArcOobeTest()747   ~ArcOobeTest() override {
748     // Fake display host have to be shut down first, as it may access
749     // configuration.
750     fake_login_display_host_.reset();
751     oobe_configuration_.reset();
752     chromeos::DBusThreadManager::Shutdown();
753   }
754 
755  protected:
CreateLoginDisplayHost()756   void CreateLoginDisplayHost() {
757     fake_login_display_host_ =
758         std::make_unique<chromeos::FakeLoginDisplayHost>();
759   }
760 
login_display_host()761   chromeos::FakeLoginDisplayHost* login_display_host() {
762     return fake_login_display_host_.get();
763   }
764 
CloseLoginDisplayHost()765   void CloseLoginDisplayHost() { fake_login_display_host_.reset(); }
766 
767  private:
768   std::unique_ptr<chromeos::OobeConfiguration> oobe_configuration_;
769   std::unique_ptr<chromeos::FakeLoginDisplayHost> fake_login_display_host_;
770 
771   DISALLOW_COPY_AND_ASSIGN(ArcOobeTest);
772 };
773 
TEST_F(ArcOobeTest,TermsOfServiceOobeNegotiationNeededForManagedUser)774 TEST_F(ArcOobeTest, TermsOfServiceOobeNegotiationNeededForManagedUser) {
775   base::CommandLine::ForCurrentProcess()->InitFromArgv(
776       {"", "--arc-availability=officially-supported"});
777   DisableDBusForProfileManager();
778   ScopedLogIn login(GetFakeUserManager(),
779                     AccountId::FromUserEmailGaiaId(
780                         profile()->GetProfileUserName(), kTestGaiaId));
781 
782   GetFakeUserManager()->set_current_user_new(true);
783   CreateLoginDisplayHost();
784   EXPECT_TRUE(IsArcOobeOptInActive());
785 
786   SetProfileIsManagedForTesting(profile());
787   profile()->GetTestingPrefService()->SetManagedPref(
788       prefs::kArcEnabled, std::make_unique<base::Value>(true));
789   EXPECT_TRUE(IsArcTermsOfServiceNegotiationNeeded(profile()));
790   EXPECT_TRUE(IsArcTermsOfServiceOobeNegotiationNeeded());
791 
792   profile()->GetTestingPrefService()->SetManagedPref(
793       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
794   EXPECT_TRUE(IsArcTermsOfServiceNegotiationNeeded(profile()));
795   EXPECT_TRUE(IsArcTermsOfServiceOobeNegotiationNeeded());
796 
797   profile()->GetTestingPrefService()->SetManagedPref(
798       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
799   EXPECT_FALSE(IsArcTermsOfServiceNegotiationNeeded(profile()));
800   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
801 
802   profile()->GetTestingPrefService()->SetManagedPref(
803       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(true));
804   profile()->GetTestingPrefService()->SetManagedPref(
805       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
806   EXPECT_FALSE(IsArcTermsOfServiceNegotiationNeeded(profile()));
807   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
808 
809   profile()->GetTestingPrefService()->SetManagedPref(
810       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
811   profile()->GetTestingPrefService()->SetManagedPref(
812       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(true));
813   EXPECT_FALSE(IsArcTermsOfServiceNegotiationNeeded(profile()));
814   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
815 
816   profile()->GetTestingPrefService()->SetManagedPref(
817       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(true));
818   profile()->GetTestingPrefService()->SetManagedPref(
819       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(true));
820   EXPECT_FALSE(IsArcTermsOfServiceNegotiationNeeded(profile()));
821   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
822 }
823 
TEST_F(ArcOobeTest,ShouldStartArcSilentlyForManagedProfile)824 TEST_F(ArcOobeTest, ShouldStartArcSilentlyForManagedProfile) {
825   base::CommandLine::ForCurrentProcess()->InitFromArgv(
826       {"", "--arc-availability=officially-supported"});
827   DisableDBusForProfileManager();
828   ScopedLogIn login(GetFakeUserManager(),
829                     AccountId::FromUserEmailGaiaId(
830                         profile()->GetProfileUserName(), kTestGaiaId));
831 
832   GetFakeUserManager()->set_current_user_new(true);
833   CreateLoginDisplayHost();
834   EXPECT_TRUE(IsArcOobeOptInActive());
835 
836   SetProfileIsManagedForTesting(profile());
837   profile()->GetTestingPrefService()->SetManagedPref(
838       prefs::kArcEnabled, std::make_unique<base::Value>(true));
839   EXPECT_FALSE(ShouldStartArcSilentlyForManagedProfile(profile()));
840 
841   profile()->GetTestingPrefService()->SetManagedPref(
842       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
843   EXPECT_FALSE(ShouldStartArcSilentlyForManagedProfile(profile()));
844 
845   profile()->GetTestingPrefService()->SetManagedPref(
846       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
847   EXPECT_TRUE(ShouldStartArcSilentlyForManagedProfile(profile()));
848 
849   profile()->GetTestingPrefService()->SetManagedPref(
850       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(true));
851   profile()->GetTestingPrefService()->SetManagedPref(
852       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(false));
853   EXPECT_TRUE(ShouldStartArcSilentlyForManagedProfile(profile()));
854 
855   profile()->GetTestingPrefService()->SetManagedPref(
856       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(false));
857   profile()->GetTestingPrefService()->SetManagedPref(
858       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(true));
859   EXPECT_TRUE(ShouldStartArcSilentlyForManagedProfile(profile()));
860 
861   profile()->GetTestingPrefService()->SetManagedPref(
862       prefs::kArcBackupRestoreEnabled, std::make_unique<base::Value>(true));
863   profile()->GetTestingPrefService()->SetManagedPref(
864       prefs::kArcLocationServiceEnabled, std::make_unique<base::Value>(true));
865   EXPECT_TRUE(ShouldStartArcSilentlyForManagedProfile(profile()));
866 }
867 
868 using ArcOobeOpaOptInActiveInTest = ArcOobeTest;
869 
TEST_F(ArcOobeOpaOptInActiveInTest,OobeOptInActive)870 TEST_F(ArcOobeOpaOptInActiveInTest, OobeOptInActive) {
871   // OOBE OptIn is active in case of OOBE controller is alive and the ARC ToS
872   // screen is currently showing.
873   EXPECT_FALSE(IsArcOobeOptInActive());
874   CreateLoginDisplayHost();
875   EXPECT_FALSE(IsArcOobeOptInActive());
876   GetFakeUserManager()->set_current_user_new(true);
877   EXPECT_TRUE(IsArcOobeOptInActive());
878   // OOBE OptIn can be started only for new user flow.
879   GetFakeUserManager()->set_current_user_new(false);
880   EXPECT_FALSE(IsArcOobeOptInActive());
881   // ARC ToS wizard but not for new user.
882   login_display_host()->StartWizard(
883       chromeos::ArcTermsOfServiceScreenView::kScreenId);
884   EXPECT_FALSE(IsArcOobeOptInActive());
885 }
886 
887 using DemoSetupFlowArcOptInTest = ArcOobeTest;
888 
TEST_F(DemoSetupFlowArcOptInTest,NoTermsOfServiceOobeNegotiationNeeded)889 TEST_F(DemoSetupFlowArcOptInTest, NoTermsOfServiceOobeNegotiationNeeded) {
890   base::CommandLine::ForCurrentProcess()->InitFromArgv(
891       {"", "--arc-availability=officially-supported"});
892   DisableDBusForProfileManager();
893   CreateLoginDisplayHost();
894   EXPECT_FALSE(IsArcDemoModeSetupFlow());
895   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
896 }
897 
TEST_F(DemoSetupFlowArcOptInTest,TermsOfServiceOobeNegotiationNeeded)898 TEST_F(DemoSetupFlowArcOptInTest, TermsOfServiceOobeNegotiationNeeded) {
899   base::CommandLine::ForCurrentProcess()->InitFromArgv(
900       {"", "--arc-availability=officially-supported"});
901   DisableDBusForProfileManager();
902   CreateLoginDisplayHost();
903   login_display_host()->StartWizard(
904       chromeos::DemoPreferencesScreenView::kScreenId);
905   login_display_host()
906       ->GetWizardController()
907       ->SimulateDemoModeSetupForTesting();
908   EXPECT_TRUE(IsArcDemoModeSetupFlow());
909   EXPECT_TRUE(IsArcTermsOfServiceOobeNegotiationNeeded());
910 }
911 
TEST_F(DemoSetupFlowArcOptInTest,NoPlayStoreNoTermsOfServiceOobeNegotiationNeeded)912 TEST_F(DemoSetupFlowArcOptInTest,
913        NoPlayStoreNoTermsOfServiceOobeNegotiationNeeded) {
914   base::CommandLine::ForCurrentProcess()->InitFromArgv(
915       {"", "--arc-availability=officially-supported",
916        "--arc-start-mode=always-start-with-no-play-store"});
917   DisableDBusForProfileManager();
918   CreateLoginDisplayHost();
919   login_display_host()->StartWizard(
920       chromeos::DemoPreferencesScreenView::kScreenId);
921   login_display_host()
922       ->GetWizardController()
923       ->SimulateDemoModeSetupForTesting();
924   EXPECT_TRUE(IsArcDemoModeSetupFlow());
925   EXPECT_FALSE(IsArcTermsOfServiceOobeNegotiationNeeded());
926 }
927 
928 }  // namespace util
929 }  // namespace arc
930