Hailey Wang | 55bd7c9 | 2025-04-25 23:28:59 | [diff] [blame] | 1 | // Copyright 2025 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 "chrome/browser/collaboration/collaboration_service_factory.h" |
| 6 | #include "chrome/browser/policy/policy_test_utils.h" |
| 7 | #include "chrome/browser/profiles/profile.h" |
| 8 | #include "chrome/browser/signin/identity_test_environment_profile_adaptor.h" |
| 9 | #include "chrome/browser/ui/browser.h" |
| 10 | #include "components/collaboration/public/collaboration_service.h" |
| 11 | #include "components/collaboration/public/pref_names.h" |
| 12 | #include "components/collaboration/public/service_status.h" |
| 13 | #include "components/data_sharing/public/features.h" |
| 14 | #include "components/policy/core/common/policy_map.h" |
| 15 | #include "components/policy/policy_constants.h" |
| 16 | #include "components/prefs/pref_service.h" |
| 17 | #include "content/public/test/browser_test.h" |
| 18 | |
| 19 | namespace policy { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class MockCollaborationServiceObserver |
| 24 | : public collaboration::CollaborationService::Observer { |
| 25 | public: |
| 26 | MockCollaborationServiceObserver() = default; |
| 27 | ~MockCollaborationServiceObserver() override = default; |
| 28 | |
| 29 | MOCK_METHOD(void, |
| 30 | OnServiceStatusChanged, |
| 31 | (const ServiceStatusUpdate& update), |
| 32 | (override)); |
| 33 | }; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | class TabGroupSharingEnabledTest : public PolicyTest { |
| 38 | public: |
| 39 | TabGroupSharingEnabledTest() { |
| 40 | feature_list_.InitWithFeatureStates({ |
| 41 | {data_sharing::features::kDataSharingFeature, true}, |
| 42 | {data_sharing::features::kCollaborationEntrepriseV2, true}, |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | void SetUpOnMainThread() override { |
| 47 | PolicyTest::SetUpOnMainThread(); |
| 48 | |
| 49 | // Sign in. |
| 50 | identity_test_env_adaptor_ = |
| 51 | std::make_unique<IdentityTestEnvironmentProfileAdaptor>( |
| 52 | browser()->profile()); |
| 53 | auto account_info = |
| 54 | identity_test_env_adaptor_->identity_test_env() |
| 55 | ->MakePrimaryAccountAvailable("[email protected]", |
| 56 | signin::ConsentLevel::kSignin); |
| 57 | identity_test_env_adaptor_->identity_test_env() |
| 58 | ->UpdateAccountInfoForAccount(account_info); |
| 59 | } |
| 60 | |
| 61 | void SetUpInProcessBrowserTestFixture() override { |
| 62 | PolicyTest::SetUpInProcessBrowserTestFixture(); |
| 63 | |
| 64 | create_services_subscription_ = |
| 65 | BrowserContextDependencyManager::GetInstance() |
| 66 | ->RegisterCreateServicesCallbackForTesting(base::BindRepeating( |
| 67 | &TabGroupSharingEnabledTest::OnWillCreateBrowserContextServices, |
| 68 | base::Unretained(this))); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | void OnWillCreateBrowserContextServices(content::BrowserContext* context) { |
| 73 | IdentityTestEnvironmentProfileAdaptor:: |
| 74 | SetIdentityTestEnvironmentFactoriesOnBrowserContext(context); |
| 75 | } |
| 76 | |
| 77 | base::test::ScopedFeatureList feature_list_; |
| 78 | |
| 79 | // Identity test support. |
| 80 | std::unique_ptr<IdentityTestEnvironmentProfileAdaptor> |
| 81 | identity_test_env_adaptor_; |
| 82 | base::CallbackListSubscription create_services_subscription_; |
| 83 | }; |
| 84 | |
| 85 | IN_PROC_BROWSER_TEST_F(TabGroupSharingEnabledTest, TabGroupSharingEnabled) { |
| 86 | const PrefService* const prefs = |
| 87 | chrome_test_utils::GetProfile(this)->GetPrefs(); |
| 88 | EXPECT_EQ(0 /*disabled*/, |
| 89 | prefs->GetInteger( |
| 90 | collaboration::prefs::kSharedTabGroupsManagedAccountSetting)); |
| 91 | |
| 92 | testing::StrictMock<MockCollaborationServiceObserver> mock_observer; |
| 93 | auto* profile = browser()->profile(); |
| 94 | auto* service = |
| 95 | collaboration::CollaborationServiceFactory::GetForProfile(profile); |
| 96 | service->AddObserver(&mock_observer); |
| 97 | collaboration::CollaborationService::Observer::ServiceStatusUpdate update; |
| 98 | EXPECT_CALL(mock_observer, OnServiceStatusChanged(testing::_)) |
| 99 | .WillOnce(testing::SaveArg<0>(&update)); |
| 100 | |
| 101 | EXPECT_TRUE(service->GetServiceStatus().IsAllowedToJoin()); |
| 102 | |
| 103 | // Now set the policy and check that shared tab group managed account is |
| 104 | // turned off by policy. |
| 105 | PolicyMap policies; |
| 106 | policies.Set(key::kTabGroupSharingSettings, POLICY_LEVEL_MANDATORY, |
| 107 | POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(1), nullptr); |
| 108 | UpdateProviderPolicy(policies); |
| 109 | EXPECT_EQ(1 /*enabled*/, |
| 110 | prefs->GetInteger( |
| 111 | collaboration::prefs::kSharedTabGroupsManagedAccountSetting)); |
| 112 | EXPECT_FALSE(service->GetServiceStatus().IsAllowedToJoin()); |
| 113 | EXPECT_EQ(update.new_status.collaboration_status, |
| 114 | collaboration::CollaborationStatus::kDisabledForPolicy); |
| 115 | } |
| 116 | |
| 117 | } // namespace policy |