[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 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. |
| 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" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser_finder.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 14 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 15 | #include "chrome/common/chrome_notification_types.h" |
| 16 | #include "chrome/common/chrome_paths.h" |
| 17 | #include "chrome/common/chrome_switches.h" |
| 18 | #include "chrome/test/base/in_process_browser_test.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" |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 24 | #include "net/base/net_util.h" |
| 25 | |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 26 | // These tests don't apply to the Mac version; see GetCommandLineForRelaunch |
| 27 | // for details. |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 28 | #if !defined(OS_MACOSX) |
| 29 | |
| 30 | class ChromeMainTest : public InProcessBrowserTest { |
| 31 | public: |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 32 | ChromeMainTest() {} |
| 33 | |
| 34 | void Relaunch(const CommandLine& new_command_line) { |
| 35 | base::LaunchProcess(new_command_line, base::LaunchOptions(), NULL); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 36 | } |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // Make sure that the second invocation creates a new window. |
| 40 | IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) { |
| 41 | ui_test_utils::BrowserAddedObserver observer; |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 42 | Relaunch(GetCommandLineForRelaunch()); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 43 | observer.WaitForSingleNewBrowser(); |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 44 | ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(), |
| 45 | browser()->host_desktop_type())); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 49 | base::FilePath test_file_path = ui_test_utils::GetTestFilePath( |
| 50 | base::FilePath(), base::FilePath().AppendASCII("empty.html")); |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 51 | CommandLine new_command_line(GetCommandLineForRelaunch()); |
| 52 | new_command_line.AppendArgPath(test_file_path); |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 53 | content::WindowedNotificationObserver observer( |
[email protected] | 884033e | 2012-04-16 19:38:42 | [diff] [blame] | 54 | chrome::NOTIFICATION_TAB_ADDED, |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 55 | content::NotificationService::AllSources()); |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 56 | Relaunch(new_command_line); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 57 | observer.Wait(); |
| 58 | |
| 59 | GURL url = net::FilePathToFileURL(test_file_path); |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 60 | content::WebContents* tab = |
| 61 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 62 | ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL()); |
| 63 | } |
| 64 | |
[email protected] | 3e3ce57 | 2012-05-30 22:55:31 | [diff] [blame] | 65 | // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux. |
| 66 | // http://crbug.com/130395 |
| 67 | #if defined(OS_WIN) || defined(OS_LINUX) |
| 68 | #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl |
| 69 | #else |
| 70 | #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl |
| 71 | #endif |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 72 | |
[email protected] | 3e3ce57 | 2012-05-30 22:55:31 | [diff] [blame] | 73 | IN_PROC_BROWSER_TEST_F(ChromeMainTest, MAYBE_SecondLaunchWithIncognitoUrl) { |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 74 | // We should start with one normal window. |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 75 | ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), |
| 76 | browser()->host_desktop_type())); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 77 | |
| 78 | // Run with --incognito switch and an URL specified. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 79 | base::FilePath test_file_path = ui_test_utils::GetTestFilePath( |
| 80 | base::FilePath(), base::FilePath().AppendASCII("empty.html")); |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 81 | CommandLine new_command_line(GetCommandLineForRelaunch()); |
| 82 | new_command_line.AppendSwitch(switches::kIncognito); |
| 83 | new_command_line.AppendArgPath(test_file_path); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 84 | |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 85 | Relaunch(new_command_line); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 86 | |
| 87 | // There should be one normal and one incognito window now. |
| 88 | ui_test_utils::BrowserAddedObserver observer; |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 89 | Relaunch(new_command_line); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 90 | observer.WaitForSingleNewBrowser(); |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 91 | ASSERT_EQ(2u, chrome::GetTotalBrowserCount()); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 92 | |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 93 | ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), |
| 94 | browser()->host_desktop_type())); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) { |
| 98 | // We should start with one normal window. |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 99 | ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), |
| 100 | browser()->host_desktop_type())); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 101 | |
| 102 | // Create an incognito window. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 103 | chrome::NewIncognitoWindow(browser()); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 104 | |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 105 | ASSERT_EQ(2u, chrome::GetTotalBrowserCount()); |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 106 | ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), |
| 107 | browser()->host_desktop_type())); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 108 | |
| 109 | // Close the first window. |
| 110 | Profile* profile = browser()->profile(); |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 111 | chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type(); |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 112 | content::WindowedNotificationObserver observer( |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 113 | chrome::NOTIFICATION_BROWSER_CLOSED, |
| 114 | content::NotificationService::AllSources()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 115 | chrome::CloseWindow(browser()); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 116 | observer.Wait(); |
| 117 | |
| 118 | // There should only be the incognito window open now. |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 119 | ASSERT_EQ(1u, chrome::GetTotalBrowserCount()); |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 120 | ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile, host_desktop_type)); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 121 | |
| 122 | // Run with just an URL specified, no --incognito switch. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 123 | base::FilePath test_file_path = ui_test_utils::GetTestFilePath( |
| 124 | base::FilePath(), base::FilePath().AppendASCII("empty.html")); |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 125 | CommandLine new_command_line(GetCommandLineForRelaunch()); |
| 126 | new_command_line.AppendArgPath(test_file_path); |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 127 | content::WindowedNotificationObserver tab_observer( |
[email protected] | 884033e | 2012-04-16 19:38:42 | [diff] [blame] | 128 | chrome::NOTIFICATION_TAB_ADDED, |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 129 | content::NotificationService::AllSources()); |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 130 | Relaunch(new_command_line); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 131 | tab_observer.Wait(); |
| 132 | |
| 133 | // There should be one normal and one incognito window now. |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 134 | ASSERT_EQ(2u, chrome::GetTotalBrowserCount()); |
[email protected] | c987a24 | 2013-02-28 01:17:41 | [diff] [blame] | 135 | ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile, host_desktop_type)); |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 4ad3b61 | 2012-04-03 16:03:07 | [diff] [blame] | 138 | #endif // !OS_MACOSX |