File: family_user_device_metrics_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (366 lines) | stat: -rw-r--r-- 14,522 bytes parent folder | download | duplicates (3)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2020 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/child_accounts/family_user_device_metrics.h"

#include <memory>
#include <optional>
#include <tuple>

#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ash/login/test/device_state_mixin.h"
#include "chrome/browser/ash/login/test/logged_in_user_mixin.h"
#include "chrome/browser/ash/login/test/login_manager_mixin.h"
#include "chrome/browser/ash/login/test/scoped_policy_update.h"
#include "chrome/browser/ash/login/test/user_policy_mixin.h"
#include "components/account_id/account_id.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "components/user_manager/test_helper.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_type.h"
#include "content/public/test/browser_test.h"
#include "google_apis/gaia/gaia_id.h"

namespace ash {

namespace {
const AccountId kDefaultOwnerAccountId =
    AccountId::FromUserEmailGaiaId(test::kTestEmail, GaiaId(test::kTestGaiaId));
constexpr char kKioskAppUserEmail[] =
    "example@kiosk-apps.device-local.localhost";
constexpr char kWebKioskAppUserEmail[] =
    "example@web-kiosk-apps.device-local.localhost";
constexpr char kPublicAccountUserEmail[] =
    "example@public-accounts.device-local.localhost";

}  // namespace

// Test params:
//  - LogInType: regular or child.
//  - IsUserExisting: if false, no existing users on the login screen.
class FamilyUserDeviceMetricsTest
    : public MixinBasedInProcessBrowserTest,
      public testing::WithParamInterface<
          std::tuple<LoggedInUserMixin::LogInType, /*IsUserExisting=*/bool>> {
 protected:
  bool IsUserChild() const {
    return GetLogInType() == LoggedInUserMixin::LogInType::kChild;
  }
  bool IsUserExisting() const { return std::get<1>(GetParam()); }

  LoggedInUserMixin logged_in_user_mixin_{
      &mixin_host_, /*test_base=*/this, embedded_test_server(), GetLogInType(),
      /*include_initial_user=*/IsUserExisting()};

  // MixinBasedInProcessBrowserTest:
  void SetUpInProcessBrowserTestFixture() override {
    if (IsUserExisting()) {
      // Append another user of the same type.
      if (IsUserChild()) {
        logged_in_user_mixin_.GetLoginManagerMixin()->AppendChildUsers(1);
      } else {
        logged_in_user_mixin_.GetLoginManagerMixin()->AppendRegularUsers(1);
      }
    }
    MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
  }

  void SetUpOnMainThread() override {
    MixinBasedInProcessBrowserTest::SetUpOnMainThread();
    // Child users require user policy. Set up an empty one so the user can get
    // through login.
    user_policy_mixin_.RequestPolicyUpdate();
  }

 private:
  LoggedInUserMixin::LogInType GetLogInType() const {
    return std::get<0>(GetParam());
  }

  UserPolicyMixin user_policy_mixin_{&mixin_host_, kDefaultOwnerAccountId};
};

// TODO(crbug.com/40892366): Test is flaky. Too many histogram entries are
// sometimes generated.
#define MAYBE_IsDeviceOwner DISABLED_IsDeviceOwner
IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, MAYBE_IsDeviceOwner) {
  base::HistogramTester histogram_tester;

  // Set the device owner to the logged in user.
  user_manager::UserManager::Get()->SetOwnerId(
      logged_in_user_mixin_.GetAccountId());
  logged_in_user_mixin_.LogInUser(
      {ash::LoggedInUserMixin::LoginDetails::kNoBrowserLaunch});

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetDeviceOwnerHistogramNameForTest(),
      /*sample=*/true, /*expected_count=*/1);
}

// TODO(crbug.com/40892366): Test is flaky. Too many histogram entries are
// sometimes generated.
#define MAYBE_IsNotDeviceOwner DISABLED_IsNotDeviceOwner
IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, MAYBE_IsNotDeviceOwner) {
  base::HistogramTester histogram_tester;

  // Set the device owner to an arbitrary account that's not logged in.
  user_manager::UserManager::Get()->SetOwnerId(kDefaultOwnerAccountId);
  logged_in_user_mixin_.LogInUser(
      {ash::LoggedInUserMixin::LoginDetails::kNoBrowserLaunch});

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetDeviceOwnerHistogramNameForTest(),
      /*sample=*/false, /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, SingleUserAdded) {
  base::HistogramTester histogram_tester;

  logged_in_user_mixin_.LogInUser(
      {ash::LoggedInUserMixin::LoginDetails::kNoBrowserLaunch});

  if (IsUserExisting()) {
    // This user has signed into this device before, so they are not new.
    histogram_tester.ExpectTotalCount(
        FamilyUserDeviceMetrics::GetNewUserAddedHistogramNameForTest(), 0);
  } else {
    // This is the first time this user is signing into this device.
    FamilyUserDeviceMetrics::NewUserAdded sample =
        IsUserChild()
            ? FamilyUserDeviceMetrics::NewUserAdded::kFamilyLinkUserAdded
            : FamilyUserDeviceMetrics::NewUserAdded::kRegularUserAdded;
    histogram_tester.ExpectUniqueSample(
        FamilyUserDeviceMetrics::GetNewUserAddedHistogramNameForTest(), sample,
        /*expected_count=*/1);
  }
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, SingleUserCount) {
  if (!IsUserExisting()) {
    GTEST_SKIP() << "This test makes sense only for existing user";
  }
  base::HistogramTester histogram_tester;

  logged_in_user_mixin_.LogInUser(
      {ash::LoggedInUserMixin::LoginDetails::kNoBrowserLaunch});

  // Current user + extra user from setup.
  const int gaia_users_count = 2;
  const int family_link_users_count = IsUserChild() ? gaia_users_count : 0;

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetFamilyLinkUsersCountHistogramNameForTest(),
      /*sample=*/family_link_users_count,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, LoginAsNewChildUser) {
  if (IsUserExisting() && !IsUserChild()) {
    GTEST_SKIP() << "As this test runs LoginAsNewChildUser"
                    " it is expected that if user exists, it is a child user";
  }
  base::HistogramTester histogram_tester;

  logged_in_user_mixin_.GetLoginManagerMixin()->SkipPostLoginScreens();
  logged_in_user_mixin_.GetLoginManagerMixin()->LoginAsNewChildUser();
  logged_in_user_mixin_.GetLoginManagerMixin()->WaitForActiveSession();

  // If existing Family Link user on login screen, then two Family Link users.
  const int family_link_users_count = IsUserExisting() && IsUserChild() ? 2 : 1;
  // If no existing users on login screen, then this user is the first and only.
  const int gaia_users_count = IsUserExisting() ? 2 : 1;
  // If user existed before, then no users were added.
  const int family_link_users_added = IsUserExisting() ? 0 : 1;

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetNewUserAddedHistogramNameForTest(),
      FamilyUserDeviceMetrics::NewUserAdded::kFamilyLinkUserAdded,
      /*expected_count=*/family_link_users_added);
  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetFamilyLinkUsersCountHistogramNameForTest(),
      /*sample=*/family_link_users_count,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics ::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, LoginAsNewRegularUser) {
  base::HistogramTester histogram_tester;

  logged_in_user_mixin_.GetLoginManagerMixin()->SkipPostLoginScreens();
  logged_in_user_mixin_.GetLoginManagerMixin()->LoginAsNewRegularUser();
  logged_in_user_mixin_.GetLoginManagerMixin()->WaitForActiveSession();

  // If existing Family Link user on login screen, then one Family Link user.
  const int family_link_users_count = IsUserExisting() && IsUserChild() ? 1 : 0;
  // If no existing users on login screen, then this user is the first and only.
  const int gaia_users_count = IsUserExisting() ? 2 : 1;
  // If user existed before, then no users were added.
  const int regular_users_added = IsUserExisting() ? 0 : 1;

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetNewUserAddedHistogramNameForTest(),
      FamilyUserDeviceMetrics::NewUserAdded::kRegularUserAdded,
      /*expected_count=*/regular_users_added);
  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetFamilyLinkUsersCountHistogramNameForTest(),
      /*sample=*/family_link_users_count,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsTest, GuestUser) {
  base::HistogramTester histogram_tester;

  auto* user_manager = user_manager::UserManager::Get();
  ASSERT_TRUE(user_manager::TestHelper(user_manager).AddGuestUser());

  logged_in_user_mixin_.GetLoginManagerMixin()->SkipPostLoginScreens();
  logged_in_user_mixin_.GetLoginManagerMixin()->LoginAsNewRegularUser();
  logged_in_user_mixin_.GetLoginManagerMixin()->WaitForActiveSession();

  // If no existing users on login screen, then this user is the first and only.
  const size_t gaia_users_count = IsUserExisting() ? 2 : 1;
  EXPECT_EQ(gaia_users_count, user_manager->GetPersistedUsers().size());

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    FamilyUserDeviceMetricsTest,
    testing::Combine(testing::Values(LoggedInUserMixin::LogInType::kChild,
                                     LoggedInUserMixin::LogInType::kConsumer),
                     /*IsUserExisting=*/testing::Bool()));

class FamilyUserDeviceMetricsManagedDeviceTest
    : public FamilyUserDeviceMetricsTest {
 protected:
  void LoginAsNewRegularUser() {
    logged_in_user_mixin_.GetLoginManagerMixin()->SkipPostLoginScreens();
    logged_in_user_mixin_.GetLoginManagerMixin()->LoginAsNewRegularUser();
    logged_in_user_mixin_.GetLoginManagerMixin()->WaitForActiveSession();
  }

  DeviceStateMixin device_state_{
      &mixin_host_, DeviceStateMixin::State::OOBE_COMPLETED_CLOUD_ENROLLED};
};

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsManagedDeviceTest, KioskAppUser) {
  base::HistogramTester histogram_tester;

  auto* user_manager = user_manager::UserManager::Get();
  ASSERT_TRUE(user_manager::TestHelper(user_manager)
                  .AddKioskChromeAppUser(kKioskAppUserEmail));
  LoginAsNewRegularUser();

  size_t total_user_count = IsUserExisting() ? 3 : 2;
  EXPECT_EQ(total_user_count, user_manager->GetPersistedUsers().size());

  // If no existing users on login screen, then this user is the first and only.
  const int gaia_users_count = IsUserExisting() ? 2 : 1;

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsManagedDeviceTest,
                       WebKioskAppUser) {
  base::HistogramTester histogram_tester;

  auto* user_manager = user_manager::UserManager::Get();
  ASSERT_TRUE(user_manager::TestHelper(user_manager)
                  .AddKioskWebAppUser(kWebKioskAppUserEmail));
  LoginAsNewRegularUser();

  size_t total_user_count = IsUserExisting() ? 3 : 2;
  EXPECT_EQ(total_user_count, user_manager->GetPersistedUsers().size());

  // If no existing users on login screen, then this user is the first and only.
  const int gaia_users_count = IsUserExisting() ? 2 : 1;

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsManagedDeviceTest,
                       PublicAccountUser) {
  base::HistogramTester histogram_tester;

  auto* user_manager = user_manager::UserManager::Get();
  ASSERT_TRUE(user_manager::TestHelper(user_manager)
                  .AddPublicAccountUser(kPublicAccountUserEmail));
  LoginAsNewRegularUser();

  size_t total_user_count = IsUserExisting() ? 3 : 2;
  EXPECT_EQ(total_user_count, user_manager->GetPersistedUsers().size());

  // If no existing users on login screen, then this user is the first and only.
  const int gaia_users_count = IsUserExisting() ? 2 : 1;

  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/gaia_users_count,
      /*expected_count=*/1);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    FamilyUserDeviceMetricsManagedDeviceTest,
    testing::Combine(testing::Values(LoggedInUserMixin::LogInType::kChild,
                                     LoggedInUserMixin::LogInType::kConsumer),
                     /*IsUserExisting=*/testing::Bool()));

class FamilyUserDeviceMetricsEphemeralUserTest
    : public FamilyUserDeviceMetricsManagedDeviceTest {
 protected:
  // MixinBaseInProcessBrowserTest:
  void SetUpInProcessBrowserTestFixture() override {
    MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
    std::unique_ptr<ScopedDevicePolicyUpdate> device_policy_update =
        device_state_.RequestDevicePolicyUpdate();
    device_policy_update->policy_payload()
        ->mutable_ephemeral_users_enabled()
        ->set_ephemeral_users_enabled(true);

    device_policy_update.reset();
  }
};

// Tests that regular ephemeral users report 0 for FamilyUser.GaiaUsersCount.
IN_PROC_BROWSER_TEST_P(FamilyUserDeviceMetricsEphemeralUserTest,
                       EphemeralUser) {
  base::HistogramTester histogram_tester;
  LoginAsNewRegularUser();
  histogram_tester.ExpectUniqueSample(
      FamilyUserDeviceMetrics::GetGaiaUsersCountHistogramNameForTest(),
      /*sample=*/0,
      /*expected_count=*/1);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    FamilyUserDeviceMetricsEphemeralUserTest,
    testing::Combine(testing::Values(LoggedInUserMixin::LogInType::kChild,
                                     LoggedInUserMixin::LogInType::kConsumer),
                     /*IsUserExisting=*/testing::Values(false)));

}  // namespace ash