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" |
(...skipping 24 matching lines...) Expand all Loading... |
35 browser()->GetSelectedTabContentsWrapper(); | 35 browser()->GetSelectedTabContentsWrapper(); |
36 | 36 |
37 scoped_refptr<printing::PrintPreviewTabController> | 37 scoped_refptr<printing::PrintPreviewTabController> |
38 tab_controller(new printing::PrintPreviewTabController()); | 38 tab_controller(new printing::PrintPreviewTabController()); |
39 ASSERT_TRUE(tab_controller); | 39 ASSERT_TRUE(tab_controller); |
40 | 40 |
41 // Get the preview tab for initiator tab. | 41 // Get the preview tab for initiator tab. |
42 TabContentsWrapper* preview_tab = | 42 TabContentsWrapper* preview_tab = |
43 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 43 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
44 | 44 |
45 // New print preview tab is created. | 45 // New print preview tab is created. Current focus is on preview tab. |
46 EXPECT_EQ(1, browser()->tab_count()); | 46 EXPECT_EQ(2, browser()->tab_count()); |
47 EXPECT_NE(initiator_tab, preview_tab); | 47 EXPECT_NE(initiator_tab, preview_tab); |
48 | 48 |
49 // Get the print preview tab for initiator tab. | 49 // Get the print preview tab for initiator tab. |
50 TabContentsWrapper* new_preview_tab = | 50 TabContentsWrapper* new_preview_tab = |
51 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 51 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
52 | 52 |
53 // Preview tab already exists. Tab count remains the same. | 53 // Preview tab already exists. Tab count remains the same. |
54 EXPECT_EQ(1, browser()->tab_count()); | 54 EXPECT_EQ(2, browser()->tab_count()); |
55 | 55 |
56 // 1:1 relationship between initiator and preview tab. | 56 // 1:1 relationship between initiator and preview tab. |
57 EXPECT_EQ(new_preview_tab, preview_tab); | 57 EXPECT_EQ(new_preview_tab, preview_tab); |
58 } | 58 } |
59 | 59 |
| 60 // Test to verify the initiator tab title is stored in |PrintPreviewUI| after |
| 61 // preview tab reload. |
| 62 TEST_F(PrintPreviewTabControllerUnitTest, TitleAfterReload) { |
| 63 ASSERT_TRUE(browser()); |
| 64 BrowserList::SetLastActive(browser()); |
| 65 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 |
60 // To show multiple print preview tabs exist in the same browser for | 110 // To show multiple print preview tabs exist in the same browser for |
61 // different initiator tabs. If preview tab already exists for an initiator, it | 111 // different initiator tabs. If preview tab already exists for an initiator, it |
62 // gets focused. | 112 // gets focused. |
63 TEST_F(PrintPreviewTabControllerUnitTest, MultiplePreviewTabs) { | 113 TEST_F(PrintPreviewTabControllerUnitTest, MultiplePreviewTabs) { |
64 ASSERT_TRUE(browser()); | 114 ASSERT_TRUE(browser()); |
65 BrowserList::SetLastActive(browser()); | 115 BrowserList::SetLastActive(browser()); |
66 ASSERT_TRUE(BrowserList::GetLastActive()); | 116 ASSERT_TRUE(BrowserList::GetLastActive()); |
67 | 117 |
68 // Lets start with one window and two tabs. | 118 // Lets start with one window and two tabs. |
69 EXPECT_EQ(1u, BrowserList::size()); | 119 EXPECT_EQ(1u, BrowserList::size()); |
(...skipping 12 matching lines...) Expand all Loading... |
82 | 132 |
83 scoped_refptr<printing::PrintPreviewTabController> | 133 scoped_refptr<printing::PrintPreviewTabController> |
84 tab_controller(new printing::PrintPreviewTabController()); | 134 tab_controller(new printing::PrintPreviewTabController()); |
85 ASSERT_TRUE(tab_controller); | 135 ASSERT_TRUE(tab_controller); |
86 | 136 |
87 // Create preview tab for |tab_contents_1| | 137 // Create preview tab for |tab_contents_1| |
88 TabContentsWrapper* preview_tab_1 = | 138 TabContentsWrapper* preview_tab_1 = |
89 tab_controller->GetOrCreatePreviewTab(tab_contents_1); | 139 tab_controller->GetOrCreatePreviewTab(tab_contents_1); |
90 | 140 |
91 EXPECT_NE(tab_contents_1, preview_tab_1); | 141 EXPECT_NE(tab_contents_1, preview_tab_1); |
92 EXPECT_EQ(2, browser()->tab_count()); | 142 EXPECT_EQ(3, browser()->tab_count()); |
93 | 143 |
94 // Create preview tab for |tab_contents_2| | 144 // Create preview tab for |tab_contents_2| |
95 TabContentsWrapper* preview_tab_2 = | 145 TabContentsWrapper* preview_tab_2 = |
96 tab_controller->GetOrCreatePreviewTab(tab_contents_2); | 146 tab_controller->GetOrCreatePreviewTab(tab_contents_2); |
97 | 147 |
98 EXPECT_NE(tab_contents_2, preview_tab_2); | 148 EXPECT_NE(tab_contents_2, preview_tab_2); |
99 // 2 initiator tab and 2 preview tabs exist in the same browser. | 149 // 2 initiator tab and 2 preview tabs exist in the same browser. |
100 // The preview tabs are constrained in their respective initiator tabs. | 150 EXPECT_EQ(4, browser()->tab_count()); |
101 EXPECT_EQ(2, browser()->tab_count()); | |
102 | 151 |
103 TabStripModel* model = browser()->tabstrip_model(); | 152 TabStripModel* model = browser()->tabstrip_model(); |
104 ASSERT_TRUE(model); | 153 ASSERT_TRUE(model); |
105 | 154 |
106 int tab_1_index = model->GetIndexOfTabContents(tab_contents_1); | |
107 int tab_2_index = model->GetIndexOfTabContents(tab_contents_2); | |
108 int preview_tab_1_index = model->GetIndexOfTabContents(preview_tab_1); | 155 int preview_tab_1_index = model->GetIndexOfTabContents(preview_tab_1); |
109 int preview_tab_2_index = model->GetIndexOfTabContents(preview_tab_2); | 156 int preview_tab_2_index = model->GetIndexOfTabContents(preview_tab_2); |
110 | 157 |
111 EXPECT_EQ(-1, preview_tab_1_index); | 158 EXPECT_NE(-1, preview_tab_1_index); |
112 EXPECT_EQ(-1, preview_tab_2_index); | 159 EXPECT_NE(-1, preview_tab_2_index); |
113 EXPECT_EQ(tab_2_index, browser()->active_index()); | 160 // Current tab is |preview_tab_2|. |
| 161 EXPECT_EQ(preview_tab_2_index, browser()->active_index()); |
114 | 162 |
115 // When we get the preview tab for |tab_contents_1|, | 163 // When we get the preview tab for |tab_contents_1|, |
116 // |preview_tab_1| is activated and focused. | 164 // |preview_tab_1| is activated and focused. |
117 tab_controller->GetOrCreatePreviewTab(tab_contents_1); | 165 tab_controller->GetOrCreatePreviewTab(tab_contents_1); |
118 EXPECT_EQ(tab_1_index, browser()->active_index()); | 166 EXPECT_EQ(preview_tab_1_index, browser()->active_index()); |
119 } | 167 } |
120 | 168 |
121 // Clear the initiator tab details associated with preview tab. | 169 // Clear the initiator tab details associated with preview tab. |
122 TEST_F(PrintPreviewTabControllerUnitTest, ClearInitiatorTabDetails) { | 170 TEST_F(PrintPreviewTabControllerUnitTest, ClearInitiatorTabDetails) { |
123 ASSERT_TRUE(browser()); | 171 ASSERT_TRUE(browser()); |
124 BrowserList::SetLastActive(browser()); | 172 BrowserList::SetLastActive(browser()); |
125 ASSERT_TRUE(BrowserList::GetLastActive()); | 173 ASSERT_TRUE(BrowserList::GetLastActive()); |
126 | 174 |
127 // Lets start with one window with one tab. | 175 // Lets start with one window with one tab. |
128 EXPECT_EQ(1u, BrowserList::size()); | 176 EXPECT_EQ(1u, BrowserList::size()); |
129 EXPECT_EQ(0, browser()->tab_count()); | 177 EXPECT_EQ(0, browser()->tab_count()); |
130 browser()->NewTab(); | 178 browser()->NewTab(); |
131 EXPECT_EQ(1, browser()->tab_count()); | 179 EXPECT_EQ(1, browser()->tab_count()); |
132 | 180 |
133 // Create a reference to initiator tab contents. | 181 // Create a reference to initiator tab contents. |
134 TabContentsWrapper* initiator_tab = | 182 TabContentsWrapper* initiator_tab = |
135 browser()->GetSelectedTabContentsWrapper(); | 183 browser()->GetSelectedTabContentsWrapper(); |
136 | 184 |
137 scoped_refptr<printing::PrintPreviewTabController> | 185 scoped_refptr<printing::PrintPreviewTabController> |
138 tab_controller(new printing::PrintPreviewTabController()); | 186 tab_controller(new printing::PrintPreviewTabController()); |
139 ASSERT_TRUE(tab_controller); | 187 ASSERT_TRUE(tab_controller); |
140 | 188 |
141 // Get the preview tab for initiator tab. | 189 // Get the preview tab for initiator tab. |
142 TabContentsWrapper* preview_tab = | 190 TabContentsWrapper* preview_tab = |
143 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 191 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
144 | 192 |
145 // New print preview tab is created. Current focus is on preview tab. | 193 // New print preview tab is created. Current focus is on preview tab. |
146 EXPECT_EQ(1, browser()->tab_count()); | 194 EXPECT_EQ(2, browser()->tab_count()); |
147 EXPECT_NE(initiator_tab, preview_tab); | 195 EXPECT_NE(initiator_tab, preview_tab); |
148 | 196 |
149 // Clear the initiator tab details associated with the preview tab. | 197 // Clear the initiator tab details associated with the preview tab. |
150 tab_controller->EraseInitiatorTabInfo(preview_tab); | 198 tab_controller->EraseInitiatorTabInfo(preview_tab); |
151 | 199 |
152 // Get the print preview tab for initiator tab. | 200 // Get the print preview tab for initiator tab. |
153 TabContentsWrapper* new_preview_tab = | 201 TabContentsWrapper* new_preview_tab = |
154 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 202 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
155 | 203 |
156 // New preview tab is created. | 204 // New preview tab is created. |
157 EXPECT_EQ(1, browser()->tab_count()); | 205 EXPECT_EQ(3, browser()->tab_count()); |
158 EXPECT_NE(new_preview_tab, preview_tab); | 206 EXPECT_NE(new_preview_tab, preview_tab); |
159 } | 207 } |
OLD | NEW |