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 "base/command_line.h" 6 #include "base/path_service.h" 7 #include "base/process/launch.h" 8 #include "chrome/browser/chrome_notification_types.h" 9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser_commands.h" 11 #include "chrome/browser/ui/browser_finder.h" 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" 13 #include "chrome/common/chrome_paths.h" 14 #include "chrome/common/chrome_switches.h" 15 #include "chrome/test/base/in_process_browser_test.h" 16 #include "chrome/test/base/test_switches.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 "net/base/filename_util.h" 23 24 // These tests don't apply to the Mac version; see GetCommandLineForRelaunch 25 // for details. 26 #if !defined(OS_MACOSX) 27 28 class ChromeMainTest : public InProcessBrowserTest { 29 public: 30 ChromeMainTest() {} 31 32 void Relaunch(const CommandLine& new_command_line) { 33 base::LaunchProcess(new_command_line, base::LaunchOptionsForTest(), NULL); 34 } 35 }; 36 37 // Make sure that the second invocation creates a new window. 38 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) { 39 #if defined(OS_WIN) && defined(USE_ASH) 40 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 41 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 42 return; 43 #endif 44 45 ui_test_utils::BrowserAddedObserver observer; 46 Relaunch(GetCommandLineForRelaunch()); 47 observer.WaitForSingleNewBrowser(); 48 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(), 49 browser()->host_desktop_type())); 50 } 51 52 IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) { 53 #if defined(OS_WIN) && defined(USE_ASH) 54 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 55 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 56 return; 57 #endif 58 59 base::FilePath test_file_path = ui_test_utils::GetTestFilePath( 60 base::FilePath(), base::FilePath().AppendASCII("empty.html")); 61 CommandLine new_command_line(GetCommandLineForRelaunch()); 62 new_command_line.AppendArgPath(test_file_path); 63 content::WindowedNotificationObserver observer( 64 chrome::NOTIFICATION_TAB_ADDED, 65 content::NotificationService::AllSources()); 66 Relaunch(new_command_line); 67 observer.Wait(); 68 69 GURL url = net::FilePathToFileURL(test_file_path); 70 content::WebContents* tab = 71 browser()->tab_strip_model()->GetActiveWebContents(); 72 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL()); 73 } 74 75 // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux. 76 // http://crbug.com/130395 77 #if defined(OS_WIN) || defined(OS_LINUX) 78 #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl 79 #else 80 #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl 81 #endif 82 83 IN_PROC_BROWSER_TEST_F(ChromeMainTest, MAYBE_SecondLaunchWithIncognitoUrl) { 84 // We should start with one normal window. 85 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), 86 browser()->host_desktop_type())); 87 88 // Run with --incognito switch and an URL specified. 89 base::FilePath test_file_path = ui_test_utils::GetTestFilePath( 90 base::FilePath(), base::FilePath().AppendASCII("empty.html")); 91 CommandLine new_command_line(GetCommandLineForRelaunch()); 92 new_command_line.AppendSwitch(switches::kIncognito); 93 new_command_line.AppendArgPath(test_file_path); 94 95 Relaunch(new_command_line); 96 97 // There should be one normal and one incognito window now. 98 ui_test_utils::BrowserAddedObserver observer; 99 Relaunch(new_command_line); 100 observer.WaitForSingleNewBrowser(); 101 ASSERT_EQ(2u, chrome::GetTotalBrowserCount()); 102 103 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), 104 browser()->host_desktop_type())); 105 } 106 107 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) { 108 #if defined(OS_WIN) && defined(USE_ASH) 109 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 110 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 111 return; 112 #endif 113 114 // We should start with one normal window. 115 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), 116 browser()->host_desktop_type())); 117 118 // Create an incognito window. 119 chrome::NewIncognitoWindow(browser()); 120 121 ASSERT_EQ(2u, chrome::GetTotalBrowserCount()); 122 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(), 123 browser()->host_desktop_type())); 124 125 // Close the first window. 126 Profile* profile = browser()->profile(); 127 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type(); 128 content::WindowedNotificationObserver observer( 129 chrome::NOTIFICATION_BROWSER_CLOSED, 130 content::NotificationService::AllSources()); 131 chrome::CloseWindow(browser()); 132 observer.Wait(); 133 134 // There should only be the incognito window open now. 135 ASSERT_EQ(1u, chrome::GetTotalBrowserCount()); 136 ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile, host_desktop_type)); 137 138 // Run with just an URL specified, no --incognito switch. 139 base::FilePath test_file_path = ui_test_utils::GetTestFilePath( 140 base::FilePath(), base::FilePath().AppendASCII("empty.html")); 141 CommandLine new_command_line(GetCommandLineForRelaunch()); 142 new_command_line.AppendArgPath(test_file_path); 143 content::WindowedNotificationObserver tab_observer( 144 chrome::NOTIFICATION_TAB_ADDED, 145 content::NotificationService::AllSources()); 146 Relaunch(new_command_line); 147 tab_observer.Wait(); 148 149 // There should be one normal and one incognito window now. 150 ASSERT_EQ(2u, chrome::GetTotalBrowserCount()); 151 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile, host_desktop_type)); 152 } 153 154 #endif // !OS_MACOSX 155