OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/base/module_system_test.h" | 5 #include "chrome/test/base/module_system_test.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
10 #include "chrome/renderer/extensions/object_backed_native_handler.h" | 10 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 using extensions::ModuleSystem; | 16 using extensions::ModuleSystem; |
17 using extensions::NativeHandler; | 17 using extensions::NativeHandler; |
18 using extensions::ObjectBackedNativeHandler; | 18 using extensions::ObjectBackedNativeHandler; |
19 | 19 |
20 // Native JS functions for doing asserts. | 20 // Native JS functions for doing asserts. |
21 class AssertNatives : public ObjectBackedNativeHandler { | 21 class AssertNatives : public ObjectBackedNativeHandler { |
22 public: | 22 public: |
23 explicit AssertNatives(v8::Handle<v8::Context> context) | 23 explicit AssertNatives(extensions::ChromeV8Context* context) |
24 : ObjectBackedNativeHandler(context), | 24 : ObjectBackedNativeHandler(context), |
25 assertion_made_(false), | 25 assertion_made_(false), |
26 failed_(false) { | 26 failed_(false) { |
27 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, | 27 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, |
28 base::Unretained(this))); | 28 base::Unretained(this))); |
29 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, | 29 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, |
30 base::Unretained(this))); | 30 base::Unretained(this))); |
31 } | 31 } |
32 | 32 |
33 bool assertion_made() { return assertion_made_; } | 33 bool assertion_made() { return assertion_made_; } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 class FailsOnException : public ModuleSystem::ExceptionHandler { | 80 class FailsOnException : public ModuleSystem::ExceptionHandler { |
81 public: | 81 public: |
82 virtual void HandleUncaughtException() OVERRIDE { | 82 virtual void HandleUncaughtException() OVERRIDE { |
83 FAIL(); | 83 FAIL(); |
84 } | 84 } |
85 }; | 85 }; |
86 | 86 |
87 ModuleSystemTest::ModuleSystemTest() | 87 ModuleSystemTest::ModuleSystemTest() |
88 : isolate_(v8::Isolate::GetCurrent()), | 88 : isolate_(v8::Isolate::GetCurrent()), |
89 handle_scope_(isolate_), | 89 handle_scope_(isolate_), |
90 context_(v8::Context::New(isolate_)), | 90 context_( |
91 new extensions::ChromeV8Context( | |
92 v8::Context::New(isolate_), | |
93 reinterpret_cast<WebKit::WebFrame*>(1), | |
94 NULL, | |
95 extensions::Feature::UNSPECIFIED_CONTEXT)), | |
91 source_map_(new StringSourceMap()), | 96 source_map_(new StringSourceMap()), |
92 should_assertions_be_made_(true) { | 97 should_assertions_be_made_(true) { |
93 context_->Enter(); | 98 context_->v8_context()->Enter(); |
94 assert_natives_ = new AssertNatives(context_.get()); | 99 assert_natives_ = new AssertNatives(context_.get()); |
95 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get())); | 100 module_system_.reset( |
101 new ModuleSystem(context_.get(), source_map_.get())); | |
96 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( | 102 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
97 assert_natives_)); | 103 assert_natives_)); |
98 module_system_->SetExceptionHandlerForTest( | 104 module_system_->SetExceptionHandlerForTest( |
99 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); | 105 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
100 } | 106 } |
101 | 107 |
102 ModuleSystemTest::~ModuleSystemTest() { | 108 ModuleSystemTest::~ModuleSystemTest() { |
103 module_system_.reset(); | 109 module_system_.reset(); |
104 context_->Exit(); | 110 context_->v8_context()->Exit(); |
not at google - send to devlin
2013/05/29 17:41:55
idly wonder why this doesn't hold onto a context s
| |
105 } | 111 } |
106 | 112 |
107 void ModuleSystemTest::RegisterModule(const std::string& name, | 113 void ModuleSystemTest::RegisterModule(const std::string& name, |
108 const std::string& code) { | 114 const std::string& code) { |
109 source_map_->RegisterModule(name, code); | 115 source_map_->RegisterModule(name, code); |
110 } | 116 } |
111 | 117 |
112 void ModuleSystemTest::RegisterModule(const std::string& name, | 118 void ModuleSystemTest::RegisterModule(const std::string& name, |
113 int resource_id) { | 119 int resource_id) { |
114 const std::string& code = ResourceBundle::GetSharedInstance(). | 120 const std::string& code = ResourceBundle::GetSharedInstance(). |
(...skipping 18 matching lines...) Expand all Loading... | |
133 should_assertions_be_made_ = false; | 139 should_assertions_be_made_ = false; |
134 } | 140 } |
135 | 141 |
136 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { | 142 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { |
137 v8::HandleScope handle_scope; | 143 v8::HandleScope handle_scope; |
138 v8::Handle<v8::Object> object = v8::Object::New(); | 144 v8::Handle<v8::Object> object = v8::Object::New(); |
139 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), | 145 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), |
140 object); | 146 object); |
141 return handle_scope.Close(object); | 147 return handle_scope.Close(object); |
142 } | 148 } |
OLD | NEW |