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 147 148 149 150 151 152 153
|
// Copyright 2025 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/metrics/class_management_enabled_metrics_provider.h"
#include <optional>
#include "ash/constants/ash_switches.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ash/login/existing_user_controller.h"
#include "chrome/browser/ash/login/test/logged_in_user_mixin.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/login/wizard_controller.h"
#include "chrome/browser/ash/policy/core/device_policy_cros_browser_test.h"
#include "chrome/browser/ash/policy/test_support/embedded_policy_test_server_mixin.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/fake_gaia_mixin.h"
#include "chromeos/ash/components/dbus/session_manager/fake_session_manager_client.h"
#include "components/account_id/account_id.h"
#include "components/metrics/metrics_service.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/cloud/test/policy_builder.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "content/public/test/browser_test.h"
namespace {
using ClassManagementEnabled =
ClassManagementEnabledMetricsProvider::ClassManagementEnabled;
using testing::InvokeWithoutArgs;
constexpr std::string_view kClassManagementDisabled = "disabled";
constexpr std::string_view kClassManagementStudent = "student";
constexpr std::string_view kClassManagementTeacher = "teacher";
constexpr std::string_view kHistogramName = "ChromeOS.ClassManagementEnabled";
void ProvideHistograms() {
// The purpose of the below call is to avoid a DCHECK failure in an unrelated
// metrics provider, in |FieldTrialsProvider::ProvideCurrentSessionData()|.
metrics::SystemProfileProto system_profile_proto;
// Downstream functions do not use system_profile_proto so there is no risk of
// UAF.
g_browser_process->metrics_service()
->GetDelegatingProviderForTesting()
->ProvideSystemProfileMetricsWithLogCreationTime(base::TimeTicks::Now(),
&system_profile_proto);
g_browser_process->metrics_service()
->GetDelegatingProviderForTesting()
->OnDidCreateMetricsLog();
}
class TestCase {
public:
explicit TestCase(std::string_view policy_value,
ClassManagementEnabled segment)
: policy_value_(policy_value), segment_(segment) {}
ClassManagementEnabled GetSegment() const { return segment_; }
std::string_view GetPolicyValue() const { return policy_value_; }
private:
const std::string_view policy_value_;
const ClassManagementEnabled segment_;
};
class ClassManagementEnabledMetricsProviderTest
: public policy::DevicePolicyCrosBrowserTest,
public testing::WithParamInterface<TestCase> {
protected:
ClassManagementEnabledMetricsProviderTest() {
scoped_feature_list_.InitAndEnableFeature(
features::kClassManagementEnabledMetricsProvider);
}
void SetUpInProcessBrowserTestFixture() override {
policy::DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
InitializePolicy();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(ash::switches::kOobeSkipPostLogin);
DevicePolicyCrosBrowserTest::SetUpCommandLine(command_line);
}
void InitializePolicy() {
device_policy()->policy_data().set_public_key_version(1);
}
void SetDevicePolicy() {
device_local_account_policy_.SetDefaultSigningKey();
device_local_account_policy_.Build();
logged_in_user_mixin_.GetEmbeddedPolicyTestServerMixin()
->UpdateExternalPolicy(
policy::dm_protocol::kChromePublicAccountPolicyType,
FakeGaiaMixin::kEnterpriseUser1,
device_local_account_policy_.payload().SerializeAsString());
session_manager_client()->set_device_local_account_policy(
FakeGaiaMixin::kEnterpriseUser1,
device_local_account_policy_.GetBlob());
}
void LogInUser() {
std::unique_ptr<ash::ScopedUserPolicyUpdate> policy =
logged_in_user_mixin_.GetUserPolicyMixin()->RequestPolicyUpdate();
policy->policy_payload()
->mutable_subproto1()
->mutable_classmanagementenabled()
->set_value(GetParam().GetPolicyValue());
logged_in_user_mixin_.LogInUser();
}
int GetExpectedUmaValue() {
return static_cast<int>(GetParam().GetSegment());
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
ash::LoggedInUserMixin logged_in_user_mixin_{
&mixin_host_, /*test_base=*/this, embedded_test_server(),
ash::LoggedInUserMixin::LogInType::kManaged};
policy::UserPolicyBuilder device_local_account_policy_;
};
IN_PROC_BROWSER_TEST_P(ClassManagementEnabledMetricsProviderTest, Uma) {
base::HistogramTester histogram_tester;
SetDevicePolicy();
// Simulate calling ProvideHistograms() prior to logging in.
ProvideHistograms();
// No metrics were recorded.
histogram_tester.ExpectTotalCount(kHistogramName, 0);
LogInUser();
// Simulate calling ProvideHistograms() after logging in.
ProvideHistograms();
histogram_tester.ExpectUniqueSample(kHistogramName, GetExpectedUmaValue(), 1);
}
INSTANTIATE_TEST_SUITE_P(
All,
ClassManagementEnabledMetricsProviderTest,
testing::Values(
TestCase(kClassManagementDisabled, ClassManagementEnabled::kDisabled),
TestCase(kClassManagementStudent, ClassManagementEnabled::kStudent),
TestCase(kClassManagementTeacher, ClassManagementEnabled::kTeacher)));
} // namespace
|