blob: 78464d99f18ffa31af55c6e60fb26c4507424e28 [file] [log] [blame]
[email protected]1164b862012-05-09 22:38:371// 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]3c6babd2012-08-28 03:17:2910#include "chrome/renderer/extensions/native_handler.h"
[email protected]11844fa2012-05-10 00:35:5911#include "ui/base/resource/resource_bundle.h"
[email protected]1164b862012-05-09 22:38:3712
13#include <map>
14#include <string>
15
[email protected]3c6babd2012-08-28 03:17:2916using extensions::ModuleSystem;
17using extensions::NativeHandler;
18
[email protected]1164b862012-05-09 22:38:3719// Native JS functions for doing asserts.
20class AssertNatives : public NativeHandler {
21 public:
[email protected]2e0e0bc2013-02-04 10:30:3422 explicit AssertNatives(v8::Isolate* isolate)
23 : NativeHandler(isolate),
24 assertion_made_(false),
[email protected]1164b862012-05-09 22:38:3725 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.
55class 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]144114942012-12-04 07:23:2379class FailsOnException : public ModuleSystem::ExceptionHandler {
80 public:
81 virtual void HandleUncaughtException() {
82 FAIL();
83 }
84};
85
[email protected]1164b862012-05-09 22:38:3786ModuleSystemTest::ModuleSystemTest()
87 : context_(v8::Context::New()),
88 source_map_(new StringSourceMap()),
89 should_assertions_be_made_(true) {
90 context_->Enter();
[email protected]2e0e0bc2013-02-04 10:30:3491 assert_natives_ = new AssertNatives(context_->GetIsolate());
[email protected]1164b862012-05-09 22:38:3792 module_system_.reset(new ModuleSystem(context_, source_map_.get()));
93 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
94 assert_natives_));
[email protected]144114942012-12-04 07:23:2395 module_system_->set_exception_handler(
96 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:3797}
98
99ModuleSystemTest::~ModuleSystemTest() {
100 module_system_.reset();
101 context_->Exit();
[email protected]2e0e0bc2013-02-04 10:30:34102 context_.Dispose(context_->GetIsolate());
[email protected]1164b862012-05-09 22:38:37103}
104
105void ModuleSystemTest::RegisterModule(const std::string& name,
106 const std::string& code) {
107 source_map_->RegisterModule(name, code);
108}
109
[email protected]11844fa2012-05-10 00:35:59110void ModuleSystemTest::RegisterModule(const std::string& name,
111 int resource_id) {
112 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40113 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59114 source_map_->RegisterModule(name, code);
115}
116
117void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
118 const std::string& code) {
119 RegisterModule(name, code);
120 module_system_->OverrideNativeHandler(name);
121}
122
[email protected]1164b862012-05-09 22:38:37123void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37124 // 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
130void ModuleSystemTest::ExpectNoAssertionsMade() {
131 should_assertions_be_made_ = false;
132}
133
134v8::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}