From 1038ecb77198ad02d6e6f8c4fbe46584a31f32fd Mon Sep 17 00:00:00 2001 From: "gab@chromium.org" Date: Wed, 30 Jan 2013 20:57:17 +0000 Subject: Introduce BrowserIterator. An iterator meant to iterate over all the browsers in BrowserListImpls across multiple desktops (i.e. multiple HostDesktopTypes). As a first step, used it in TabContentsIterator (pretty much bringing back the implementation to the one that was there prior to https://codereview.chromium.org/11316340/ -- except that the iterator is now aware of multiple desktops). Another CL will follow in which all BrowserList::const_iterator, etc. methods will be replaced by BrowserIterator. The interface of BrowserIterator is derived from the interface of TabContentsIterator for consistency (which is being slightly tweaked in parallel in https://codereview.chromium.org/12049038/). BUG=129187 Review URL: https://chromiumcodereview.appspot.com/12038053 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179695 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/ui/browser_iterator_unittest.cc | 122 +++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 chrome/browser/ui/browser_iterator_unittest.cc (limited to 'chrome/browser/ui/browser_iterator_unittest.cc') diff --git a/chrome/browser/ui/browser_iterator_unittest.cc b/chrome/browser/ui/browser_iterator_unittest.cc new file mode 100644 index 0000000..33d3be2 --- /dev/null +++ b/chrome/browser/ui/browser_iterator_unittest.cc @@ -0,0 +1,122 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/browser_iterator.h" + +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_list_impl.h" +#include "chrome/browser/ui/host_desktop.h" +#include "chrome/test/base/browser_with_test_window_test.h" + +typedef BrowserWithTestWindowTest BrowserIteratorTest; + +namespace { + +// BrowserWithTestWindowTest's default window is on the native desktop by +// default. Force it to be on the Ash desktop to be able to test the iterator +// in a situation with no browsers on the native desktop. +class BrowserIteratorTestWithInitialWindowInAsh + : public BrowserWithTestWindowTest { + public: + BrowserIteratorTestWithInitialWindowInAsh() + : BrowserWithTestWindowTest(chrome::HOST_DESKTOP_TYPE_ASH) { + } +}; + +// Helper function to iterate and count all the browsers. +size_t CountAllBrowsers() { + size_t count = 0; + for (chrome::BrowserIterator iterator; !iterator.done(); iterator.Next()) + ++count; + return count; +} + +} + +TEST_F(BrowserIteratorTest, BrowsersOnMultipleDesktops) { + Browser::CreateParams native_params(profile(), + chrome::HOST_DESKTOP_TYPE_NATIVE); + Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH); + // Note, browser 1 is on the native desktop. + scoped_ptr browser2( + chrome::CreateBrowserWithTestWindowForParams(&native_params)); + scoped_ptr browser3( + chrome::CreateBrowserWithTestWindowForParams(&native_params)); + scoped_ptr browser4( + chrome::CreateBrowserWithTestWindowForParams(&ash_params)); + scoped_ptr browser5( + chrome::CreateBrowserWithTestWindowForParams(&ash_params)); + + // Sanity checks. + chrome::BrowserListImpl* native_list = + chrome::BrowserListImpl::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); +#if defined(OS_CHROMEOS) + // On Chrome OS, the native list and the ash list are the same. + EXPECT_EQ(5U, native_list->size()); +#else + chrome::BrowserListImpl* ash_list = + chrome::BrowserListImpl::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); + EXPECT_EQ(3U, native_list->size()); + EXPECT_EQ(2U, ash_list->size()); +#endif + + // Make sure the iterator finds all 5 browsers regardless of which desktop + // they are on. + EXPECT_EQ(5U, CountAllBrowsers()); +} + +TEST_F(BrowserIteratorTest, NoBrowsersOnAshDesktop) { + Browser::CreateParams native_params(profile(), + chrome::HOST_DESKTOP_TYPE_NATIVE); + // Note, browser 1 is on the native desktop. + scoped_ptr browser2( + chrome::CreateBrowserWithTestWindowForParams(&native_params)); + scoped_ptr browser3( + chrome::CreateBrowserWithTestWindowForParams(&native_params)); + + // Sanity checks. + chrome::BrowserListImpl* native_list = + chrome::BrowserListImpl::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); + EXPECT_EQ(3U, native_list->size()); +#if !defined(OS_CHROMEOS) + // On non-Chrome OS platforms the Ash list is independent from the native + // list, double-check that it's empty. + chrome::BrowserListImpl* ash_list = + chrome::BrowserListImpl::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); + EXPECT_EQ(0U, ash_list->size()); +#endif + + // Make sure the iterator finds all 3 browsers on the native desktop and + // doesn't trip over the empty Ash desktop list. + EXPECT_EQ(3U, CountAllBrowsers()); +} + +TEST_F(BrowserIteratorTestWithInitialWindowInAsh, NoBrowsersOnNativeDesktop) { + Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH); + // Note, browser 1 is on the ash desktop. + scoped_ptr browser2( + chrome::CreateBrowserWithTestWindowForParams(&ash_params)); + scoped_ptr browser3( + chrome::CreateBrowserWithTestWindowForParams(&ash_params)); + + chrome::BrowserListImpl* ash_list = + chrome::BrowserListImpl::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); + EXPECT_EQ(3U, ash_list->size()); +#if !defined(OS_CHROMEOS) + // On non-Chrome OS platforms the native list is independent from the Ash + // list; double-check that it's empty. + chrome::BrowserListImpl* native_list = + chrome::BrowserListImpl::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE); + EXPECT_EQ(0U, native_list->size()); +#endif + + // Make sure the iterator finds all 3 browsers on the ash desktop and + // doesn't trip over the empty native desktop list. + EXPECT_EQ(3U, CountAllBrowsers()); +} + +// Verify that the iterator still behaves if there are no browsers at all. +TEST(BrowserIteratorTestWithNoTestWindow, NoBrowser) { + EXPECT_EQ(0U, CountAllBrowsers()); +} -- cgit v1.1