File: printing_restrictions_policy_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (232 lines) | stat: -rw-r--r-- 7,675 bytes parent folder | download | duplicates (4)
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
// 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/policy/printing_restrictions_policy_handler.h"

#include "base/json/json_reader.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "printing/buildflags/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
class PrintingRestrictionsPolicyHandlerTest : public testing::Test {
 protected:
  void SetPolicy(base::Value value) {
    policies_.Set(key::kPrintingPaperSizeDefault, POLICY_LEVEL_MANDATORY,
                  POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, std::move(value),
                  nullptr);
  }

  bool CheckPolicy(base::Value value) {
    SetPolicy(std::move(value));
    return handler_.CheckPolicySettings(policies_, &errors_);
  }

  void ApplyPolicies() { handler_.ApplyPolicySettings(policies_, &prefs_); }

  void CheckValidPolicy(const std::string& policy_value) {
    std::optional<base::Value> printing_paper_size_default =
        base::JSONReader::Read(policy_value);
    ASSERT_TRUE(printing_paper_size_default.has_value());
    EXPECT_TRUE(CheckPolicy(printing_paper_size_default.value().Clone()));
    EXPECT_EQ(0U, errors_.size());

    ApplyPolicies();
    base::Value* value;
    EXPECT_TRUE(prefs_.GetValue(prefs::kPrintingPaperSizeDefault, &value));
    ASSERT_TRUE(value);
    EXPECT_EQ(*value, printing_paper_size_default.value());
  }

  void CheckInvalidPolicy(const std::string& policy_value) {
    std::optional<base::Value> printing_paper_size_default =
        base::JSONReader::Read(policy_value);
    ASSERT_TRUE(printing_paper_size_default.has_value());
    EXPECT_FALSE(CheckPolicy(printing_paper_size_default.value().Clone()));
    EXPECT_EQ(1U, errors_.size());
  }

  PrintingPaperSizeDefaultPolicyHandler handler_;
  PolicyErrorMap errors_;
  PolicyMap policies_;
  PrefValueMap prefs_;
};

TEST_F(PrintingRestrictionsPolicyHandlerTest, NoPolicy) {
  EXPECT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
  EXPECT_EQ(0U, errors_.size());
}

TEST_F(PrintingRestrictionsPolicyHandlerTest, StandardPaperSize) {
  const char kStandardPaperSize[] = R"(
    {
      "name": "iso_a5_148x210mm"
    })";
  CheckValidPolicy(kStandardPaperSize);
}

TEST_F(PrintingRestrictionsPolicyHandlerTest, CustomPaperSize) {
  const char kCustomPaperSize[] = R"(
    {
      "name": "custom",
      "custom_size": {
        "width": 100000,
        "height": 100000
      }
    })";
  CheckValidPolicy(kCustomPaperSize);
}

TEST_F(PrintingRestrictionsPolicyHandlerTest, BothStandardAndCustomPaperSize) {
  const char kBothStandardAndCustomPaperSize[] = R"(
    {
      "name": "iso_a5_148x210mm",
      "custom_size": {
        "width": 100000,
        "height": 100000
      }
    })";
  CheckInvalidPolicy(kBothStandardAndCustomPaperSize);
}

TEST_F(PrintingRestrictionsPolicyHandlerTest, NoCustomPaperSize) {
  const char kNoCustomPaperSize[] = R"(
    {
      "name": "custom"
    })";
  CheckInvalidPolicy(kNoCustomPaperSize);
}

TEST_F(PrintingRestrictionsPolicyHandlerTest, NoWidthInCustomSize) {
  const char kNoWidthInCustomSize[] = R"(
    {
      "name": "custom",
      "custom_size": {
        "height": 100000
      }
    })";
  CheckInvalidPolicy(kNoWidthInCustomSize);
}

TEST_F(PrintingRestrictionsPolicyHandlerTest, NoHeightInCustomSize) {
  const char kNoHeightInCustomSize[] = R"(
    {
      "name": "custom",
      "custom_size": {
        "width": 100000
      }
    })";
  CheckInvalidPolicy(kNoHeightInCustomSize);
}

class PrintPdfAsImageRestrictionsPolicyHandlerTest : public testing::Test {
 protected:
  void SetPolicy(const std::string& policy_name, base::Value value) {
    policies_.Set(policy_name, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE,
                  POLICY_SOURCE_ENTERPRISE_DEFAULT, std::move(value), nullptr);
  }

  bool CheckPolicy(const std::string& policy_name, base::Value value) {
    SetPolicy(policy_name, std::move(value));
    return handler_.CheckPolicySettings(policies_, &errors_);
  }

  bool CheckPolicies() {
    return handler_.CheckPolicySettings(policies_, &errors_);
  }

  void ApplyPolicies() { handler_.ApplyPolicySettings(policies_, &prefs_); }

  void CheckValidPolicy(const std::string& policy_name,
                        base::Value policy_value) {
    EXPECT_TRUE(CheckPolicy(policy_name, std::move(policy_value)));
    EXPECT_EQ(0U, errors_.size());
  }

  void CheckInvalidPolicy(const std::string& policy_name,
                          base::Value policy_value) {
    EXPECT_FALSE(CheckPolicy(policy_name, std::move(policy_value)));
    EXPECT_EQ(1U, errors_.size());
  }

  PrintPdfAsImageDefaultPolicyHandler handler_;
  PolicyErrorMap errors_;
  PolicyMap policies_;
  PrefValueMap prefs_;
};

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
TEST_F(PrintPdfAsImageRestrictionsPolicyHandlerTest,
       DefaultWithAvailabilityEnabled) {
  // For platforms that require PrintPdfAsImageAvailability, demonstrate that
  // it is valid to supply PrintPdfAsImageDefault policy when
  // PrintPdfAsImageAvailability has been enabled.
  base::Value availability_value(true);
  base::Value default_value(true);

  CheckValidPolicy(key::kPrintPdfAsImageAvailability,
                   availability_value.Clone());
  CheckValidPolicy(key::kPrintPdfAsImageDefault, default_value.Clone());

  ApplyPolicies();
  base::Value* value;
  EXPECT_TRUE(prefs_.GetValue(prefs::kPrintPdfAsImageDefault, &value));
  ASSERT_TRUE(value);
  EXPECT_EQ(*value, default_value);
}

TEST_F(PrintPdfAsImageRestrictionsPolicyHandlerTest,
       DefaultWithAvailabilityDisabled) {
  // For platforms that require PrintPdfAsImageAvailability, demonstrate that
  // it is invalid to supply PrintPdfAsImageDefault policy when
  // PrintPdfAsImageAvailability has been disabled.
  base::Value availability_value(false);
  base::Value default_value(true);

  CheckValidPolicy(key::kPrintPdfAsImageAvailability,
                   availability_value.Clone());
  CheckInvalidPolicy(key::kPrintPdfAsImageDefault, default_value.Clone());
}

TEST_F(PrintPdfAsImageRestrictionsPolicyHandlerTest,
       DefaultWithoutAvailability) {
  // For platforms that require PrintPdfAsImageAvailability, demonstrate that
  // it is invalid to supply PrintPdfAsImageDefault policy when
  // PrintPdfAsImageAvailability has not been provided.
  base::Value default_value(true);

  CheckInvalidPolicy(key::kPrintPdfAsImageDefault, default_value.Clone());
}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST_F(PrintPdfAsImageRestrictionsPolicyHandlerTest,
       DefaultWithoutAvailability) {
  // For platforms that do not require PrintPdfAsImageAvailability, demonstrate
  // that it is valid to supply PrintPdfAsImageDefault policy by itself without
  // providing PrintPdfAsImageAvailability.
  base::Value default_value(true);

  CheckValidPolicy(key::kPrintPdfAsImageDefault, default_value.Clone());

  ApplyPolicies();
  base::Value* value;
  EXPECT_TRUE(prefs_.GetValue(prefs::kPrintPdfAsImageDefault, &value));
  ASSERT_TRUE(value);
  EXPECT_EQ(*value, default_value);
}
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

#endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)

}  // namespace policy