| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ui/tab_contents/tab_contents_iterator.h" | 5 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/browser_shutdown.h" | 8 #include "chrome/browser/browser_shutdown.h" |
| 9 #include "chrome/browser/lifetime/application_lifetime.h" | 9 #include "chrome/browser/lifetime/application_lifetime.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/printing/background_printing_manager.h" | 11 #include "chrome/browser/printing/background_printing_manager.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/base/browser_with_test_window_test.h" | 16 #include "chrome/test/base/browser_with_test_window_test.h" |
| 17 #include "chrome/test/base/testing_browser_process.h" | 17 #include "chrome/test/base/testing_browser_process.h" |
| 18 #include "chrome/test/base/testing_pref_service.h" | 18 #include "chrome/test/base/testing_pref_service.h" |
| 19 #include "chrome/test/base/testing_profile_manager.h" | 19 #include "chrome/test/base/testing_profile_manager.h" |
| 20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/test/test_renderer_host.h" | 21 #include "content/public/test/test_renderer_host.h" |
| 22 | 22 |
| 23 typedef BrowserWithTestWindowTest BrowserListTest; | 23 typedef BrowserWithTestWindowTest BrowserListTest; |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // Helper function to iterate and count all the tabs. | 27 // Helper function to iterate and count all the tabs. |
| 28 size_t CountAllTabs() { | 28 size_t CountAllTabs() { |
| 29 size_t count = 0; | 29 size_t count = 0; |
| 30 for (TabContentsIterator iterator; !iterator.done(); ++iterator) | 30 for (TabContentsIterator iterator; !iterator.done(); ++iterator) |
| 31 ++count; | 31 ++count; |
| 32 return count; | 32 return count; |
| 33 } | 33 } |
| 34 | 34 |
| 35 // Helper function to navigate to the print preview page. | 35 // Helper function to navigate to the print preview page. |
| 36 void NavigateToPrintUrl(TabContentsWrapper* tab, int page_id) { | 36 void NavigateToPrintUrl(TabContents* tab, int page_id) { |
| 37 content::RenderViewHostTester::For( | 37 content::RenderViewHostTester::For( |
| 38 tab->web_contents()->GetRenderViewHost())->SendNavigate( | 38 tab->web_contents()->GetRenderViewHost())->SendNavigate( |
| 39 page_id, GURL(chrome::kChromeUIPrintURL)); | 39 page_id, GURL(chrome::kChromeUIPrintURL)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 TEST_F(BrowserListTest, TabContentsIteratorVerifyCount) { | 44 TEST_F(BrowserListTest, TabContentsIteratorVerifyCount) { |
| 45 // Make sure we have 1 window to start with. | 45 // Make sure we have 1 window to start with. |
| 46 EXPECT_EQ(1U, BrowserList::size()); | 46 EXPECT_EQ(1U, BrowserList::size()); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 184 |
| 185 EXPECT_EQ(4U, CountAllTabs()); | 185 EXPECT_EQ(4U, CountAllTabs()); |
| 186 | 186 |
| 187 TestingBrowserProcess* browser_process = | 187 TestingBrowserProcess* browser_process = |
| 188 static_cast<TestingBrowserProcess*>(g_browser_process); | 188 static_cast<TestingBrowserProcess*>(g_browser_process); |
| 189 printing::BackgroundPrintingManager* bg_print_manager = | 189 printing::BackgroundPrintingManager* bg_print_manager = |
| 190 browser_process->background_printing_manager(); | 190 browser_process->background_printing_manager(); |
| 191 | 191 |
| 192 // Grab a tab and give ownership to BackgroundPrintingManager. | 192 // Grab a tab and give ownership to BackgroundPrintingManager. |
| 193 TabContentsIterator tab_iterator; | 193 TabContentsIterator tab_iterator; |
| 194 TabContentsWrapper* tab = *tab_iterator; | 194 TabContents* tab = *tab_iterator; |
| 195 int page_id = 1; | 195 int page_id = 1; |
| 196 NavigateToPrintUrl(tab, page_id++); | 196 NavigateToPrintUrl(tab, page_id++); |
| 197 | 197 |
| 198 bg_print_manager->OwnPrintPreviewTab(tab); | 198 bg_print_manager->OwnPrintPreviewTab(tab); |
| 199 | 199 |
| 200 EXPECT_EQ(4U, CountAllTabs()); | 200 EXPECT_EQ(4U, CountAllTabs()); |
| 201 | 201 |
| 202 // Close remaining tabs. | 202 // Close remaining tabs. |
| 203 browser2->CloseAllTabs(); | 203 browser2->CloseAllTabs(); |
| 204 browser3->CloseAllTabs(); | 204 browser3->CloseAllTabs(); |
| 205 | 205 |
| 206 EXPECT_EQ(1U, CountAllTabs()); | 206 EXPECT_EQ(1U, CountAllTabs()); |
| 207 | 207 |
| 208 // Delete the last remaining tab. | 208 // Delete the last remaining tab. |
| 209 delete tab; | 209 delete tab; |
| 210 | 210 |
| 211 EXPECT_EQ(0U, CountAllTabs()); | 211 EXPECT_EQ(0U, CountAllTabs()); |
| 212 | 212 |
| 213 // Add some tabs. | 213 // Add some tabs. |
| 214 for (size_t i = 0; i < 3; ++i) { | 214 for (size_t i = 0; i < 3; ++i) { |
| 215 browser2->NewTab(); | 215 browser2->NewTab(); |
| 216 browser3->NewTab(); | 216 browser3->NewTab(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 EXPECT_EQ(6U, CountAllTabs()); | 219 EXPECT_EQ(6U, CountAllTabs()); |
| 220 | 220 |
| 221 // Tell BackgroundPrintingManager to take ownership of all tabs. | 221 // Tell BackgroundPrintingManager to take ownership of all tabs. |
| 222 // Save the tabs in |owned_tabs| because manipulating tabs in the middle of | 222 // Save the tabs in |owned_tabs| because manipulating tabs in the middle of |
| 223 // TabContentsIterator is a bad idea. | 223 // TabContentsIterator is a bad idea. |
| 224 std::vector<TabContentsWrapper*> owned_tabs; | 224 std::vector<TabContents*> owned_tabs; |
| 225 for (TabContentsIterator iterator; !iterator.done(); ++iterator) { | 225 for (TabContentsIterator iterator; !iterator.done(); ++iterator) { |
| 226 NavigateToPrintUrl(*iterator, page_id++); | 226 NavigateToPrintUrl(*iterator, page_id++); |
| 227 owned_tabs.push_back(*iterator); | 227 owned_tabs.push_back(*iterator); |
| 228 } | 228 } |
| 229 for (std::vector<TabContentsWrapper*>::iterator it = owned_tabs.begin(); | 229 for (std::vector<TabContents*>::iterator it = owned_tabs.begin(); |
| 230 it != owned_tabs.end(); ++it) { | 230 it != owned_tabs.end(); ++it) { |
| 231 bg_print_manager->OwnPrintPreviewTab(*it); | 231 bg_print_manager->OwnPrintPreviewTab(*it); |
| 232 } | 232 } |
| 233 | 233 |
| 234 EXPECT_EQ(6U, CountAllTabs()); | 234 EXPECT_EQ(6U, CountAllTabs()); |
| 235 | 235 |
| 236 // Delete all tabs to clean up. | 236 // Delete all tabs to clean up. |
| 237 for (std::vector<TabContentsWrapper*>::iterator it = owned_tabs.begin(); | 237 for (std::vector<TabContents*>::iterator it = owned_tabs.begin(); |
| 238 it != owned_tabs.end(); ++it) { | 238 it != owned_tabs.end(); ++it) { |
| 239 delete *it; | 239 delete *it; |
| 240 } | 240 } |
| 241 | 241 |
| 242 EXPECT_EQ(0U, CountAllTabs()); | 242 EXPECT_EQ(0U, CountAllTabs()); |
| 243 } | 243 } |
| 244 #endif | 244 #endif |
| 245 | 245 |
| 246 #if defined(OS_CHROMEOS) | 246 #if defined(OS_CHROMEOS) |
| 247 // Calling AttemptRestart on ChromeOS will exit the test. | 247 // Calling AttemptRestart on ChromeOS will exit the test. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 265 testing_browser_process->SetProfileManager(profile_manager); | 265 testing_browser_process->SetProfileManager(profile_manager); |
| 266 | 266 |
| 267 browser::AttemptRestart(); | 267 browser::AttemptRestart(); |
| 268 // Cancel the effects of us calling browser::AttemptRestart. Otherwise tests | 268 // Cancel the effects of us calling browser::AttemptRestart. Otherwise tests |
| 269 // ran after this one will fail. | 269 // ran after this one will fail. |
| 270 browser_shutdown::SetTryingToQuit(false); | 270 browser_shutdown::SetTryingToQuit(false); |
| 271 | 271 |
| 272 EXPECT_TRUE(testing_pref_service.GetBoolean(prefs::kWasRestarted)); | 272 EXPECT_TRUE(testing_pref_service.GetBoolean(prefs::kWasRestarted)); |
| 273 testing_browser_process->SetLocalState(NULL); | 273 testing_browser_process->SetLocalState(NULL); |
| 274 } | 274 } |
| OLD | NEW |