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 "chrome/test/ui/ui_test.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/process/launch.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/test_switches.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"
24 #include "net/base/net_util.h"
26 // These tests don't apply to the Mac version; see GetCommandLineForRelaunch
28 #if !defined(OS_MACOSX)
30 class ChromeMainTest
: public InProcessBrowserTest
{
34 void Relaunch(const CommandLine
& new_command_line
) {
35 base::LaunchProcess(new_command_line
, base::LaunchOptions(), NULL
);
39 // Make sure that the second invocation creates a new window.
40 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, SecondLaunch
) {
41 #if defined(OS_WIN) && defined(USE_ASH)
42 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
43 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
47 ui_test_utils::BrowserAddedObserver observer
;
48 Relaunch(GetCommandLineForRelaunch());
49 observer
.WaitForSingleNewBrowser();
50 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
51 browser()->host_desktop_type()));
54 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, ReuseBrowserInstanceWhenOpeningFile
) {
55 #if defined(OS_WIN) && defined(USE_ASH)
56 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
57 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
61 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
62 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
63 CommandLine
new_command_line(GetCommandLineForRelaunch());
64 new_command_line
.AppendArgPath(test_file_path
);
65 content::WindowedNotificationObserver
observer(
66 chrome::NOTIFICATION_TAB_ADDED
,
67 content::NotificationService::AllSources());
68 Relaunch(new_command_line
);
71 GURL url
= net::FilePathToFileURL(test_file_path
);
72 content::WebContents
* tab
=
73 browser()->tab_strip_model()->GetActiveWebContents();
74 ASSERT_EQ(url
, tab
->GetController().GetActiveEntry()->GetVirtualURL());
77 // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux.
78 // http://crbug.com/130395
79 #if defined(OS_WIN) || defined(OS_LINUX)
80 #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl
82 #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl
85 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, MAYBE_SecondLaunchWithIncognitoUrl
) {
86 // We should start with one normal window.
87 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
88 browser()->host_desktop_type()));
90 // Run with --incognito switch and an URL specified.
91 base::FilePath test_file_path
= ui_test_utils::GetTestFilePath(
92 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
93 CommandLine
new_command_line(GetCommandLineForRelaunch());
94 new_command_line
.AppendSwitch(switches::kIncognito
);
95 new_command_line
.AppendArgPath(test_file_path
);
97 Relaunch(new_command_line
);
99 // There should be one normal and one incognito window now.
100 ui_test_utils::BrowserAddedObserver observer
;
101 Relaunch(new_command_line
);
102 observer
.WaitForSingleNewBrowser();
103 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
105 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
106 browser()->host_desktop_type()));
109 IN_PROC_BROWSER_TEST_F(ChromeMainTest
, SecondLaunchFromIncognitoWithNormalUrl
) {
110 #if defined(OS_WIN) && defined(USE_ASH)
111 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
112 if (CommandLine::ForCurrentProcess()->HasSwitch(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 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
));