blob: ea144c26745c118efb7733893e9e5b44fe4dce25 [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"
[email protected]4570a252013-03-31 00:35:439#include "base/strings/string_piece.h"
[email protected]68e63ea12013-06-05 05:00:5410#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]4f1633f2013-03-09 14:26:2411#include "chrome/renderer/extensions/object_backed_native_handler.h"
[email protected]11844fa2012-05-10 00:35:5912#include "ui/base/resource/resource_bundle.h"
[email protected]1164b862012-05-09 22:38:3713
14#include <map>
15#include <string>
16
[email protected]3c6babd2012-08-28 03:17:2917using extensions::ModuleSystem;
18using extensions::NativeHandler;
[email protected]4f1633f2013-03-09 14:26:2419using extensions::ObjectBackedNativeHandler;
[email protected]3c6babd2012-08-28 03:17:2920
[email protected]1164b862012-05-09 22:38:3721// Native JS functions for doing asserts.
[email protected]4f1633f2013-03-09 14:26:2422class AssertNatives : public ObjectBackedNativeHandler {
[email protected]1164b862012-05-09 22:38:3723 public:
[email protected]9a598442013-06-04 16:39:1224 explicit AssertNatives(extensions::ChromeV8Context* context)
[email protected]4f1633f2013-03-09 14:26:2425 : ObjectBackedNativeHandler(context),
[email protected]2e0e0bc2013-02-04 10:30:3426 assertion_made_(false),
[email protected]1164b862012-05-09 22:38:3727 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.
57class StringSourceMap : public ModuleSystem::SourceMap {
58 public:
59 StringSourceMap() {}
60 virtual ~StringSourceMap() {}
61
[email protected]be9826e62013-02-07 02:00:5862 virtual v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:3763 if (source_map_.count(name) == 0)
64 return v8::Undefined();
65 return v8::String::New(source_map_[name].c_str());
66 }
67
[email protected]be9826e62013-02-07 02:00:5868 virtual bool Contains(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:3769 return source_map_.count(name);
70 }
71
72 void RegisterModule(const std::string& name, const std::string& source) {
[email protected]68e63ea12013-06-05 05:00:5473 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found";
[email protected]1164b862012-05-09 22:38:3774 source_map_[name] = source;
75 }
76
77 private:
78 std::map<std::string, std::string> source_map_;
79};
80
[email protected]144114942012-12-04 07:23:2381class FailsOnException : public ModuleSystem::ExceptionHandler {
82 public:
[email protected]68e63ea12013-06-05 05:00:5483 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
84 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch);
[email protected]144114942012-12-04 07:23:2385 }
86};
87
[email protected]1164b862012-05-09 22:38:3788ModuleSystemTest::ModuleSystemTest()
[email protected]48002af2013-05-08 23:06:2489 : isolate_(v8::Isolate::GetCurrent()),
90 handle_scope_(isolate_),
[email protected]9a598442013-06-04 16:39:1291 context_(
92 new extensions::ChromeV8Context(
93 v8::Context::New(isolate_),
[email protected]68e63ea12013-06-05 05:00:5494 NULL, // WebFrame
95 NULL, // Extension
[email protected]9a598442013-06-04 16:39:1296 extensions::Feature::UNSPECIFIED_CONTEXT)),
[email protected]1164b862012-05-09 22:38:3797 source_map_(new StringSourceMap()),
98 should_assertions_be_made_(true) {
[email protected]9a598442013-06-04 16:39:1299 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:24100 assert_natives_ = new AssertNatives(context_.get());
101 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get()));
[email protected]1164b862012-05-09 22:38:37102 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
103 assert_natives_));
[email protected]95ee77da2013-03-19 21:11:11104 module_system_->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23105 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37106}
107
108ModuleSystemTest::~ModuleSystemTest() {
109 module_system_.reset();
[email protected]9a598442013-06-04 16:39:12110 context_->v8_context()->Exit();
[email protected]1164b862012-05-09 22:38:37111}
112
113void ModuleSystemTest::RegisterModule(const std::string& name,
114 const std::string& code) {
115 source_map_->RegisterModule(name, code);
116}
117
[email protected]11844fa2012-05-10 00:35:59118void ModuleSystemTest::RegisterModule(const std::string& name,
119 int resource_id) {
120 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40121 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59122 source_map_->RegisterModule(name, code);
123}
124
125void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
126 const std::string& code) {
127 RegisterModule(name, code);
[email protected]95ee77da2013-03-19 21:11:11128 module_system_->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59129}
130
[email protected]1164b862012-05-09 22:38:37131void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37132 // 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
138void ModuleSystemTest::ExpectNoAssertionsMade() {
139 should_assertions_be_made_ = false;
140}
141
142v8::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}