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 } // namespace | 33 } |
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()); |
68 | 69 |
69 preview_ui_ = static_cast<PrintPreviewUI*>(preview_tab_->web_ui()); | 70 preview_ui_ = static_cast<PrintPreviewUI*>(preview_tab_->web_ui()); |
70 ASSERT_TRUE(preview_ui_); | 71 ASSERT_TRUE(preview_ui_); |
71 } | 72 } |
72 | 73 |
73 void DeletePrintPreviewTab() { | 74 void DeletePrintPreviewTab() { |
74 printing::BackgroundPrintingManager* bg_printing_manager = | 75 printing::BackgroundPrintingManager* bg_printing_manager = |
75 g_browser_process->background_printing_manager(); | 76 g_browser_process->background_printing_manager(); |
76 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); | 77 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); |
77 | 78 |
(...skipping 28 matching lines...) Expand all Loading... |
106 // Put |settings| in to |args| as a JSON string. | 107 // Put |settings| in to |args| as a JSON string. |
107 std::string json_string; | 108 std::string json_string; |
108 base::JSONWriter::Write(&settings, false, &json_string); | 109 base::JSONWriter::Write(&settings, false, &json_string); |
109 ListValue args; | 110 ListValue args; |
110 args.Append(new base::StringValue(json_string)); // |args| takes ownership. | 111 args.Append(new base::StringValue(json_string)); // |args| takes ownership. |
111 preview_ui_->handler_->HandlePrint(&args); | 112 preview_ui_->handler_->HandlePrint(&args); |
112 } | 113 } |
113 | 114 |
114 void RequestPrintWithCustomMargins( | 115 void RequestPrintWithCustomMargins( |
115 const double margin_top, const double margin_right, | 116 const double margin_top, const double margin_right, |
116 const double margin_bottom, const double margin_left) { | 117 const double margin_bottom,const double margin_left) { |
117 // Set the minimal dummy settings to make the HandlePrint() code happy. | 118 // Set the minimal dummy settings to make the HandlePrint() code happy. |
118 DictionaryValue settings; | 119 DictionaryValue settings; |
119 settings.SetBoolean(printing::kSettingPreviewModifiable, true); | 120 settings.SetBoolean(printing::kSettingPreviewModifiable, true); |
120 settings.SetInteger(printing::kSettingColor, printing::COLOR); | 121 settings.SetInteger(printing::kSettingColor, printing::COLOR); |
121 settings.SetBoolean(printing::kSettingPrintToPDF, false); | 122 settings.SetBoolean(printing::kSettingPrintToPDF, false); |
122 settings.SetInteger(printing::kSettingMarginsType, | 123 settings.SetInteger(printing::kSettingMarginsType, |
123 printing::CUSTOM_MARGINS); | 124 printing::CUSTOM_MARGINS); |
124 | 125 |
125 // Creating custom margins dictionary and nesting it in |settings|. | 126 // Creating custom margins dictionary and nesting it in |settings|. |
126 DictionaryValue* custom_settings = GetCustomMarginsDictionary( | 127 DictionaryValue* custom_settings = GetCustomMarginsDictionary( |
(...skipping 11 matching lines...) Expand all Loading... |
138 | 139 |
139 TabContentsWrapper* preview_tab_; | 140 TabContentsWrapper* preview_tab_; |
140 PrintPreviewUI* preview_ui_; | 141 PrintPreviewUI* preview_ui_; |
141 | 142 |
142 private: | 143 private: |
143 void ClearStickySettings() { | 144 void ClearStickySettings() { |
144 PrintPreviewHandler::last_used_margins_type_ = printing::DEFAULT_MARGINS; | 145 PrintPreviewHandler::last_used_margins_type_ = printing::DEFAULT_MARGINS; |
145 delete PrintPreviewHandler::last_used_page_size_margins_; | 146 delete PrintPreviewHandler::last_used_page_size_margins_; |
146 PrintPreviewHandler::last_used_page_size_margins_ = NULL; | 147 PrintPreviewHandler::last_used_page_size_margins_ = NULL; |
147 } | 148 } |
| 149 |
148 }; | 150 }; |
149 | 151 |
150 // Tests that margin settings are saved correctly when printing with custom | 152 // Tests that margin settings are saved correctly when printing with custom |
151 // margins selected. | 153 // margins selected. |
152 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { | 154 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { |
153 const double kMarginTop = 25.5; | 155 const double kMarginTop = 25.5; |
154 const double kMarginRight = 26.5; | 156 const double kMarginRight = 26.5; |
155 const double kMarginBottom = 27.5; | 157 const double kMarginBottom = 27.5; |
156 const double kMarginLeft = 28.5; | 158 const double kMarginLeft = 28.5; |
157 RequestPrintWithCustomMargins( | 159 RequestPrintWithCustomMargins( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 RequestPrintWithCustomMargins( | 191 RequestPrintWithCustomMargins( |
190 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | 192 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); |
191 EXPECT_EQ(1, browser()->tab_count()); | 193 EXPECT_EQ(1, browser()->tab_count()); |
192 DeletePrintPreviewTab(); | 194 DeletePrintPreviewTab(); |
193 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | 195 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
194 printing::CUSTOM_MARGINS); | 196 printing::CUSTOM_MARGINS); |
195 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | 197 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); |
196 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | 198 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); |
197 | 199 |
198 OpenPrintPreviewTab(); | 200 OpenPrintPreviewTab(); |
| 201 EXPECT_EQ(2, browser()->tab_count()); |
199 RequestPrintWithDefaultMargins(); | 202 RequestPrintWithDefaultMargins(); |
200 | 203 |
201 // Checking that sticky settings were saved correctly. | 204 // Checking that sticky settings were saved correctly. |
202 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | 205 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); |
203 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | 206 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
204 printing::DEFAULT_MARGINS); | 207 printing::DEFAULT_MARGINS); |
205 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | 208 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); |
206 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | 209 CheckCustomMargins(kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); |
207 } | 210 } |
208 | 211 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 double margin_value; | 252 double margin_value; |
250 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, | 253 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, |
251 &margin_value)); | 254 &margin_value)); |
252 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, | 255 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, |
253 &margin_value)); | 256 &margin_value)); |
254 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, | 257 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, |
255 &margin_value)); | 258 &margin_value)); |
256 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, | 259 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, |
257 &margin_value)); | 260 &margin_value)); |
258 } | 261 } |
OLD | NEW |