1 // Copyright (c) 2013 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. 4 5 #include "chrome/browser/ui/browser_iterator.h" 6 7 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/browser_list.h" 9 #include "chrome/browser/ui/host_desktop.h" 10 #include "chrome/test/base/browser_with_test_window_test.h" 11 12 typedef BrowserWithTestWindowTest BrowserIteratorTest; 13 14 namespace { 15 16 // BrowserWithTestWindowTest's default window is on the native desktop by 17 // default. Force it to be on the Ash desktop to be able to test the iterator 18 // in a situation with no browsers on the native desktop. 19 class BrowserIteratorTestWithInitialWindowInAsh 20 : public BrowserWithTestWindowTest { 21 public: 22 BrowserIteratorTestWithInitialWindowInAsh() 23 : BrowserWithTestWindowTest(Browser::TYPE_TABBED, 24 chrome::HOST_DESKTOP_TYPE_ASH, 25 false) { 26 } 27 }; 28 29 // Helper function to iterate and count all the browsers. 30 size_t CountAllBrowsers() { 31 size_t count = 0; 32 for (chrome::BrowserIterator iterator; !iterator.done(); iterator.Next()) 33 ++count; 34 return count; 35 } 36 37 } 38 39 TEST_F(BrowserIteratorTest, BrowsersOnMultipleDesktops) { 40 Browser::CreateParams native_params(profile(), 41 chrome::HOST_DESKTOP_TYPE_NATIVE); 42 Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH); 43 // Note, browser 1 is on the native desktop. 44 scoped_ptr<Browser> browser2( 45 chrome::CreateBrowserWithTestWindowForParams(&native_params)); 46 scoped_ptr<Browser> browser3( 47 chrome::CreateBrowserWithTestWindowForParams(&native_params)); 48 scoped_ptr<Browser> browser4( 49 chrome::CreateBrowserWithTestWindowForParams(&ash_params)); 50 scoped_ptr<Browser> browser5( 51 chrome::CreateBrowserWithTestWindowForParams(&ash_params)); 52 53 // Sanity checks. 54 BrowserList* native_list = 55 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); 56 #if defined(OS_CHROMEOS) 57 // On Chrome OS, the native list and the ash list are the same. 58 EXPECT_EQ(5U, native_list->size()); 59 #else 60 BrowserList* ash_list = 61 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); 62 EXPECT_EQ(3U, native_list->size()); 63 EXPECT_EQ(2U, ash_list->size()); 64 #endif 65 66 // Make sure the iterator finds all 5 browsers regardless of which desktop 67 // they are on. 68 EXPECT_EQ(5U, CountAllBrowsers()); 69 } 70 71 TEST_F(BrowserIteratorTest, NoBrowsersOnAshDesktop) { 72 Browser::CreateParams native_params(profile(), 73 chrome::HOST_DESKTOP_TYPE_NATIVE); 74 // Note, browser 1 is on the native desktop. 75 scoped_ptr<Browser> browser2( 76 chrome::CreateBrowserWithTestWindowForParams(&native_params)); 77 scoped_ptr<Browser> browser3( 78 chrome::CreateBrowserWithTestWindowForParams(&native_params)); 79 80 // Sanity checks. 81 BrowserList* native_list = 82 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); 83 EXPECT_EQ(3U, native_list->size()); 84 #if !defined(OS_CHROMEOS) 85 // On non-Chrome OS platforms the Ash list is independent from the native 86 // list, double-check that it's empty. 87 BrowserList* ash_list = 88 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); 89 EXPECT_EQ(0U, ash_list->size()); 90 #endif 91 92 // Make sure the iterator finds all 3 browsers on the native desktop and 93 // doesn't trip over the empty Ash desktop list. 94 EXPECT_EQ(3U, CountAllBrowsers()); 95 } 96 97 TEST_F(BrowserIteratorTestWithInitialWindowInAsh, NoBrowsersOnNativeDesktop) { 98 Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH); 99 // Note, browser 1 is on the ash desktop. 100 scoped_ptr<Browser> browser2( 101 chrome::CreateBrowserWithTestWindowForParams(&ash_params)); 102 scoped_ptr<Browser> browser3( 103 chrome::CreateBrowserWithTestWindowForParams(&ash_params)); 104 105 BrowserList* ash_list = 106 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); 107 EXPECT_EQ(3U, ash_list->size()); 108 #if !defined(OS_CHROMEOS) 109 // On non-Chrome OS platforms the native list is independent from the Ash 110 // list; double-check that it's empty. 111 BrowserList* native_list = 112 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); 113 EXPECT_EQ(0U, native_list->size()); 114 #endif 115 116 // Make sure the iterator finds all 3 browsers on the ash desktop and 117 // doesn't trip over the empty native desktop list. 118 EXPECT_EQ(3U, CountAllBrowsers()); 119 } 120 121 // Verify that the iterator still behaves if there are no browsers at all. 122 TEST(BrowserIteratorTestWithNoTestWindow, NoBrowser) { 123 EXPECT_EQ(0U, CountAllBrowsers()); 124 } 125