[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/test/base/module_system_test.h" |
| 6 | |
| 7 | #include "base/callback.h" |
| 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | 4570a25 | 2013-03-31 00:35:43 | [diff] [blame] | 9 | #include "base/strings/string_piece.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 10 | #include "chrome/renderer/extensions/object_backed_native_handler.h" |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 11 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 12 | |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 16 | using extensions::ModuleSystem; |
| 17 | using extensions::NativeHandler; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 18 | using extensions::ObjectBackedNativeHandler; |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 19 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 20 | // Native JS functions for doing asserts. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 21 | class AssertNatives : public ObjectBackedNativeHandler { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 22 | public: |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame^] | 23 | explicit AssertNatives(extensions::ChromeV8Context* context) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 24 | : ObjectBackedNativeHandler(context), |
[email protected] | 2e0e0bc | 2013-02-04 10:30:34 | [diff] [blame] | 25 | assertion_made_(false), |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 26 | failed_(false) { |
| 27 | RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, |
| 28 | base::Unretained(this))); |
| 29 | RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse, |
| 30 | base::Unretained(this))); |
| 31 | } |
| 32 | |
| 33 | bool assertion_made() { return assertion_made_; } |
| 34 | bool failed() { return failed_; } |
| 35 | |
| 36 | v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) { |
| 37 | CHECK_EQ(1, args.Length()); |
| 38 | assertion_made_ = true; |
| 39 | failed_ = failed_ || !args[0]->ToBoolean()->Value(); |
| 40 | return v8::Undefined(); |
| 41 | } |
| 42 | |
| 43 | v8::Handle<v8::Value> AssertFalse(const v8::Arguments& args) { |
| 44 | CHECK_EQ(1, args.Length()); |
| 45 | assertion_made_ = true; |
| 46 | failed_ = failed_ || args[0]->ToBoolean()->Value(); |
| 47 | return v8::Undefined(); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | bool assertion_made_; |
| 52 | bool failed_; |
| 53 | }; |
| 54 | |
| 55 | // Source map that operates on std::strings. |
| 56 | class StringSourceMap : public ModuleSystem::SourceMap { |
| 57 | public: |
| 58 | StringSourceMap() {} |
| 59 | virtual ~StringSourceMap() {} |
| 60 | |
[email protected] | be9826e6 | 2013-02-07 02:00:58 | [diff] [blame] | 61 | virtual v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 62 | if (source_map_.count(name) == 0) |
| 63 | return v8::Undefined(); |
| 64 | return v8::String::New(source_map_[name].c_str()); |
| 65 | } |
| 66 | |
[email protected] | be9826e6 | 2013-02-07 02:00:58 | [diff] [blame] | 67 | virtual bool Contains(const std::string& name) OVERRIDE { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 68 | return source_map_.count(name); |
| 69 | } |
| 70 | |
| 71 | void RegisterModule(const std::string& name, const std::string& source) { |
| 72 | CHECK_EQ(0u, source_map_.count(name)); |
| 73 | source_map_[name] = source; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::map<std::string, std::string> source_map_; |
| 78 | }; |
| 79 | |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 80 | class FailsOnException : public ModuleSystem::ExceptionHandler { |
| 81 | public: |
[email protected] | be9826e6 | 2013-02-07 02:00:58 | [diff] [blame] | 82 | virtual void HandleUncaughtException() OVERRIDE { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 83 | FAIL(); |
| 84 | } |
| 85 | }; |
| 86 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 87 | ModuleSystemTest::ModuleSystemTest() |
[email protected] | 48002af | 2013-05-08 23:06:24 | [diff] [blame] | 88 | : isolate_(v8::Isolate::GetCurrent()), |
| 89 | handle_scope_(isolate_), |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame^] | 90 | context_( |
| 91 | new extensions::ChromeV8Context( |
| 92 | v8::Context::New(isolate_), |
| 93 | reinterpret_cast<WebKit::WebFrame*>(1), |
| 94 | NULL, |
| 95 | extensions::Feature::UNSPECIFIED_CONTEXT)), |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 96 | source_map_(new StringSourceMap()), |
| 97 | should_assertions_be_made_(true) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame^] | 98 | context_->v8_context()->Enter(); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 99 | assert_natives_ = new AssertNatives(context_.get()); |
| 100 | module_system_.reset(new ModuleSystem(context_.get(), source_map_.get())); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 101 | module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
| 102 | assert_natives_)); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 103 | module_system_->SetExceptionHandlerForTest( |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 104 | scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException)); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ModuleSystemTest::~ModuleSystemTest() { |
| 108 | module_system_.reset(); |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame^] | 109 | context_->v8_context()->Exit(); |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void ModuleSystemTest::RegisterModule(const std::string& name, |
| 113 | const std::string& code) { |
| 114 | source_map_->RegisterModule(name, code); |
| 115 | } |
| 116 | |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 117 | void ModuleSystemTest::RegisterModule(const std::string& name, |
| 118 | int resource_id) { |
| 119 | const std::string& code = ResourceBundle::GetSharedInstance(). |
[email protected] | 4d8bb1a9 | 2012-11-01 21:12:40 | [diff] [blame] | 120 | GetRawDataResource(resource_id).as_string(); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 121 | source_map_->RegisterModule(name, code); |
| 122 | } |
| 123 | |
| 124 | void ModuleSystemTest::OverrideNativeHandler(const std::string& name, |
| 125 | const std::string& code) { |
| 126 | RegisterModule(name, code); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 127 | module_system_->OverrideNativeHandlerForTest(name); |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 130 | void ModuleSystemTest::TearDown() { |
[email protected] | 1164b86 | 2012-05-09 22:38:37 | [diff] [blame] | 131 | // All tests must assert at least once unless otherwise specified. |
| 132 | EXPECT_EQ(should_assertions_be_made_, |
| 133 | assert_natives_->assertion_made()); |
| 134 | EXPECT_FALSE(assert_natives_->failed()); |
| 135 | } |
| 136 | |
| 137 | void ModuleSystemTest::ExpectNoAssertionsMade() { |
| 138 | should_assertions_be_made_ = false; |
| 139 | } |
| 140 | |
| 141 | v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) { |
| 142 | v8::HandleScope handle_scope; |
| 143 | v8::Handle<v8::Object> object = v8::Object::New(); |
| 144 | v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()), |
| 145 | object); |
| 146 | return handle_scope.Close(object); |
| 147 | } |