Home | History | Annotate | Download | only in test
      1 // Copyright 2014 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/blink_test_environment.h"
      6 
      7 #include <string>
      8 
      9 #include "base/command_line.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "base/run_loop.h"
     12 #include "base/strings/string_tokenizer.h"
     13 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
     14 #include "content/public/common/content_switches.h"
     15 #include "content/public/common/user_agent.h"
     16 #include "content/public/test/test_content_client_initializer.h"
     17 #include "content/test/test_webkit_platform_support.h"
     18 #include "third_party/WebKit/public/web/WebCache.h"
     19 #include "third_party/WebKit/public/web/WebKit.h"
     20 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
     21 #include "url/url_util.h"
     22 
     23 #if defined(OS_WIN)
     24 #include "ui/gfx/win/dpi.h"
     25 #endif
     26 
     27 #if defined(OS_ANDROID)
     28 #include "base/android/jni_android.h"
     29 #include "net/android/network_library.h"
     30 #endif
     31 
     32 #if defined(OS_MACOSX)
     33 #include "base/test/mock_chrome_application_mac.h"
     34 #endif
     35 
     36 namespace content {
     37 
     38 namespace {
     39 
     40 void EnableBlinkPlatformLogChannels(const std::string& channels) {
     41   if (channels.empty())
     42     return;
     43   base::StringTokenizer t(channels, ", ");
     44   while (t.GetNext())
     45     blink::enableLogChannel(t.token().c_str());
     46 }
     47 
     48 void ParseBlinkCommandLineArgumentsForUnitTests() {
     49   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
     50   EnableBlinkPlatformLogChannels(
     51       command_line.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels));
     52 }
     53 
     54 class TestEnvironment {
     55  public:
     56 #if defined(OS_ANDROID)
     57   // Android UI message loop goes through Java, so don't use it in tests.
     58   typedef base::MessageLoop MessageLoopType;
     59 #else
     60   typedef base::MessageLoopForUI MessageLoopType;
     61 #endif
     62 
     63   TestEnvironment() {
     64     main_message_loop_.reset(new MessageLoopType);
     65 
     66     // TestWebKitPlatformSupport must be instantiated after MessageLoopType.
     67     webkit_platform_support_.reset(new TestWebKitPlatformSupport);
     68     content_initializer_.reset(new content::TestContentClientInitializer());
     69   }
     70 
     71   ~TestEnvironment() {
     72   }
     73 
     74   TestWebKitPlatformSupport* webkit_platform_support() const {
     75     return webkit_platform_support_.get();
     76   }
     77 
     78  private:
     79   scoped_ptr<MessageLoopType> main_message_loop_;
     80   scoped_ptr<TestWebKitPlatformSupport> webkit_platform_support_;
     81   scoped_ptr<TestContentClientInitializer> content_initializer_;
     82 };
     83 
     84 TestEnvironment* test_environment;
     85 
     86 }  // namespace
     87 
     88 void SetUpBlinkTestEnvironment() {
     89   ParseBlinkCommandLineArgumentsForUnitTests();
     90 
     91   blink::WebRuntimeFeatures::enableExperimentalFeatures(true);
     92   blink::WebRuntimeFeatures::enableTestOnlyFeatures(true);
     93 
     94 #if defined(OS_ANDROID)
     95   JNIEnv* env = base::android::AttachCurrentThread();
     96   net::android::RegisterNetworkLibrary(env);
     97 #endif
     98 
     99 #if defined(OS_MACOSX)
    100   mock_cr_app::RegisterMockCrApp();
    101 #endif
    102 
    103 #if defined(OS_WIN)
    104   gfx::InitDeviceScaleFactor(1.0f);
    105 #endif
    106 
    107   // Explicitly initialize the GURL library before spawning any threads.
    108   // Otherwise crash may happend when different threads try to create a GURL
    109   // at same time.
    110   url::Initialize();
    111   test_environment = new TestEnvironment;
    112 }
    113 
    114 void TearDownBlinkTestEnvironment() {
    115   // Flush any remaining messages before we kill ourselves.
    116   // http://code.google.com/p/chromium/issues/detail?id=9500
    117   base::RunLoop().RunUntilIdle();
    118 
    119   if (RunningOnValgrind())
    120     blink::WebCache::clear();
    121   delete test_environment;
    122   test_environment = NULL;
    123 }
    124 
    125 }  // namespace content
    126