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