Home | History | Annotate | Download | only in unittests
      1 // Copyright 2014 the V8 project 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 "test/unittests/test-utils.h"
      6 
      7 #include "include/libplatform/libplatform.h"
      8 #include "src/base/platform/time.h"
      9 #include "src/debug/debug.h"
     10 #include "src/flags.h"
     11 #include "src/isolate.h"
     12 #include "src/v8.h"
     13 
     14 namespace v8 {
     15 
     16 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
     17  public:
     18   virtual void* Allocate(size_t length) {
     19     void* data = AllocateUninitialized(length);
     20     return data == NULL ? data : memset(data, 0, length);
     21   }
     22   virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
     23   virtual void Free(void* data, size_t) { free(data); }
     24 };
     25 
     26 
     27 // static
     28 ArrayBufferAllocator* TestWithIsolate::array_buffer_allocator_ = NULL;
     29 
     30 // static
     31 Isolate* TestWithIsolate::isolate_ = NULL;
     32 
     33 
     34 TestWithIsolate::TestWithIsolate()
     35     : isolate_scope_(isolate()), handle_scope_(isolate()) {}
     36 
     37 
     38 TestWithIsolate::~TestWithIsolate() {}
     39 
     40 
     41 // static
     42 void TestWithIsolate::SetUpTestCase() {
     43   Test::SetUpTestCase();
     44   EXPECT_EQ(NULL, isolate_);
     45   v8::Isolate::CreateParams create_params;
     46   array_buffer_allocator_ = new ArrayBufferAllocator;
     47   create_params.array_buffer_allocator = array_buffer_allocator_;
     48   isolate_ = v8::Isolate::New(create_params);
     49   EXPECT_TRUE(isolate_ != NULL);
     50 }
     51 
     52 
     53 // static
     54 void TestWithIsolate::TearDownTestCase() {
     55   ASSERT_TRUE(isolate_ != NULL);
     56   v8::Platform* platform = internal::V8::GetCurrentPlatform();
     57   ASSERT_TRUE(platform != NULL);
     58   while (platform::PumpMessageLoop(platform, isolate_)) continue;
     59   isolate_->Dispose();
     60   isolate_ = NULL;
     61   delete array_buffer_allocator_;
     62   Test::TearDownTestCase();
     63 }
     64 
     65 
     66 TestWithContext::TestWithContext()
     67     : context_(Context::New(isolate())), context_scope_(context_) {}
     68 
     69 
     70 TestWithContext::~TestWithContext() {}
     71 
     72 
     73 namespace base {
     74 namespace {
     75 
     76 inline int64_t GetRandomSeedFromFlag(int random_seed) {
     77   return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
     78 }
     79 
     80 }  // namespace
     81 
     82 TestWithRandomNumberGenerator::TestWithRandomNumberGenerator()
     83     : rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
     84 
     85 
     86 TestWithRandomNumberGenerator::~TestWithRandomNumberGenerator() {}
     87 
     88 }  // namespace base
     89 
     90 
     91 namespace internal {
     92 
     93 TestWithIsolate::~TestWithIsolate() {}
     94 
     95 TestWithIsolateAndZone::~TestWithIsolateAndZone() {}
     96 
     97 Factory* TestWithIsolate::factory() const { return isolate()->factory(); }
     98 
     99 
    100 base::RandomNumberGenerator* TestWithIsolate::random_number_generator() const {
    101   return isolate()->random_number_generator();
    102 }
    103 
    104 
    105 TestWithZone::~TestWithZone() {}
    106 
    107 }  // namespace internal
    108 }  // namespace v8
    109