Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5 | #ifndef V8_UNITTESTS_TEST_UTILS_H_ |
| 6 | #define V8_UNITTESTS_TEST_UTILS_H_ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | |
| 8 | #include "include/v8.h" |
| 9 | #include "src/base/macros.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10 | #include "src/base/utils/random-number-generator.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/zone.h" |
| 12 | #include "testing/gtest-support.h" |
| 13 | |
| 14 | namespace v8 { |
| 15 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | class ArrayBufferAllocator; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17 | |
| 18 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | class TestWithIsolate : public virtual ::testing::Test { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | public: |
| 21 | TestWithIsolate(); |
| 22 | virtual ~TestWithIsolate(); |
| 23 | |
| 24 | Isolate* isolate() const { return isolate_; } |
| 25 | |
| 26 | static void SetUpTestCase(); |
| 27 | static void TearDownTestCase(); |
| 28 | |
| 29 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | static ArrayBufferAllocator* array_buffer_allocator_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | static Isolate* isolate_; |
| 32 | Isolate::Scope isolate_scope_; |
| 33 | HandleScope handle_scope_; |
| 34 | |
| 35 | DISALLOW_COPY_AND_ASSIGN(TestWithIsolate); |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | class TestWithContext : public virtual TestWithIsolate { |
| 40 | public: |
| 41 | TestWithContext(); |
| 42 | virtual ~TestWithContext(); |
| 43 | |
| 44 | const Local<Context>& context() const { return context_; } |
| 45 | |
| 46 | private: |
| 47 | Local<Context> context_; |
| 48 | Context::Scope context_scope_; |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(TestWithContext); |
| 51 | }; |
| 52 | |
| 53 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 54 | namespace base { |
| 55 | |
| 56 | class TestWithRandomNumberGenerator : public ::testing::Test { |
| 57 | public: |
| 58 | TestWithRandomNumberGenerator(); |
| 59 | virtual ~TestWithRandomNumberGenerator(); |
| 60 | |
| 61 | RandomNumberGenerator* rng() { return &rng_; } |
| 62 | |
| 63 | private: |
| 64 | RandomNumberGenerator rng_; |
| 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(TestWithRandomNumberGenerator); |
| 67 | }; |
| 68 | |
| 69 | } // namespace base |
| 70 | |
| 71 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | namespace internal { |
| 73 | |
| 74 | // Forward declarations. |
| 75 | class Factory; |
| 76 | |
| 77 | |
| 78 | class TestWithIsolate : public virtual ::v8::TestWithIsolate { |
| 79 | public: |
| 80 | TestWithIsolate() {} |
| 81 | virtual ~TestWithIsolate(); |
| 82 | |
| 83 | Factory* factory() const; |
| 84 | Isolate* isolate() const { |
| 85 | return reinterpret_cast<Isolate*>(::v8::TestWithIsolate::isolate()); |
| 86 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 87 | base::RandomNumberGenerator* random_number_generator() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | DISALLOW_COPY_AND_ASSIGN(TestWithIsolate); |
| 91 | }; |
| 92 | |
| 93 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 94 | class TestWithZone : public virtual ::testing::Test { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | public: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 96 | TestWithZone() : zone_(&allocator_) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | virtual ~TestWithZone(); |
| 98 | |
| 99 | Zone* zone() { return &zone_; } |
| 100 | |
| 101 | private: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 102 | base::AccountingAllocator allocator_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | Zone zone_; |
| 104 | |
| 105 | DISALLOW_COPY_AND_ASSIGN(TestWithZone); |
| 106 | }; |
| 107 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 108 | |
| 109 | class TestWithIsolateAndZone : public virtual TestWithIsolate { |
| 110 | public: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 111 | TestWithIsolateAndZone() : zone_(&allocator_) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | virtual ~TestWithIsolateAndZone(); |
| 113 | |
| 114 | Zone* zone() { return &zone_; } |
| 115 | |
| 116 | private: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 117 | base::AccountingAllocator allocator_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | Zone zone_; |
| 119 | |
| 120 | DISALLOW_COPY_AND_ASSIGN(TestWithIsolateAndZone); |
| 121 | }; |
| 122 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | } // namespace internal |
| 124 | } // namespace v8 |
| 125 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 126 | #endif // V8_UNITTESTS_TEST_UTILS_H_ |