1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/printing/print_job.h"
6
7 #include <memory>
8 #include <utility>
9 #include <vector>
10
11 #include "base/run_loop.h"
12 #include "base/strings/string16.h"
13 #include "build/build_config.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/printing/print_job_worker.h"
16 #include "chrome/browser/printing/printer_query.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/common/child_process_host.h"
20 #include "content/public/test/browser_task_environment.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace printing {
24
25 namespace {
26
27 class TestPrintJobWorker : public PrintJobWorker {
28 public:
TestPrintJobWorker()29 TestPrintJobWorker()
30 : PrintJobWorker(content::ChildProcessHost::kInvalidUniqueID,
31 content::ChildProcessHost::kInvalidUniqueID) {}
32 friend class TestQuery;
33 };
34
35 class TestQuery : public PrinterQuery {
36 public:
TestQuery()37 TestQuery()
38 : PrinterQuery(content::ChildProcessHost::kInvalidUniqueID,
39 content::ChildProcessHost::kInvalidUniqueID) {}
40
GetSettingsDone(base::OnceClosure callback,std::unique_ptr<PrintSettings> new_settings,PrintingContext::Result result)41 void GetSettingsDone(base::OnceClosure callback,
42 std::unique_ptr<PrintSettings> new_settings,
43 PrintingContext::Result result) override {
44 FAIL();
45 }
46
~TestQuery()47 ~TestQuery() override {}
48
DetachWorker()49 std::unique_ptr<PrintJobWorker> DetachWorker() override {
50 {
51 // Do an actual detach to keep the parent class happy.
52 auto real_worker = PrinterQuery::DetachWorker();
53 }
54
55 // We're screwing up here since we're calling worker from the main thread.
56 // That's fine for testing. It is actually simulating PrinterQuery behavior.
57 auto worker = std::make_unique<TestPrintJobWorker>();
58 EXPECT_TRUE(worker->Start());
59 worker->printing_context()->UseDefaultSettings();
60 SetSettingsForTest(worker->printing_context()->TakeAndResetSettings());
61
62 return std::move(worker);
63 }
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(TestQuery);
67 };
68
69 class TestPrintJob : public PrintJob {
70 public:
TestPrintJob(volatile bool * check)71 explicit TestPrintJob(volatile bool* check) : check_(check) {
72 }
73 private:
~TestPrintJob()74 ~TestPrintJob() override { *check_ = true; }
75 volatile bool* check_;
76 };
77
78 class TestPrintNotificationObserver : public content::NotificationObserver {
79 public:
80 // content::NotificationObserver
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)81 void Observe(int type,
82 const content::NotificationSource& source,
83 const content::NotificationDetails& details) override {
84 ADD_FAILURE();
85 }
86 };
87
88 } // namespace
89
TEST(PrintJobTest,SimplePrint)90 TEST(PrintJobTest, SimplePrint) {
91 // Test the multi-threaded nature of PrintJob to make sure we can use it with
92 // known lifetime.
93
94 content::BrowserTaskEnvironment task_environment;
95 content::NotificationRegistrar registrar;
96 TestPrintNotificationObserver observer;
97 registrar.Add(&observer, content::NOTIFICATION_ALL,
98 content::NotificationService::AllSources());
99 volatile bool check = false;
100 scoped_refptr<PrintJob> job(new TestPrintJob(&check));
101 job->Initialize(std::make_unique<TestQuery>(), base::string16(), 1);
102 #if defined(OS_CHROMEOS)
103 job->SetSource(PrintJob::Source::PRINT_PREVIEW, /*source_id=*/"");
104 #endif // defined(OS_CHROMEOS)
105 job->Stop();
106 while (job->document()) {
107 base::RunLoop().RunUntilIdle();
108 }
109 EXPECT_FALSE(job->document());
110 job = nullptr;
111 while (!check) {
112 base::RunLoop().RunUntilIdle();
113 }
114 EXPECT_TRUE(check);
115 }
116
TEST(PrintJobTest,SimplePrintLateInit)117 TEST(PrintJobTest, SimplePrintLateInit) {
118 volatile bool check = false;
119 content::BrowserTaskEnvironment task_environment;
120 scoped_refptr<PrintJob> job(new TestPrintJob(&check));
121 job = nullptr;
122 EXPECT_TRUE(check);
123 /* TODO(maruel): Test these.
124 job->Initialize()
125 job->Observe();
126 job->GetSettingsDone();
127 job->DetachWorker();
128 job->settings();
129 job->cookie();
130 job->GetSettings(DEFAULTS, ASK_USER, nullptr);
131 job->StartPrinting();
132 job->Stop();
133 job->Cancel();
134 job->RequestMissingPages();
135 job->FlushJob(timeout);
136 job->is_job_pending();
137 job->document();
138 // Private
139 job->UpdatePrintedDocument(nullptr);
140 scoped_refptr<JobEventDetails> event_details;
141 job->OnNotifyPrintJobEvent(event_details);
142 job->OnDocumentDone();
143 job->ControlledWorkerShutdown();
144 */
145 }
146
147 #if defined(OS_WIN)
TEST(PrintJobTest,PageRangeMapping)148 TEST(PrintJobTest, PageRangeMapping) {
149 content::BrowserTaskEnvironment task_environment;
150
151 int page_count = 4;
152 std::vector<uint32_t> input_full = {0, 1, 2, 3};
153 std::vector<uint32_t> expected_output_full = {0, 1, 2, 3};
154 EXPECT_EQ(expected_output_full,
155 PrintJob::GetFullPageMapping(input_full, page_count));
156
157 std::vector<uint32_t> input_12 = {1, 2};
158 std::vector<uint32_t> expected_output_12 = {kInvalidPageIndex, 1, 2,
159 kInvalidPageIndex};
160 EXPECT_EQ(expected_output_12,
161 PrintJob::GetFullPageMapping(input_12, page_count));
162
163 std::vector<uint32_t> input_03 = {0, 3};
164 std::vector<uint32_t> expected_output_03 = {0, kInvalidPageIndex,
165 kInvalidPageIndex, 3};
166 EXPECT_EQ(expected_output_03,
167 PrintJob::GetFullPageMapping(input_03, page_count));
168
169 std::vector<uint32_t> input_0 = {0};
170 std::vector<uint32_t> expected_output_0 = {
171 0, kInvalidPageIndex, kInvalidPageIndex, kInvalidPageIndex};
172 EXPECT_EQ(expected_output_0,
173 PrintJob::GetFullPageMapping(input_0, page_count));
174
175 std::vector<uint32_t> input_invalid = {4, 100};
176 std::vector<uint32_t> expected_output_invalid = {
177 kInvalidPageIndex, kInvalidPageIndex, kInvalidPageIndex,
178 kInvalidPageIndex};
179 EXPECT_EQ(expected_output_invalid,
180 PrintJob::GetFullPageMapping(input_invalid, page_count));
181 }
182 #endif // defined(OS_WIN)
183
184 } // namespace printing
185