| OLD | NEW |
| 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 "base/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.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/printing/print_view_manager.h" | 11 #include "chrome/browser/printing/print_view_manager.h" |
| 12 #include "chrome/browser/ui/browser_commands.h" | 12 #include "chrome/browser/ui/browser_commands.h" |
| 13 #include "chrome/browser/ui/browser_tabstrip.h" | 13 #include "chrome/browser/ui/browser_tabstrip.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" | 15 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
| 16 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 16 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 17 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | |
| 18 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/test/base/browser_with_test_window_test.h" | 18 #include "chrome/test/base/browser_with_test_window_test.h" |
| 20 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 21 #include "printing/page_size_margins.h" | 20 #include "printing/page_size_margins.h" |
| 22 #include "printing/print_job_constants.h" | 21 #include "printing/print_job_constants.h" |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 DictionaryValue* GetCustomMarginsDictionary( | 25 DictionaryValue* GetCustomMarginsDictionary( |
| 27 const double margin_top, const double margin_right, | 26 const double margin_top, const double margin_right, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 50 | 49 |
| 51 profile()->GetPrefs()->SetBoolean(prefs::kPrintPreviewDisabled, false); | 50 profile()->GetPrefs()->SetBoolean(prefs::kPrintPreviewDisabled, false); |
| 52 | 51 |
| 53 chrome::NewTab(browser()); | 52 chrome::NewTab(browser()); |
| 54 EXPECT_EQ(1, browser()->tab_count()); | 53 EXPECT_EQ(1, browser()->tab_count()); |
| 55 OpenPrintPreviewTab(); | 54 OpenPrintPreviewTab(); |
| 56 } | 55 } |
| 57 | 56 |
| 58 virtual void TearDown() OVERRIDE { | 57 virtual void TearDown() OVERRIDE { |
| 59 DeletePrintPreviewTab(); | 58 DeletePrintPreviewTab(); |
| 60 ClearStickySettings(); | |
| 61 | 59 |
| 62 BrowserWithTestWindowTest::TearDown(); | 60 BrowserWithTestWindowTest::TearDown(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 void OpenPrintPreviewTab() { | 63 void OpenPrintPreviewTab() { |
| 66 TabContents* initiator_tab = chrome::GetActiveTabContents(browser()); | 64 TabContents* initiator_tab = chrome::GetActiveTabContents(browser()); |
| 67 ASSERT_TRUE(initiator_tab); | 65 ASSERT_TRUE(initiator_tab); |
| 68 | 66 |
| 69 printing::PrintPreviewTabController* controller = | 67 printing::PrintPreviewTabController* controller = |
| 70 printing::PrintPreviewTabController::GetInstance(); | 68 printing::PrintPreviewTabController::GetInstance(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 82 void DeletePrintPreviewTab() { | 80 void DeletePrintPreviewTab() { |
| 83 printing::BackgroundPrintingManager* bg_printing_manager = | 81 printing::BackgroundPrintingManager* bg_printing_manager = |
| 84 g_browser_process->background_printing_manager(); | 82 g_browser_process->background_printing_manager(); |
| 85 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); | 83 ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); |
| 86 | 84 |
| 87 // Deleting TabContents* to avoid warings from pref_notifier_impl.cc | 85 // Deleting TabContents* to avoid warings from pref_notifier_impl.cc |
| 88 // after the test ends. | 86 // after the test ends. |
| 89 delete preview_tab_; | 87 delete preview_tab_; |
| 90 } | 88 } |
| 91 | 89 |
| 92 void CheckCustomMargins(const double margin_top, | |
| 93 const double margin_right, | |
| 94 const double margin_bottom, | |
| 95 const double margin_left) { | |
| 96 printing::PageSizeMargins* margins = | |
| 97 PrintPreviewHandler::GetStickySettings()-> | |
| 98 page_size_margins_.get(); | |
| 99 EXPECT_EQ(margin_top, margins->margin_top); | |
| 100 EXPECT_EQ(margin_right, margins->margin_right); | |
| 101 EXPECT_EQ(margin_bottom, margins->margin_bottom); | |
| 102 EXPECT_EQ(margin_left, margins->margin_left); | |
| 103 } | |
| 104 | |
| 105 // Checking that sticky settings were saved according to expectations. | |
| 106 void CheckStickySettings(printing::ColorModels color_model, | |
| 107 printing::MarginType margin_type, | |
| 108 bool margins_saved, | |
| 109 const double margin_top, | |
| 110 const double margin_right, | |
| 111 const double margin_bottom, | |
| 112 const double margin_left) { | |
| 113 printing::StickySettings* sticky_settings = | |
| 114 PrintPreviewHandler::GetStickySettings(); | |
| 115 EXPECT_EQ(color_model, sticky_settings->color_model()); | |
| 116 EXPECT_EQ(margin_type, sticky_settings->margins_type_); | |
| 117 | |
| 118 if (margins_saved) { | |
| 119 ASSERT_TRUE(sticky_settings->page_size_margins_.get()); | |
| 120 CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); | |
| 121 } else { | |
| 122 ASSERT_FALSE(sticky_settings->page_size_margins_.get()); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void RequestPrintWithDefaultMargins() { | 90 void RequestPrintWithDefaultMargins() { |
| 127 // Set the minimal dummy settings to make the HandlePrint() code happy. | 91 // Set the minimal dummy settings to make the HandlePrint() code happy. |
| 128 DictionaryValue settings; | 92 DictionaryValue settings; |
| 129 settings.SetBoolean(printing::kSettingPreviewModifiable, true); | 93 settings.SetBoolean(printing::kSettingPreviewModifiable, true); |
| 130 settings.SetInteger(printing::kSettingColor, printing::COLOR); | 94 settings.SetInteger(printing::kSettingColor, printing::COLOR); |
| 131 settings.SetBoolean(printing::kSettingPrintToPDF, false); | 95 settings.SetBoolean(printing::kSettingPrintToPDF, false); |
| 132 settings.SetInteger(printing::kSettingMarginsType, | 96 settings.SetInteger(printing::kSettingMarginsType, |
| 133 printing::DEFAULT_MARGINS); | 97 printing::DEFAULT_MARGINS); |
| 134 | 98 |
| 135 // Put |settings| in to |args| as a JSON string. | 99 // Put |settings| in to |args| as a JSON string. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 161 std::string json_string; | 125 std::string json_string; |
| 162 base::JSONWriter::Write(&settings, &json_string); | 126 base::JSONWriter::Write(&settings, &json_string); |
| 163 ListValue args; | 127 ListValue args; |
| 164 args.Append(new base::StringValue(json_string)); // |args| takes ownership. | 128 args.Append(new base::StringValue(json_string)); // |args| takes ownership. |
| 165 preview_ui_->handler_->HandlePrint(&args); | 129 preview_ui_->handler_->HandlePrint(&args); |
| 166 } | 130 } |
| 167 | 131 |
| 168 PrintPreviewUI* preview_ui_; | 132 PrintPreviewUI* preview_ui_; |
| 169 | 133 |
| 170 private: | 134 private: |
| 171 void ClearStickySettings() { | |
| 172 PrintPreviewHandler::GetStickySettings()->margins_type_ = | |
| 173 printing::DEFAULT_MARGINS; | |
| 174 PrintPreviewHandler::GetStickySettings()->page_size_margins_.reset(); | |
| 175 } | |
| 176 | 135 |
| 177 TabContents* preview_tab_; | 136 TabContents* preview_tab_; |
| 178 }; | 137 }; |
| 179 | |
| 180 // Tests that margin settings are saved correctly when printing with custom | |
| 181 // margins selected. | |
| 182 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { | |
| 183 const double kMarginTop = 25.5; | |
| 184 const double kMarginRight = 26.5; | |
| 185 const double kMarginBottom = 27.5; | |
| 186 const double kMarginLeft = 28.5; | |
| 187 RequestPrintWithCustomMargins( | |
| 188 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | |
| 189 EXPECT_EQ(1, browser()->tab_count()); | |
| 190 | |
| 191 // Checking that sticky settings were saved correctly. | |
| 192 CheckStickySettings(printing::COLOR, printing::CUSTOM_MARGINS, true, | |
| 193 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | |
| 194 } | |
| 195 | |
| 196 // Tests that margin settings are saved correctly when printing with default | |
| 197 // margins selected. | |
| 198 TEST_F(PrintPreviewHandlerTest, StickyMarginsDefault) { | |
| 199 RequestPrintWithDefaultMargins(); | |
| 200 EXPECT_EQ(1, browser()->tab_count()); | |
| 201 | |
| 202 // Checking that sticky settings were saved correctly. | |
| 203 CheckStickySettings( | |
| 204 printing::COLOR, printing::DEFAULT_MARGINS, false, 0, 0, 0, 0); | |
| 205 } | |
| 206 | |
| 207 // Tests that margin settings are saved correctly when printing with custom | |
| 208 // margins selected and then again with default margins selected. | |
| 209 TEST_F(PrintPreviewHandlerTest, StickyMarginsCustomThenDefault) { | |
| 210 const double kMarginTop = 125.5; | |
| 211 const double kMarginRight = 126.5; | |
| 212 const double kMarginBottom = 127.5; | |
| 213 const double kMarginLeft = 128.5; | |
| 214 RequestPrintWithCustomMargins( | |
| 215 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | |
| 216 EXPECT_EQ(1, browser()->tab_count()); | |
| 217 DeletePrintPreviewTab(); | |
| 218 CheckStickySettings(printing::COLOR, printing::CUSTOM_MARGINS, true, | |
| 219 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | |
| 220 | |
| 221 OpenPrintPreviewTab(); | |
| 222 RequestPrintWithDefaultMargins(); | |
| 223 | |
| 224 // Checking that sticky settings were saved correctly. | |
| 225 CheckStickySettings(printing::COLOR, printing::DEFAULT_MARGINS, true, | |
| 226 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | |
| 227 } | |
| 228 | |
| 229 // Tests that margin settings are retrieved correctly after printing with custom | |
| 230 // margins. | |
| 231 TEST_F(PrintPreviewHandlerTest, GetLastUsedMarginSettingsCustom) { | |
| 232 const double kMarginTop = 125.5; | |
| 233 const double kMarginRight = 126.5; | |
| 234 const double kMarginBottom = 127.5; | |
| 235 const double kMarginLeft = 128.5; | |
| 236 RequestPrintWithCustomMargins( | |
| 237 kMarginTop, kMarginRight, kMarginBottom, kMarginLeft); | |
| 238 base::DictionaryValue initial_settings; | |
| 239 PrintPreviewHandler::GetStickySettings()-> | |
| 240 GetLastUsedMarginSettings(&initial_settings); | |
| 241 int margins_type; | |
| 242 EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, | |
| 243 &margins_type)); | |
| 244 EXPECT_EQ(margins_type, printing::CUSTOM_MARGINS); | |
| 245 double margin_value; | |
| 246 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginTop, | |
| 247 &margin_value)); | |
| 248 EXPECT_EQ(kMarginTop, margin_value); | |
| 249 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginRight, | |
| 250 &margin_value)); | |
| 251 EXPECT_EQ(kMarginRight, margin_value); | |
| 252 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginBottom, | |
| 253 &margin_value)); | |
| 254 EXPECT_EQ(kMarginBottom, margin_value); | |
| 255 EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginLeft, | |
| 256 &margin_value)); | |
| 257 EXPECT_EQ(kMarginLeft, margin_value); | |
| 258 } | |
| 259 | |
| 260 // Tests that margin settings are retrieved correctly after printing with | |
| 261 // default margins. | |
| 262 TEST_F(PrintPreviewHandlerTest, GetLastUsedMarginSettingsDefault) { | |
| 263 RequestPrintWithDefaultMargins(); | |
| 264 base::DictionaryValue initial_settings; | |
| 265 PrintPreviewHandler::GetStickySettings()-> | |
| 266 GetLastUsedMarginSettings(&initial_settings); | |
| 267 int margins_type; | |
| 268 EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, | |
| 269 &margins_type)); | |
| 270 EXPECT_EQ(margins_type, printing::DEFAULT_MARGINS); | |
| 271 double margin_value; | |
| 272 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, | |
| 273 &margin_value)); | |
| 274 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, | |
| 275 &margin_value)); | |
| 276 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, | |
| 277 &margin_value)); | |
| 278 EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, | |
| 279 &margin_value)); | |
| 280 } | |
| OLD | NEW |