File: boca_role_util_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 4,450 bytes parent folder | download | duplicates (7)
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
// 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_role_util.h"

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/test/scoped_feature_list.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/fake_user_manager.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class BocaRoleUtilTest : public testing::Test {
 public:
  BocaRoleUtilTest() = default;
  ~BocaRoleUtilTest() override = default;

  void SetUp() override {
    user_manager::UserManagerImpl::RegisterPrefs(local_state_.registry());
    ash::boca_util::RegisterPrefs(local_state_.registry());
    user_manager_ =
        std::make_unique<user_manager::FakeUserManager>(&local_state_);
    user_manager_->Initialize();
    affiliated_user_ = user_manager_->AddGaiaUser(
        affiliated_user_account_, user_manager::UserType::kRegular);
    user_manager_->SetUserPolicyStatus(affiliated_user_account_,
                                       /*is_managed=*/true,
                                       /*is_affiliated=*/true);
    unaffiliated_user_ = user_manager_->AddGaiaUser(
        unaffiliated_user_account_, user_manager::UserType::kRegular);
  }

  void TearDown() override {
    affiliated_user_ = nullptr;
    unaffiliated_user_ = nullptr;
    user_manager_->Destroy();
    user_manager_.reset();
  }

 protected:
  const AccountId affiliated_user_account_ =
      AccountId::FromUserEmailGaiaId("user1@gmail.com", GaiaId("fakegaia1"));
  const AccountId unaffiliated_user_account_ =
      AccountId::FromUserEmailGaiaId("user2@gmail.com", GaiaId("fakegaia2"));
  base::test::ScopedFeatureList scoped_feature_list_;
  TestingPrefServiceSimple local_state_;
  raw_ptr<const user_manager::User> affiliated_user_;
  raw_ptr<const user_manager::User> unaffiliated_user_;
  std::unique_ptr<user_manager::FakeUserManager> user_manager_;
};

TEST_F(BocaRoleUtilTest, TestDisabledForUnAffliatedUser) {
  user_manager_->OnUserProfileCreated(unaffiliated_user_account_,
                                      &local_state_);
  EXPECT_FALSE(ash::boca_util::IsEnabled(unaffiliated_user_));
}

TEST_F(BocaRoleUtilTest, TestBocaDisabledFromPref) {
  user_manager_->OnUserProfileCreated(affiliated_user_account_, &local_state_);
  local_state_.SetString(ash::prefs::kClassManagementToolsAvailabilitySetting,
                         "disabled");
  EXPECT_FALSE(ash::boca_util::IsEnabled(affiliated_user_));
}

TEST_F(BocaRoleUtilTest, TestBocaDisabledByDefaultFromPref) {
  user_manager_->OnUserProfileCreated(affiliated_user_account_, &local_state_);
  local_state_.SetString(ash::prefs::kClassManagementToolsAvailabilitySetting,
                         "");
  EXPECT_FALSE(ash::boca_util::IsEnabled(affiliated_user_));
}

TEST_F(BocaRoleUtilTest, TestBocaSetTeacherFromPref) {
  user_manager_->OnUserProfileCreated(affiliated_user_account_, &local_state_);
  local_state_.SetString(ash::prefs::kClassManagementToolsAvailabilitySetting,
                         "teacher");
  EXPECT_TRUE(ash::boca_util::IsEnabled(affiliated_user_));
  EXPECT_TRUE(ash::boca_util::IsProducer(affiliated_user_));
  EXPECT_FALSE(ash::boca_util::IsConsumer(affiliated_user_));
}

TEST_F(BocaRoleUtilTest, TestBocaSetStudentFromPref) {
  user_manager_->OnUserProfileCreated(affiliated_user_account_, &local_state_);
  local_state_.SetString(ash::prefs::kClassManagementToolsAvailabilitySetting,
                         "student");
  EXPECT_TRUE(ash::boca_util::IsEnabled(affiliated_user_));
  EXPECT_FALSE(ash::boca_util::IsProducer(affiliated_user_));
  EXPECT_TRUE(ash::boca_util::IsConsumer(affiliated_user_));
}

TEST_F(BocaRoleUtilTest, TestBocaEnableStudentFromFeatureFlag) {
  scoped_feature_list_.InitWithFeatures(
      {ash::features::kBoca, ash::features::kBocaConsumer},
      /*disabled_features=*/{});
  EXPECT_TRUE(ash::boca_util::IsEnabled(nullptr));

  EXPECT_TRUE(ash::boca_util::IsConsumer(nullptr));
}

TEST_F(BocaRoleUtilTest, TestBocaEnableTeacherFromFeatureFlag) {
  scoped_feature_list_.InitWithFeatures({ash::features::kBoca},
                                        /*disabled_features=*/{});
  EXPECT_TRUE(ash::boca_util::IsEnabled(nullptr));
  EXPECT_TRUE(ash::boca_util::IsProducer(nullptr));
}

}  // namespace