Home | History | Annotate | Download | only in test
      1 // Copyright 2013 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/test/test_webkit_platform_support.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/file_util.h"
      9 #include "base/files/file_path.h"
     10 #include "base/files/scoped_temp_dir.h"
     11 #include "base/metrics/stats_counters.h"
     12 #include "base/path_service.h"
     13 #include "base/strings/utf_string_conversions.h"
     14 #include "content/public/common/content_switches.h"
     15 #include "content/test/mock_webclipboard_impl.h"
     16 #include "content/test/web_gesture_curve_mock.h"
     17 #include "content/test/web_layer_tree_view_impl_for_testing.h"
     18 #include "content/test/weburl_loader_mock_factory.h"
     19 #include "media/base/media.h"
     20 #include "net/cookies/cookie_monster.h"
     21 #include "net/test/spawned_test_server/spawned_test_server.h"
     22 #include "third_party/WebKit/public/platform/WebData.h"
     23 #include "third_party/WebKit/public/platform/WebFileSystem.h"
     24 #include "third_party/WebKit/public/platform/WebStorageArea.h"
     25 #include "third_party/WebKit/public/platform/WebStorageNamespace.h"
     26 #include "third_party/WebKit/public/platform/WebString.h"
     27 #include "third_party/WebKit/public/platform/WebURL.h"
     28 #include "third_party/WebKit/public/web/WebDatabase.h"
     29 #include "third_party/WebKit/public/web/WebKit.h"
     30 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
     31 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
     32 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h"
     33 #include "v8/include/v8.h"
     34 #include "webkit/browser/database/vfs_backend.h"
     35 
     36 #if defined(OS_MACOSX)
     37 #include "base/mac/mac_util.h"
     38 #endif
     39 
     40 namespace content {
     41 
     42 TestWebKitPlatformSupport::TestWebKitPlatformSupport() {
     43   url_loader_factory_.reset(new WebURLLoaderMockFactory());
     44   mock_clipboard_.reset(new MockWebClipboardImpl());
     45 
     46   // Create an anonymous stats table since we don't need to share between
     47   // processes.
     48   stats_table_.reset(
     49       new base::StatsTable(base::StatsTable::TableIdentifier(), 20, 200));
     50   base::StatsTable::set_current(stats_table_.get());
     51 
     52   blink::initialize(this);
     53   blink::mainThreadIsolate()->SetCounterFunction(
     54       base::StatsTable::FindLocation);
     55   blink::setLayoutTestMode(true);
     56   blink::WebSecurityPolicy::registerURLSchemeAsLocal(
     57       blink::WebString::fromUTF8("test-shell-resource"));
     58   blink::WebSecurityPolicy::registerURLSchemeAsNoAccess(
     59       blink::WebString::fromUTF8("test-shell-resource"));
     60   blink::WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(
     61       blink::WebString::fromUTF8("test-shell-resource"));
     62   blink::WebSecurityPolicy::registerURLSchemeAsEmptyDocument(
     63       blink::WebString::fromUTF8("test-shell-resource"));
     64   blink::WebRuntimeFeatures::enableApplicationCache(true);
     65   blink::WebRuntimeFeatures::enableDatabase(true);
     66   blink::WebRuntimeFeatures::enableNotifications(true);
     67   blink::WebRuntimeFeatures::enableTouch(true);
     68 
     69   // Load libraries for media and enable the media player.
     70   bool enable_media = false;
     71   base::FilePath module_path;
     72   if (PathService::Get(base::DIR_MODULE, &module_path)) {
     73 #if defined(OS_MACOSX)
     74     if (base::mac::AmIBundled())
     75       module_path = module_path.DirName().DirName().DirName();
     76 #endif
     77     if (media::InitializeMediaLibrary(module_path))
     78       enable_media = true;
     79   }
     80   blink::WebRuntimeFeatures::enableMediaPlayer(enable_media);
     81   LOG_IF(WARNING, !enable_media) << "Failed to initialize the media library.\n";
     82 
     83   file_utilities_.set_sandbox_enabled(false);
     84 
     85   if (!file_system_root_.CreateUniqueTempDir()) {
     86     LOG(WARNING) << "Failed to create a temp dir for the filesystem."
     87                     "FileSystem feature will be disabled.";
     88     DCHECK(file_system_root_.path().empty());
     89   }
     90 
     91 #if defined(OS_WIN)
     92   // Ensure we pick up the default theme engine.
     93   SetThemeEngine(NULL);
     94 #endif
     95 
     96   CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableFileCookies);
     97 
     98   // Test shell always exposes the GC.
     99   std::string flags("--expose-gc");
    100   v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
    101 }
    102 
    103 TestWebKitPlatformSupport::~TestWebKitPlatformSupport() {
    104   url_loader_factory_.reset();
    105   mock_clipboard_.reset();
    106   blink::shutdown();
    107   base::StatsTable::set_current(NULL);
    108   stats_table_.reset();
    109 }
    110 
    111 blink::WebMimeRegistry* TestWebKitPlatformSupport::mimeRegistry() {
    112   return &mime_registry_;
    113 }
    114 
    115 blink::WebClipboard* TestWebKitPlatformSupport::clipboard() {
    116   // Mock out clipboard calls so that tests don't mess
    117   // with each other's copies/pastes when running in parallel.
    118   return mock_clipboard_.get();
    119 }
    120 
    121 blink::WebFileUtilities* TestWebKitPlatformSupport::fileUtilities() {
    122   return &file_utilities_;
    123 }
    124 
    125 blink::WebIDBFactory* TestWebKitPlatformSupport::idbFactory() {
    126   NOTREACHED() <<
    127       "IndexedDB cannot be tested with in-process harnesses.";
    128   return NULL;
    129 }
    130 
    131 blink::WebURLLoader* TestWebKitPlatformSupport::createURLLoader() {
    132   return url_loader_factory_->CreateURLLoader(
    133       BlinkPlatformImpl::createURLLoader());
    134 }
    135 
    136 blink::WebString TestWebKitPlatformSupport::userAgent() {
    137   return blink::WebString::fromUTF8("DumpRenderTree/0.0.0.0");
    138 }
    139 
    140 blink::WebData TestWebKitPlatformSupport::loadResource(const char* name) {
    141   if (!strcmp(name, "deleteButton")) {
    142     // Create a red 30x30 square.
    143     const char red_square[] =
    144         "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
    145         "\x00\x00\x00\x1e\x00\x00\x00\x1e\x04\x03\x00\x00\x00\xc9\x1e\xb3"
    146         "\x91\x00\x00\x00\x30\x50\x4c\x54\x45\x00\x00\x00\x80\x00\x00\x00"
    147         "\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
    148         "\x80\xc0\xc0\xc0\xff\x00\x00\x00\xff\x00\xff\xff\x00\x00\x00\xff"
    149         "\xff\x00\xff\x00\xff\xff\xff\xff\xff\x7b\x1f\xb1\xc4\x00\x00\x00"
    150         "\x09\x70\x48\x59\x73\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a"
    151         "\x9c\x18\x00\x00\x00\x17\x49\x44\x41\x54\x78\x01\x63\x98\x89\x0a"
    152         "\x18\x50\xb9\x33\x47\xf9\xa8\x01\x32\xd4\xc2\x03\x00\x33\x84\x0d"
    153         "\x02\x3a\x91\xeb\xa5\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60"
    154         "\x82";
    155     return blink::WebData(red_square, arraysize(red_square));
    156   }
    157   return blink::WebData();
    158 }
    159 
    160 blink::WebString TestWebKitPlatformSupport::queryLocalizedString(
    161     blink::WebLocalizedString::Name name) {
    162   // Returns placeholder strings to check if they are correctly localized.
    163   switch (name) {
    164     case blink::WebLocalizedString::OtherDateLabel:
    165       return base::ASCIIToUTF16("<<OtherDateLabel>>");
    166     case blink::WebLocalizedString::OtherMonthLabel:
    167       return base::ASCIIToUTF16("<<OtherMonthLabel>>");
    168     case blink::WebLocalizedString::OtherTimeLabel:
    169       return base::ASCIIToUTF16("<<OtherTimeLabel>>");
    170     case blink::WebLocalizedString::OtherWeekLabel:
    171       return base::ASCIIToUTF16("<<OtherWeekLabel>>");
    172     case blink::WebLocalizedString::CalendarClear:
    173       return base::ASCIIToUTF16("<<CalendarClear>>");
    174     case blink::WebLocalizedString::CalendarToday:
    175       return base::ASCIIToUTF16("<<CalendarToday>>");
    176     case blink::WebLocalizedString::ThisMonthButtonLabel:
    177       return base::ASCIIToUTF16("<<ThisMonthLabel>>");
    178     case blink::WebLocalizedString::ThisWeekButtonLabel:
    179       return base::ASCIIToUTF16("<<ThisWeekLabel>>");
    180     case blink::WebLocalizedString::WeekFormatTemplate:
    181       return base::ASCIIToUTF16("Week $2, $1");
    182     default:
    183       return blink::WebString();
    184   }
    185 }
    186 
    187 blink::WebString TestWebKitPlatformSupport::queryLocalizedString(
    188     blink::WebLocalizedString::Name name,
    189     const blink::WebString& value) {
    190   if (name == blink::WebLocalizedString::ValidationRangeUnderflow)
    191     return base::ASCIIToUTF16("range underflow");
    192   if (name == blink::WebLocalizedString::ValidationRangeOverflow)
    193     return base::ASCIIToUTF16("range overflow");
    194   return BlinkPlatformImpl::queryLocalizedString(name, value);
    195 }
    196 
    197 blink::WebString TestWebKitPlatformSupport::queryLocalizedString(
    198     blink::WebLocalizedString::Name name,
    199     const blink::WebString& value1,
    200     const blink::WebString& value2) {
    201   if (name == blink::WebLocalizedString::ValidationTooLong)
    202     return base::ASCIIToUTF16("too long");
    203   if (name == blink::WebLocalizedString::ValidationStepMismatch)
    204     return base::ASCIIToUTF16("step mismatch");
    205   return BlinkPlatformImpl::queryLocalizedString(name, value1, value2);
    206 }
    207 
    208 blink::WebString TestWebKitPlatformSupport::defaultLocale() {
    209   return base::ASCIIToUTF16("en-US");
    210 }
    211 
    212 #if defined(OS_WIN) || defined(OS_MACOSX)
    213 void TestWebKitPlatformSupport::SetThemeEngine(blink::WebThemeEngine* engine) {
    214   active_theme_engine_ = engine ? engine : BlinkPlatformImpl::themeEngine();
    215 }
    216 
    217 blink::WebThemeEngine* TestWebKitPlatformSupport::themeEngine() {
    218   return active_theme_engine_;
    219 }
    220 #endif
    221 
    222 blink::WebCompositorSupport* TestWebKitPlatformSupport::compositorSupport() {
    223   return &compositor_support_;
    224 }
    225 
    226 blink::WebGestureCurve* TestWebKitPlatformSupport::createFlingAnimationCurve(
    227     blink::WebGestureDevice device_source,
    228     const blink::WebFloatPoint& velocity,
    229     const blink::WebSize& cumulative_scroll) {
    230   // Caller will retain and release.
    231   return new WebGestureCurveMock(velocity, cumulative_scroll);
    232 }
    233 
    234 blink::WebUnitTestSupport* TestWebKitPlatformSupport::unitTestSupport() {
    235   return this;
    236 }
    237 
    238 void TestWebKitPlatformSupport::registerMockedURL(
    239     const blink::WebURL& url,
    240     const blink::WebURLResponse& response,
    241     const blink::WebString& file_path) {
    242   url_loader_factory_->RegisterURL(url, response, file_path);
    243 }
    244 
    245 void TestWebKitPlatformSupport::registerMockedErrorURL(
    246     const blink::WebURL& url,
    247     const blink::WebURLResponse& response,
    248     const blink::WebURLError& error) {
    249   url_loader_factory_->RegisterErrorURL(url, response, error);
    250 }
    251 
    252 void TestWebKitPlatformSupport::unregisterMockedURL(const blink::WebURL& url) {
    253   url_loader_factory_->UnregisterURL(url);
    254 }
    255 
    256 void TestWebKitPlatformSupport::unregisterAllMockedURLs() {
    257   url_loader_factory_->UnregisterAllURLs();
    258 }
    259 
    260 void TestWebKitPlatformSupport::serveAsynchronousMockedRequests() {
    261   url_loader_factory_->ServeAsynchronousRequests();
    262 }
    263 
    264 blink::WebString TestWebKitPlatformSupport::webKitRootDir() {
    265   base::FilePath path;
    266   PathService::Get(base::DIR_SOURCE_ROOT, &path);
    267   path = path.Append(FILE_PATH_LITERAL("third_party/WebKit"));
    268   path = base::MakeAbsoluteFilePath(path);
    269   CHECK(!path.empty());
    270   std::string path_ascii = path.MaybeAsASCII();
    271   CHECK(!path_ascii.empty());
    272   return blink::WebString::fromUTF8(path_ascii.c_str());
    273 }
    274 
    275 blink::WebLayerTreeView*
    276 TestWebKitPlatformSupport::createLayerTreeViewForTesting() {
    277   scoped_ptr<WebLayerTreeViewImplForTesting> view(
    278       new WebLayerTreeViewImplForTesting());
    279 
    280   view->Initialize();
    281   return view.release();
    282 }
    283 
    284 blink::WebData TestWebKitPlatformSupport::readFromFile(
    285     const blink::WebString& path) {
    286   base::FilePath file_path = base::FilePath::FromUTF16Unsafe(path);
    287 
    288   std::string buffer;
    289   base::ReadFileToString(file_path, &buffer);
    290 
    291   return blink::WebData(buffer.data(), buffer.size());
    292 }
    293 
    294 }  // namespace content
    295