File: profiles_state_unittest.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 (175 lines) | stat: -rw-r--r-- 6,662 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
// 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/profiles/profiles_state.h"

#include <string>

#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

#if !BUILDFLAG(IS_ANDROID)
// Params for the parameterized test IsGuestModeRequestedTest.
struct IsGuestModeRequestedTestParams {
  bool has_switch;
  bool pref_enforced;
  bool pref_enabled;
  bool expected_guest_mode_requested;
};

// clang-format off
const IsGuestModeRequestedTestParams kIsGuestModeRequestedParams[] {
  // has_switch | pref_enforced | pref_enabled | expected_guest_mode_requested
  {  true,        true,           true,          true},
  {  true,        true,           false,         false},
  {  true,        false,          true,          true},
  {  true,        false,          false,         false},
  {  false,       true,           true,          true},
  {  false,       true,           false,         false},
  {  false,       false,          true,          false},
  {  false,       false,          false,         false},
};
// clang-format on
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace

#if !BUILDFLAG(IS_ANDROID)
class IsGuestModeRequestedTest
    : public testing::TestWithParam<IsGuestModeRequestedTestParams> {};

TEST_P(IsGuestModeRequestedTest, Requested) {
  TestingPrefServiceSimple local_state;
  local_state.registry()->RegisterBooleanPref(prefs::kBrowserGuestModeEnforced,
                                              false);
  local_state.registry()->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled,
                                              false);
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  // Set parameters.
  IsGuestModeRequestedTestParams params = GetParam();
  local_state.SetBoolean(prefs::kBrowserGuestModeEnforced,
                         params.pref_enforced);
  local_state.SetBoolean(prefs::kBrowserGuestModeEnabled, params.pref_enabled);
  if (params.has_switch)
    command_line.AppendSwitch("guest");
  // Check expectation.
  EXPECT_EQ(params.expected_guest_mode_requested,
            profiles::IsGuestModeRequested(command_line, &local_state,
                                           /*show_warning=*/false));
}

INSTANTIATE_TEST_SUITE_P(ProfilesState,
                         IsGuestModeRequestedTest,
                         testing::ValuesIn(kIsGuestModeRequestedParams));

class IsGuestModeEnabledTest : public testing::TestWithParam<bool> {
 public:
  IsGuestModeEnabledTest()
      : profile_manager_(TestingBrowserProcess::GetGlobal()),
        testing_local_state_(TestingBrowserProcess::GetGlobal()) {
    testing_local_state_.Get()->SetBoolean(prefs::kBrowserGuestModeEnabled,
                                           BrowserGuestModePrefValue());
  }

  void SetUp() override { ASSERT_TRUE(profile_manager_.SetUp()); }

  void TearDown() override { profile_manager_.DeleteAllTestingProfiles(); }

  bool BrowserGuestModePrefValue() { return GetParam(); }

  bool HideGuestModeForSupervisedUsersFeatureEnabled() {
    return BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN);
  }

  Profile* CreateNormalProfile() {
    return CreateProfile(/*is_subject_to_parental_controls=*/false);
  }

  Profile* CreateSupervisedProfile() {
    return CreateProfile(/*is_subject_to_parental_controls=*/true);
  }

 private:
  Profile* CreateProfile(bool is_subject_to_parental_controls) {
    const std::string profile_name = base::StringPrintf(
        "Profile %zu",
        profile_manager_.profile_manager()->GetNumberOfProfiles());
    const std::string email = base::StringPrintf(
        "account%zu@gmail.com",
        profile_manager_.profile_manager()->GetNumberOfProfiles());

    Profile* profile = profile_manager_.CreateTestingProfile(
        profile_name, std::unique_ptr<sync_preferences::PrefServiceSyncable>(),
        base::UTF8ToUTF16(profile_name),
        /*avatar_id=*/0, /*testing_factories=*/{},
        /*is_supervised_profile=*/is_subject_to_parental_controls,
        /*is_new_profile=*/std::nullopt,
        /*policy_service=*/std::nullopt,
        /*shared_url_loader_factory=*/nullptr);

    return profile;
  }

  content::BrowserTaskEnvironment task_environment_;
  TestingProfileManager profile_manager_;
  ScopedTestingLocalState testing_local_state_;
};

TEST_P(IsGuestModeEnabledTest, NoProfiles) {
  EXPECT_EQ(profiles::IsGuestModeEnabled(), BrowserGuestModePrefValue());
}

TEST_P(IsGuestModeEnabledTest, OneNormalProfile) {
  Profile* profile = CreateNormalProfile();

  EXPECT_EQ(profiles::IsGuestModeEnabled(), BrowserGuestModePrefValue());
  EXPECT_EQ(profiles::IsGuestModeEnabled(*profile),
            BrowserGuestModePrefValue());
}

TEST_P(IsGuestModeEnabledTest, OneSupervisedProfile) {
  Profile* profile = CreateSupervisedProfile();

  EXPECT_EQ(profiles::IsGuestModeEnabled(),
            BrowserGuestModePrefValue() &&
                !HideGuestModeForSupervisedUsersFeatureEnabled());
  EXPECT_EQ(profiles::IsGuestModeEnabled(*profile),
            BrowserGuestModePrefValue() &&
                !HideGuestModeForSupervisedUsersFeatureEnabled());
}

TEST_P(IsGuestModeEnabledTest, MixedProfiles) {
  Profile* normal_profile = CreateNormalProfile();
  Profile* supervised_profile = CreateSupervisedProfile();

  EXPECT_EQ(profiles::IsGuestModeEnabled(),
            BrowserGuestModePrefValue() &&
                !HideGuestModeForSupervisedUsersFeatureEnabled());
  EXPECT_EQ(profiles::IsGuestModeEnabled(*normal_profile),
            BrowserGuestModePrefValue());
  EXPECT_EQ(profiles::IsGuestModeEnabled(*supervised_profile),
            BrowserGuestModePrefValue() &&
                !HideGuestModeForSupervisedUsersFeatureEnabled());
}

INSTANTIATE_TEST_SUITE_P(ProfilesState,
                         IsGuestModeEnabledTest,
                         testing::Bool());

#endif  // !BUILDFLAG(IS_ANDROID)