| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/ui/ui_test.h" | 5 #include "chrome/test/ui/ui_test.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
| 11 #include "base/test/test_suite.h" | |
| 12 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_list.h" | 12 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
| 20 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
| 21 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 23 #include "content/test/test_launcher.h" | |
| 24 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 25 | 23 |
| 26 // These tests don't apply to the Mac version; see | 24 // These tests don't apply to the Mac version; see GetCommandLineForRelaunch |
| 27 // LaunchAnotherBrowserBlockUntilClosed for details. | 25 // for details. |
| 28 #if !defined(OS_MACOSX) | 26 #if !defined(OS_MACOSX) |
| 29 | 27 |
| 30 class ChromeMainTest : public InProcessBrowserTest { | 28 class ChromeMainTest : public InProcessBrowserTest { |
| 31 public: | 29 public: |
| 32 ChromeMainTest() | 30 ChromeMainTest() {} |
| 33 : InProcessBrowserTest(), | 31 |
| 34 new_command_line_(CommandLine::ForCurrentProcess()->GetProgram()) { | 32 void Relaunch(const CommandLine& new_command_line) { |
| 33 base::LaunchProcess(new_command_line, base::LaunchOptions(), NULL); |
| 35 } | 34 } |
| 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); | |
| 54 new_command_line_.AppendSwitch(TestSuite::kSilent); | |
| 55 } | |
| 56 | |
| 57 void Relaunch() { | |
| 58 base::LaunchProcess(new_command_line_, base::LaunchOptions(), NULL); | |
| 59 } | |
| 60 | |
| 61 protected: | |
| 62 CommandLine new_command_line_; | |
| 63 }; | 35 }; |
| 64 | 36 |
| 65 // Make sure that the second invocation creates a new window. | 37 // Make sure that the second invocation creates a new window. |
| 66 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) { | 38 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) { |
| 67 ui_test_utils::BrowserAddedObserver observer; | 39 ui_test_utils::BrowserAddedObserver observer; |
| 68 Relaunch(); | 40 Relaunch(GetCommandLineForRelaunch()); |
| 69 observer.WaitForSingleNewBrowser(); | 41 observer.WaitForSingleNewBrowser(); |
| 70 ASSERT_EQ(BrowserList::GetBrowserCount(browser()->profile()), 2u); | 42 ASSERT_EQ(BrowserList::GetBrowserCount(browser()->profile()), 2u); |
| 71 } | 43 } |
| 72 | 44 |
| 73 IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) { | 45 IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) { |
| 74 FilePath test_file_path = ui_test_utils::GetTestFilePath( | 46 FilePath test_file_path = ui_test_utils::GetTestFilePath( |
| 75 FilePath(), FilePath().AppendASCII("empty.html")); | 47 FilePath(), FilePath().AppendASCII("empty.html")); |
| 76 new_command_line_.AppendArgPath(test_file_path); | 48 CommandLine new_command_line(GetCommandLineForRelaunch()); |
| 49 new_command_line.AppendArgPath(test_file_path); |
| 77 ui_test_utils::WindowedNotificationObserver observer( | 50 ui_test_utils::WindowedNotificationObserver observer( |
| 78 chrome::NOTIFICATION_TAB_ADDED, | 51 chrome::NOTIFICATION_TAB_ADDED, |
| 79 content::NotificationService::AllSources()); | 52 content::NotificationService::AllSources()); |
| 80 Relaunch(); | 53 Relaunch(new_command_line); |
| 81 observer.Wait(); | 54 observer.Wait(); |
| 82 | 55 |
| 83 GURL url = net::FilePathToFileURL(test_file_path); | 56 GURL url = net::FilePathToFileURL(test_file_path); |
| 84 content::WebContents* tab = browser()->GetSelectedWebContents(); | 57 content::WebContents* tab = browser()->GetSelectedWebContents(); |
| 85 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL()); | 58 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL()); |
| 86 } | 59 } |
| 87 | 60 |
| 88 | 61 |
| 89 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithIncognitoUrl) { | 62 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchWithIncognitoUrl) { |
| 90 // We should start with one normal window. | 63 // We should start with one normal window. |
| 91 ASSERT_EQ(1u, | 64 ASSERT_EQ(1u, |
| 92 BrowserList::GetBrowserCountForType(browser()->profile(), true)); | 65 BrowserList::GetBrowserCountForType(browser()->profile(), true)); |
| 93 | 66 |
| 94 // Run with --incognito switch and an URL specified. | 67 // Run with --incognito switch and an URL specified. |
| 95 FilePath test_file_path = ui_test_utils::GetTestFilePath( | 68 FilePath test_file_path = ui_test_utils::GetTestFilePath( |
| 96 FilePath(), FilePath().AppendASCII("empty.html")); | 69 FilePath(), FilePath().AppendASCII("empty.html")); |
| 97 new_command_line_.AppendSwitch(switches::kIncognito); | 70 CommandLine new_command_line(GetCommandLineForRelaunch()); |
| 98 new_command_line_.AppendArgPath(test_file_path); | 71 new_command_line.AppendSwitch(switches::kIncognito); |
| 72 new_command_line.AppendArgPath(test_file_path); |
| 99 | 73 |
| 100 Relaunch(); | 74 Relaunch(new_command_line); |
| 101 | 75 |
| 102 // There should be one normal and one incognito window now. | 76 // There should be one normal and one incognito window now. |
| 103 ui_test_utils::BrowserAddedObserver observer; | 77 ui_test_utils::BrowserAddedObserver observer; |
| 104 Relaunch(); | 78 Relaunch(new_command_line); |
| 105 observer.WaitForSingleNewBrowser(); | 79 observer.WaitForSingleNewBrowser(); |
| 106 ASSERT_EQ(2u, BrowserList::size()); | 80 ASSERT_EQ(2u, BrowserList::size()); |
| 107 | 81 |
| 108 ASSERT_EQ(1u, | 82 ASSERT_EQ(1u, |
| 109 BrowserList::GetBrowserCountForType(browser()->profile(), true)); | 83 BrowserList::GetBrowserCountForType(browser()->profile(), true)); |
| 110 } | 84 } |
| 111 | 85 |
| 112 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) { | 86 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) { |
| 113 // We should start with one normal window. | 87 // We should start with one normal window. |
| 114 ASSERT_EQ(1u, | 88 ASSERT_EQ(1u, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 129 browser()->CloseWindow(); | 103 browser()->CloseWindow(); |
| 130 observer.Wait(); | 104 observer.Wait(); |
| 131 | 105 |
| 132 // There should only be the incognito window open now. | 106 // There should only be the incognito window open now. |
| 133 ASSERT_EQ(1u, BrowserList::size()); | 107 ASSERT_EQ(1u, BrowserList::size()); |
| 134 ASSERT_EQ(0u, BrowserList::GetBrowserCountForType(profile, true)); | 108 ASSERT_EQ(0u, BrowserList::GetBrowserCountForType(profile, true)); |
| 135 | 109 |
| 136 // Run with just an URL specified, no --incognito switch. | 110 // Run with just an URL specified, no --incognito switch. |
| 137 FilePath test_file_path = ui_test_utils::GetTestFilePath( | 111 FilePath test_file_path = ui_test_utils::GetTestFilePath( |
| 138 FilePath(), FilePath().AppendASCII("empty.html")); | 112 FilePath(), FilePath().AppendASCII("empty.html")); |
| 139 new_command_line_.AppendArgPath(test_file_path); | 113 CommandLine new_command_line(GetCommandLineForRelaunch()); |
| 114 new_command_line.AppendArgPath(test_file_path); |
| 140 ui_test_utils::WindowedNotificationObserver tab_observer( | 115 ui_test_utils::WindowedNotificationObserver tab_observer( |
| 141 chrome::NOTIFICATION_TAB_ADDED, | 116 chrome::NOTIFICATION_TAB_ADDED, |
| 142 content::NotificationService::AllSources()); | 117 content::NotificationService::AllSources()); |
| 143 Relaunch(); | 118 Relaunch(new_command_line); |
| 144 tab_observer.Wait(); | 119 tab_observer.Wait(); |
| 145 | 120 |
| 146 // There should be one normal and one incognito window now. | 121 // There should be one normal and one incognito window now. |
| 147 ASSERT_EQ(2u, BrowserList::size()); | 122 ASSERT_EQ(2u, BrowserList::size()); |
| 148 ASSERT_EQ(1u, BrowserList::GetBrowserCountForType(profile, true)); | 123 ASSERT_EQ(1u, BrowserList::GetBrowserCountForType(profile, true)); |
| 149 } | 124 } |
| 150 | 125 |
| 151 #endif // !OS_MACOSX | 126 #endif // !OS_MACOSX |
| OLD | NEW |