| 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/chrome_v8_context.h" |
| 10 #include "chrome/renderer/extensions/object_backed_native_handler.h" | 11 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
| 11 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 12 | 13 |
| 13 #include <map> | 14 #include <map> |
| 14 #include <string> | 15 #include <string> |
| 15 | 16 |
| 16 using extensions::ModuleSystem; | 17 using extensions::ModuleSystem; |
| 17 using extensions::NativeHandler; | 18 using extensions::NativeHandler; |
| 18 using extensions::ObjectBackedNativeHandler; | 19 using extensions::ObjectBackedNativeHandler; |
| 19 | 20 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 if (source_map_.count(name) == 0) | 63 if (source_map_.count(name) == 0) |
| 63 return v8::Undefined(); | 64 return v8::Undefined(); |
| 64 return v8::String::New(source_map_[name].c_str()); | 65 return v8::String::New(source_map_[name].c_str()); |
| 65 } | 66 } |
| 66 | 67 |
| 67 virtual bool Contains(const std::string& name) OVERRIDE { | 68 virtual bool Contains(const std::string& name) OVERRIDE { |
| 68 return source_map_.count(name); | 69 return source_map_.count(name); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void RegisterModule(const std::string& name, const std::string& source) { | 72 void RegisterModule(const std::string& name, const std::string& source) { |
| 72 CHECK_EQ(0u, source_map_.count(name)); | 73 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; |
| 73 source_map_[name] = source; | 74 source_map_[name] = source; |
| 74 } | 75 } |
| 75 | 76 |
| 76 private: | 77 private: |
| 77 std::map<std::string, std::string> source_map_; | 78 std::map<std::string, std::string> source_map_; |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 class FailsOnException : public ModuleSystem::ExceptionHandler { | 81 class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 81 public: | 82 public: |
| 82 virtual void HandleUncaughtException() OVERRIDE { | 83 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { |
| 83 FAIL(); | 84 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
| 84 } | 85 } |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 ModuleSystemTest::ModuleSystemTest() | 88 ModuleSystemTest::ModuleSystemTest() |
| 88 : isolate_(v8::Isolate::GetCurrent()), | 89 : isolate_(v8::Isolate::GetCurrent()), |
| 89 handle_scope_(isolate_), | 90 handle_scope_(isolate_), |
| 90 context_( | 91 context_( |
| 91 new extensions::ChromeV8Context( | 92 new extensions::ChromeV8Context( |
| 92 v8::Context::New(isolate_), | 93 v8::Context::New(isolate_), |
| 93 reinterpret_cast<WebKit::WebFrame*>(1), | 94 NULL, // WebFrame |
| 94 NULL, | 95 NULL, // Extension |
| 95 extensions::Feature::UNSPECIFIED_CONTEXT)), | 96 extensions::Feature::UNSPECIFIED_CONTEXT)), |
| 96 source_map_(new StringSourceMap()), | 97 source_map_(new StringSourceMap()), |
| 97 should_assertions_be_made_(true) { | 98 should_assertions_be_made_(true) { |
| 98 context_->v8_context()->Enter(); | 99 context_->v8_context()->Enter(); |
| 99 assert_natives_ = new AssertNatives(context_.get()); | 100 assert_natives_ = new AssertNatives(context_.get()); |
| 100 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get())); | 101 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get())); |
| 101 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( | 102 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
| 102 assert_natives_)); | 103 assert_natives_)); |
| 103 module_system_->SetExceptionHandlerForTest( | 104 module_system_->SetExceptionHandlerForTest( |
| 104 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); | 105 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 should_assertions_be_made_ = false; | 139 should_assertions_be_made_ = false; |
| 139 } | 140 } |
| 140 | 141 |
| 141 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { | 142 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { |
| 142 v8::HandleScope handle_scope; | 143 v8::HandleScope handle_scope; |
| 143 v8::Handle<v8::Object> object = v8::Object::New(); | 144 v8::Handle<v8::Object> object = v8::Object::New(); |
| 144 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), | 145 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), |
| 145 object); | 146 object); |
| 146 return handle_scope.Close(object); | 147 return handle_scope.Close(object); |
| 147 } | 148 } |
| OLD | NEW |