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