Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / chrome / browser / chrome_main_browsertest.cc
blob7d14ea86ddfb0e766608e100f55551c8859454ff
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.
5 #include "chrome/test/ui/ui_test.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/process_util.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_commands.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.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 "net/base/net_util.h"
25 // These tests don't apply to the Mac version; see GetCommandLineForRelaunch
26 // for details.
27 #if !defined(OS_MACOSX)
29 class ChromeMainTest : public InProcessBrowserTest {
30 public:
31 ChromeMainTest() {}
33 void Relaunch(const CommandLine& new_command_line) {
34 base::LaunchProcess(new_command_line, base::LaunchOptions(), NULL);
38 // Make sure that the second invocation creates a new window.
39 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunch) {
40 ui_test_utils::BrowserAddedObserver observer;
41 Relaunch(GetCommandLineForRelaunch());
42 observer.WaitForSingleNewBrowser();
43 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
44 browser()->host_desktop_type()));
47 IN_PROC_BROWSER_TEST_F(ChromeMainTest, ReuseBrowserInstanceWhenOpeningFile) {
48 base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
49 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
50 CommandLine new_command_line(GetCommandLineForRelaunch());
51 new_command_line.AppendArgPath(test_file_path);
52 content::WindowedNotificationObserver observer(
53 chrome::NOTIFICATION_TAB_ADDED,
54 content::NotificationService::AllSources());
55 Relaunch(new_command_line);
56 observer.Wait();
58 GURL url = net::FilePathToFileURL(test_file_path);
59 content::WebContents* tab =
60 browser()->tab_strip_model()->GetActiveWebContents();
61 ASSERT_EQ(url, tab->GetController().GetActiveEntry()->GetVirtualURL());
64 // ChromeMainTest.SecondLaunchWithIncognitoUrl is flaky on Win and Linux.
65 // http://crbug.com/130395
66 #if defined(OS_WIN) || defined(OS_LINUX)
67 #define MAYBE_SecondLaunchWithIncognitoUrl DISABLED_SecondLaunchWithIncognitoUrl
68 #else
69 #define MAYBE_SecondLaunchWithIncognitoUrl SecondLaunchWithIncognitoUrl
70 #endif
72 IN_PROC_BROWSER_TEST_F(ChromeMainTest, MAYBE_SecondLaunchWithIncognitoUrl) {
73 // We should start with one normal window.
74 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
75 browser()->host_desktop_type()));
77 // Run with --incognito switch and an URL specified.
78 base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
79 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
80 CommandLine new_command_line(GetCommandLineForRelaunch());
81 new_command_line.AppendSwitch(switches::kIncognito);
82 new_command_line.AppendArgPath(test_file_path);
84 Relaunch(new_command_line);
86 // There should be one normal and one incognito window now.
87 ui_test_utils::BrowserAddedObserver observer;
88 Relaunch(new_command_line);
89 observer.WaitForSingleNewBrowser();
90 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
92 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
93 browser()->host_desktop_type()));
96 IN_PROC_BROWSER_TEST_F(ChromeMainTest, SecondLaunchFromIncognitoWithNormalUrl) {
97 // We should start with one normal window.
98 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
99 browser()->host_desktop_type()));
101 // Create an incognito window.
102 chrome::NewIncognitoWindow(browser());
104 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
105 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(browser()->profile(),
106 browser()->host_desktop_type()));
108 // Close the first window.
109 Profile* profile = browser()->profile();
110 chrome::HostDesktopType host_desktop_type = browser()->host_desktop_type();
111 content::WindowedNotificationObserver observer(
112 chrome::NOTIFICATION_BROWSER_CLOSED,
113 content::NotificationService::AllSources());
114 chrome::CloseWindow(browser());
115 observer.Wait();
117 // There should only be the incognito window open now.
118 ASSERT_EQ(1u, chrome::GetTotalBrowserCount());
119 ASSERT_EQ(0u, chrome::GetTabbedBrowserCount(profile, host_desktop_type));
121 // Run with just an URL specified, no --incognito switch.
122 base::FilePath test_file_path = ui_test_utils::GetTestFilePath(
123 base::FilePath(), base::FilePath().AppendASCII("empty.html"));
124 CommandLine new_command_line(GetCommandLineForRelaunch());
125 new_command_line.AppendArgPath(test_file_path);
126 content::WindowedNotificationObserver tab_observer(
127 chrome::NOTIFICATION_TAB_ADDED,
128 content::NotificationService::AllSources());
129 Relaunch(new_command_line);
130 tab_observer.Wait();
132 // There should be one normal and one incognito window now.
133 ASSERT_EQ(2u, chrome::GetTotalBrowserCount());
134 ASSERT_EQ(1u, chrome::GetTabbedBrowserCount(profile, host_desktop_type));
137 #endif // !OS_MACOSX