File: managed_ui_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (120 lines) | stat: -rw-r--r-- 4,091 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 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/ui/webui/managed_ui_handler.h"

#include <optional>

#include "base/token.h"
#include "base/values.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/testing_profile.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_ui.h"
#include "content/public/test/test_web_ui_data_source.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

class TestManagedUIHandler : public ManagedUIHandler {
 public:
  using ManagedUIHandler::InitializeInternal;
};

class ManagedUIHandlerTest : public testing::Test {
 public:
  ManagedUIHandlerTest()
      : source_(content::TestWebUIDataSource::Create(
            base::Token::CreateRandom().ToString())) {
    // Create a TestingProfile that uses our MockConfigurationPolicyProvider.
    policy_provider()->Init();
    policy::PolicyServiceImpl::Providers providers = {policy_provider()};
    TestingProfile::Builder builder;
    builder.SetPolicyService(
        std::make_unique<policy::PolicyServiceImpl>(std::move(providers)));
    profile_ = builder.Build();

    // `source_` has been created with a random name, so calling
    // AddDataSourceForBrowserContext() won't accidentally destroy other data
    // sources.
    source_->AddDataSourceForBrowserContext(profile());
  }

  void TearDown() override { policy_provider()->Shutdown(); }

  TestingProfile* profile() { return profile_.get(); }
  policy::MockConfigurationPolicyProvider* policy_provider() {
    return &policy_provider_;
  }
  policy::ProfilePolicyConnector* profile_policy_connector() {
    return profile()->GetProfilePolicyConnector();
  }

  void InitializeHandler() {
    TestManagedUIHandler::InitializeInternal(
        &web_ui_, source_->GetWebUIDataSource(), profile());
    web_ui_.HandleReceivedMessage("observeManagedUI", base::Value::List());
  }

  bool IsSourceManaged() {
    const auto* local_strings = source_->GetLocalizedStrings();
    std::optional<bool> managed = local_strings->FindBool("isManaged");
    if (!managed.has_value()) {
      ADD_FAILURE();
      return false;
    }
    return managed.value();
  }

 private:
  content::BrowserTaskEnvironment task_environment_;

  testing::NiceMock<policy::MockConfigurationPolicyProvider> policy_provider_;
  std::unique_ptr<TestingProfile> profile_;

  content::TestWebUI web_ui_;
  std::unique_ptr<content::TestWebUIDataSource> source_;
};

TEST_F(ManagedUIHandlerTest, ManagedUIDisabledByDefault) {
  InitializeHandler();
  EXPECT_FALSE(IsSourceManaged());
}

TEST_F(ManagedUIHandlerTest, ManagedUIEnabledWhenManaged) {
  profile_policy_connector()->OverrideIsManagedForTesting(true);
  InitializeHandler();
  EXPECT_TRUE(IsSourceManaged());
}

TEST_F(ManagedUIHandlerTest, ManagedUIBecomesEnabledByProfile) {
  InitializeHandler();
  EXPECT_FALSE(IsSourceManaged());

  // Make ProfilePolicyConnector::IsManaged() return true.
  profile_policy_connector()->OverrideIsManagedForTesting(true);
  policy::PolicyMap non_empty_map;
  non_empty_map.Set("FakePolicyName", policy::POLICY_LEVEL_MANDATORY,
                    policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
                    base::Value("fake"), nullptr);
  policy_provider()->UpdateChromePolicy(non_empty_map);

  // Source should auto-update.
  EXPECT_TRUE(IsSourceManaged());
}

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(ManagedUIHandlerTest, ManagedUIDisabledForChildAccount) {
  profile_policy_connector()->OverrideIsManagedForTesting(true);
  profile()->SetIsSupervisedProfile();

  InitializeHandler();

  EXPECT_FALSE(IsSourceManaged());
}
#endif