OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/printing/print_preview_tab_controller.h" | 5 #include "chrome/browser/printing/print_preview_tab_controller.h" |
6 #include "chrome/browser/tabs/tab_strip_model.h" | 6 #include "chrome/browser/tabs/tab_strip_model.h" |
7 #include "chrome/browser/ui/browser_list.h" | 7 #include "chrome/browser/ui/browser_list.h" |
8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
9 #include "chrome/browser/ui/webui/print_preview_ui.h" | 9 #include "chrome/browser/ui/webui/print_preview_ui.h" |
10 #include "chrome/test/base/browser_with_test_window_test.h" | 10 #include "chrome/test/base/browser_with_test_window_test.h" |
11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
12 #include "content/browser/tab_contents/navigation_entry.h" | 12 #include "content/browser/tab_contents/navigation_entry.h" |
13 #include "content/browser/tab_contents/navigation_details.h" | 13 #include "content/browser/tab_contents/navigation_details.h" |
14 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
18 | 18 |
19 typedef BrowserWithTestWindowTest PrintPreviewTabControllerUnitTest; | 19 typedef BrowserWithTestWindowTest PrintPreviewTabControllerUnitTest; |
20 | 20 |
| 21 // Test crashs on TouchUI due to initiator tab's native view having no parent. |
| 22 // http://crbug.com/104284 |
| 23 #if defined(TOUCH_UI) |
| 24 #define MAYBE_GetOrCreatePreviewTab DISABLED_GetOrCreatePreviewTab |
| 25 #else |
| 26 #define MAYBE_GetOrCreatePreviewTab GetOrCreatePreviewTab |
| 27 #endif |
21 // Create/Get a preview tab for initiator tab. | 28 // Create/Get a preview tab for initiator tab. |
22 TEST_F(PrintPreviewTabControllerUnitTest, GetOrCreatePreviewTab) { | 29 TEST_F(PrintPreviewTabControllerUnitTest, MAYBE_GetOrCreatePreviewTab) { |
23 ASSERT_TRUE(browser()); | 30 ASSERT_TRUE(browser()); |
24 BrowserList::SetLastActive(browser()); | 31 BrowserList::SetLastActive(browser()); |
25 ASSERT_TRUE(BrowserList::GetLastActive()); | 32 ASSERT_TRUE(BrowserList::GetLastActive()); |
26 | 33 |
27 // Lets start with one window with one tab. | 34 // Lets start with one window with one tab. |
28 EXPECT_EQ(1u, BrowserList::size()); | 35 EXPECT_EQ(1u, BrowserList::size()); |
29 EXPECT_EQ(0, browser()->tab_count()); | 36 EXPECT_EQ(0, browser()->tab_count()); |
30 browser()->NewTab(); | 37 browser()->NewTab(); |
31 EXPECT_EQ(1, browser()->tab_count()); | 38 EXPECT_EQ(1, browser()->tab_count()); |
32 | 39 |
33 // Create a reference to initiator tab contents. | 40 // Create a reference to initiator tab contents. |
34 TabContentsWrapper* initiator_tab = | 41 TabContentsWrapper* initiator_tab = |
35 browser()->GetSelectedTabContentsWrapper(); | 42 browser()->GetSelectedTabContentsWrapper(); |
36 | 43 |
37 scoped_refptr<printing::PrintPreviewTabController> | 44 scoped_refptr<printing::PrintPreviewTabController> |
38 tab_controller(new printing::PrintPreviewTabController()); | 45 tab_controller(new printing::PrintPreviewTabController()); |
39 ASSERT_TRUE(tab_controller); | 46 ASSERT_TRUE(tab_controller); |
40 | 47 |
41 // Get the preview tab for initiator tab. | 48 // Get the preview tab for initiator tab. |
42 TabContentsWrapper* preview_tab = | 49 TabContentsWrapper* preview_tab = |
43 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 50 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
44 | 51 |
45 // New print preview tab is created. Current focus is on preview tab. | 52 // New print preview tab is created. |
46 EXPECT_EQ(2, browser()->tab_count()); | 53 EXPECT_EQ(1, browser()->tab_count()); |
47 EXPECT_NE(initiator_tab, preview_tab); | 54 EXPECT_NE(initiator_tab, preview_tab); |
48 | 55 |
49 // Get the print preview tab for initiator tab. | 56 // Get the print preview tab for initiator tab. |
50 TabContentsWrapper* new_preview_tab = | 57 TabContentsWrapper* new_preview_tab = |
51 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 58 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
52 | 59 |
53 // Preview tab already exists. Tab count remains the same. | 60 // Preview tab already exists. Tab count remains the same. |
54 EXPECT_EQ(2, browser()->tab_count()); | 61 EXPECT_EQ(1, browser()->tab_count()); |
55 | 62 |
56 // 1:1 relationship between initiator and preview tab. | 63 // 1:1 relationship between initiator and preview tab. |
57 EXPECT_EQ(new_preview_tab, preview_tab); | 64 EXPECT_EQ(new_preview_tab, preview_tab); |
58 } | 65 } |
59 | 66 |
60 // Test to verify the initiator tab title is stored in |PrintPreviewUI| after | 67 // http://crbug.com/104284 |
61 // preview tab reload. | 68 #if defined(TOUCH_UI) |
62 TEST_F(PrintPreviewTabControllerUnitTest, TitleAfterReload) { | 69 #define MAYBE_MultiplePreviewTabs DISABLED_MultiplePreviewTabs |
| 70 #else |
| 71 #define MAYBE_MultiplePreviewTabs MultiplePreviewTabs |
| 72 #endif |
| 73 // To show multiple print preview tabs exist in the same browser for |
| 74 // different initiator tabs. If preview tab already exists for an initiator, it |
| 75 // gets focused. |
| 76 TEST_F(PrintPreviewTabControllerUnitTest, MAYBE_MultiplePreviewTabs) { |
63 ASSERT_TRUE(browser()); | 77 ASSERT_TRUE(browser()); |
64 BrowserList::SetLastActive(browser()); | 78 BrowserList::SetLastActive(browser()); |
65 ASSERT_TRUE(BrowserList::GetLastActive()); | 79 ASSERT_TRUE(BrowserList::GetLastActive()); |
66 | |
67 // Lets start with one window with one tab. | |
68 EXPECT_EQ(1u, BrowserList::size()); | |
69 EXPECT_EQ(0, browser()->tab_count()); | |
70 browser()->NewTab(); | |
71 EXPECT_EQ(1, browser()->tab_count()); | |
72 | |
73 // Create a reference to initiator tab contents. | |
74 TabContentsWrapper* initiator_tab = | |
75 browser()->GetSelectedTabContentsWrapper(); | |
76 | |
77 scoped_refptr<printing::PrintPreviewTabController> | |
78 tab_controller(new printing::PrintPreviewTabController()); | |
79 ASSERT_TRUE(tab_controller); | |
80 | |
81 // Get the preview tab for initiator tab. | |
82 TabContentsWrapper* preview_tab = | |
83 tab_controller->GetOrCreatePreviewTab(initiator_tab); | |
84 | |
85 // New print preview tab is created. Current focus is on preview tab. | |
86 EXPECT_EQ(2, browser()->tab_count()); | |
87 EXPECT_NE(initiator_tab, preview_tab); | |
88 | |
89 // Set up a PrintPreviewUI for |preview_tab|. | |
90 PrintPreviewUI* preview_ui = new PrintPreviewUI(preview_tab->tab_contents()); | |
91 // RenderViewHostManager takes ownership of |preview_ui|. | |
92 preview_tab->tab_contents()->render_manager_for_testing()-> | |
93 SetWebUIPostCommit(preview_ui); | |
94 | |
95 // Simulate a reload event on |preview_tab|. | |
96 scoped_ptr<NavigationEntry> entry; | |
97 entry.reset(new NavigationEntry()); | |
98 entry->set_transition_type(content::PAGE_TRANSITION_RELOAD); | |
99 content::LoadCommittedDetails details; | |
100 details.type = content::NAVIGATION_TYPE_SAME_PAGE; | |
101 details.entry = entry.get(); | |
102 content::NotificationService::current()->Notify( | |
103 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
104 content::Source<NavigationController>(&preview_tab->controller()), | |
105 content::Details<content::LoadCommittedDetails>(&details)); | |
106 EXPECT_EQ(initiator_tab->tab_contents()->GetTitle(), | |
107 preview_ui->initiator_tab_title_); | |
108 } | |
109 | |
110 // To show multiple print preview tabs exist in the same browser for | |
111 // different initiator tabs. If preview tab already exists for an initiator, it | |
112 // gets focused. | |
113 TEST_F(PrintPreviewTabControllerUnitTest, MultiplePreviewTabs) { | |
114 ASSERT_TRUE(browser()); | |
115 BrowserList::SetLastActive(browser()); | |
116 ASSERT_TRUE(BrowserList::GetLastActive()); | |
117 | 80 |
118 // Lets start with one window and two tabs. | 81 // Lets start with one window and two tabs. |
119 EXPECT_EQ(1u, BrowserList::size()); | 82 EXPECT_EQ(1u, BrowserList::size()); |
120 EXPECT_EQ(0, browser()->tab_count()); | 83 EXPECT_EQ(0, browser()->tab_count()); |
121 | 84 |
122 browser()->NewTab(); | 85 browser()->NewTab(); |
123 TabContentsWrapper* tab_contents_1 = | 86 TabContentsWrapper* tab_contents_1 = |
124 browser()->GetSelectedTabContentsWrapper(); | 87 browser()->GetSelectedTabContentsWrapper(); |
125 ASSERT_TRUE(tab_contents_1); | 88 ASSERT_TRUE(tab_contents_1); |
126 | 89 |
127 browser()->NewTab(); | 90 browser()->NewTab(); |
128 TabContentsWrapper* tab_contents_2 = | 91 TabContentsWrapper* tab_contents_2 = |
129 browser()->GetSelectedTabContentsWrapper(); | 92 browser()->GetSelectedTabContentsWrapper(); |
130 ASSERT_TRUE(tab_contents_2); | 93 ASSERT_TRUE(tab_contents_2); |
131 EXPECT_EQ(2, browser()->tab_count()); | 94 EXPECT_EQ(2, browser()->tab_count()); |
132 | 95 |
133 scoped_refptr<printing::PrintPreviewTabController> | 96 scoped_refptr<printing::PrintPreviewTabController> |
134 tab_controller(new printing::PrintPreviewTabController()); | 97 tab_controller(new printing::PrintPreviewTabController()); |
135 ASSERT_TRUE(tab_controller); | 98 ASSERT_TRUE(tab_controller); |
136 | 99 |
137 // Create preview tab for |tab_contents_1| | 100 // Create preview tab for |tab_contents_1| |
138 TabContentsWrapper* preview_tab_1 = | 101 TabContentsWrapper* preview_tab_1 = |
139 tab_controller->GetOrCreatePreviewTab(tab_contents_1); | 102 tab_controller->GetOrCreatePreviewTab(tab_contents_1); |
140 | 103 |
141 EXPECT_NE(tab_contents_1, preview_tab_1); | 104 EXPECT_NE(tab_contents_1, preview_tab_1); |
142 EXPECT_EQ(3, browser()->tab_count()); | 105 EXPECT_EQ(2, browser()->tab_count()); |
143 | 106 |
144 // Create preview tab for |tab_contents_2| | 107 // Create preview tab for |tab_contents_2| |
145 TabContentsWrapper* preview_tab_2 = | 108 TabContentsWrapper* preview_tab_2 = |
146 tab_controller->GetOrCreatePreviewTab(tab_contents_2); | 109 tab_controller->GetOrCreatePreviewTab(tab_contents_2); |
147 | 110 |
148 EXPECT_NE(tab_contents_2, preview_tab_2); | 111 EXPECT_NE(tab_contents_2, preview_tab_2); |
149 // 2 initiator tab and 2 preview tabs exist in the same browser. | 112 // 2 initiator tab and 2 preview tabs exist in the same browser. |
150 EXPECT_EQ(4, browser()->tab_count()); | 113 // The preview tabs are constrained in their respective initiator tabs. |
| 114 EXPECT_EQ(2, browser()->tab_count()); |
151 | 115 |
152 TabStripModel* model = browser()->tabstrip_model(); | 116 TabStripModel* model = browser()->tabstrip_model(); |
153 ASSERT_TRUE(model); | 117 ASSERT_TRUE(model); |
154 | 118 |
| 119 int tab_1_index = model->GetIndexOfTabContents(tab_contents_1); |
| 120 int tab_2_index = model->GetIndexOfTabContents(tab_contents_2); |
155 int preview_tab_1_index = model->GetIndexOfTabContents(preview_tab_1); | 121 int preview_tab_1_index = model->GetIndexOfTabContents(preview_tab_1); |
156 int preview_tab_2_index = model->GetIndexOfTabContents(preview_tab_2); | 122 int preview_tab_2_index = model->GetIndexOfTabContents(preview_tab_2); |
157 | 123 |
158 EXPECT_NE(-1, preview_tab_1_index); | 124 EXPECT_EQ(-1, preview_tab_1_index); |
159 EXPECT_NE(-1, preview_tab_2_index); | 125 EXPECT_EQ(-1, preview_tab_2_index); |
160 // Current tab is |preview_tab_2|. | 126 EXPECT_EQ(tab_2_index, browser()->active_index()); |
161 EXPECT_EQ(preview_tab_2_index, browser()->active_index()); | |
162 | 127 |
163 // When we get the preview tab for |tab_contents_1|, | 128 // When we get the preview tab for |tab_contents_1|, |
164 // |preview_tab_1| is activated and focused. | 129 // |preview_tab_1| is activated and focused. |
165 tab_controller->GetOrCreatePreviewTab(tab_contents_1); | 130 tab_controller->GetOrCreatePreviewTab(tab_contents_1); |
166 EXPECT_EQ(preview_tab_1_index, browser()->active_index()); | 131 EXPECT_EQ(tab_1_index, browser()->active_index()); |
167 } | 132 } |
168 | 133 |
| 134 // http://crbug.com/104284 |
| 135 #if defined(TOUCH_UI) |
| 136 #define MAYBE_ClearInitiatorTabDetails DISABLED_ClearInitiatorTabDetails |
| 137 #else |
| 138 #define MAYBE_ClearInitiatorTabDetails ClearInitiatorTabDetails |
| 139 #endif |
169 // Clear the initiator tab details associated with preview tab. | 140 // Clear the initiator tab details associated with preview tab. |
170 TEST_F(PrintPreviewTabControllerUnitTest, ClearInitiatorTabDetails) { | 141 TEST_F(PrintPreviewTabControllerUnitTest, MAYBE_ClearInitiatorTabDetails) { |
171 ASSERT_TRUE(browser()); | 142 ASSERT_TRUE(browser()); |
172 BrowserList::SetLastActive(browser()); | 143 BrowserList::SetLastActive(browser()); |
173 ASSERT_TRUE(BrowserList::GetLastActive()); | 144 ASSERT_TRUE(BrowserList::GetLastActive()); |
174 | 145 |
175 // Lets start with one window with one tab. | 146 // Lets start with one window with one tab. |
176 EXPECT_EQ(1u, BrowserList::size()); | 147 EXPECT_EQ(1u, BrowserList::size()); |
177 EXPECT_EQ(0, browser()->tab_count()); | 148 EXPECT_EQ(0, browser()->tab_count()); |
178 browser()->NewTab(); | 149 browser()->NewTab(); |
179 EXPECT_EQ(1, browser()->tab_count()); | 150 EXPECT_EQ(1, browser()->tab_count()); |
180 | 151 |
181 // Create a reference to initiator tab contents. | 152 // Create a reference to initiator tab contents. |
182 TabContentsWrapper* initiator_tab = | 153 TabContentsWrapper* initiator_tab = |
183 browser()->GetSelectedTabContentsWrapper(); | 154 browser()->GetSelectedTabContentsWrapper(); |
184 | 155 |
185 scoped_refptr<printing::PrintPreviewTabController> | 156 scoped_refptr<printing::PrintPreviewTabController> |
186 tab_controller(new printing::PrintPreviewTabController()); | 157 tab_controller(new printing::PrintPreviewTabController()); |
187 ASSERT_TRUE(tab_controller); | 158 ASSERT_TRUE(tab_controller); |
188 | 159 |
189 // Get the preview tab for initiator tab. | 160 // Get the preview tab for initiator tab. |
190 TabContentsWrapper* preview_tab = | 161 TabContentsWrapper* preview_tab = |
191 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 162 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
192 | 163 |
193 // New print preview tab is created. Current focus is on preview tab. | 164 // New print preview tab is created. Current focus is on preview tab. |
194 EXPECT_EQ(2, browser()->tab_count()); | 165 EXPECT_EQ(1, browser()->tab_count()); |
195 EXPECT_NE(initiator_tab, preview_tab); | 166 EXPECT_NE(initiator_tab, preview_tab); |
196 | 167 |
197 // Clear the initiator tab details associated with the preview tab. | 168 // Clear the initiator tab details associated with the preview tab. |
198 tab_controller->EraseInitiatorTabInfo(preview_tab); | 169 tab_controller->EraseInitiatorTabInfo(preview_tab); |
199 | 170 |
200 // Get the print preview tab for initiator tab. | 171 // Get the print preview tab for initiator tab. |
201 TabContentsWrapper* new_preview_tab = | 172 TabContentsWrapper* new_preview_tab = |
202 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 173 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
203 | 174 |
204 // New preview tab is created. | 175 // New preview tab is created. |
205 EXPECT_EQ(3, browser()->tab_count()); | 176 EXPECT_EQ(1, browser()->tab_count()); |
206 EXPECT_NE(new_preview_tab, preview_tab); | 177 EXPECT_NE(new_preview_tab, preview_tab); |
207 } | 178 } |
OLD | NEW |