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