blob: c194e55f258489fa1dfb8516166ec7a44959462f [file] [log] [blame]
[email protected]4ad3b612012-04-03 16:03:071// Copyright (c) 2012 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 "chrome/test/ui/ui_test.h"
6
7#include "base/command_line.h"
8#include "base/file_util.h"
9#include "base/path_service.h"
10#include "base/process_util.h"
11#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1012#include "chrome/browser/ui/browser_commands.h"
[email protected]d8748142012-05-16 21:13:4313#include "chrome/browser/ui/browser_finder.h"
[email protected]47ae23372013-01-29 01:50:4814#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]4ad3b612012-04-03 16:03:0715#include "chrome/common/chrome_notification_types.h"
16#include "chrome/common/chrome_paths.h"
17#include "chrome/common/chrome_switches.h"
18#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/ui_test_utils.h"
20#include "content/public/browser/navigation_controller.h"
21#include "content/public/browser/navigation_entry.h"
22#include "content/public/browser/notification_service.h"
23#include "content/public/browser/web_contents.h"
[email protected]4ad3b612012-04-03 16:03:0724#include "net/base/net_util.h"
25
[email protected]c4340bc02012-04-25 02:49:3826// These tests don't apply to the Mac version; see GetCommandLineForRelaunch
27// for details.
[email protected]4ad3b612012-04-03 16:03:0728#if !defined(OS_MACOSX)
29
30class ChromeMainTest : public InProcessBrowserTest {
31 public:
[email protected]c4340bc02012-04-25 02:49:3832 ChromeMainTest() {}
33
34 void Relaunch(const CommandLine& new_command_line) {
35 base::LaunchProcess(new_command_line, base::LaunchOptions(), NULL);
[email protected]4ad3b612012-04-03 16:03:0736 }
[email protected]4ad3b612012-04-03 16:03:0737};
38
39// Make sure that the second invocation creates a new window.
40IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) {
41 ui_test_utils::BrowserAddedObserver observer;
[email protected]c4340bc02012-04-25 02:49:3842 Relaunch(GetCommandLineForRelaunch());
[email protected]4ad3b612012-04-03 16:03:0743 observer.WaitForSingleNewBrowser();
[email protected]c987a242013-02-28 01:17:4144 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
45 browser()->host_desktop_type()));
[email protected]4ad3b612012-04-03 16:03:0746}
47
48IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) {
[email protected]650b2d52013-02-10 03:41:4549 base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
50 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
[email protected]c4340bc02012-04-25 02:49:3851 CommandLine new_command_line(GetCommandLineForRelaunch());
52 new_command_line.AppendArgPath(test_file_path);
[email protected]a7fe9112012-07-20 02:34:4553 content::WindowedNotificationObserver observer(
[email protected]884033e2012-04-16 19:38:4254 chrome::NOTIFICATION_TAB_ADDED,
[email protected]4ad3b612012-04-03 16:03:0755 content::NotificationService::AllSources());
[email protected]c4340bc02012-04-25 02:49:3856 Relaunch(new_command_line);
[email protected]4ad3b612012-04-03 16:03:0757 observer.Wait();
58
59 GURL url = net::FilePathToFileURL(test_file_path);
[email protected]47ae23372013-01-29 01:50:4860 content::WebContents* tab =
61 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]4ad3b612012-04-03 16:03:0762 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL());
63}
64
[email protected]3e3ce572012-05-30 22:55:3165// ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux.
66// http://crbug.com/130395
67#if defined(OS_WIN) || defined(OS_LINUX)
68#define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl
69#else
70#define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl
71#endif
[email protected]4ad3b612012-04-03 16:03:0772
[email protected]3e3ce572012-05-30 22:55:3173IN_PROC_BROWSER_TEST_F(ChromeMainTest, MAYBE_SecondLaunchWithIncognitoUrl) {
[email protected]4ad3b612012-04-03 16:03:0774 // We should start with one normal window.
[email protected]c987a242013-02-28 01:17:4175 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
76 browser()->host_desktop_type()));
[email protected]4ad3b612012-04-03 16:03:0777
78 // Run with --incognito switch and an URL specified.
[email protected]650b2d52013-02-10 03:41:4579 base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
80 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
[email protected]c4340bc02012-04-25 02:49:3881 CommandLine new_command_line(GetCommandLineForRelaunch());
82 new_command_line.AppendSwitch(switches::kIncognito);
83 new_command_line.AppendArgPath(test_file_path);
[email protected]4ad3b612012-04-03 16:03:0784
[email protected]c4340bc02012-04-25 02:49:3885 Relaunch(new_command_line);
[email protected]4ad3b612012-04-03 16:03:0786
87 // There should be one normal and one incognito window now.
88 ui_test_utils::BrowserAddedObserver observer;
[email protected]c4340bc02012-04-25 02:49:3889 Relaunch(new_command_line);
[email protected]4ad3b612012-04-03 16:03:0790 observer.WaitForSingleNewBrowser();
[email protected]0665ebe2013-02-13 09:53:1991 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
[email protected]4ad3b612012-04-03 16:03:0792
[email protected]c987a242013-02-28 01:17:4193 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
94 browser()->host_desktop_type()));
[email protected]4ad3b612012-04-03 16:03:0795}
96
97IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) {
98 // We should start with one normal window.
[email protected]c987a242013-02-28 01:17:4199 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
100 browser()->host_desktop_type()));
[email protected]4ad3b612012-04-03 16:03:07101
102 // Create an incognito window.
[email protected]a37d4b02012-06-25 21:56:10103 chrome::NewIncognitoWindow(browser());
[email protected]4ad3b612012-04-03 16:03:07104
[email protected]0665ebe2013-02-13 09:53:19105 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
[email protected]c987a242013-02-28 01:17:41106 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
107 browser()->host_desktop_type()));
[email protected]4ad3b612012-04-03 16:03:07108
109 // Close the first window.
110 Profile* profile = browser()->profile();
[email protected]c987a242013-02-28 01:17:41111 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type();
[email protected]a7fe9112012-07-20 02:34:45112 content::WindowedNotificationObserver observer(
[email protected]4ad3b612012-04-03 16:03:07113 chrome::NOTIFICATION_BROWSER_CLOSED,
114 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10115 chrome::CloseWindow(browser());
[email protected]4ad3b612012-04-03 16:03:07116 observer.Wait();
117
118 // There should only be the incognito window open now.
[email protected]0665ebe2013-02-13 09:53:19119 ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
[email protected]c987a242013-02-28 01:17:41120 ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile, host_desktop_type));
[email protected]4ad3b612012-04-03 16:03:07121
122 // Run with just an URL specified, no --incognito switch.
[email protected]650b2d52013-02-10 03:41:45123 base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
124 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
[email protected]c4340bc02012-04-25 02:49:38125 CommandLine new_command_line(GetCommandLineForRelaunch());
126 new_command_line.AppendArgPath(test_file_path);
[email protected]a7fe9112012-07-20 02:34:45127 content::WindowedNotificationObserver tab_observer(
[email protected]884033e2012-04-16 19:38:42128 chrome::NOTIFICATION_TAB_ADDED,
[email protected]4ad3b612012-04-03 16:03:07129 content::NotificationService::AllSources());
[email protected]c4340bc02012-04-25 02:49:38130 Relaunch(new_command_line);
[email protected]4ad3b612012-04-03 16:03:07131 tab_observer.Wait();
132
133 // There should be one normal and one incognito window now.
[email protected]0665ebe2013-02-13 09:53:19134 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
[email protected]c987a242013-02-28 01:17:41135 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile, host_desktop_type));
[email protected]4ad3b612012-04-03 16:03:07136}
137
[email protected]4ad3b612012-04-03 16:03:07138#endif // !OS_MACOSX