Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: components/printing/test/print_web_view_helper_browsertest.cc

Issue 2697683004: Add automated testing to check for page size with scaling (Closed)
Patch Set: Address comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/printing/renderer/print_web_view_helper.h" 5 #include "components/printing/renderer/print_web_view_helper.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <tuple> 10 #include <tuple>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "components/printing/common/print_messages.h" 17 #include "components/printing/common/print_messages.h"
18 #include "components/printing/test/mock_printer.h" 18 #include "components/printing/test/mock_printer.h"
19 #include "components/printing/test/print_mock_render_thread.h" 19 #include "components/printing/test/print_mock_render_thread.h"
20 #include "components/printing/test/print_test_content_renderer_client.h" 20 #include "components/printing/test/print_test_content_renderer_client.h"
21 #include "content/public/renderer/render_frame.h" 21 #include "content/public/renderer/render_frame.h"
22 #include "content/public/renderer/render_view.h" 22 #include "content/public/renderer/render_view.h"
23 #include "content/public/test/render_view_test.h" 23 #include "content/public/test/render_view_test.h"
24 #include "ipc/ipc_listener.h" 24 #include "ipc/ipc_listener.h"
25 #include "printing/features/features.h" 25 #include "printing/features/features.h"
26 #include "printing/print_job_constants.h" 26 #include "printing/print_job_constants.h"
27 #include "printing/units.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/WebKit/public/platform/WebMouseEvent.h" 29 #include "third_party/WebKit/public/platform/WebMouseEvent.h"
29 #include "third_party/WebKit/public/platform/WebString.h" 30 #include "third_party/WebKit/public/platform/WebString.h"
30 #include "third_party/WebKit/public/web/WebLocalFrame.h" 31 #include "third_party/WebKit/public/web/WebLocalFrame.h"
31 #include "third_party/WebKit/public/web/WebRange.h" 32 #include "third_party/WebKit/public/web/WebRange.h"
32 #include "third_party/WebKit/public/web/WebView.h" 33 #include "third_party/WebKit/public/web/WebView.h"
33 34
34 #if defined(OS_WIN) || defined(OS_MACOSX) 35 #if defined(OS_WIN) || defined(OS_MACOSX)
35 #include "base/files/file_util.h" 36 #include "base/files/file_util.h"
36 #include "printing/image.h" 37 #include "printing/image.h"
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 render_thread_->sink().GetUniqueMessageMatching( 204 render_thread_->sink().GetUniqueMessageMatching(
204 PrintHostMsg_DidGetPreviewPageCount::ID); 205 PrintHostMsg_DidGetPreviewPageCount::ID);
205 ASSERT_TRUE(page_cnt_msg); 206 ASSERT_TRUE(page_cnt_msg);
206 PrintHostMsg_DidGetPreviewPageCount::Param post_page_count_param; 207 PrintHostMsg_DidGetPreviewPageCount::Param post_page_count_param;
207 PrintHostMsg_DidGetPreviewPageCount::Read(page_cnt_msg, 208 PrintHostMsg_DidGetPreviewPageCount::Read(page_cnt_msg,
208 &post_page_count_param); 209 &post_page_count_param);
209 EXPECT_EQ(count, std::get<0>(post_page_count_param).page_count); 210 EXPECT_EQ(count, std::get<0>(post_page_count_param).page_count);
210 } 211 }
211 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) 212 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
212 213
214 #if defined(OS_WIN)
215 // Verifies that the correct page size was returned.
216 void VerifyPrintedPageSize(
217 gfx::Size page_size,
Lei Zhang 2017/02/23 23:13:54 One more pass by const ref.
rbpotter 2017/02/24 00:34:27 Done.
218 PrintHostMsg_DidPrintPage::Param post_did_print_page_param) {
Lei Zhang 2017/02/23 23:13:54 Make that 2?
rbpotter 2017/02/24 00:34:28 Done.
219 gfx::Size page_size_received =
220 std::get<0>(post_did_print_page_param).page_size;
221 EXPECT_EQ(page_size.height(), page_size_received.height());
Lei Zhang 2017/02/23 23:13:54 Fun fact, gfx::Size integrates well with gtest. As
rbpotter 2017/02/24 00:34:27 Done.
222 EXPECT_EQ(page_size.width(), page_size_received.width());
223 }
224 #endif
225
213 // Verifies whether the pages printed or not. 226 // Verifies whether the pages printed or not.
214 void VerifyPagesPrinted(bool printed) { 227 void VerifyPagesPrinted(bool printed, gfx::Size page_size = gfx::Size(0, 0)) {
215 const IPC::Message* print_msg = 228 const IPC::Message* print_msg =
216 render_thread_->sink().GetUniqueMessageMatching( 229 render_thread_->sink().GetUniqueMessageMatching(
217 PrintHostMsg_DidPrintPage::ID); 230 PrintHostMsg_DidPrintPage::ID);
218 bool did_print_msg = !!print_msg; 231 bool did_print_msg = !!print_msg;
219 ASSERT_EQ(printed, did_print_msg); 232 ASSERT_EQ(printed, did_print_msg);
220 if (printed) { 233 if (printed) {
221 PrintHostMsg_DidPrintPage::Param post_did_print_page_param; 234 PrintHostMsg_DidPrintPage::Param post_did_print_page_param;
222 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param); 235 PrintHostMsg_DidPrintPage::Read(print_msg, &post_did_print_page_param);
223 EXPECT_EQ(0, std::get<0>(post_did_print_page_param).page_number); 236 EXPECT_EQ(0, std::get<0>(post_did_print_page_param).page_number);
237 #if defined(OS_WIN)
238 if (!page_size.IsEmpty())
239 VerifyPrintedPageSize(page_size, post_did_print_page_param);
240 #endif
224 } 241 }
225 } 242 }
226 243
227 #if BUILDFLAG(ENABLE_BASIC_PRINTING) 244 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
228 void OnPrintPages() { 245 void OnPrintPages() {
229 GetPrintWebViewHelper()->OnPrintPages(); 246 GetPrintWebViewHelper()->OnPrintPages();
230 ProcessPendingMessages(); 247 ProcessPendingMessages();
231 } 248 }
232 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) 249 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
233 250
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 981
965 // Fill in some dummy values. 982 // Fill in some dummy values.
966 base::DictionaryValue dict; 983 base::DictionaryValue dict;
967 CreatePrintSettingsDictionary(&dict); 984 CreatePrintSettingsDictionary(&dict);
968 OnPrintForPrintPreview(dict); 985 OnPrintForPrintPreview(dict);
969 986
970 VerifyPrintFailed(false); 987 VerifyPrintFailed(false);
971 VerifyPagesPrinted(true); 988 VerifyPagesPrinted(true);
972 } 989 }
973 990
991 // Tests that when printing non-default scaling values, the page size returned
992 // by PrintWebViewHelper is still the real physical page size. See
993 // crbug.com/686384
994 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewWithScaling) {
995 LoadHTML(kPrintPreviewHTML);
996
997 // Fill in some dummy values.
998 base::DictionaryValue dict;
999 CreatePrintSettingsDictionary(&dict);
1000
1001 // Media size
1002 gfx::Size page_size_in = gfx::Size(240, 480);
1003 float device_microns_per_unit =
1004 (printing::kHundrethsMMPerInch * 10.0f) / printing::kDefaultPdfDpi;
1005 int height_microns =
1006 static_cast<int>(page_size_in.height() * device_microns_per_unit);
1007 int width_microns =
1008 static_cast<int>(page_size_in.width() * device_microns_per_unit);
1009 auto media_size = base::MakeUnique<base::DictionaryValue>();
1010 media_size->SetInteger(kSettingMediaSizeHeightMicrons, height_microns);
1011 media_size->SetInteger(kSettingMediaSizeWidthMicrons, width_microns);
1012
1013 // Non default scaling value
1014 dict.SetInteger(kSettingScaleFactor, 80);
1015 dict.Set(kSettingMediaSize, media_size.release());
1016
1017 OnPrintForPrintPreview(dict);
1018
1019 VerifyPrintFailed(false);
1020 VerifyPagesPrinted(true, page_size_in);
1021 }
1022
974 // Tests that printing from print preview fails and receiving error messages 1023 // Tests that printing from print preview fails and receiving error messages
975 // through that channel all works. 1024 // through that channel all works.
976 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) { 1025 TEST_F(MAYBE_PrintWebViewHelperPreviewTest, OnPrintForPrintPreviewFail) {
977 LoadHTML(kPrintPreviewHTML); 1026 LoadHTML(kPrintPreviewHTML);
978 1027
979 // An empty dictionary should fail. 1028 // An empty dictionary should fail.
980 base::DictionaryValue empty_dict; 1029 base::DictionaryValue empty_dict;
981 OnPrintForPrintPreview(empty_dict); 1030 OnPrintForPrintPreview(empty_dict);
982 1031
983 VerifyPagesPrinted(false); 1032 VerifyPagesPrinted(false);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1062 1111
1063 VerifyPrintFailed(true); 1112 VerifyPrintFailed(true);
1064 VerifyPagesPrinted(false); 1113 VerifyPagesPrinted(false);
1065 } 1114 }
1066 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) 1115 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1067 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) 1116 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
1068 1117
1069 #endif // !defined(OS_CHROMEOS) 1118 #endif // !defined(OS_CHROMEOS)
1070 1119
1071 } // namespace printing 1120 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698