OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/printing/background_printing_manager.h" | 9 #include "chrome/browser/printing/background_printing_manager.h" |
10 #include "chrome/browser/printing/print_preview_tab_controller.h" | 10 #include "chrome/browser/printing/print_preview_tab_controller.h" |
11 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
13 #include "chrome/browser/ui/webui/print_preview_handler.h" | 13 #include "chrome/browser/ui/webui/print_preview_handler.h" |
14 #include "chrome/browser/ui/webui/print_preview_ui.h" | 14 #include "chrome/browser/ui/webui/print_preview_ui.h" |
15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/test/base/browser_with_test_window_test.h" | 16 #include "chrome/test/base/browser_with_test_window_test.h" |
17 #include "printing/page_size_margins.h" | 17 #include "printing/page_size_margins.h" |
18 #include "printing/print_job_constants.h" | 18 #include "printing/print_job_constants.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 DictionaryValue* GetCustomMarginsDictionary( | 22 DictionaryValue* GetCustomMarginsDictionary( |
23 const double margin_top, const double margin_right, | 23 const double margin_top, const double margin_right, |
24 const double margin_bottom,const double margin_left) { | 24 const double margin_bottom, const double margin_left) { |
25 base::DictionaryValue* custom_settings = new base::DictionaryValue(); | 25 base::DictionaryValue* custom_settings = new base::DictionaryValue(); |
26 custom_settings->SetDouble(printing::kSettingMarginTop, margin_top); | 26 custom_settings->SetDouble(printing::kSettingMarginTop, margin_top); |
27 custom_settings->SetDouble(printing::kSettingMarginRight, margin_right); | 27 custom_settings->SetDouble(printing::kSettingMarginRight, margin_right); |
28 custom_settings->SetDouble(printing::kSettingMarginBottom, margin_bottom); | 28 custom_settings->SetDouble(printing::kSettingMarginBottom, margin_bottom); |
29 custom_settings->SetDouble(printing::kSettingMarginLeft, margin_left); | 29 custom_settings->SetDouble(printing::kSettingMarginLeft, margin_left); |
30 return custom_settings; | 30 return custom_settings; |
31 } | 31 } |
32 | 32 |
33 } | 33 } // namespace |
34 | 34 |
35 class PrintPreviewHandlerTest : public BrowserWithTestWindowTest { | 35 class PrintPreviewHandlerTest : public BrowserWithTestWindowTest { |
36 protected: | 36 protected: |
37 void SetUp() { | 37 void SetUp() { |
38 BrowserWithTestWindowTest::SetUp(); | 38 BrowserWithTestWindowTest::SetUp(); |
39 #if !defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS) | 39 #if !defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS) |
40 CommandLine::ForCurrentProcess()->AppendSwitch( | 40 CommandLine::ForCurrentProcess()->AppendSwitch( |
41 switches::kEnablePrintPreview); | 41 switches::kEnablePrintPreview); |
42 #endif | 42 #endif |
43 ASSERT_TRUE(browser()); | 43 ASSERT_TRUE(browser()); |
(...skipping 14 matching lines...) Expand all Loading... |
58 TabContentsWrapper* initiator_tab = | 58 TabContentsWrapper* initiator_tab = |
59 browser()->GetSelectedTabContentsWrapper(); | 59 browser()->GetSelectedTabContentsWrapper(); |
60 ASSERT_TRUE(initiator_tab); | 60 ASSERT_TRUE(initiator_tab); |
61 | 61 |
62 scoped_refptr<printing::PrintPreviewTabController> | 62 scoped_refptr<printing::PrintPreviewTabController> |
63 controller(new printing::PrintPreviewTabController()); | 63 controller(new printing::PrintPreviewTabController()); |
64 ASSERT_TRUE(controller.get()); | 64 ASSERT_TRUE(controller.get()); |
65 | 65 |
66 preview_tab_ = controller->GetOrCreatePreviewTab(initiator_tab); | 66 preview_tab_ = controller->GetOrCreatePreviewTab(initiator_tab); |
67 ASSERT_TRUE(preview_tab_); | 67 ASSERT_TRUE(preview_tab_); |
68 EXPECT_EQ(2, browser()->tab_count()); | |
69 | 68 |
70 preview_ui_ = static_cast<PrintPreviewUI*>(preview_tab_->web_ui()); | 69 preview_ui_ = static_cast<PrintPreviewUI*>(preview_tab_->web_ui()); |
71 ASSERT_TRUE(preview_ui_); | 70 ASSERT_TRUE(preview_ui_); |
72 } | 71 } |
73 | 72 |
74 void DeletePrintPreviewTab() { | 73 void DeletePrintPreviewTab() { |
75 printing::BackgroundPrintingManager* bg_printing_manager = | 74 printing::BackgroundPrintingManager* bg_printing_manager = |
76 g_browser_process->background_printing_manager(); | 75 g_browser_process->background_printing_manager(); |
77 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); | 76 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); |
78 | 77 |
(...skipping 28 matching lines...) Expand all Loading... |
107 // Put |settings| in to |args| as a JSON string. | 106 // Put |settings| in to |args| as a JSON string. |
108 std::string json_string; | 107 std::string json_string; |
109 base::JSONWriter::Write(&settings, false, &json_string); | 108 base::JSONWriter::Write(&settings, false, &json_string); |
110 ListValue args; | 109 ListValue args; |
111 args.Append(new base::StringValue(json_string)); // |args| takes ownership. | 110 args.Append(new base::StringValue(json_string)); // |args| takes ownership. |
112 preview_ui_->handler_->HandlePrint(&args); | 111 preview_ui_->handler_->HandlePrint(&args); |
113 } | 112 } |
114 | 113 |
115 void RequestPrintWithCustomMargins( | 114 void RequestPrintWithCustomMargins( |
116 const double margin_top, const double margin_right, | 115 const double margin_top, const double margin_right, |
117 const double margin_bottom,const double margin_left) { | 116 const double margin_bottom, const double margin_left) { |
118 // Set the minimal dummy settings to make the HandlePrint() code happy. | 117 // Set the minimal dummy settings to make the HandlePrint() code happy. |
119 DictionaryValue settings; | 118 DictionaryValue settings; |
120 settings.SetBoolean(printing::kSettingPreviewModifiable, true); | 119 settings.SetBoolean(printing::kSettingPreviewModifiable, true); |
121 settings.SetInteger(printing::kSettingColor, printing::COLOR); | 120 settings.SetInteger(printing::kSettingColor, printing::COLOR); |
122 settings.SetBoolean(printing::kSettingPrintToPDF, false); | 121 settings.SetBoolean(printing::kSettingPrintToPDF, false); |
123 settings.SetInteger(printing::kSettingMarginsType, | 122 settings.SetInteger(printing::kSettingMarginsType, |
124 printing::CUSTOM_MARGINS); | 123 printing::CUSTOM_MARGINS); |
125 | 124 |
126 // Creating custom margins dictionary and nesting it in |settings|. | 125 // Creating custom margins dictionary and nesting it in |settings|. |
127 DictionaryValue* custom_settings = GetCustomMarginsDictionary( | 126 DictionaryValue* custom_settings = GetCustomMarginsDictionary( |
(...skipping 11 matching lines...) Expand all Loading... |
139 | 138 |
140 TabContentsWrapper* preview_tab_; | 139 TabContentsWrapper* preview_tab_; |
141 PrintPreviewUI* preview_ui_; | 140 PrintPreviewUI* preview_ui_; |
142 | 141 |
143 private: | 142 private: |
144 void ClearStickySettings() { | 143 void ClearStickySettings() { |
145 PrintPreviewHandler::last_used_margins_type_ = printing::DEFAULT_MARGINS; | 144 PrintPreviewHandler::last_used_margins_type_ = printing::DEFAULT_MARGINS; |
146 delete PrintPreviewHandler::last_used_page_size_margins_; | 145 delete PrintPreviewHandler::last_used_page_size_margins_; |
147 PrintPreviewHandler::last_used_page_size_margins_ = NULL; | 146 PrintPreviewHandler::last_used_page_size_margins_ = NULL; |
148 } | 147 } |
149 | |
150 }; | 148 }; |
151 | 149 |
152 // Tests that margin settings are saved correctly when printing with custom | 150 // Tests that margin settings are saved correctly when printing with custom |
153 // margins selected. | 151 // margins selected. |
154 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { | 152 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { |
155 const double kMarginTop = 25.5; | 153 const double kMarginTop = 25.5; |
156 const double kMarginRight = 26.5; | 154 const double kMarginRight = 26.5; |
157 const double kMarginBottom = 27.5; | 155 const double kMarginBottom = 27.5; |
158 const double kMarginLeft = 28.5; | 156 const double kMarginLeft = 28.5; |
159 RequestPrintWithCustomMargins( | 157 RequestPrintWithCustomMargins( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 RequestPrintWithCustomMargins( | 189 RequestPrintWithCustomMargins( |
192 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | 190 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); |
193 EXPECT_EQ(1, browser()->tab_count()); | 191 EXPECT_EQ(1, browser()->tab_count()); |
194 DeletePrintPreviewTab(); | 192 DeletePrintPreviewTab(); |
195 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | 193 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
196 printing::CUSTOM_MARGINS); | 194 printing::CUSTOM_MARGINS); |
197 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | 195 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); |
198 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | 196 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); |
199 | 197 |
200 OpenPrintPreviewTab(); | 198 OpenPrintPreviewTab(); |
201 EXPECT_EQ(2, browser()->tab_count()); | |
202 RequestPrintWithDefaultMargins(); | 199 RequestPrintWithDefaultMargins(); |
203 | 200 |
204 // Checking that sticky settings were saved correctly. | 201 // Checking that sticky settings were saved correctly. |
205 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | 202 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); |
206 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | 203 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
207 printing::DEFAULT_MARGINS); | 204 printing::DEFAULT_MARGINS); |
208 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | 205 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); |
209 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | 206 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); |
210 } | 207 } |
211 | 208 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 double margin_value; | 249 double margin_value; |
253 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, | 250 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, |
254 &margin_value)); | 251 &margin_value)); |
255 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, | 252 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, |
256 &margin_value)); | 253 &margin_value)); |
257 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, | 254 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, |
258 &margin_value)); | 255 &margin_value)); |
259 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, | 256 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, |
260 &margin_value)); | 257 &margin_value)); |
261 } | 258 } |
OLD | NEW |