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