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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
// 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 "chromeos/ash/components/boca/boca_metrics_manager.h"
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/boca/boca_app_client.h"
#include "chromeos/ash/components/boca/boca_metrics_util.h"
#include "chromeos/ash/components/boca/proto/bundle.pb.h"
#include "chromeos/ash/components/boca/proto/roster.pb.h"
#include "chromeos/ash/components/boca/proto/session.pb.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "google_apis/common/request_sender.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::NiceMock;
using ::testing::Return;
namespace ash::boca {
constexpr char kTestUrl1[] = "https://www.test1.com";
constexpr char kTestUrl2[] = "https://www.test2.com";
constexpr char kTestEmail1[] = "test1@gmail.com";
constexpr char kTestEmail2[] = "test2@gmail.com";
constexpr char kTestEmail3[] = "test3@gmail.com";
class MockBocaAppClient : public BocaAppClient {
public:
MOCK_METHOD(BocaSessionManager*, GetSessionManager, (), (override));
MOCK_METHOD(void, AddSessionManager, (BocaSessionManager*), (override));
MOCK_METHOD(signin::IdentityManager*, GetIdentityManager, (), (override));
MOCK_METHOD(scoped_refptr<network::SharedURLLoaderFactory>,
GetURLLoaderFactory,
(),
(override));
};
class MockSessionManager : public BocaSessionManager {
public:
MockSessionManager()
: BocaSessionManager(/*session_client_impl=*/nullptr,
/*pref_service=*/nullptr,
AccountId::FromUserEmail(kTestEmail1),
/*is_producer=*/true) {}
~MockSessionManager() override = default;
MOCK_METHOD(::boca::Session*, GetPreviousSession, (), (override));
};
class BocaMetricsManagerTest : public testing::Test {
protected:
void SetUp() override {
ON_CALL(boca_app_client_, GetIdentityManager())
.WillByDefault(Return(nullptr));
ON_CALL(boca_app_client_, GetSessionManager())
.WillByDefault(Return(&session_manager_));
}
const base::TimeDelta fast_forward_timeskip =
base::Seconds(60) + base::Seconds(1);
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
signin::IdentityTestEnvironment identity_test_env_;
NiceMock<MockBocaAppClient> boca_app_client_;
NiceMock<MockSessionManager> session_manager_;
};
class BocaMetricsManagerProducerTest : public BocaMetricsManagerTest {
protected:
BocaMetricsManager metrics_manager_{/*is_producer*/ true};
};
TEST_F(BocaMetricsManagerProducerTest,
RecordLockedAndUnlockedStateDurationPercentageMetricsCorrectly) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Bundle bundle_1;
bundle_1.set_locked(true);
metrics_manager_.OnBundleUpdated(bundle_1);
task_environment_.FastForwardBy(fast_forward_timeskip);
::boca::Bundle bundle_2;
bundle_2.set_locked(false);
metrics_manager_.OnBundleUpdated(bundle_2);
task_environment_.FastForwardBy(2 * fast_forward_timeskip);
metrics_manager_.OnSessionEnded("test_session_id");
const base::TimeDelta total_duration = 3 * fast_forward_timeskip;
// We waited for one `fast_forward_timeskip` duration before we got the update
// to unlock so the percentage for locked mode is 1 `fast_forward_timeskip`.
// Similarly, we waited two `fast_forward_timeskip` after the update and for
// session end so the percentage for unlocked mode is 2 *
// `fast_forward_timeskip`.
const double expected_percentage_locked =
100.0 * (fast_forward_timeskip / total_duration);
const double expected_percentage_unlocked =
100.0 - expected_percentage_locked;
histograms.ExpectTotalCount(kBocaOnTaskLockedSessionDurationPercentage, 1);
histograms.ExpectBucketCount(kBocaOnTaskLockedSessionDurationPercentage,
expected_percentage_locked, 1);
histograms.ExpectTotalCount(kBocaOnTaskUnlockedSessionDurationPercentage, 1);
histograms.ExpectBucketCount(kBocaOnTaskUnlockedSessionDurationPercentage,
expected_percentage_unlocked, 1);
}
TEST_F(
BocaMetricsManagerProducerTest,
RecordNumOfStudentsJoinedViaCodeDuringSessionMetricsForProducerCorrectly) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Roster roster;
auto* student_groups_1 = roster.mutable_student_groups()->Add();
student_groups_1->set_group_source(::boca::StudentGroup::JOIN_CODE);
student_groups_1->add_students()->set_email(kTestEmail1);
student_groups_1->add_students()->set_email(kTestEmail2);
auto* student_groups_2 = roster.mutable_student_groups()->Add();
student_groups_2->set_group_source(::boca::StudentGroup::CLASSROOM);
student_groups_2->add_students()->set_email(kTestEmail3);
metrics_manager_.OnSessionRosterUpdated(roster);
metrics_manager_.OnSessionEnded("test_session_id");
const int expected_num_of_students = 2;
histograms.ExpectTotalCount(kBocaNumOfStudentsJoinedViaCodeDuringSession, 1);
histograms.ExpectBucketCount(kBocaNumOfStudentsJoinedViaCodeDuringSession,
expected_num_of_students, 1);
}
TEST_F(BocaMetricsManagerProducerTest,
RecordNumOfActiveStudentsWhenSessionEndedMetricsForProducerCorrectly) {
base::HistogramTester histograms;
::boca::Session session;
session.set_session_id("test_session_id");
session.set_session_state(::boca::Session::ACTIVE);
::boca::StudentStatus status_1;
status_1.set_state(::boca::StudentStatus::ACTIVE);
::boca::StudentStatus status_2;
status_2.set_state(::boca::StudentStatus::ADDED);
session.mutable_student_statuses()->emplace("student_1", std::move(status_1));
session.mutable_student_statuses()->emplace("student_2", std::move(status_2));
ON_CALL(session_manager_, GetPreviousSession())
.WillByDefault(Return(&session));
metrics_manager_.OnSessionEnded("test_session_id");
const int expected_num_of_active_students = 1;
histograms.ExpectTotalCount(kBocaNumOfActiveStudentsWhenSessionEnded, 1);
histograms.ExpectBucketCount(kBocaNumOfActiveStudentsWhenSessionEnded,
expected_num_of_active_students, 1);
}
TEST_F(BocaMetricsManagerProducerTest,
RecordNumOfTabsWhenSessionEndedMetricsForProducerCorrectly) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Bundle bundle_1;
bundle_1.add_content_configs()->set_url(kTestUrl1);
bundle_1.add_content_configs()->set_url(kTestUrl2);
metrics_manager_.OnBundleUpdated(bundle_1);
::boca::Bundle bundle_2;
bundle_2.add_content_configs()->set_url(kTestUrl1);
metrics_manager_.OnBundleUpdated(bundle_2);
metrics_manager_.OnSessionEnded("test_session_id");
const int expected_num_of_tabs = 1;
histograms.ExpectTotalCount(kBocaOnTaskNumOfTabsWhenSessionEnded, 1);
histograms.ExpectBucketCount(kBocaOnTaskNumOfTabsWhenSessionEnded,
expected_num_of_tabs, 1);
}
TEST_F(BocaMetricsManagerProducerTest,
RecordMaxNumOfTabsDuringSessionMetricsForProducerCorrectly) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Bundle bundle_1;
bundle_1.add_content_configs()->set_url(kTestUrl1);
bundle_1.add_content_configs()->set_url(kTestUrl2);
metrics_manager_.OnBundleUpdated(bundle_1);
::boca::Bundle bundle_2;
bundle_2.add_content_configs()->set_url(kTestUrl1);
metrics_manager_.OnBundleUpdated(bundle_2);
metrics_manager_.OnSessionEnded("test_session_id");
const int expected_max_num_of_tabs = 2;
histograms.ExpectTotalCount(kBocaOnTaskMaxNumOfTabsDuringSession, 1);
histograms.ExpectBucketCount(kBocaOnTaskMaxNumOfTabsDuringSession,
expected_max_num_of_tabs, 1);
}
TEST_F(BocaMetricsManagerProducerTest,
DoNotRecordStudentJoinedSessionActionMetricsForProducer) {
base::UserActionTester actions;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
metrics_manager_.OnSessionEnded("test_session_id");
EXPECT_EQ(actions.GetActionCount(kBocaActionOfStudentJoinedSession), 0);
}
class BocaMetricsManagerConsumerTest : public BocaMetricsManagerTest {
protected:
BocaMetricsManager metrics_manager_{/*is_producer*/ false};
};
TEST_F(BocaMetricsManagerConsumerTest,
DoNotRecordLockedAndUnlockedStateDurationPercentageMetricsForConsumer) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Bundle bundle;
bundle.set_locked(true);
metrics_manager_.OnBundleUpdated(bundle);
task_environment_.FastForwardBy(fast_forward_timeskip);
metrics_manager_.OnSessionEnded("test_session_id");
histograms.ExpectTotalCount(kBocaOnTaskLockedSessionDurationPercentage, 0);
histograms.ExpectTotalCount(kBocaOnTaskUnlockedSessionDurationPercentage, 0);
}
TEST_F(BocaMetricsManagerConsumerTest,
DoNotRecordNumOfStudentsJoinedViaCodeDuringSessionMetricsForConsumer) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Roster roster;
auto* student_groups = roster.mutable_student_groups()->Add();
student_groups->set_group_source(::boca::StudentGroup::JOIN_CODE);
student_groups->add_students()->set_email(kTestEmail1);
student_groups->add_students()->set_email(kTestEmail2);
metrics_manager_.OnSessionRosterUpdated(roster);
metrics_manager_.OnSessionEnded("test_session_id");
histograms.ExpectTotalCount(kBocaNumOfStudentsJoinedViaCodeDuringSession, 0);
}
TEST_F(BocaMetricsManagerConsumerTest,
DoNotRecordNumOfActiveStudentsWhenSessionEndedMetricsForConsumer) {
base::HistogramTester histograms;
::boca::Session session;
session.set_session_id("test_session_id");
session.set_session_state(::boca::Session::ACTIVE);
::boca::StudentStatus status_1;
status_1.set_state(::boca::StudentStatus::ACTIVE);
::boca::StudentStatus status_2;
status_2.set_state(::boca::StudentStatus::ACTIVE);
session.mutable_student_statuses()->emplace("student_1", std::move(status_1));
session.mutable_student_statuses()->emplace("student_2", std::move(status_2));
ON_CALL(session_manager_, GetPreviousSession())
.WillByDefault(Return(&session));
histograms.ExpectTotalCount(kBocaNumOfActiveStudentsWhenSessionEnded, 0);
}
TEST_F(BocaMetricsManagerConsumerTest,
DoNotRecordNumOfTabsWhenSessionEndedMetricsForConsumer) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Bundle bundle;
bundle.add_content_configs()->set_url(kTestUrl1);
bundle.add_content_configs()->set_url(kTestUrl2);
metrics_manager_.OnBundleUpdated(bundle);
metrics_manager_.OnSessionEnded("test_session_id");
histograms.ExpectTotalCount(kBocaOnTaskNumOfTabsWhenSessionEnded, 0);
}
TEST_F(BocaMetricsManagerConsumerTest,
DoNotRecordMaxNumOfTabsDuringSessionMetricsForConsumer) {
base::HistogramTester histograms;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
::boca::Bundle bundle;
bundle.add_content_configs()->set_url(kTestUrl1);
bundle.add_content_configs()->set_url(kTestUrl2);
metrics_manager_.OnBundleUpdated(bundle);
metrics_manager_.OnSessionEnded("test_session_id");
histograms.ExpectTotalCount(kBocaOnTaskMaxNumOfTabsDuringSession, 0);
}
TEST_F(BocaMetricsManagerConsumerTest,
RecordStudentJoinedSessionActionMetricsForConsumerCorrectly) {
base::UserActionTester actions;
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
metrics_manager_.OnSessionEnded("test_session_id");
metrics_manager_.OnSessionStarted("test_session_id", ::boca::UserIdentity());
metrics_manager_.OnSessionEnded("test_session_id");
const int expected_number_of_students = 2;
EXPECT_EQ(actions.GetActionCount(kBocaActionOfStudentJoinedSession),
expected_number_of_students);
}
} // namespace ash::boca
|