| 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/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "chrome/renderer/extensions/native_handler.h" | 10 #include "chrome/renderer/extensions/native_handler.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 void RegisterModule(const std::string& name, const std::string& source) { | 69 void RegisterModule(const std::string& name, const std::string& source) { |
| 70 CHECK_EQ(0u, source_map_.count(name)); | 70 CHECK_EQ(0u, source_map_.count(name)); |
| 71 source_map_[name] = source; | 71 source_map_[name] = source; |
| 72 } | 72 } |
| 73 | 73 |
| 74 private: | 74 private: |
| 75 std::map<std::string, std::string> source_map_; | 75 std::map<std::string, std::string> source_map_; |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 79 public: |
| 80 virtual void HandleUncaughtException() { |
| 81 FAIL(); |
| 82 } |
| 83 }; |
| 84 |
| 78 ModuleSystemTest::ModuleSystemTest() | 85 ModuleSystemTest::ModuleSystemTest() |
| 79 : context_(v8::Context::New()), | 86 : context_(v8::Context::New()), |
| 80 source_map_(new StringSourceMap()), | 87 source_map_(new StringSourceMap()), |
| 81 should_assertions_be_made_(true) { | 88 should_assertions_be_made_(true) { |
| 82 context_->Enter(); | 89 context_->Enter(); |
| 83 assert_natives_ = new AssertNatives(); | 90 assert_natives_ = new AssertNatives(); |
| 84 module_system_.reset(new ModuleSystem(context_, source_map_.get())); | 91 module_system_.reset(new ModuleSystem(context_, source_map_.get())); |
| 85 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( | 92 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
| 86 assert_natives_)); | 93 assert_natives_)); |
| 87 try_catch_.SetCaptureMessage(true); | 94 module_system_->set_exception_handler( |
| 95 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
| 88 } | 96 } |
| 89 | 97 |
| 90 ModuleSystemTest::~ModuleSystemTest() { | 98 ModuleSystemTest::~ModuleSystemTest() { |
| 91 module_system_.reset(); | 99 module_system_.reset(); |
| 92 context_->Exit(); | 100 context_->Exit(); |
| 93 context_.Dispose(); | 101 context_.Dispose(); |
| 94 } | 102 } |
| 95 | 103 |
| 96 void ModuleSystemTest::RegisterModule(const std::string& name, | 104 void ModuleSystemTest::RegisterModule(const std::string& name, |
| 97 const std::string& code) { | 105 const std::string& code) { |
| 98 source_map_->RegisterModule(name, code); | 106 source_map_->RegisterModule(name, code); |
| 99 } | 107 } |
| 100 | 108 |
| 101 void ModuleSystemTest::RegisterModule(const std::string& name, | 109 void ModuleSystemTest::RegisterModule(const std::string& name, |
| 102 int resource_id) { | 110 int resource_id) { |
| 103 const std::string& code = ResourceBundle::GetSharedInstance(). | 111 const std::string& code = ResourceBundle::GetSharedInstance(). |
| 104 GetRawDataResource(resource_id).as_string(); | 112 GetRawDataResource(resource_id).as_string(); |
| 105 source_map_->RegisterModule(name, code); | 113 source_map_->RegisterModule(name, code); |
| 106 } | 114 } |
| 107 | 115 |
| 108 void ModuleSystemTest::OverrideNativeHandler(const std::string& name, | 116 void ModuleSystemTest::OverrideNativeHandler(const std::string& name, |
| 109 const std::string& code) { | 117 const std::string& code) { |
| 110 RegisterModule(name, code); | 118 RegisterModule(name, code); |
| 111 module_system_->OverrideNativeHandler(name); | 119 module_system_->OverrideNativeHandler(name); |
| 112 } | 120 } |
| 113 | 121 |
| 114 void ModuleSystemTest::TearDown() { | 122 void ModuleSystemTest::TearDown() { |
| 115 if (try_catch_.HasCaught()) | |
| 116 ModuleSystem::DumpException(try_catch_); | |
| 117 EXPECT_FALSE(try_catch_.HasCaught()); | |
| 118 // All tests must assert at least once unless otherwise specified. | 123 // All tests must assert at least once unless otherwise specified. |
| 119 EXPECT_EQ(should_assertions_be_made_, | 124 EXPECT_EQ(should_assertions_be_made_, |
| 120 assert_natives_->assertion_made()); | 125 assert_natives_->assertion_made()); |
| 121 EXPECT_FALSE(assert_natives_->failed()); | 126 EXPECT_FALSE(assert_natives_->failed()); |
| 122 } | 127 } |
| 123 | 128 |
| 124 void ModuleSystemTest::ExpectNoAssertionsMade() { | 129 void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 125 should_assertions_be_made_ = false; | 130 should_assertions_be_made_ = false; |
| 126 } | 131 } |
| 127 | 132 |
| 128 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { | 133 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { |
| 129 v8::HandleScope handle_scope; | 134 v8::HandleScope handle_scope; |
| 130 v8::Handle<v8::Object> object = v8::Object::New(); | 135 v8::Handle<v8::Object> object = v8::Object::New(); |
| 131 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), | 136 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), |
| 132 object); | 137 object); |
| 133 return handle_scope.Close(object); | 138 return handle_scope.Close(object); |
| 134 } | 139 } |
| OLD | NEW |