12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_iterator.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_list.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/browser_with_test_window_test.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef BrowserWithTestWindowTest BrowserIteratorTest;
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// BrowserWithTestWindowTest's default window is on the native desktop by
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// default. Force it to be on the Ash desktop to be able to test the iterator
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// in a situation with no browsers on the native desktop.
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class BrowserIteratorTestWithInitialWindowInAsh
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : public BrowserWithTestWindowTest {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  BrowserIteratorTestWithInitialWindowInAsh()
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : BrowserWithTestWindowTest(Browser::TYPE_TABBED,
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  chrome::HOST_DESKTOP_TYPE_ASH,
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  false) {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Helper function to iterate and count all the browsers.
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)size_t CountAllBrowsers() {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t count = 0;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (chrome::BrowserIterator iterator; !iterator.done(); iterator.Next())
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++count;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return count;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserIteratorTest, BrowsersOnMultipleDesktops) {
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser::CreateParams native_params(profile(),
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      chrome::HOST_DESKTOP_TYPE_NATIVE);
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note, browser 1 is on the native desktop.
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser2(
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&native_params));
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser3(
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&native_params));
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser4(
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&ash_params));
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser5(
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&ash_params));
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sanity checks.
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserList* native_list =
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_CHROMEOS)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // On Chrome OS, the native list and the ash list are the same.
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(5U, native_list->size());
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserList* ash_list =
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3U, native_list->size());
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2U, ash_list->size());
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Make sure the iterator finds all 5 browsers regardless of which desktop
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // they are on.
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(5U, CountAllBrowsers());
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserIteratorTest, NoBrowsersOnAshDesktop) {
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser::CreateParams native_params(profile(),
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      chrome::HOST_DESKTOP_TYPE_NATIVE);
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note, browser 1 is on the native desktop.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser2(
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&native_params));
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser3(
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&native_params));
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sanity checks.
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserList* native_list =
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE);
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3U, native_list->size());
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if !defined(OS_CHROMEOS)
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // On non-Chrome OS platforms the Ash list is independent from the native
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // list, double-check that it's empty.
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserList* ash_list =
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0U, ash_list->size());
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Make sure the iterator finds all 3 browsers on the native desktop and
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // doesn't trip over the empty Ash desktop list.
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3U, CountAllBrowsers());
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(BrowserIteratorTestWithInitialWindowInAsh, NoBrowsersOnNativeDesktop) {
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH);
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note, browser 1 is on the ash desktop.
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser2(
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&ash_params));
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<Browser> browser3(
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::CreateBrowserWithTestWindowForParams(&ash_params));
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserList* ash_list =
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3U, ash_list->size());
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if !defined(OS_CHROMEOS)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // On non-Chrome OS platforms the native list is independent from the Ash
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // list; double-check that it's empty.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserList* native_list =
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE);
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0U, native_list->size());
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Make sure the iterator finds all 3 browsers on the ash desktop and
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // doesn't trip over the empty native desktop list.
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3U, CountAllBrowsers());
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Verify that the iterator still behaves if there are no browsers at all.
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST(BrowserIteratorTestWithNoTestWindow, NoBrowser) {
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0U, CountAllBrowsers());
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
125