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