1 // 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.
5 #include "base/command_line.h"
6 #include "base/process/launch.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/test_switches.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/web_contents.h"
21 #include "net/base/filename_util.h"
23 // These tests don't apply to the Mac version; see GetCommandLineForRelaunch
25 #if !defined(OS_MACOSX)
27 class ChromeMainTest
: public InProcessBrowserTest
{
31 void Relaunch(const base::CommandLine
& new_command_line
) {
32 base::LaunchProcess(new_command_line
, base::LaunchOptionsForTest());
36 // Make sure that the second invocation creates a new window.
37 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, SecondLaunch
) {
38 #if defined(OS_WIN) && defined(USE_ASH)
39 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
40 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
41 switches::kAshBrowserTests
))
45 ui_test_utils::BrowserAddedObserver observer
;
46 Relaunch(GetCommandLineForRelaunch());
47 observer
.WaitForSingleNewBrowser();
48 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
49 browser()->host_desktop_type()));
52 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, ReuseBrowserInstanceWhenOpeningFile
) {
53 #if defined(OS_WIN) && defined(USE_ASH)
54 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
55 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
56 switches::kAshBrowserTests
))
60 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
61 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
62 base::CommandLine
new_command_line(GetCommandLineForRelaunch());
63 new_command_line
.AppendArgPath(test_file_path
);
64 content::WindowedNotificationObserver
observer(
65 chrome::NOTIFICATION_TAB_ADDED
,
66 content::NotificationService::AllSources());
67 Relaunch(new_command_line
);
70 GURL url
= net::FilePathToFileURL(test_file_path
);
71 content::WebContents
* tab
=
72 browser()->tab_strip_model()->GetActiveWebContents();
73 ASSERT_EQ(url
, tab
->GetController().GetActiveEntry()->GetVirtualURL());
76 // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux.
77 // http://crbug.com/130395
78 #if defined(OS_WIN) || defined(OS_LINUX)
79 #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl
81 #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl
84 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, MAYBE_SecondLaunchWithIncognitoUrl
) {
85 // We should start with one normal window.
86 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
87 browser()->host_desktop_type()));
89 // Run with --incognito switch and an URL specified.
90 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
91 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
92 base::CommandLine
new_command_line(GetCommandLineForRelaunch());
93 new_command_line
.AppendSwitch(switches::kIncognito
);
94 new_command_line
.AppendArgPath(test_file_path
);
96 Relaunch(new_command_line
);
98 // There should be one normal and one incognito window now.
99 ui_test_utils::BrowserAddedObserver observer
;
100 Relaunch(new_command_line
);
101 observer
.WaitForSingleNewBrowser();
102 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
104 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
105 browser()->host_desktop_type()));
108 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, SecondLaunchFromIncognitoWithNormalUrl
) {
109 #if defined(OS_WIN) && defined(USE_ASH)
110 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
111 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
112 switches::kAshBrowserTests
))
116 // We should start with one normal window.
117 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
118 browser()->host_desktop_type()));
120 // Create an incognito window.
121 chrome::NewIncognitoWindow(browser());
123 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
124 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
125 browser()->host_desktop_type()));
127 // Close the first window.
128 Profile
* profile
= browser()->profile();
129 chrome::HostDesktopType host_desktop_type
= browser()->host_desktop_type();
130 content::WindowedNotificationObserver
observer(
131 chrome::NOTIFICATION_BROWSER_CLOSED
,
132 content::NotificationService::AllSources());
133 chrome::CloseWindow(browser());
136 // There should only be the incognito window open now.
137 ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
138 ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile
, host_desktop_type
));
140 // Run with just an URL specified, no --incognito switch.
141 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
142 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
143 base::CommandLine
new_command_line(GetCommandLineForRelaunch());
144 new_command_line
.AppendArgPath(test_file_path
);
145 content::WindowedNotificationObserver
tab_observer(
146 chrome::NOTIFICATION_TAB_ADDED
,
147 content::NotificationService::AllSources());
148 Relaunch(new_command_line
);
151 // There should be one normal and one incognito window now.
152 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
153 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile
, host_desktop_type
));