summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/browser_iterator_unittest.cc
diff options
context:
space:
mode:
authorgab@chromium.org <gab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-30 20:57:17 +0000
committergab@chromium.org <gab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-30 20:57:17 +0000
commit1038ecb77198ad02d6e6f8c4fbe46584a31f32fd (patch)
treee1fb814b66987590d8dfdc19701d10b3e7aaec41 /chrome/browser/ui/browser_iterator_unittest.cc
parent172a3a8063c95204c3279e5a36dc0ecb4c1b50be (diff)
downloadchromium_src-1038ecb77198ad02d6e6f8c4fbe46584a31f32fd.zip
chromium_src-1038ecb77198ad02d6e6f8c4fbe46584a31f32fd.tar.gz
chromium_src-1038ecb77198ad02d6e6f8c4fbe46584a31f32fd.tar.bz2
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
Diffstat (limited to 'chrome/browser/ui/browser_iterator_unittest.cc')
-rw-r--r--chrome/browser/ui/browser_iterator_unittest.cc122
1 files changed, 122 insertions, 0 deletions
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<Browser> browser2(
+ chrome::CreateBrowserWithTestWindowForParams(&native_params));
+ scoped_ptr<Browser> browser3(
+ chrome::CreateBrowserWithTestWindowForParams(&native_params));
+ scoped_ptr<Browser> browser4(
+ chrome::CreateBrowserWithTestWindowForParams(&ash_params));
+ scoped_ptr<Browser> 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<Browser> browser2(
+ chrome::CreateBrowserWithTestWindowForParams(&native_params));
+ scoped_ptr<Browser> 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<Browser> browser2(
+ chrome::CreateBrowserWithTestWindowForParams(&ash_params));
+ scoped_ptr<Browser> 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());
+}