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