Home | History | Annotate | Download | only in shell
      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 "content/shell/shell_browser_main.h"
      6 
      7 #include <iostream>
      8 
      9 #include "base/command_line.h"
     10 #include "base/file_util.h"
     11 #include "base/files/file_path.h"
     12 #include "base/files/scoped_temp_dir.h"
     13 #include "base/logging.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/message_loop/message_loop.h"
     16 #include "base/strings/sys_string_conversions.h"
     17 #include "base/strings/utf_string_conversions.h"
     18 #include "base/threading/thread_restrictions.h"
     19 #include "content/public/browser/browser_main_runner.h"
     20 #include "content/shell/common/shell_switches.h"
     21 #include "content/shell/common/webkit_test_helpers.h"
     22 #include "content/shell/shell.h"
     23 #include "content/shell/webkit_test_controller.h"
     24 #include "net/base/net_util.h"
     25 #include "webkit/support/webkit_support.h"
     26 
     27 #if defined(OS_ANDROID)
     28 #include "base/run_loop.h"
     29 #include "content/shell/shell_layout_tests_android.h"
     30 #endif
     31 
     32 namespace {
     33 
     34 GURL GetURLForLayoutTest(const std::string& test_name,
     35                          base::FilePath* current_working_directory,
     36                          bool* enable_pixel_dumping,
     37                          std::string* expected_pixel_hash) {
     38   // A test name is formated like file:///path/to/test'--pixel-test'pixelhash
     39   std::string path_or_url = test_name;
     40   std::string pixel_switch;
     41   std::string pixel_hash;
     42   std::string::size_type separator_position = path_or_url.find('\'');
     43   if (separator_position != std::string::npos) {
     44     pixel_switch = path_or_url.substr(separator_position + 1);
     45     path_or_url.erase(separator_position);
     46   }
     47   separator_position = pixel_switch.find('\'');
     48   if (separator_position != std::string::npos) {
     49     pixel_hash = pixel_switch.substr(separator_position + 1);
     50     pixel_switch.erase(separator_position);
     51   }
     52   if (enable_pixel_dumping) {
     53     *enable_pixel_dumping =
     54         (pixel_switch == "--pixel-test" || pixel_switch == "-p");
     55   }
     56   if (expected_pixel_hash)
     57     *expected_pixel_hash = pixel_hash;
     58 
     59   GURL test_url;
     60 #if defined(OS_ANDROID)
     61   if (content::GetTestUrlForAndroid(path_or_url, &test_url))
     62     return test_url;
     63 #endif
     64 
     65   test_url = GURL(path_or_url);
     66   if (!(test_url.is_valid() && test_url.has_scheme())) {
     67     // We're outside of the message loop here, and this is a test.
     68     base::ThreadRestrictions::ScopedAllowIO allow_io;
     69 #if defined(OS_WIN)
     70     std::wstring wide_path_or_url =
     71         base::SysNativeMBToWide(path_or_url);
     72     base::FilePath local_file(wide_path_or_url);
     73 #else
     74     base::FilePath local_file(path_or_url);
     75 #endif
     76     if (!base::PathExists(local_file)) {
     77       local_file = content::GetWebKitRootDirFilePath()
     78           .Append(FILE_PATH_LITERAL("LayoutTests")).Append(local_file);
     79     }
     80     test_url = net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file));
     81   }
     82   base::FilePath local_path;
     83   if (current_working_directory) {
     84     // We're outside of the message loop here, and this is a test.
     85     base::ThreadRestrictions::ScopedAllowIO allow_io;
     86     if (net::FileURLToFilePath(test_url, &local_path))
     87       *current_working_directory = local_path.DirName();
     88     else
     89       file_util::GetCurrentDirectory(current_working_directory);
     90   }
     91   return test_url;
     92 }
     93 
     94 bool GetNextTest(const CommandLine::StringVector& args,
     95                  size_t* position,
     96                  std::string* test) {
     97   if (*position >= args.size())
     98     return false;
     99   if (args[*position] == FILE_PATH_LITERAL("-"))
    100     return !!std::getline(std::cin, *test, '\n');
    101 #if defined(OS_WIN)
    102   *test = WideToUTF8(args[(*position)++]);
    103 #else
    104   *test = args[(*position)++];
    105 #endif
    106   return true;
    107 }
    108 
    109 }  // namespace
    110 
    111 // Main routine for running as the Browser process.
    112 int ShellBrowserMain(
    113     const content::MainFunctionParams& parameters,
    114     const scoped_ptr<content::BrowserMainRunner>& main_runner) {
    115   bool layout_test_mode =
    116       CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree);
    117   base::ScopedTempDir browser_context_path_for_layout_tests;
    118 
    119   if (layout_test_mode) {
    120     CHECK(browser_context_path_for_layout_tests.CreateUniqueTempDir());
    121     CHECK(!browser_context_path_for_layout_tests.path().MaybeAsASCII().empty());
    122     CommandLine::ForCurrentProcess()->AppendSwitchASCII(
    123         switches::kContentShellDataPath,
    124         browser_context_path_for_layout_tests.path().MaybeAsASCII());
    125 
    126 #if defined(OS_ANDROID)
    127     content::EnsureInitializeForAndroidLayoutTests();
    128 #endif
    129   }
    130 
    131   int exit_code = main_runner->Initialize(parameters);
    132   DCHECK_LT(exit_code, 0)
    133       << "BrowserMainRunner::Initialize failed in ShellBrowserMain";
    134 
    135   if (exit_code >= 0)
    136     return exit_code;
    137 
    138   if (CommandLine::ForCurrentProcess()->HasSwitch(
    139         switches::kCheckLayoutTestSysDeps)) {
    140     base::MessageLoop::current()->PostTask(FROM_HERE,
    141                                            base::MessageLoop::QuitClosure());
    142     main_runner->Run();
    143     content::Shell::CloseAllWindows();
    144     main_runner->Shutdown();
    145     return 0;
    146   }
    147 
    148   if (layout_test_mode) {
    149     content::WebKitTestController test_controller;
    150     {
    151       // We're outside of the message loop here, and this is a test.
    152       base::ThreadRestrictions::ScopedAllowIO allow_io;
    153       base::FilePath temp_path;
    154       file_util::GetTempDir(&temp_path);
    155       test_controller.SetTempPath(temp_path);
    156     }
    157     std::string test_string;
    158     CommandLine::StringVector args =
    159         CommandLine::ForCurrentProcess()->GetArgs();
    160     size_t command_line_position = 0;
    161     bool ran_at_least_once = false;
    162 
    163 #if defined(OS_ANDROID)
    164     std::cout << "#READY\n";
    165     std::cout.flush();
    166 #endif
    167 
    168     while (GetNextTest(args, &command_line_position, &test_string)) {
    169       if (test_string.empty())
    170         continue;
    171       if (test_string == "QUIT")
    172         break;
    173 
    174       bool enable_pixel_dumps;
    175       std::string pixel_hash;
    176       base::FilePath cwd;
    177       GURL test_url = GetURLForLayoutTest(
    178           test_string, &cwd, &enable_pixel_dumps, &pixel_hash);
    179       if (!content::WebKitTestController::Get()->PrepareForLayoutTest(
    180               test_url, cwd, enable_pixel_dumps, pixel_hash)) {
    181         break;
    182       }
    183 
    184       ran_at_least_once = true;
    185 #if defined(OS_ANDROID)
    186       // The message loop on Android is provided by the system, and does not
    187       // offer a blocking Run() method. For layout tests, use a nested loop
    188       // together with a base::RunLoop so it can block until a QuitClosure.
    189       base::RunLoop run_loop;
    190       run_loop.Run();
    191 #else
    192       main_runner->Run();
    193 #endif
    194 
    195       if (!content::WebKitTestController::Get()->ResetAfterLayoutTest())
    196         break;
    197     }
    198     if (!ran_at_least_once) {
    199       base::MessageLoop::current()->PostTask(FROM_HERE,
    200                                              base::MessageLoop::QuitClosure());
    201       main_runner->Run();
    202     }
    203     exit_code = 0;
    204   }
    205 
    206 #if !defined(OS_ANDROID)
    207   if (!layout_test_mode)
    208     exit_code = main_runner->Run();
    209 
    210   main_runner->Shutdown();
    211 #endif
    212 
    213   return exit_code;
    214 }
    215