[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / components / printing / test / print_mock_render_thread.cc
blobcff4019b6991286f85b5b87ce7ff2d419d0f1be6
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.
5 #include "components/printing/test/print_mock_render_thread.h"
7 #include <vector>
9 #include "base/values.h"
10 #include "components/printing/test/mock_printer.h"
11 #include "ipc/ipc_sync_message.h"
12 #include "printing/page_range.h"
13 #include "printing/print_job_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_CHROMEOS)
17 #include <fcntl.h>
19 #include "base/files/file_util.h"
20 #endif
22 #if defined(ENABLE_PRINTING)
23 #include "components/printing/common/print_messages.h"
24 #endif
26 PrintMockRenderThread::PrintMockRenderThread()
27 #if defined(ENABLE_PRINTING)
28 : printer_(new MockPrinter),
29 print_dialog_user_response_(true),
30 print_preview_cancel_page_number_(-1),
31 print_preview_pages_remaining_(0)
32 #endif
36 PrintMockRenderThread::~PrintMockRenderThread() {
39 scoped_refptr<base::SingleThreadTaskRunner>
40 PrintMockRenderThread::GetIOMessageLoopProxy() {
41 return io_task_runner_;
44 void PrintMockRenderThread::set_io_message_loop_proxy(
45 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) {
46 io_task_runner_ = task_runner;
49 bool PrintMockRenderThread::OnMessageReceived(const IPC::Message& msg) {
50 if (content::MockRenderThread::OnMessageReceived(msg))
51 return true;
53 // Some messages we do special handling.
54 bool handled = true;
55 IPC_BEGIN_MESSAGE_MAP(PrintMockRenderThread, msg)
56 #if defined(ENABLE_PRINTING)
57 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings,
58 OnGetDefaultPrintSettings)
59 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
60 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings)
61 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
62 OnDidGetPrintedPagesCount)
63 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
64 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
65 OnDidGetPreviewPageCount)
66 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage)
67 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel)
68 #if defined(OS_WIN)
69 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
70 #endif
71 #if defined(OS_CHROMEOS)
72 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
73 OnAllocateTempFileForPrinting)
74 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
75 OnTempFileForPrintingWritten)
76 #endif // defined(OS_CHROMEOS)
77 #endif // defined(ENABLE_PRINTING)
78 IPC_MESSAGE_UNHANDLED(handled = false)
79 IPC_END_MESSAGE_MAP()
80 return handled;
83 #if defined(ENABLE_PRINTING)
84 #if defined(OS_CHROMEOS)
85 void PrintMockRenderThread::OnAllocateTempFileForPrinting(
86 int render_view_id,
87 base::FileDescriptor* renderer_fd,
88 int* browser_fd) {
89 renderer_fd->fd = *browser_fd = -1;
90 renderer_fd->auto_close = false;
92 base::FilePath path;
93 if (base::CreateTemporaryFile(&path)) {
94 int fd = open(path.value().c_str(), O_WRONLY);
95 DCHECK_GE(fd, 0);
96 renderer_fd->fd = *browser_fd = fd;
100 void PrintMockRenderThread::OnTempFileForPrintingWritten(int render_view_id,
101 int browser_fd) {
102 close(browser_fd);
104 #endif // defined(OS_CHROMEOS)
106 void PrintMockRenderThread::OnGetDefaultPrintSettings(
107 PrintMsg_Print_Params* params) {
108 printer_->GetDefaultPrintSettings(params);
111 void PrintMockRenderThread::OnScriptedPrint(
112 const PrintHostMsg_ScriptedPrint_Params& params,
113 PrintMsg_PrintPages_Params* settings) {
114 if (print_dialog_user_response_) {
115 printer_->ScriptedPrint(params.cookie, params.expected_pages_count,
116 params.has_selection, settings);
120 void PrintMockRenderThread::OnDidGetPrintedPagesCount(int cookie,
121 int number_pages) {
122 printer_->SetPrintedPagesCount(cookie, number_pages);
125 void PrintMockRenderThread::OnDidPrintPage(
126 const PrintHostMsg_DidPrintPage_Params& params) {
127 printer_->PrintPage(params);
130 void PrintMockRenderThread::OnDidGetPreviewPageCount(
131 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
132 print_preview_pages_remaining_ = params.page_count;
135 void PrintMockRenderThread::OnDidPreviewPage(
136 const PrintHostMsg_DidPreviewPage_Params& params) {
137 DCHECK_GE(params.page_number, printing::FIRST_PAGE_INDEX);
138 print_preview_pages_remaining_--;
141 void PrintMockRenderThread::OnCheckForCancel(int32 preview_ui_id,
142 int preview_request_id,
143 bool* cancel) {
144 *cancel =
145 (print_preview_pages_remaining_ == print_preview_cancel_page_number_);
148 void PrintMockRenderThread::OnUpdatePrintSettings(
149 int document_cookie,
150 const base::DictionaryValue& job_settings,
151 PrintMsg_PrintPages_Params* params,
152 bool* canceled) {
153 if (canceled)
154 *canceled = false;
155 // Check and make sure the required settings are all there.
156 // We don't actually care about the values.
157 std::string dummy_string;
158 int margins_type = 0;
159 if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
160 !job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
161 !job_settings.GetInteger(printing::kSettingColor, NULL) ||
162 !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) ||
163 !job_settings.GetBoolean(printing::kIsFirstRequest, NULL) ||
164 !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) ||
165 !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
166 !job_settings.GetInteger(printing::kSettingCopies, NULL) ||
167 !job_settings.GetInteger(printing::kPreviewUIID, NULL) ||
168 !job_settings.GetInteger(printing::kPreviewRequestID, NULL) ||
169 !job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) {
170 return;
173 // Just return the default settings.
174 const base::ListValue* page_range_array;
175 printing::PageRanges new_ranges;
176 if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) {
177 for (size_t index = 0; index < page_range_array->GetSize(); ++index) {
178 const base::DictionaryValue* dict;
179 if (!page_range_array->GetDictionary(index, &dict))
180 continue;
181 printing::PageRange range;
182 if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) ||
183 !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) {
184 continue;
186 // Page numbers are 1-based in the dictionary.
187 // Page numbers are 0-based for the printing context.
188 range.from--;
189 range.to--;
190 new_ranges.push_back(range);
193 std::vector<int> pages(printing::PageRange::GetPages(new_ranges));
194 printer_->UpdateSettings(document_cookie, params, pages, margins_type);
196 job_settings.GetBoolean(printing::kSettingShouldPrintSelectionOnly,
197 &params->params.selection_only);
198 job_settings.GetBoolean(printing::kSettingShouldPrintBackgrounds,
199 &params->params.should_print_backgrounds);
202 MockPrinter* PrintMockRenderThread::printer() {
203 return printer_.get();
206 void PrintMockRenderThread::set_print_dialog_user_response(bool response) {
207 print_dialog_user_response_ = response;
210 void PrintMockRenderThread::set_print_preview_cancel_page_number(int page) {
211 print_preview_cancel_page_number_ = page;
214 int PrintMockRenderThread::print_preview_pages_remaining() const {
215 return print_preview_pages_remaining_;
217 #endif // defined(ENABLE_PRINTING)