blob: b34080a994b0001863480079b2f53357f265a9db [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"
12#include "chrome/browser/ui/browser_list.h"
13#include "chrome/common/chrome_notification_types.h"
14#include "chrome/common/chrome_paths.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/test/base/in_process_browser_test.h"
17#include "chrome/test/base/ui_test_utils.h"
18#include "content/public/browser/navigation_controller.h"
19#include "content/public/browser/navigation_entry.h"
20#include "content/public/browser/notification_service.h"
21#include "content/public/browser/web_contents.h"
22#include "content/test/test_launcher.h"
23#include "net/base/net_util.h"
24
25// These tests don't apply to the Mac version; see
26// LaunchAnotherBrowserBlockUntilClosed for details.
27#if !defined(OS_MACOSX)
28
29class ChromeMainTest : public InProcessBrowserTest {
30 public:
31 ChromeMainTest()
32 : InProcessBrowserTest(),
33 new_command_line_(CommandLine::ForCurrentProcess()->GetProgram()) {
34 }
35
36 virtual void SetUpOnMainThread() OVERRIDE {
37 CommandLine::SwitchMap switches =
38 CommandLine::ForCurrentProcess()->GetSwitches();
39 switches.erase(switches::kUserDataDir);
40 switches.erase(test_launcher::kGTestFilterFlag);
41
42 for (CommandLine::SwitchMap::const_iterator iter = switches.begin();
43 iter != switches.end(); ++iter) {
44 new_command_line_.AppendSwitchNative((*iter).first, (*iter).second);
45 }
46
47 FilePath user_data_dir;
48 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
49 new_command_line_.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
50
51 new_command_line_.AppendSwitchASCII(
52 test_launcher::kGTestFilterFlag, test_launcher::kEmptyTestName);
53 }
54
55 void Relaunch() {
56 base::LaunchProcess(new_command_line_, base::LaunchOptions(), NULL);
57 }
58
59 protected:
60 CommandLine new_command_line_;
61};
62
63// Make sure that the second invocation creates a new window.
64IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) {
65 ui_test_utils::BrowserAddedObserver observer;
66 Relaunch();
67 observer.WaitForSingleNewBrowser();
68 ASSERT_EQ(BrowserList::GetBrowserCount(browser()->profile()), 2u);
69}
70
71IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) {
72 FilePath test_file_path = ui_test_utils::GetTestFilePath(
73 FilePath(), FilePath().AppendASCII("empty.html"));
74 new_command_line_.AppendArgPath(test_file_path);
75 ui_test_utils::WindowedNotificationObserver observer(
[email protected]884033e2012-04-16 19:38:4276 chrome::NOTIFICATION_TAB_ADDED,
[email protected]4ad3b612012-04-03 16:03:0777 content::NotificationService::AllSources());
78 Relaunch();
79 observer.Wait();
80
81 GURL url = net::FilePathToFileURL(test_file_path);
82 content::WebContents* tab = browser()->GetSelectedWebContents();
83 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL());
84}
85
86
87IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithIncognitoUrl) {
88 // We should start with one normal window.
89 ASSERT_EQ(1u,
90 BrowserList::GetBrowserCountForType(browser()->profile(), true));
91
92 // Run with --incognito switch and an URL specified.
93 FilePath test_file_path = ui_test_utils::GetTestFilePath(
94 FilePath(), FilePath().AppendASCII("empty.html"));
95 new_command_line_.AppendSwitch(switches::kIncognito);
96 new_command_line_.AppendArgPath(test_file_path);
97
98 Relaunch();
99
100 // There should be one normal and one incognito window now.
101 ui_test_utils::BrowserAddedObserver observer;
102 Relaunch();
103 observer.WaitForSingleNewBrowser();
104 ASSERT_EQ(2u, BrowserList::size());
105
106 ASSERT_EQ(1u,
107 BrowserList::GetBrowserCountForType(browser()->profile(), true));
108}
109
110IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) {
111 // We should start with one normal window.
112 ASSERT_EQ(1u,
113 BrowserList::GetBrowserCountForType(browser()->profile(), true));
114
115 // Create an incognito window.
116 browser()->NewIncognitoWindow();
117
118 ASSERT_EQ(2u, BrowserList::size());
119 ASSERT_EQ(1u,
120 BrowserList::GetBrowserCountForType(browser()->profile(), true));
121
122 // Close the first window.
123 Profile* profile = browser()->profile();
124 ui_test_utils::WindowedNotificationObserver observer(
125 chrome::NOTIFICATION_BROWSER_CLOSED,
126 content::NotificationService::AllSources());
127 browser()->CloseWindow();
128 observer.Wait();
129
130 // There should only be the incognito window open now.
131 ASSERT_EQ(1u, BrowserList::size());
132 ASSERT_EQ(0u, BrowserList::GetBrowserCountForType(profile, true));
133
134 // Run with just an URL specified, no --incognito switch.
135 FilePath test_file_path = ui_test_utils::GetTestFilePath(
136 FilePath(), FilePath().AppendASCII("empty.html"));
137 new_command_line_.AppendArgPath(test_file_path);
138 ui_test_utils::WindowedNotificationObserver tab_observer(
[email protected]884033e2012-04-16 19:38:42139 chrome::NOTIFICATION_TAB_ADDED,
[email protected]4ad3b612012-04-03 16:03:07140 content::NotificationService::AllSources());
141 Relaunch();
142 tab_observer.Wait();
143
144 // There should be one normal and one incognito window now.
145 ASSERT_EQ(2u, BrowserList::size());
146 ASSERT_EQ(1u, BrowserList::GetBrowserCountForType(profile, true));
147}
148
149#endif // !OS_MACOSX