Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview_handler_unittest.cc |
| diff --git a/chrome/browser/ui/webui/print_preview_handler_unittest.cc b/chrome/browser/ui/webui/print_preview_handler_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a430e38344a4ce7f6686b3182084e8dc7fdc37b0 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/print_preview_handler_unittest.cc |
| @@ -0,0 +1,260 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/command_line.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/printing/background_printing_manager.h" |
| +#include "chrome/browser/printing/print_preview_tab_controller.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| +#include "chrome/browser/ui/webui/print_preview_handler.h" |
| +#include "chrome/browser/ui/webui/print_preview_ui.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/test/base/browser_with_test_window_test.h" |
| +#include "printing/page_size_margins.h" |
| +#include "printing/print_job_constants.h" |
| + |
| +//typedef BrowserWithTestWindowTest PrintPreviewHandlerTest; |
| + |
| +namespace { |
| + |
| +DictionaryValue* GetCustomMarginsDictionary( |
| + const double margin_top, const double margin_right, |
| + const double margin_bottom,const double margin_left) { |
| + base::DictionaryValue* custom_settings = new base::DictionaryValue(); |
| + custom_settings->SetDouble(printing::kSettingMarginTop, margin_top); |
| + custom_settings->SetDouble(printing::kSettingMarginRight, margin_right); |
| + custom_settings->SetDouble(printing::kSettingMarginBottom, margin_bottom); |
| + custom_settings->SetDouble(printing::kSettingMarginLeft, margin_left); |
| + return custom_settings; |
| +} |
| + |
| +} |
| + |
| +class PrintPreviewHandlerTest : public BrowserWithTestWindowTest { |
| + protected: |
| + void SetUp() { |
| + BrowserWithTestWindowTest::SetUp(); |
| +#if !defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS) |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnablePrintPreview); |
| +#endif |
| + ASSERT_TRUE(browser()); |
| + BrowserList::SetLastActive(browser()); |
| + ASSERT_TRUE(BrowserList::GetLastActive()); |
| + |
| + browser()->NewTab(); |
| + EXPECT_EQ(1, browser()->tab_count()); |
| + OpenPrintPreviewTab(); |
| + } |
| + |
| + virtual void TearDown() { |
| + DeletePrintPreviewTab(); |
| + ClearStickySettings(); |
| + } |
| + |
| + void OpenPrintPreviewTab() { |
| + TabContentsWrapper* initiator_tab = |
| + browser()->GetSelectedTabContentsWrapper(); |
| + ASSERT_TRUE(initiator_tab); |
| + |
| + scoped_refptr<printing::PrintPreviewTabController> |
| + controller(new printing::PrintPreviewTabController()); |
| + ASSERT_TRUE(controller.get()); |
| + |
| + preview_tab_ = controller->GetOrCreatePreviewTab(initiator_tab); |
| + ASSERT_TRUE(preview_tab_); |
| + EXPECT_EQ(2, browser()->tab_count()); |
| + |
| + preview_ui_ = static_cast<PrintPreviewUI*>(preview_tab_->web_ui()); |
| + ASSERT_TRUE(preview_ui_); |
| + } |
| + |
| + void DeletePrintPreviewTab() { |
| + printing::BackgroundPrintingManager* bg_printing_manager = |
| + g_browser_process->background_printing_manager(); |
| + ASSERT_TRUE(bg_printing_manager->HasPrintPreviewTab(preview_tab_)); |
| + |
| + // Deleting TabContentsWrapper* to avoid warings from pref_notifier_impl.cc |
| + // after the test ends. |
| + delete preview_tab_; |
| + } |
| + |
| + void CheckCustomMargins(const double margin_top, |
| + const double margin_right, |
| + const double margin_bottom, |
| + const double margin_left) { |
| + EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_top, |
| + margin_top); |
| + EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_right, |
| + margin_right); |
| + EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_bottom, |
| + margin_bottom); |
| + EXPECT_EQ(PrintPreviewHandler::last_used_page_size_margins_->margin_left, |
| + margin_left); |
| + } |
| + |
| + void RequestPrintWithDefaultMargins() { |
| + // Set the minimal dummy settings to make the HandlePrint() code happy. |
| + DictionaryValue settings; |
| + settings.SetBoolean(printing::kSettingPreviewModifiable, true); |
| + settings.SetInteger(printing::kSettingColor, printing::COLOR); |
| + settings.SetBoolean(printing::kSettingPrintToPDF, false); |
| + settings.SetInteger(printing::kSettingMarginsType, |
| + printing::DEFAULT_MARGINS); |
| + |
| + // Put |settings| in to |args| as a JSON string. |
| + std::string json_string; |
| + base::JSONWriter::Write(&settings, false, &json_string); |
| + ListValue args; |
| + args.Append(new base::StringValue(json_string)); // |args| takes ownership. |
| + preview_ui_->handler_->HandlePrint(&args); |
| + } |
| + |
| + void RequestPrintWithCustomMargins( |
| + const double margin_top, const double margin_right, |
| + const double margin_bottom,const double margin_left) { |
| + // Set the minimal dummy settings to make the HandlePrint() code happy. |
| + DictionaryValue settings; |
| + settings.SetBoolean(printing::kSettingPreviewModifiable, true); |
| + settings.SetInteger(printing::kSettingColor, printing::COLOR); |
| + settings.SetBoolean(printing::kSettingPrintToPDF, false); |
| + settings.SetInteger(printing::kSettingMarginsType, |
| + printing::CUSTOM_MARGINS); |
| + |
| + // Creating custom margins dictionary and nesting it in |settings|. |
| + DictionaryValue* custom_settings = GetCustomMarginsDictionary( |
| + margin_top, margin_right, margin_bottom, margin_left); |
| + // |settings| takes ownership. |
| + settings.Set(printing::kSettingMarginsCustom, custom_settings); |
| + |
| + // Put |settings| in to |args| as a JSON string. |
| + std::string json_string; |
| + base::JSONWriter::Write(&settings, false, &json_string); |
| + ListValue args; |
| + args.Append(new base::StringValue(json_string)); // |args| takes ownership. |
| + preview_ui_->handler_->HandlePrint(&args); |
| + } |
| + |
| + TabContentsWrapper* preview_tab_; |
| + PrintPreviewUI* preview_ui_; |
| + |
| + private: |
| + void ClearStickySettings() { |
| + PrintPreviewHandler::last_used_margins_type_ = printing::DEFAULT_MARGINS; |
| + PrintPreviewHandler::last_used_page_size_margins_ = NULL; |
|
dpapad
2011/11/14 16:31:55
Deleting this instead of just setting it to null g
|
| + //scoped_ptr<printing::PageSizeMargins> testing( |
| + // PrintPreviewHandler::last_used_page_size_margins_); |
| + } |
| + |
| +}; |
| + |
| +// Tests that margin settings are saved correctly when printing with custom |
| +// margins selected. |
| +TEST_F(PrintPreviewHandlerTest, StickyMarginsCustom) { |
| + const double margin_top = 25.5; |
| + const double margin_right = 26.5; |
| + const double margin_bottom = 27.5; |
| + const double margin_left = 28.5; |
| + RequestPrintWithCustomMargins( |
| + margin_top, margin_right, margin_bottom, margin_left); |
| + EXPECT_EQ(1, browser()->tab_count()); |
| + |
| + // Checking that sticky settings were saved correctly. |
| + EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); |
| + EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
| + printing::CUSTOM_MARGINS); |
| + ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); |
| + CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); |
| +} |
| + |
| +// Tests that margin settings are saved correctly when printing with default |
| +// margins selected. |
| +TEST_F(PrintPreviewHandlerTest, StickyMarginsDefault) { |
| + RequestPrintWithDefaultMargins(); |
| + EXPECT_EQ(1, browser()->tab_count()); |
| + |
| + // Checking that sticky settings were saved correctly. |
| + EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); |
| + EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
| + printing::DEFAULT_MARGINS); |
| + 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.
|
| +} |
| + |
| +// Tests that margin settings are saved correctly when printing with custom |
| +// margins selected and then again with default margins selected. |
| +TEST_F(PrintPreviewHandlerTest, StickyMarginsCustomThenDefault) { |
| + const double margin_top = 125.5; |
| + const double margin_right = 126.5; |
| + const double margin_bottom = 127.5; |
| + const double margin_left = 128.5; |
| + RequestPrintWithCustomMargins( |
| + margin_top, margin_right, margin_bottom, margin_left); |
| + EXPECT_EQ(1, browser()->tab_count()); |
| + DeletePrintPreviewTab(); |
| + |
|
vandebo (ex-Chrome)
2011/11/14 17:05:39
Check custom margins here too.
dpapad
2011/11/14 17:20:15
Done.
|
| + OpenPrintPreviewTab(); |
| + EXPECT_EQ(2, browser()->tab_count()); |
| + RequestPrintWithDefaultMargins(); |
| + |
| + // Checking that sticky settings were saved correctly. |
| + EXPECT_EQ(PrintPreviewHandler::last_used_color_model_, printing::COLOR); |
| + EXPECT_EQ(PrintPreviewHandler::last_used_margins_type_, |
| + printing::DEFAULT_MARGINS); |
| + ASSERT_TRUE(PrintPreviewHandler::last_used_page_size_margins_); |
| + CheckCustomMargins(margin_top, margin_right, margin_bottom, margin_left); |
| +} |
| + |
| +// Tests that margin settings are retrieved correctly after printing with custom |
| +// margins. |
| +TEST_F(PrintPreviewHandlerTest, TestGetLastUsedMarginSettingsCustom) { |
| + const double margin_top = 125.5; |
| + const double margin_right = 126.5; |
| + const double margin_bottom = 127.5; |
| + const double margin_left = 128.5; |
| + RequestPrintWithCustomMargins( |
| + margin_top, margin_right, margin_bottom, margin_left); |
| + base::DictionaryValue initial_settings; |
| + preview_ui_->handler_->GetLastUsedMarginSettings(&initial_settings); |
| + int margins_type; |
| + EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, |
| + &margins_type)); |
| + EXPECT_EQ(margins_type, printing::CUSTOM_MARGINS); |
| + double margin_value; |
| + EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginTop, |
| + &margin_value)); |
| + EXPECT_EQ(margin_top, margin_value); |
| + EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginRight, |
| + &margin_value)); |
| + EXPECT_EQ(margin_right, margin_value); |
| + EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginBottom, |
| + &margin_value)); |
| + EXPECT_EQ(margin_bottom, margin_value); |
| + EXPECT_TRUE(initial_settings.GetDouble(printing::kSettingMarginLeft, |
| + &margin_value)); |
| + EXPECT_EQ(margin_left, margin_value); |
| +} |
| + |
| +// Tests that margin settings are retrieved correctly after printing with |
| +// default margins. |
| +TEST_F(PrintPreviewHandlerTest, TestGetLastUsedMarginSettingsDefault) { |
| + RequestPrintWithDefaultMargins(); |
| + base::DictionaryValue initial_settings; |
| + preview_ui_->handler_->GetLastUsedMarginSettings(&initial_settings); |
| + int margins_type; |
| + EXPECT_TRUE(initial_settings.GetInteger(printing::kSettingMarginsType, |
| + &margins_type)); |
| + EXPECT_EQ(margins_type, printing::DEFAULT_MARGINS); |
| + double margin_value; |
| + EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginTop, |
| + &margin_value)); |
| + EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginRight, |
| + &margin_value)); |
| + EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginBottom, |
| + &margin_value)); |
| + EXPECT_FALSE(initial_settings.GetDouble(printing::kSettingMarginLeft, |
| + &margin_value)); |
| +} |