| 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_(v8::Context::New(isolate_)), | 91 context_(v8::Context::New(isolate_)), |
| 91 source_map_(new StringSourceMap()), | 92 source_map_(new StringSourceMap()), |
| 92 should_assertions_be_made_(true) { | 93 should_assertions_be_made_(true) { |
| 93 context_->Enter(); | 94 context_->Enter(); |
| 94 assert_natives_ = new AssertNatives(context_.get()); | 95 assert_natives_ = new AssertNatives(context_.get()); |
| 95 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get())); | 96 module_system_.reset(new ModuleSystem( |
| 97 new extensions::ChromeV8Context(context_.get(), |
| 98 NULL, // WebFrame |
| 99 NULL, // Extension |
| 100 extensions::Feature::UNSPECIFIED_CONTEXT), |
| 101 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_->Exit(); |
| 105 } | 111 } |
| (...skipping 27 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 |