Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_ui_unittest.cc
blob700d81f244e90c984273390ad22a916ac3065cf5
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/common/pref_names.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "components/web_modal/web_contents_modal_dialog_manager.h"
19 #include "content/public/browser/plugin_service.h"
20 #include "content/public/browser/site_instance.h"
21 #include "content/public/browser/web_contents.h"
22 #include "printing/print_job_constants.h"
24 using content::WebContents;
25 using web_modal::WebContentsModalDialogManager;
27 namespace {
29 base::RefCountedBytes* CreateTestData() {
30 const unsigned char blob1[] =
31 "12346102356120394751634516591348710478123649165419234519234512349134";
32 std::vector<unsigned char> preview_data(blob1, blob1 + sizeof(blob1));
33 return new base::RefCountedBytes(preview_data);
36 bool IsShowingWebContentsModalDialog(WebContents* tab) {
37 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
38 WebContentsModalDialogManager::FromWebContents(tab);
39 return web_contents_modal_dialog_manager->IsDialogActive();
42 } // namespace
44 class PrintPreviewUIUnitTest : public PrintPreviewTest {
45 public:
46 PrintPreviewUIUnitTest();
47 virtual ~PrintPreviewUIUnitTest();
49 protected:
50 virtual void SetUp() OVERRIDE;
52 DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIUnitTest);
55 PrintPreviewUIUnitTest::PrintPreviewUIUnitTest() {}
56 PrintPreviewUIUnitTest::~PrintPreviewUIUnitTest() {}
58 void PrintPreviewUIUnitTest::SetUp() {
59 PrintPreviewTest::SetUp();
61 chrome::NewTab(browser());
64 // Create/Get a preview tab for initiator.
65 TEST_F(PrintPreviewUIUnitTest, PrintPreviewData) {
66 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();
67 ASSERT_TRUE(initiator);
68 EXPECT_FALSE(IsShowingWebContentsModalDialog(initiator));
70 printing::PrintPreviewDialogController* controller =
71 printing::PrintPreviewDialogController::GetInstance();
72 ASSERT_TRUE(controller);
74 printing::PrintViewManager* print_view_manager =
75 printing::PrintViewManager::FromWebContents(initiator);
76 print_view_manager->PrintPreviewNow(false);
77 WebContents* preview_dialog = controller->GetOrCreatePreviewDialog(initiator);
79 EXPECT_NE(initiator, preview_dialog);
80 EXPECT_EQ(1, browser()->tab_strip_model()->count());
81 EXPECT_TRUE(IsShowingWebContentsModalDialog(initiator));
83 PrintPreviewUI* preview_ui = static_cast<PrintPreviewUI*>(
84 preview_dialog->GetWebUI()->GetController());
85 ASSERT_TRUE(preview_ui != NULL);
87 scoped_refptr<base::RefCountedBytes> data;
88 preview_ui->GetPrintPreviewDataForIndex(
89 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
90 &data);
91 EXPECT_EQ(NULL, data.get());
93 scoped_refptr<base::RefCountedBytes> dummy_data = CreateTestData();
95 preview_ui->SetPrintPreviewDataForIndex(
96 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
97 dummy_data.get());
98 preview_ui->GetPrintPreviewDataForIndex(
99 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
100 &data);
101 EXPECT_EQ(dummy_data->size(), data->size());
102 EXPECT_EQ(dummy_data.get(), data.get());
104 // This should not cause any memory leaks.
105 dummy_data = new base::RefCountedBytes();
106 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX,
107 dummy_data.get());
109 // Clear the preview data.
110 preview_ui->ClearAllPreviewData();
112 preview_ui->GetPrintPreviewDataForIndex(
113 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX,
114 &data);
115 EXPECT_EQ(NULL, data.get());
118 // Set and get the individual draft pages.
119 TEST_F(PrintPreviewUIUnitTest, PrintPreviewDraftPages) {
120 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();
121 ASSERT_TRUE(initiator);
123 printing::PrintPreviewDialogController* controller =
124 printing::PrintPreviewDialogController::GetInstance();
125 ASSERT_TRUE(controller);
127 printing::PrintViewManager* print_view_manager =
128 printing::PrintViewManager::FromWebContents(initiator);
129 print_view_manager->PrintPreviewNow(false);
130 WebContents* preview_dialog = controller->GetOrCreatePreviewDialog(initiator);
132 EXPECT_NE(initiator, preview_dialog);
133 EXPECT_EQ(1, browser()->tab_strip_model()->count());
134 EXPECT_TRUE(IsShowingWebContentsModalDialog(initiator));
136 PrintPreviewUI* preview_ui = static_cast<PrintPreviewUI*>(
137 preview_dialog->GetWebUI()->GetController());
138 ASSERT_TRUE(preview_ui != NULL);
140 scoped_refptr<base::RefCountedBytes> data;
141 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data);
142 EXPECT_EQ(NULL, data.get());
144 scoped_refptr<base::RefCountedBytes> dummy_data = CreateTestData();
146 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX,
147 dummy_data.get());
148 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data);
149 EXPECT_EQ(dummy_data->size(), data->size());
150 EXPECT_EQ(dummy_data.get(), data.get());
152 // Set and get the third page data.
153 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 2,
154 dummy_data.get());
155 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 2,
156 &data);
157 EXPECT_EQ(dummy_data->size(), data->size());
158 EXPECT_EQ(dummy_data.get(), data.get());
160 // Get the second page data.
161 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 1,
162 &data);
163 EXPECT_EQ(NULL, data.get());
165 preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 1,
166 dummy_data.get());
167 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX + 1,
168 &data);
169 EXPECT_EQ(dummy_data->size(), data->size());
170 EXPECT_EQ(dummy_data.get(), data.get());
172 // Clear the preview data.
173 preview_ui->ClearAllPreviewData();
174 preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data);
175 EXPECT_EQ(NULL, data.get());
178 // Test the browser-side print preview cancellation functionality.
179 TEST_F(PrintPreviewUIUnitTest, GetCurrentPrintPreviewStatus) {
180 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents();
181 ASSERT_TRUE(initiator);
183 printing::PrintPreviewDialogController* controller =
184 printing::PrintPreviewDialogController::GetInstance();
185 ASSERT_TRUE(controller);
187 printing::PrintViewManager* print_view_manager =
188 printing::PrintViewManager::FromWebContents(initiator);
189 print_view_manager->PrintPreviewNow(false);
190 WebContents* preview_dialog = controller->GetOrCreatePreviewDialog(initiator);
192 EXPECT_NE(initiator, preview_dialog);
193 EXPECT_EQ(1, browser()->tab_strip_model()->count());
194 EXPECT_TRUE(IsShowingWebContentsModalDialog(initiator));
196 PrintPreviewUI* preview_ui = static_cast<PrintPreviewUI*>(
197 preview_dialog->GetWebUI()->GetController());
198 ASSERT_TRUE(preview_ui != NULL);
200 // Test with invalid |preview_ui_addr|.
201 bool cancel = false;
202 const int32 kInvalidId = -5;
203 preview_ui->GetCurrentPrintPreviewStatus(kInvalidId, 0, &cancel);
204 EXPECT_TRUE(cancel);
206 const int kFirstRequestId = 1000;
207 const int kSecondRequestId = 1001;
208 const int32 preview_ui_addr = preview_ui->GetIDForPrintPreviewUI();
210 // Test with kFirstRequestId.
211 preview_ui->OnPrintPreviewRequest(kFirstRequestId);
212 cancel = true;
213 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kFirstRequestId,
214 &cancel);
215 EXPECT_FALSE(cancel);
217 cancel = false;
218 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kSecondRequestId,
219 &cancel);
220 EXPECT_TRUE(cancel);
222 // Test with kSecondRequestId.
223 preview_ui->OnPrintPreviewRequest(kSecondRequestId);
224 cancel = false;
225 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kFirstRequestId,
226 &cancel);
227 EXPECT_TRUE(cancel);
229 cancel = true;
230 preview_ui->GetCurrentPrintPreviewStatus(preview_ui_addr, kSecondRequestId,
231 &cancel);
232 EXPECT_FALSE(cancel);