File: printing_apitest.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 (238 lines) | stat: -rw-r--r-- 8,437 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
233
234
235
236
237
238
// 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 <optional>
#include <string>

#include "chrome/browser/extensions/api/printing/print_job_submitter.h"
#include "chrome/browser/extensions/api/printing/printing_test_utils.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/printing/local_printer_utils_chromeos.h"
#include "chrome/browser/ui/browser.h"
#include "content/public/test/browser_test.h"
#include "extensions/test/test_extension_dir.h"
#include "printing/printing_features.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace {

constexpr char kId[] = "id";
constexpr char kName[] = "name";

}  // namespace

// TODO(crbug.com/308709702): Remove the bool param from this as soon as
// the `kPrintingMarginsAndScale` feature is enabled by default. At the moment,
// this is used to run the same test with and without the feature enabled.
class PrintingApiTestBase
    : public ExtensionApiTest,
      public testing::WithParamInterface<std::tuple<bool, ExtensionType>> {
 public:
  void SetUp() override {
    if (GetEnableMarginAndScale()) {
      feature_list_.InitAndEnableFeature(
          printing::features::kApiPrintingMarginsAndScale);
    } else {
      feature_list_.InitAndDisableFeature(
          printing::features::kApiPrintingMarginsAndScale);
    }
    ExtensionApiTest::SetUp();
  }

  void SetUpOnMainThread() override {
    ExtensionApiTest::SetUpOnMainThread();
    PrintJobSubmitter::SkipConfirmationDialogForTesting();
  }

 protected:
  bool GetEnableMarginAndScale() const { return std::get<0>(GetParam()); }
  ExtensionType GetExtensionType() const { return std::get<1>(GetParam()); }

  void RunTest(const char* html_test_page, bool expect_success = true) {
    auto dir = CreatePrintingExtension(GetExtensionType());
    auto run_options = GetExtensionType() == ExtensionType::kChromeApp
                           ? RunOptions{.custom_arg = html_test_page,
                                        .launch_as_platform_app = true}
                           : RunOptions({.extension_url = html_test_page});
    ASSERT_EQ(RunExtensionTest(dir->UnpackedPath(), run_options, {}),
              expect_success);
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

class PrintingApiTest : public PrintingApiTestBase {
 public:
  void PreRunTestOnMainThread() override {
    PrintingApiTestBase::PreRunTestOnMainThread();
    helper_->Init(browser()->profile());
  }

  void TearDownOnMainThread() override {
    helper_.reset();
    PrintingApiTestBase::TearDownOnMainThread();
  }

  void SetUpInProcessBrowserTestFixture() override {
    PrintingApiTestBase::SetUpInProcessBrowserTestFixture();
    helper_ = std::make_unique<PrintingTestHelper>();
  }

 protected:
  void AddPrinterWithSemanticCaps(
      const std::string& printer_id,
      const std::string& printer_display_name,
      std::unique_ptr<printing::PrinterSemanticCapsAndDefaults> caps) {
    helper_->AddAvailablePrinter(printer_id, printer_display_name,
                                 std::move(caps));
  }

 private:
  std::unique_ptr<PrintingTestHelper> helper_;
};

using PrintingPromiseApiTest = PrintingApiTest;

IN_PROC_BROWSER_TEST_P(PrintingApiTest, GetPrinters) {
  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("get_printers.html");
}

IN_PROC_BROWSER_TEST_P(PrintingApiTest, GetPrinterInfo) {
  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("get_printer_info.html");

  // Expect failure/success depending on whether the feature is enabled or not.
  // TODO(crbug.com/308709702): Remove this and merge two files once the feature
  // is enabled by default.
  const bool expect_success = GetEnableMarginAndScale();
  RunTest("get_printer_info_margin_and_scale.html", expect_success);
}

// Verifies that:
// a) PrintingHooksDelegate substitutes corresponding Blob UUID and DCHECK
// doesn't fail.
// b) Whole API arguments handling pipeline works correctly.
// We use fake version of PrintJobController because we don't have a mock
// version of PrintingContext which is required to handle sending print job to
// the printer.
IN_PROC_BROWSER_TEST_P(PrintingApiTest, SubmitJob) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("submit_job.html");
}

IN_PROC_BROWSER_TEST_P(PrintingApiTest, SubmitJobWithMarginsAndScale) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("submit_job_margins_and_scale.html");
}

IN_PROC_BROWSER_TEST_P(PrintingApiTest, SubmitJobWithUnsupportedMargins) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  // If the feature is disabled, the test must succeed regardless of the margins
  // used.
  // TODO(crbug.com/308709702): Remove this and expect the test to always fail
  // once the feature is enabled by default as provided margins in this test
  // are not supported by the setup printer.
  const bool expect_success = !GetEnableMarginAndScale();

  auto caps = ConstructPrinterCapabilities();
  std::vector<printing::PrinterSemanticCapsAndDefaults::Paper> papers;
  // Override papers with custom margins.
  for (const auto& paper : caps->papers) {
    papers.emplace_back(paper.display_name(), paper.vendor_id(),
                        paper.size_um(), paper.printable_area_um(),
                        paper.max_height_um(), paper.has_borderless_variant(),
                        printing::PaperMargins(2340, 1234, 1234, 1234));
  }
  caps->papers = std::move(papers);
  AddPrinterWithSemanticCaps(kId, kName, std::move(caps));

  RunTest("submit_job_margins_and_scale.html", expect_success);
}

IN_PROC_BROWSER_TEST_P(PrintingApiTest, SubmitJobWithUnsupportedScale) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  // If the feature is disabled, the test must succeed regardless of the scale
  // used.
  // TODO(crbug.com/308709702): Remove this and expect the test to always fail
  // once the feature is enabled by default as provided scale in this test
  // is not supported by the setup printer.
  const bool expect_success = !GetEnableMarginAndScale();

  auto caps = ConstructPrinterCapabilities();
  // Override with custom scaling type different from defined in the js/html
  // file of the test.
  caps->print_scaling_types = {printing::mojom::PrintScalingType::kFill};
  caps->print_scaling_type_default = printing::mojom::PrintScalingType::kFill;
  AddPrinterWithSemanticCaps(kId, kName, std::move(caps));

  RunTest("submit_job_margins_and_scale.html", expect_success);
}

// As above, but tests using promise based API calls.
IN_PROC_BROWSER_TEST_P(PrintingPromiseApiTest, SubmitJob) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("submit_job_promise.html");
}

IN_PROC_BROWSER_TEST_P(PrintingPromiseApiTest, SubmitJobWithMarginsAndScale) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("submit_job_promise_margins_and_scale.html");
}

// Verifies that:
// a) Cancel job request works smoothly.
// b) OnJobStatusChanged() events are dispatched correctly.
IN_PROC_BROWSER_TEST_P(PrintingApiTest, CancelJob) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("cancel_job.html");
}

IN_PROC_BROWSER_TEST_P(PrintingApiTest, GetJobStatus) {
  ASSERT_TRUE(StartEmbeddedTestServer());

  AddPrinterWithSemanticCaps(kId, kName, ConstructPrinterCapabilities());

  RunTest("get_print_job_status.html");
}

INSTANTIATE_TEST_SUITE_P(
    /**/,
    PrintingApiTest,
    testing::Combine(testing::Bool(),
                     testing::Values(ExtensionType::kChromeApp,
                                     ExtensionType::kExtensionMV2,
                                     ExtensionType::kExtensionMV3)));

// We only run the promise based tests for MV3 extensions as promise based API
// calls are only exposed to MV3.
INSTANTIATE_TEST_SUITE_P(
    /**/,
    PrintingPromiseApiTest,
    testing::Combine(testing::Bool(),
                     testing::Values(ExtensionType::kExtensionMV3)));

}  // namespace extensions