OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/json/json_writer.h" | |
7 #include "base/values.h" | |
8 #include "chrome/browser/browser_process.h" | |
9 #include "chrome/browser/printing/background_printing_manager.h" | |
10 #include "chrome/browser/printing/print_preview_tab_controller.h" | |
11 #include "chrome/browser/ui/browser_list.h" | |
12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
13 #include "chrome/browser/ui/webui/print_preview_handler.h" | |
14 #include "chrome/browser/ui/webui/print_preview_ui.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "chrome/test/base/browser_with_test_window_test.h" | |
17 #include "printing/page_size_margins.h" | |
18 #include "printing/print_job_constants.h" | |
19 | |
20 //typedef BrowserWithTestWindowTest PrintPreviewHandlerTest; | |
21 | |
22 namespace { | |
23 | |
24 DictionaryValue* GetCustomMarginsDictionary( | |
25 const double margin_top, const double margin_right, | |
26 const double margin_bottom,const double margin_left) { | |
27 base::DictionaryValue* custom_settings = new base::DictionaryValue(); | |
28 custom_settings->SetDouble(printing::kSettingMarginTop, margin_top); | |
29 custom_settings->SetDouble(printing::kSettingMarginRight, margin_right); | |
30 custom_settings->SetDouble(printing::kSettingMarginBottom, margin_bottom); | |
31 custom_settings->SetDouble(printing::kSettingMarginLeft, margin_left); | |
32 return custom_settings; | |
33 } | |
34 | |
35 } | |
36 | |
37 class PrintPreviewHandlerTest : public BrowserWithTestWindowTest { | |
38 protected: | |
39 void SetUp() { | |
40 BrowserWithTestWindowTest::SetUp(); | |
41 #if !defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS) | |
42 CommandLine::ForCurrentProcess()->AppendSwitch( | |
43 switches::kEnablePrintPreview); | |
44 #endif | |
45 ASSERT_TRUE(browser()); | |
46 BrowserList::SetLastActive(browser()); | |
47 ASSERT_TRUE(BrowserList::GetLastActive()); | |
48 | |
49 browser()->NewTab(); | |
50 EXPECT_EQ(1, browser()->tab_count()); | |
51 OpenPrintPreviewTab(); | |
52 } | |
53 | |
54 virtual void TearDown() { | |
55 DeletePrintPreviewTab(); | |
56 ClearStickySettings(); | |
57 } | |
58 | |
59 void OpenPrintPreviewTab() { | |
60 TabContentsWrapper* initiator_tab = | |
61 browser()->GetSelectedTabContentsWrapper(); | |
62 ASSERT_TRUE(initiator_tab); | |
63 | |
64 scoped_refptr<printing::PrintPreviewTabController> | |
65 controller(new printing::PrintPreviewTabController()); | |
66 ASSERT_TRUE(controller.get()); | |
67 | |
68 preview_tab_ = controller->GetOrCreatePreviewTab(initiator_tab); | |
69 ASSERT_TRUE(preview_tab_); | |
70 EXPECT_EQ(2, browser()->tab_count()); | |
71 | |
72 preview_ui_ = static_cast<PrintPreviewUI*>(preview_tab_->web_ui()); | |
73 ASSERT_TRUE(preview_ui_); | |
74 } | |
75 | |
76 void DeletePrintPreviewTab() { | |
77 printing::BackgroundPrintingManager* bg_printing_manager = | |
78 g_browser_process->background_printing_manager(); | |
79 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); | |
80 | |
81 // Deleting TabContentsWrapper* to avoid warings from pref_notifier_impl.cc | |
82 // after the test ends. | |
83 delete preview_tab_; | |
84 } | |
85 | |
86 void CheckCustomMargins(const double margin_top, | |
87 const double margin_right, | |
88 const double margin_bottom, | |
89 const double margin_left) { | |
90 EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_top, | |
91 margin_top); | |
92 EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_right, | |
93 margin_right); | |
94 EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_bottom, | |
95 margin_bottom); | |
96 EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_left, | |
97 margin_left); | |
98 } | |
99 | |
100 void RequestPrintWithDefaultMargins() { | |
101 // Set the minimal dummy settings to make the HandlePrint() code happy. | |
102 DictionaryValue settings; | |
103 settings.SetBoolean(printing::kSettingPreviewModifiable, true); | |
104 settings.SetInteger(printing::kSettingColor, printing::COLOR); | |
105 settings.SetBoolean(printing::kSettingPrintToPDF, false); | |
106 settings.SetInteger(printing::kSettingMarginsType, | |
107 printing::DEFAULT_MARGINS); | |
108 | |
109 // Put |settings| in to |args| as a JSON string. | |
110 std::string json_string; | |
111 base::JSONWriter::Write(&settings, false, &json_string); | |
112 ListValue args; | |
113 args.Append(new base::StringValue(json_string)); // |args| takes ownership. | |
114 preview_ui_->handler_->HandlePrint(&args); | |
115 } | |
116 | |
117 void RequestPrintWithCustomMargins( | |
118 const double margin_top, const double margin_right, | |
119 const double margin_bottom,const double margin_left) { | |
120 // Set the minimal dummy settings to make the HandlePrint() code happy. | |
121 DictionaryValue settings; | |
122 settings.SetBoolean(printing::kSettingPreviewModifiable, true); | |
123 settings.SetInteger(printing::kSettingColor, printing::COLOR); | |
124 settings.SetBoolean(printing::kSettingPrintToPDF, false); | |
125 settings.SetInteger(printing::kSettingMarginsType, | |
126 printing::CUSTOM_MARGINS); | |
127 | |
128 // Creating custom margins dictionary and nesting it in |settings|. | |
129 DictionaryValue* custom_settings = GetCustomMarginsDictionary( | |
130 margin_top, margin_right, margin_bottom, margin_left); | |
131 // |settings| takes ownership. | |
132 settings.Set(printing::kSettingMarginsCustom, custom_settings); | |
133 | |
134 // Put |settings| in to |args| as a JSON string. | |
135 std::string json_string; | |
136 base::JSONWriter::Write(&settings, false, &json_string); | |
137 ListValue args; | |
138 args.Append(new base::StringValue(json_string)); // |args| takes ownership. | |
139 preview_ui_->handler_->HandlePrint(&args); | |
140 } | |
141 | |
142 TabContentsWrapper* preview_tab_; | |
143 PrintPreviewUI* preview_ui_; | |
144 | |
145 private: | |
146 void ClearStickySettings() { | |
147 PrintPreviewHandler::last_used_margins_type_ = printing::DEFAULT_MARGINS; | |
148 PrintPreviewHandler::last_used_page_size_margins_ = NULL; | |
dpapad
2011/11/14 16:31:55
Deleting this instead of just setting it to null g
| |
149 //scoped_ptr<printing::PageSizeMargins> testing( | |
150 // PrintPreviewHandler::last_used_page_size_margins_); | |
151 } | |
152 | |
153 }; | |
154 | |
155 // Tests that margin settings are saved correctly when printing with custom | |
156 // margins selected. | |
157 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { | |
158 const double margin_top = 25.5; | |
159 const double margin_right = 26.5; | |
160 const double margin_bottom = 27.5; | |
161 const double margin_left = 28.5; | |
162 RequestPrintWithCustomMargins( | |
163 margin_top, margin_right, margin_bottom, margin_left); | |
164 EXPECT_EQ(1, browser()->tab_count()); | |
165 | |
166 // Checking that sticky settings were saved correctly. | |
167 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | |
168 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
169 printing::CUSTOM_MARGINS); | |
170 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | |
171 CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); | |
172 } | |
173 | |
174 // Tests that margin settings are saved correctly when printing with default | |
175 // margins selected. | |
176 TEST_F(PrintPreviewHandlerTest, StickyMarginsDefault) { | |
177 RequestPrintWithDefaultMargins(); | |
178 EXPECT_EQ(1, browser()->tab_count()); | |
179 | |
180 // Checking that sticky settings were saved correctly. | |
181 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | |
182 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
183 printing::DEFAULT_MARGINS); | |
184 ASSERT_TRUE(!PrintPreviewHandler::last_used_page_size_margins_); | |
vandebo (ex-Chrome)
2011/11/14 17:05:39
ASSERT_FALSE
dpapad
2011/11/14 17:20:15
Done.
| |
185 } | |
186 | |
187 // Tests that margin settings are saved correctly when printing with custom | |
188 // margins selected and then again with default margins selected. | |
189 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustomThenDefault) { | |
190 const double margin_top = 125.5; | |
191 const double margin_right = 126.5; | |
192 const double margin_bottom = 127.5; | |
193 const double margin_left = 128.5; | |
194 RequestPrintWithCustomMargins( | |
195 margin_top, margin_right, margin_bottom, margin_left); | |
196 EXPECT_EQ(1, browser()->tab_count()); | |
197 DeletePrintPreviewTab(); | |
198 | |
vandebo (ex-Chrome)
2011/11/14 17:05:39
Check custom margins here too.
dpapad
2011/11/14 17:20:15
Done.
| |
199 OpenPrintPreviewTab(); | |
200 EXPECT_EQ(2, browser()->tab_count()); | |
201 RequestPrintWithDefaultMargins(); | |
202 | |
203 // Checking that sticky settings were saved correctly. | |
204 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | |
205 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
206 printing::DEFAULT_MARGINS); | |
207 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | |
208 CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); | |
209 } | |
210 | |
211 // Tests that margin settings are retrieved correctly after printing with custom | |
212 // margins. | |
213 TEST_F(PrintPreviewHandlerTest, TestGetLastUsedMarginSettingsCustom) { | |
214 const double margin_top = 125.5; | |
215 const double margin_right = 126.5; | |
216 const double margin_bottom = 127.5; | |
217 const double margin_left = 128.5; | |
218 RequestPrintWithCustomMargins( | |
219 margin_top, margin_right, margin_bottom, margin_left); | |
220 base::DictionaryValue initial_settings; | |
221 preview_ui_->handler_->GetLastUsedMarginSettings(&initial_settings); | |
222 int margins_type; | |
223 EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, | |
224 &margins_type)); | |
225 EXPECT_EQ(margins_type, printing::CUSTOM_MARGINS); | |
226 double margin_value; | |
227 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginTop, | |
228 &margin_value)); | |
229 EXPECT_EQ(margin_top, margin_value); | |
230 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginRight, | |
231 &margin_value)); | |
232 EXPECT_EQ(margin_right, margin_value); | |
233 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginBottom, | |
234 &margin_value)); | |
235 EXPECT_EQ(margin_bottom, margin_value); | |
236 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginLeft, | |
237 &margin_value)); | |
238 EXPECT_EQ(margin_left, margin_value); | |
239 } | |
240 | |
241 // Tests that margin settings are retrieved correctly after printing with | |
242 // default margins. | |
243 TEST_F(PrintPreviewHandlerTest, TestGetLastUsedMarginSettingsDefault) { | |
244 RequestPrintWithDefaultMargins(); | |
245 base::DictionaryValue initial_settings; | |
246 preview_ui_->handler_->GetLastUsedMarginSettings(&initial_settings); | |
247 int margins_type; | |
248 EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, | |
249 &margins_type)); | |
250 EXPECT_EQ(margins_type, printing::DEFAULT_MARGINS); | |
251 double margin_value; | |
252 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, | |
253 &margin_value)); | |
254 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, | |
255 &margin_value)); | |
256 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, | |
257 &margin_value)); | |
258 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, | |
259 &margin_value)); | |
260 } | |
OLD | NEW |