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 CommandLine
& new_command_line
) {
32 base::LaunchProcess(new_command_line
, base::LaunchOptionsForTest(), NULL
);
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 (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
44 ui_test_utils::BrowserAddedObserver observer
;
45 Relaunch(GetCommandLineForRelaunch());
46 observer
.WaitForSingleNewBrowser();
47 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
48 browser()->host_desktop_type()));
51 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, ReuseBrowserInstanceWhenOpeningFile
) {
52 #if defined(OS_WIN) && defined(USE_ASH)
53 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
54 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
58 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
59 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
60 CommandLine
new_command_line(GetCommandLineForRelaunch());
61 new_command_line
.AppendArgPath(test_file_path
);
62 content::WindowedNotificationObserver
observer(
63 chrome::NOTIFICATION_TAB_ADDED
,
64 content::NotificationService::AllSources());
65 Relaunch(new_command_line
);
68 GURL url
= net::FilePathToFileURL(test_file_path
);
69 content::WebContents
* tab
=
70 browser()->tab_strip_model()->GetActiveWebContents();
71 ASSERT_EQ(url
, tab
->GetController().GetActiveEntry()->GetVirtualURL());
74 // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux.
75 // http://crbug.com/130395
76 #if defined(OS_WIN) || defined(OS_LINUX)
77 #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl
79 #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl
82 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, MAYBE_SecondLaunchWithIncognitoUrl
) {
83 // We should start with one normal window.
84 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
85 browser()->host_desktop_type()));
87 // Run with --incognito switch and an URL specified.
88 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
89 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
90 CommandLine
new_command_line(GetCommandLineForRelaunch());
91 new_command_line
.AppendSwitch(switches::kIncognito
);
92 new_command_line
.AppendArgPath(test_file_path
);
94 Relaunch(new_command_line
);
96 // There should be one normal and one incognito window now.
97 ui_test_utils::BrowserAddedObserver observer
;
98 Relaunch(new_command_line
);
99 observer
.WaitForSingleNewBrowser();
100 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
102 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
103 browser()->host_desktop_type()));
106 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, SecondLaunchFromIncognitoWithNormalUrl
) {
107 #if defined(OS_WIN) && defined(USE_ASH)
108 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
109 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
113 // We should start with one normal window.
114 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
115 browser()->host_desktop_type()));
117 // Create an incognito window.
118 chrome::NewIncognitoWindow(browser());
120 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
121 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
122 browser()->host_desktop_type()));
124 // Close the first window.
125 Profile
* profile
= browser()->profile();
126 chrome::HostDesktopType host_desktop_type
= browser()->host_desktop_type();
127 content::WindowedNotificationObserver
observer(
128 chrome::NOTIFICATION_BROWSER_CLOSED
,
129 content::NotificationService::AllSources());
130 chrome::CloseWindow(browser());
133 // There should only be the incognito window open now.
134 ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
135 ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile
, host_desktop_type
));
137 // Run with just an URL specified, no --incognito switch.
138 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
139 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
140 CommandLine
new_command_line(GetCommandLineForRelaunch());
141 new_command_line
.AppendArgPath(test_file_path
);
142 content::WindowedNotificationObserver
tab_observer(
143 chrome::NOTIFICATION_TAB_ADDED
,
144 content::NotificationService::AllSources());
145 Relaunch(new_command_line
);
148 // There should be one normal and one incognito window now.
149 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
150 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile
, host_desktop_type
));