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"
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(ENABLE_PRINTING)
17 #include "components/printing/common/print_messages.h"
20 PrintMockRenderThread::PrintMockRenderThread()
21 #if defined(ENABLE_PRINTING)
22 : printer_(new MockPrinter
),
23 print_dialog_user_response_(true),
24 print_preview_cancel_page_number_(-1),
25 print_preview_pages_remaining_(0)
30 PrintMockRenderThread::~PrintMockRenderThread() {
33 scoped_refptr
<base::SingleThreadTaskRunner
>
34 PrintMockRenderThread::GetIOMessageLoopProxy() {
35 return io_task_runner_
;
38 void PrintMockRenderThread::set_io_message_loop_proxy(
39 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
) {
40 io_task_runner_
= task_runner
;
43 bool PrintMockRenderThread::OnMessageReceived(const IPC::Message
& msg
) {
44 if (content::MockRenderThread::OnMessageReceived(msg
))
47 // Some messages we do special handling.
49 IPC_BEGIN_MESSAGE_MAP(PrintMockRenderThread
, msg
)
50 #if defined(ENABLE_PRINTING)
51 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings
,
52 OnGetDefaultPrintSettings
)
53 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint
, OnScriptedPrint
)
54 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings
, OnUpdatePrintSettings
)
55 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount
,
56 OnDidGetPrintedPagesCount
)
57 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage
, OnDidPrintPage
)
58 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount
,
59 OnDidGetPreviewPageCount
)
60 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage
, OnDidPreviewPage
)
61 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel
, OnCheckForCancel
)
63 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection
, OnDuplicateSection
)
65 #endif // defined(ENABLE_PRINTING)
66 IPC_MESSAGE_UNHANDLED(handled
= false)
71 #if defined(ENABLE_PRINTING)
73 void PrintMockRenderThread::OnGetDefaultPrintSettings(
74 PrintMsg_Print_Params
* params
) {
75 printer_
->GetDefaultPrintSettings(params
);
78 void PrintMockRenderThread::OnScriptedPrint(
79 const PrintHostMsg_ScriptedPrint_Params
& params
,
80 PrintMsg_PrintPages_Params
* settings
) {
81 if (print_dialog_user_response_
) {
82 printer_
->ScriptedPrint(params
.cookie
, params
.expected_pages_count
,
83 params
.has_selection
, settings
);
87 void PrintMockRenderThread::OnDidGetPrintedPagesCount(int cookie
,
89 printer_
->SetPrintedPagesCount(cookie
, number_pages
);
92 void PrintMockRenderThread::OnDidPrintPage(
93 const PrintHostMsg_DidPrintPage_Params
& params
) {
94 printer_
->PrintPage(params
);
97 void PrintMockRenderThread::OnDidGetPreviewPageCount(
98 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
) {
99 print_preview_pages_remaining_
= params
.page_count
;
102 void PrintMockRenderThread::OnDidPreviewPage(
103 const PrintHostMsg_DidPreviewPage_Params
& params
) {
104 DCHECK_GE(params
.page_number
, printing::FIRST_PAGE_INDEX
);
105 print_preview_pages_remaining_
--;
108 void PrintMockRenderThread::OnCheckForCancel(int32 preview_ui_id
,
109 int preview_request_id
,
112 (print_preview_pages_remaining_
== print_preview_cancel_page_number_
);
115 void PrintMockRenderThread::OnUpdatePrintSettings(
117 const base::DictionaryValue
& job_settings
,
118 PrintMsg_PrintPages_Params
* params
,
122 // Check and make sure the required settings are all there.
123 // We don't actually care about the values.
124 std::string dummy_string
;
125 int margins_type
= 0;
126 if (!job_settings
.GetBoolean(printing::kSettingLandscape
, NULL
) ||
127 !job_settings
.GetBoolean(printing::kSettingCollate
, NULL
) ||
128 !job_settings
.GetInteger(printing::kSettingColor
, NULL
) ||
129 !job_settings
.GetBoolean(printing::kSettingPrintToPDF
, NULL
) ||
130 !job_settings
.GetBoolean(printing::kIsFirstRequest
, NULL
) ||
131 !job_settings
.GetString(printing::kSettingDeviceName
, &dummy_string
) ||
132 !job_settings
.GetInteger(printing::kSettingDuplexMode
, NULL
) ||
133 !job_settings
.GetInteger(printing::kSettingCopies
, NULL
) ||
134 !job_settings
.GetInteger(printing::kPreviewUIID
, NULL
) ||
135 !job_settings
.GetInteger(printing::kPreviewRequestID
, NULL
) ||
136 !job_settings
.GetInteger(printing::kSettingMarginsType
, &margins_type
)) {
140 // Just return the default settings.
141 const base::ListValue
* page_range_array
;
142 printing::PageRanges new_ranges
;
143 if (job_settings
.GetList(printing::kSettingPageRange
, &page_range_array
)) {
144 for (size_t index
= 0; index
< page_range_array
->GetSize(); ++index
) {
145 const base::DictionaryValue
* dict
;
146 if (!page_range_array
->GetDictionary(index
, &dict
))
148 printing::PageRange range
;
149 if (!dict
->GetInteger(printing::kSettingPageRangeFrom
, &range
.from
) ||
150 !dict
->GetInteger(printing::kSettingPageRangeTo
, &range
.to
)) {
153 // Page numbers are 1-based in the dictionary.
154 // Page numbers are 0-based for the printing context.
157 new_ranges
.push_back(range
);
160 std::vector
<int> pages(printing::PageRange::GetPages(new_ranges
));
161 printer_
->UpdateSettings(document_cookie
, params
, pages
, margins_type
);
163 job_settings
.GetBoolean(printing::kSettingShouldPrintSelectionOnly
,
164 ¶ms
->params
.selection_only
);
165 job_settings
.GetBoolean(printing::kSettingShouldPrintBackgrounds
,
166 ¶ms
->params
.should_print_backgrounds
);
169 MockPrinter
* PrintMockRenderThread::printer() {
170 return printer_
.get();
173 void PrintMockRenderThread::set_print_dialog_user_response(bool response
) {
174 print_dialog_user_response_
= response
;
177 void PrintMockRenderThread::set_print_preview_cancel_page_number(int page
) {
178 print_preview_cancel_page_number_
= page
;
181 int PrintMockRenderThread::print_preview_pages_remaining() const {
182 return print_preview_pages_remaining_
;
184 #endif // defined(ENABLE_PRINTING)