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