Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_ui_unittest.cc
blob85b80f7031a484c8eac5dfd1f8065b18a9a9e554
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
3 // found in the LICENSE file.
5 #include <vector>
7 #include "base/memory/ref_counted_memory.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/printing/print_preview_dialog_controller.h"
10 #include "chrome/browser/printing/print_preview_test.h"
11 #include "chrome/browser/printing/print_view_manager.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/browser_tabstrip.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
16 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager.h"
18 #include "content/public/browser/plugin_service.h"
19 #include "content/public/browser/site_instance.h"
20 #include "content/public/browser/web_contents.h"
21 #include "printing/print_job_constants.h"
23 using content::WebContents;
24 using web_modal::WebContentsModalDialogManager;
26 namespace {
28 base::RefCountedBytes* CreateTestData() {
29 const unsigned char blob1[] =
30 "12346102356120394751634516591348710478123649165419234519234512349134";
31 std::vector<unsigned char> preview_data(blob1, blob1 + sizeof(blob1));
32 return new base::RefCountedBytes(preview_data);
35 bool IsShowingWebContentsModalDialog(WebContents* tab) {
36 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
37 WebContentsModalDialogManager::FromWebContents(tab);
38 return web_contents_modal_dialog_manager->IsDialogActive();
41 } // namespace
43 class PrintPreviewUIUnitTest : public PrintPreviewTest {
44 public:
45 PrintPreviewUIUnitTest();
46 ~PrintPreviewUIUnitTest() override;
48 protected:
49 void SetUp() override;
51 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIUnitTest);
54 PrintPreviewUIUnitTest::PrintPreviewUIUnitTest() {}
55 PrintPreviewUIUnitTest::~PrintPreviewUIUnitTest() {}
57 void PrintPreviewUIUnitTest::SetUp() {
58 PrintPreviewTest::SetUp();
60 chrome::NewTab(browser());
63 // Create/Get a preview tab for initiator.
64 TEST_F(PrintPreviewUIUnitTest, PrintPreviewData) {
65 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();
66 ASSERT_TRUE(initiator);
67 EXPECT_FALSE(IsShowingWebContentsModalDialog(initiator));
69 printing::PrintPreviewDialogController* controller =
70 printing::PrintPreviewDialogController::GetInstance();
71 ASSERT_TRUE(controller);
73 printing::PrintViewManager* print_view_manager =
74 printing::PrintViewManager::FromWebContents(initiator);
75 print_view_manager->PrintPreviewNow(false);
76 WebContents* preview_dialog = controller->GetOrCreatePreviewDialog(initiator);
78 EXPECT_NE(initiator, preview_dialog);
79 EXPECT_EQ(1, browser()->tab_strip_model()->count());
80 EXPECT_TRUE(IsShowingWebContentsModalDialog(initiator));
82 PrintPreviewUI* preview_ui = static_cast<PrintPreviewUI*>(
83 preview_dialog->GetWebUI()->GetController());
84 ASSERT_TRUE(preview_ui != NULL);
86 scoped_refptr<base::RefCountedBytes> data;
87 preview_ui->GetPrintPreviewDataForIndex(
88 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
89 &data);
90 EXPECT_EQ(NULL, data.get());
92 scoped_refptr<base::RefCountedBytes> dummy_data = CreateTestData();
94 preview_ui->SetPrintPreviewDataForIndex(
95 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
96 dummy_data.get());
97 preview_ui->GetPrintPreviewDataForIndex(
98 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
99 &data);
100 EXPECT_EQ(dummy_data->size(), data->size());
101 EXPECT_EQ(dummy_data.get(), data.get());
103 // This should not cause any memory leaks.
104 dummy_data = new base::RefCountedBytes();
105 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX,
106 dummy_data.get());
108 // Clear the preview data.
109 preview_ui->ClearAllPreviewData();
111 preview_ui->GetPrintPreviewDataForIndex(
112 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
113 &data);
114 EXPECT_EQ(NULL, data.get());
117 // Set and get the individual draft pages.
118 TEST_F(PrintPreviewUIUnitTest, PrintPreviewDraftPages) {
119 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();
120 ASSERT_TRUE(initiator);
122 printing::PrintPreviewDialogController* controller =
123 printing::PrintPreviewDialogController::GetInstance();
124 ASSERT_TRUE(controller);
126 printing::PrintViewManager* print_view_manager =
127 printing::PrintViewManager::FromWebContents(initiator);
128 print_view_manager->PrintPreviewNow(false);
129 WebContents* preview_dialog = controller->GetOrCreatePreviewDialog(initiator);
131 EXPECT_NE(initiator, preview_dialog);
132 EXPECT_EQ(1, browser()->tab_strip_model()->count());
133 EXPECT_TRUE(IsShowingWebContentsModalDialog(initiator));
135 PrintPreviewUI* preview_ui = static_cast<PrintPreviewUI*>(
136 preview_dialog->GetWebUI()->GetController());
137 ASSERT_TRUE(preview_ui != NULL);
139 scoped_refptr<base::RefCountedBytes> data;
140 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data);
141 EXPECT_EQ(NULL, data.get());
143 scoped_refptr<base::RefCountedBytes> dummy_data = CreateTestData();
145 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX,
146 dummy_data.get());
147 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data);
148 EXPECT_EQ(dummy_data->size(), data->size());
149 EXPECT_EQ(dummy_data.get(), data.get());
151 // Set and get the third page data.
152 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 2,
153 dummy_data.get());
154 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 2,
155 &data);
156 EXPECT_EQ(dummy_data->size(), data->size());
157 EXPECT_EQ(dummy_data.get(), data.get());
159 // Get the second page data.
160 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 1,
161 &data);
162 EXPECT_EQ(NULL, data.get());
164 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 1,
165 dummy_data.get());
166 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 1,
167 &data);
168 EXPECT_EQ(dummy_data->size(), data->size());
169 EXPECT_EQ(dummy_data.get(), data.get());
171 // Clear the preview data.
172 preview_ui->ClearAllPreviewData();
173 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data);
174 EXPECT_EQ(NULL, data.get());
177 // Test the browser-side print preview cancellation functionality.
178 TEST_F(PrintPreviewUIUnitTest, GetCurrentPrintPreviewStatus) {
179 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();
180 ASSERT_TRUE(initiator);
182 printing::PrintPreviewDialogController* controller =
183 printing::PrintPreviewDialogController::GetInstance();
184 ASSERT_TRUE(controller);
186 printing::PrintViewManager* print_view_manager =
187 printing::PrintViewManager::FromWebContents(initiator);
188 print_view_manager->PrintPreviewNow(false);
189 WebContents* preview_dialog = controller->GetOrCreatePreviewDialog(initiator);
191 EXPECT_NE(initiator, preview_dialog);
192 EXPECT_EQ(1, browser()->tab_strip_model()->count());
193 EXPECT_TRUE(IsShowingWebContentsModalDialog(initiator));
195 PrintPreviewUI* preview_ui = static_cast<PrintPreviewUI*>(
196 preview_dialog->GetWebUI()->GetController());
197 ASSERT_TRUE(preview_ui != NULL);
199 // Test with invalid |preview_ui_addr|.
200 bool cancel = false;
201 const int32 kInvalidId = -5;
202 preview_ui->GetCurrentPrintPreviewStatus(kInvalidId, 0, &cancel);
203 EXPECT_TRUE(cancel);
205 const int kFirstRequestId = 1000;
206 const int kSecondRequestId = 1001;
207 const int32 preview_ui_addr = preview_ui->GetIDForPrintPreviewUI();
209 // Test with kFirstRequestId.
210 preview_ui->OnPrintPreviewRequest(kFirstRequestId);
211 cancel = true;
212 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kFirstRequestId,
213 &cancel);
214 EXPECT_FALSE(cancel);
216 cancel = false;
217 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kSecondRequestId,
218 &cancel);
219 EXPECT_TRUE(cancel);
221 // Test with kSecondRequestId.
222 preview_ui->OnPrintPreviewRequest(kSecondRequestId);
223 cancel = false;
224 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kFirstRequestId,
225 &cancel);
226 EXPECT_TRUE(cancel);
228 cancel = true;
229 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kSecondRequestId,
230 &cancel);
231 EXPECT_FALSE(cancel);