| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/observer_list.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "chrome/browser/chromeos/arc/arc_optin_uma.h" | 18 #include "chrome/browser/chromeos/arc/arc_optin_uma.h" |
| 18 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | 19 #include "chrome/browser/chromeos/arc/arc_session_manager.h" |
| 20 #include "chrome/browser/chromeos/arc/optin/arc_terms_of_service_oobe_negotiator
.h" |
| 21 #include "chrome/browser/chromeos/arc/test/arc_data_removed_waiter.h" |
| 22 #include "chrome/browser/chromeos/login/screens/arc_terms_of_service_screen_acto
r.h" |
| 23 #include "chrome/browser/chromeos/login/screens/arc_terms_of_service_screen_acto
r_observer.h" |
| 24 #include "chrome/browser/chromeos/login/ui/login_display_host.h" |
| 19 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | 25 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 20 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 26 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 21 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 27 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
| 22 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 28 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 29 #include "chrome/browser/policy/profile_policy_connector.h" |
| 30 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 23 #include "chrome/browser/prefs/pref_service_syncable_util.h" | 31 #include "chrome/browser/prefs/pref_service_syncable_util.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | 33 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 26 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 34 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 27 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/test/base/testing_profile.h" | 36 #include "chrome/test/base/testing_profile.h" |
| 29 #include "chromeos/chromeos_switches.h" | 37 #include "chromeos/chromeos_switches.h" |
| 30 #include "chromeos/dbus/dbus_thread_manager.h" | 38 #include "chromeos/dbus/dbus_thread_manager.h" |
| 31 #include "chromeos/dbus/fake_session_manager_client.h" | 39 #include "chromeos/dbus/fake_session_manager_client.h" |
| 32 #include "components/arc/arc_service_manager.h" | 40 #include "components/arc/arc_service_manager.h" |
| 33 #include "components/arc/arc_session_runner.h" | 41 #include "components/arc/arc_session_runner.h" |
| 34 #include "components/arc/arc_util.h" | 42 #include "components/arc/arc_util.h" |
| 35 #include "components/arc/test/fake_arc_session.h" | 43 #include "components/arc/test/fake_arc_session.h" |
| 36 #include "components/prefs/pref_service.h" | 44 #include "components/prefs/pref_service.h" |
| 37 #include "components/signin/core/account_id/account_id.h" | 45 #include "components/signin/core/account_id/account_id.h" |
| 38 #include "components/sync/model/fake_sync_change_processor.h" | 46 #include "components/sync/model/fake_sync_change_processor.h" |
| 39 #include "components/sync/model/sync_error_factory_mock.h" | 47 #include "components/sync/model/sync_error_factory_mock.h" |
| 40 #include "components/sync_preferences/testing_pref_service_syncable.h" | 48 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 41 #include "components/user_manager/user_manager.h" | 49 #include "components/user_manager/user_manager.h" |
| 42 #include "components/user_manager/user_names.h" | 50 #include "components/user_manager/user_names.h" |
| 43 #include "content/public/browser/browser_thread.h" | 51 #include "content/public/browser/browser_thread.h" |
| 44 #include "content/public/test/test_browser_thread_bundle.h" | 52 #include "content/public/test/test_browser_thread_bundle.h" |
| 45 #include "google_apis/gaia/gaia_constants.h" | 53 #include "google_apis/gaia/gaia_constants.h" |
| 46 #include "google_apis/gaia/gaia_urls.h" | 54 #include "google_apis/gaia/gaia_urls.h" |
| 47 #include "net/http/http_status_code.h" | 55 #include "net/http/http_status_code.h" |
| 48 #include "testing/gtest/include/gtest/gtest.h" | 56 #include "testing/gtest/include/gtest/gtest.h" |
| 49 | 57 |
| 50 namespace arc { | 58 namespace arc { |
| 51 | 59 |
| 52 class ArcSessionManagerTestBase : public testing::Test { | 60 namespace { |
| 61 |
| 62 class FakeLoginDisplayHost : public chromeos::LoginDisplayHost { |
| 63 public: |
| 64 FakeLoginDisplayHost() { |
| 65 DCHECK(!chromeos::LoginDisplayHost::default_host_); |
| 66 chromeos::LoginDisplayHost::default_host_ = this; |
| 67 } |
| 68 |
| 69 ~FakeLoginDisplayHost() override { |
| 70 DCHECK_EQ(chromeos::LoginDisplayHost::default_host_, this); |
| 71 chromeos::LoginDisplayHost::default_host_ = nullptr; |
| 72 } |
| 73 |
| 74 /// chromeos::LoginDisplayHost: |
| 75 chromeos::LoginDisplay* CreateLoginDisplay( |
| 76 chromeos::LoginDisplay::Delegate* delegate) override { |
| 77 return nullptr; |
| 78 } |
| 79 gfx::NativeWindow GetNativeWindow() const override { return nullptr; } |
| 80 chromeos::OobeUI* GetOobeUI() const override { return nullptr; } |
| 81 chromeos::WebUILoginView* GetWebUILoginView() const override { |
| 82 return nullptr; |
| 83 } |
| 84 void BeforeSessionStart() override {} |
| 85 void Finalize() override {} |
| 86 void OnCompleteLogin() override {} |
| 87 void OpenProxySettings() override {} |
| 88 void SetStatusAreaVisible(bool visible) override {} |
| 89 chromeos::AutoEnrollmentController* GetAutoEnrollmentController() override { |
| 90 return nullptr; |
| 91 } |
| 92 void StartWizard(chromeos::OobeScreen first_screen) override {} |
| 93 chromeos::WizardController* GetWizardController() override { return nullptr; } |
| 94 chromeos::AppLaunchController* GetAppLaunchController() override { |
| 95 return nullptr; |
| 96 } |
| 97 void StartUserAdding(const base::Closure& completion_callback) override {} |
| 98 void CancelUserAdding() override {} |
| 99 void StartSignInScreen(const chromeos::LoginScreenContext& context) override { |
| 100 } |
| 101 void OnPreferencesChanged() override {} |
| 102 void PrewarmAuthentication() override {} |
| 103 void StartAppLaunch(const std::string& app_id, |
| 104 bool diagnostic_mode, |
| 105 bool is_auto_launch) override {} |
| 106 void StartDemoAppLaunch() override {} |
| 107 |
| 108 private: |
| 109 DISALLOW_COPY_AND_ASSIGN(FakeLoginDisplayHost); |
| 110 }; |
| 111 |
| 112 } // namespace |
| 113 |
| 114 // Bool parameter is used to implement ArcSessionOobeOptInTest tests for |
| 115 // managed/unmanaged users. To prevent ambiguous testing::Test inheritance |
| 116 // implement derivation here, in base class. |
| 117 class ArcSessionManagerTestBase : public testing::TestWithParam<bool> { |
| 53 public: | 118 public: |
| 54 ArcSessionManagerTestBase() | 119 ArcSessionManagerTestBase() |
| 55 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 120 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 56 user_manager_enabler_(new chromeos::FakeChromeUserManager()) {} | 121 user_manager_enabler_(new chromeos::FakeChromeUserManager()) {} |
| 57 ~ArcSessionManagerTestBase() override = default; | 122 ~ArcSessionManagerTestBase() override = default; |
| 58 | 123 |
| 59 void SetUp() override { | 124 void SetUp() override { |
| 60 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | 125 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 61 base::MakeUnique<chromeos::FakeSessionManagerClient>()); | 126 base::MakeUnique<chromeos::FakeSessionManagerClient>()); |
| 62 | 127 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 90 arc_service_manager_.reset(); | 155 arc_service_manager_.reset(); |
| 91 chromeos::DBusThreadManager::Shutdown(); | 156 chromeos::DBusThreadManager::Shutdown(); |
| 92 } | 157 } |
| 93 | 158 |
| 94 chromeos::FakeChromeUserManager* GetFakeUserManager() const { | 159 chromeos::FakeChromeUserManager* GetFakeUserManager() const { |
| 95 return static_cast<chromeos::FakeChromeUserManager*>( | 160 return static_cast<chromeos::FakeChromeUserManager*>( |
| 96 user_manager::UserManager::Get()); | 161 user_manager::UserManager::Get()); |
| 97 } | 162 } |
| 98 | 163 |
| 99 protected: | 164 protected: |
| 100 Profile* profile() { return profile_.get(); } | 165 TestingProfile* profile() { return profile_.get(); } |
| 101 | 166 |
| 102 ArcSessionManager* arc_session_manager() { | 167 ArcSessionManager* arc_session_manager() { |
| 103 return arc_session_manager_.get(); | 168 return arc_session_manager_.get(); |
| 104 } | 169 } |
| 105 | 170 |
| 106 bool WaitForDataRemoved(ArcSessionManager::State expected_state) { | 171 bool WaitForDataRemoved(ArcSessionManager::State expected_state) { |
| 107 if (arc_session_manager()->state() != | 172 if (arc_session_manager()->state() != |
| 108 ArcSessionManager::State::REMOVING_DATA_DIR) | 173 ArcSessionManager::State::REMOVING_DATA_DIR) |
| 109 return false; | 174 return false; |
| 110 | 175 |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 // and not invoked then, including TearDown(). | 585 // and not invoked then, including TearDown(). |
| 521 bool terminated = false; | 586 bool terminated = false; |
| 522 arc_session_manager()->SetAttemptUserExitCallbackForTesting( | 587 arc_session_manager()->SetAttemptUserExitCallbackForTesting( |
| 523 base::Bind([](bool* terminated) { *terminated = true; }, &terminated)); | 588 base::Bind([](bool* terminated) { *terminated = true; }, &terminated)); |
| 524 | 589 |
| 525 arc_session_manager()->OnProvisioningFinished( | 590 arc_session_manager()->OnProvisioningFinished( |
| 526 ProvisioningResult::CHROME_SERVER_COMMUNICATION_ERROR); | 591 ProvisioningResult::CHROME_SERVER_COMMUNICATION_ERROR); |
| 527 EXPECT_TRUE(terminated); | 592 EXPECT_TRUE(terminated); |
| 528 } | 593 } |
| 529 | 594 |
| 595 class ArcSessionOobeOptInTest : public ArcSessionManagerTest { |
| 596 public: |
| 597 ArcSessionOobeOptInTest() = default; |
| 598 |
| 599 protected: |
| 600 void CreateLoginDisplayHost() { |
| 601 fake_login_display_host_ = base::MakeUnique<FakeLoginDisplayHost>(); |
| 602 } |
| 603 |
| 604 void CloseLoginDisplayHost() { fake_login_display_host_.reset(); } |
| 605 |
| 606 void AppendEnableArcOOBEOptInSwitch() { |
| 607 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 608 chromeos::switches::kEnableArcOOBEOptIn); |
| 609 } |
| 610 |
| 611 private: |
| 612 std::unique_ptr<FakeLoginDisplayHost> fake_login_display_host_; |
| 613 |
| 614 DISALLOW_COPY_AND_ASSIGN(ArcSessionOobeOptInTest); |
| 615 }; |
| 616 |
| 617 TEST_F(ArcSessionOobeOptInTest, OobeOptInActive) { |
| 618 // OOBE OptIn is active in case of OOBE is started for new user and ARC OOBE |
| 619 // is enabled by switch. |
| 620 EXPECT_FALSE(ArcSessionManager::IsOobeOptInActive()); |
| 621 GetFakeUserManager()->set_current_user_new(true); |
| 622 EXPECT_FALSE(ArcSessionManager::IsOobeOptInActive()); |
| 623 CreateLoginDisplayHost(); |
| 624 EXPECT_FALSE(ArcSessionManager::IsOobeOptInActive()); |
| 625 |
| 626 AppendEnableArcOOBEOptInSwitch(); |
| 627 GetFakeUserManager()->set_current_user_new(false); |
| 628 CloseLoginDisplayHost(); |
| 629 EXPECT_FALSE(ArcSessionManager::IsOobeOptInActive()); |
| 630 GetFakeUserManager()->set_current_user_new(true); |
| 631 EXPECT_FALSE(ArcSessionManager::IsOobeOptInActive()); |
| 632 CreateLoginDisplayHost(); |
| 633 EXPECT_TRUE(ArcSessionManager::IsOobeOptInActive()); |
| 634 } |
| 635 |
| 636 class ArcSessionOobeOptInNegotiatorTest |
| 637 : public ArcSessionOobeOptInTest, |
| 638 public chromeos::ArcTermsOfServiceScreenActor { |
| 639 public: |
| 640 ArcSessionOobeOptInNegotiatorTest() = default; |
| 641 |
| 642 void SetUp() override { |
| 643 ArcSessionOobeOptInTest::SetUp(); |
| 644 |
| 645 AppendEnableArcOOBEOptInSwitch(); |
| 646 |
| 647 ArcTermsOfServiceOobeNegotiator::SetArcTermsOfServiceScreenActorForTesting( |
| 648 this); |
| 649 |
| 650 GetFakeUserManager()->set_current_user_new(true); |
| 651 |
| 652 CreateLoginDisplayHost(); |
| 653 |
| 654 if (IsManagedUser()) { |
| 655 policy::ProfilePolicyConnector* const connector = |
| 656 policy::ProfilePolicyConnectorFactory::GetForBrowserContext( |
| 657 profile()); |
| 658 connector->OverrideIsManagedForTesting(true); |
| 659 |
| 660 profile()->GetTestingPrefService()->SetManagedPref( |
| 661 prefs::kArcEnabled, new base::FundamentalValue(true)); |
| 662 } |
| 663 |
| 664 arc_session_manager()->OnPrimaryUserProfilePrepared(profile()); |
| 665 } |
| 666 |
| 667 void TearDown() override { |
| 668 // Correctly stop service. |
| 669 arc_session_manager()->Shutdown(); |
| 670 |
| 671 ArcTermsOfServiceOobeNegotiator::SetArcTermsOfServiceScreenActorForTesting( |
| 672 nullptr); |
| 673 |
| 674 ArcSessionOobeOptInTest::TearDown(); |
| 675 } |
| 676 |
| 677 protected: |
| 678 bool IsManagedUser() { return GetParam(); } |
| 679 |
| 680 void ReportResult(bool accepted) { |
| 681 for (auto& observer : observer_list_) { |
| 682 if (accepted) |
| 683 observer.OnAccept(); |
| 684 else |
| 685 observer.OnSkip(); |
| 686 } |
| 687 base::RunLoop().RunUntilIdle(); |
| 688 } |
| 689 |
| 690 void ReportActorDestroyed() { |
| 691 for (auto& observer : observer_list_) |
| 692 observer.OnActorDestroyed(this); |
| 693 base::RunLoop().RunUntilIdle(); |
| 694 } |
| 695 |
| 696 void MaybeWaitForDataRemoved() { |
| 697 // In case of managed user we no need to wait data removal because |
| 698 // ArcSessionManager is initialized with arc.enabled = true already and |
| 699 // request to remove ARC data is not issued. |
| 700 if (IsManagedUser()) |
| 701 return; |
| 702 |
| 703 DCHECK_EQ(ArcSessionManager::State::REMOVING_DATA_DIR, |
| 704 ArcSessionManager::Get()->state()); |
| 705 ArcDataRemovedWaiter().Wait(); |
| 706 } |
| 707 |
| 708 chromeos::ArcTermsOfServiceScreenActor* actor() { return this; } |
| 709 |
| 710 private: |
| 711 // ArcTermsOfServiceScreenActor: |
| 712 void AddObserver( |
| 713 chromeos::ArcTermsOfServiceScreenActorObserver* observer) override { |
| 714 observer_list_.AddObserver(observer); |
| 715 } |
| 716 |
| 717 void RemoveObserver( |
| 718 chromeos::ArcTermsOfServiceScreenActorObserver* observer) override { |
| 719 observer_list_.RemoveObserver(observer); |
| 720 } |
| 721 |
| 722 void Show() override { |
| 723 // To match ArcTermsOfServiceScreenHandler logic where prefs::kArcEnabled is |
| 724 // set to true on showing UI. |
| 725 profile()->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 726 } |
| 727 |
| 728 void Hide() override {} |
| 729 |
| 730 base::ObserverList<chromeos::ArcTermsOfServiceScreenActorObserver> |
| 731 observer_list_; |
| 732 |
| 733 DISALLOW_COPY_AND_ASSIGN(ArcSessionOobeOptInNegotiatorTest); |
| 734 }; |
| 735 |
| 736 INSTANTIATE_TEST_CASE_P(ArcSessionOobeOptInNegotiatorTestImpl, |
| 737 ArcSessionOobeOptInNegotiatorTest, |
| 738 ::testing::Values(true, false)); |
| 739 |
| 740 TEST_P(ArcSessionOobeOptInNegotiatorTest, OobeTermsAccepted) { |
| 741 actor()->Show(); |
| 742 MaybeWaitForDataRemoved(); |
| 743 EXPECT_EQ(ArcSessionManager::State::SHOWING_TERMS_OF_SERVICE, |
| 744 arc_session_manager()->state()); |
| 745 ReportResult(true); |
| 746 EXPECT_EQ(ArcSessionManager::State::ACTIVE, arc_session_manager()->state()); |
| 747 EXPECT_TRUE(arc_session_manager()->IsArcEnabled()); |
| 748 } |
| 749 |
| 750 TEST_P(ArcSessionOobeOptInNegotiatorTest, OobeTermsRejected) { |
| 751 actor()->Show(); |
| 752 MaybeWaitForDataRemoved(); |
| 753 EXPECT_EQ(ArcSessionManager::State::SHOWING_TERMS_OF_SERVICE, |
| 754 arc_session_manager()->state()); |
| 755 ReportResult(false); |
| 756 EXPECT_EQ(ArcSessionManager::State::STOPPED, arc_session_manager()->state()); |
| 757 EXPECT_FALSE(!IsManagedUser() && arc_session_manager()->IsArcEnabled()); |
| 758 } |
| 759 |
| 760 TEST_P(ArcSessionOobeOptInNegotiatorTest, OobeTermsActorDestroyed) { |
| 761 actor()->Show(); |
| 762 MaybeWaitForDataRemoved(); |
| 763 EXPECT_EQ(ArcSessionManager::State::SHOWING_TERMS_OF_SERVICE, |
| 764 arc_session_manager()->state()); |
| 765 CloseLoginDisplayHost(); |
| 766 ReportActorDestroyed(); |
| 767 EXPECT_EQ(ArcSessionManager::State::STOPPED, arc_session_manager()->state()); |
| 768 EXPECT_FALSE(!IsManagedUser() && arc_session_manager()->IsArcEnabled()); |
| 769 } |
| 770 |
| 530 } // namespace arc | 771 } // namespace arc |
| OLD | NEW |