1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/calendar/calendar_client_impl.h"
#include <memory>
#include <utility>
#include <vector>
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/web_app_id_constants.h"
#include "ash/glanceables/glanceables_metrics.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/values.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics_service_test_base.h"
#include "chrome/browser/apps/app_service/publishers/app_publisher.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
class CalendarClientImplTest : public testing::Test {
public:
CalendarClientImplTest()
: profile_manager_(
TestingProfileManager(TestingBrowserProcess::GetGlobal())) {}
void SetUp() override { ASSERT_TRUE(profile_manager_.SetUp()); }
std::unique_ptr<sync_preferences::TestingPrefServiceSyncable>
GetDefaultPrefs() const {
auto prefs =
std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
RegisterUserProfilePrefs(prefs->registry());
return prefs;
}
TestingProfile* CreateTestingProfile(
std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs) {
return profile_manager_.CreateTestingProfile(
"profile@example.com", std::move(prefs), u"User Name", /*avatar_id=*/0,
TestingProfile::TestingFactories());
}
const base::HistogramTester* histogram_tester() const {
return &histogram_tester_;
}
private:
content::BrowserTaskEnvironment task_environment_;
const base::HistogramTester histogram_tester_;
TestingProfileManager profile_manager_;
};
TEST_F(CalendarClientImplTest, IsDisabledByAdmin_Default) {
auto* const profile = CreateTestingProfile(GetDefaultPrefs());
const auto client = CalendarClientImpl(profile);
EXPECT_FALSE(client.IsDisabledByAdmin());
histogram_tester()->ExpectUniqueSample(
"Ash.ContextualGoogleIntegrations.GoogleCalendar.Status",
ContextualGoogleIntegrationStatus::kEnabled,
/*expected_bucket_count=*/1);
}
TEST_F(CalendarClientImplTest, IsDisabledByAdmin_DisabledCalendarPref) {
auto prefs = GetDefaultPrefs();
prefs->SetBoolean(prefs::kCalendarIntegrationEnabled, false);
auto* const profile = CreateTestingProfile(std::move(prefs));
const auto client = CalendarClientImpl(profile);
EXPECT_TRUE(client.IsDisabledByAdmin());
histogram_tester()->ExpectUniqueSample(
"Ash.ContextualGoogleIntegrations.GoogleCalendar.Status",
ContextualGoogleIntegrationStatus::kDisabledByPolicy,
/*expected_bucket_count=*/1);
}
TEST_F(CalendarClientImplTest,
IsDisabledByAdmin_NoCalendarInContextualGoogleIntegrationsPref) {
auto prefs = GetDefaultPrefs();
base::Value::List enabled_integrations;
enabled_integrations.Append(prefs::kGoogleClassroomIntegrationName);
enabled_integrations.Append(prefs::kGoogleTasksIntegrationName);
prefs->SetList(prefs::kContextualGoogleIntegrationsConfiguration,
std::move(enabled_integrations));
auto* const profile = CreateTestingProfile(std::move(prefs));
const auto client = CalendarClientImpl(profile);
EXPECT_TRUE(client.IsDisabledByAdmin());
histogram_tester()->ExpectUniqueSample(
"Ash.ContextualGoogleIntegrations.GoogleCalendar.Status",
ContextualGoogleIntegrationStatus::kDisabledByPolicy,
/*expected_bucket_count=*/1);
}
TEST_F(CalendarClientImplTest, IsDisabledByAdmin_DisabledCalendarApp) {
auto* const profile = CreateTestingProfile(GetDefaultPrefs());
std::vector<apps::AppPtr> app_deltas;
app_deltas.push_back(apps::AppPublisher::MakeApp(
apps::AppType::kWeb, ash::kGoogleCalendarAppId,
apps::Readiness::kDisabledByPolicy, "Calendar",
apps::InstallReason::kUser, apps::InstallSource::kBrowser));
apps::AppServiceProxyFactory::GetForProfile(profile)->OnApps(
std::move(app_deltas), apps::AppType::kWeb,
/*should_notify_initialized=*/true);
const auto client = CalendarClientImpl(profile);
EXPECT_TRUE(client.IsDisabledByAdmin());
histogram_tester()->ExpectUniqueSample(
"Ash.ContextualGoogleIntegrations.GoogleCalendar.Status",
ContextualGoogleIntegrationStatus::kDisabledByAppBlock,
/*expected_bucket_count=*/1);
}
TEST_F(CalendarClientImplTest, IsDisabledByAdmin_BlockedCalendarUrl) {
auto prefs = GetDefaultPrefs();
base::Value::List blocklist;
blocklist.Append("calendar.google.com");
prefs->SetManagedPref(policy::policy_prefs::kUrlBlocklist,
std::move(blocklist));
auto* const profile = CreateTestingProfile(std::move(prefs));
const auto client = CalendarClientImpl(profile);
EXPECT_TRUE(client.IsDisabledByAdmin());
histogram_tester()->ExpectUniqueSample(
"Ash.ContextualGoogleIntegrations.GoogleCalendar.Status",
ContextualGoogleIntegrationStatus::kDisabledByUrlBlock,
/*expected_bucket_count=*/1);
}
} // namespace ash
|