browser_init_browsertest.cc revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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 "base/command_line.h" 6#include "base/file_path.h" 7#include "base/utf_string_conversions.h" 8#include "chrome/browser/browser.h" 9#include "chrome/browser/browser_init.h" 10#include "chrome/browser/browser_list.h" 11#include "chrome/browser/browser_window.h" 12#include "chrome/browser/tab_contents/tab_contents.h" 13#include "chrome/test/in_process_browser_test.h" 14#include "testing/gtest/include/gtest/gtest.h" 15 16namespace { 17 18class BrowserInitTest : public InProcessBrowserTest { 19}; 20 21class OpenURLsPopupObserver : public BrowserList::Observer { 22 public: 23 OpenURLsPopupObserver() : added_browser_(NULL) { } 24 25 virtual void OnBrowserAdded(const Browser* browser) { 26 added_browser_ = browser; 27 } 28 29 virtual void OnBrowserRemoved(const Browser* browser) { } 30 31 const Browser* added_browser_; 32}; 33 34// Test that when there is a popup as the active browser any requests to 35// BrowserInit::LaunchWithProfile::OpenURLsInBrowser don't crash because 36// there's no explicit profile given. 37IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenURLsPopup) { 38 std::vector<GURL> urls; 39 urls.push_back(GURL("http://localhost")); 40 41 // Note that in our testing we do not ever query the BrowserList for the "last 42 // active" browser. That's because the browsers are set as "active" by 43 // platform UI toolkit messages, and those messages are not sent during unit 44 // testing sessions. 45 46 OpenURLsPopupObserver observer; 47 BrowserList::AddObserver(&observer); 48 49 Browser* popup = Browser::CreateForType(Browser::TYPE_POPUP, 50 browser()->profile()); 51 ASSERT_EQ(popup->type(), Browser::TYPE_POPUP); 52 ASSERT_EQ(popup, observer.added_browser_); 53 54 CommandLine dummy(CommandLine::NO_PROGRAM); 55 BrowserInit::LaunchWithProfile launch(FilePath(), dummy); 56 // This should create a new window, but re-use the profile from |popup|. If 57 // it used a NULL or invalid profile, it would crash. 58 launch.OpenURLsInBrowser(popup, false, urls); 59 ASSERT_NE(popup, observer.added_browser_); 60 BrowserList::RemoveObserver(&observer); 61} 62 63} // namespace 64