Chromium Code Reviews| 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; | |
|
kmadhusu
2011/11/15 02:31:03
Remove this comment.
| |
| 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 delete PrintPreviewHandler::last_used_page_size_margins_; | |
| 149 PrintPreviewHandler::last_used_page_size_margins_ = NULL; | |
| 150 } | |
| 151 | |
| 152 }; | |
| 153 | |
| 154 // Tests that margin settings are saved correctly when printing with custom | |
| 155 // margins selected. | |
| 156 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { | |
| 157 const double margin_top = 25.5; | |
| 158 const double margin_right = 26.5; | |
| 159 const double margin_bottom = 27.5; | |
| 160 const double margin_left = 28.5; | |
| 161 RequestPrintWithCustomMargins( | |
| 162 margin_top, margin_right, margin_bottom, margin_left); | |
| 163 EXPECT_EQ(1, browser()->tab_count()); | |
| 164 | |
| 165 // Checking that sticky settings were saved correctly. | |
| 166 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | |
| 167 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
| 168 printing::CUSTOM_MARGINS); | |
| 169 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | |
| 170 CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); | |
| 171 } | |
| 172 | |
| 173 // Tests that margin settings are saved correctly when printing with default | |
| 174 // margins selected. | |
| 175 TEST_F(PrintPreviewHandlerTest, StickyMarginsDefault) { | |
| 176 RequestPrintWithDefaultMargins(); | |
| 177 EXPECT_EQ(1, browser()->tab_count()); | |
| 178 | |
| 179 // Checking that sticky settings were saved correctly. | |
| 180 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | |
| 181 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
| 182 printing::DEFAULT_MARGINS); | |
| 183 ASSERT_FALSE(PrintPreviewHandler::last_used_page_size_margins_); | |
| 184 } | |
| 185 | |
| 186 // Tests that margin settings are saved correctly when printing with custom | |
| 187 // margins selected and then again with default margins selected. | |
| 188 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustomThenDefault) { | |
| 189 const double margin_top = 125.5; | |
| 190 const double margin_right = 126.5; | |
| 191 const double margin_bottom = 127.5; | |
| 192 const double margin_left = 128.5; | |
| 193 RequestPrintWithCustomMargins( | |
| 194 margin_top, margin_right, margin_bottom, margin_left); | |
| 195 EXPECT_EQ(1, browser()->tab_count()); | |
| 196 DeletePrintPreviewTab(); | |
| 197 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
| 198 printing::CUSTOM_MARGINS); | |
| 199 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | |
| 200 CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); | |
| 201 | |
| 202 OpenPrintPreviewTab(); | |
| 203 EXPECT_EQ(2, browser()->tab_count()); | |
| 204 RequestPrintWithDefaultMargins(); | |
| 205 | |
| 206 // Checking that sticky settings were saved correctly. | |
| 207 EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); | |
| 208 EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, | |
| 209 printing::DEFAULT_MARGINS); | |
| 210 ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); | |
| 211 CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); | |
| 212 } | |
| 213 | |
| 214 // Tests that margin settings are retrieved correctly after printing with custom | |
| 215 // margins. | |
| 216 TEST_F(PrintPreviewHandlerTest, TestGetLastUsedMarginSettingsCustom) { | |
| 217 const double margin_top = 125.5; | |
| 218 const double margin_right = 126.5; | |
| 219 const double margin_bottom = 127.5; | |
| 220 const double margin_left = 128.5; | |
| 221 RequestPrintWithCustomMargins( | |
| 222 margin_top, margin_right, margin_bottom, margin_left); | |
| 223 base::DictionaryValue initial_settings; | |
| 224 preview_ui_->handler_->GetLastUsedMarginSettings(&initial_settings); | |
| 225 int margins_type; | |
| 226 EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, | |
| 227 &margins_type)); | |
| 228 EXPECT_EQ(margins_type, printing::CUSTOM_MARGINS); | |
| 229 double margin_value; | |
| 230 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginTop, | |
| 231 &margin_value)); | |
| 232 EXPECT_EQ(margin_top, margin_value); | |
| 233 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginRight, | |
| 234 &margin_value)); | |
| 235 EXPECT_EQ(margin_right, margin_value); | |
| 236 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginBottom, | |
| 237 &margin_value)); | |
| 238 EXPECT_EQ(margin_bottom, margin_value); | |
| 239 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginLeft, | |
| 240 &margin_value)); | |
| 241 EXPECT_EQ(margin_left, margin_value); | |
| 242 } | |
| 243 | |
| 244 // Tests that margin settings are retrieved correctly after printing with | |
| 245 // default margins. | |
| 246 TEST_F(PrintPreviewHandlerTest, TestGetLastUsedMarginSettingsDefault) { | |
| 247 RequestPrintWithDefaultMargins(); | |
| 248 base::DictionaryValue initial_settings; | |
| 249 preview_ui_->handler_->GetLastUsedMarginSettings(&initial_settings); | |
| 250 int margins_type; | |
| 251 EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, | |
| 252 &margins_type)); | |
| 253 EXPECT_EQ(margins_type, printing::DEFAULT_MARGINS); | |
| 254 double margin_value; | |
| 255 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, | |
| 256 &margin_value)); | |
| 257 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, | |
| 258 &margin_value)); | |
| 259 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, | |
| 260 &margin_value)); | |
| 261 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, | |
| 262 &margin_value)); | |
| 263 } | |
| OLD | NEW |