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