blob: 5382fed71756a43e0782be1b4139d5773a7a01f1 [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]4f1633f2013-03-09 14:26:2410#include "chrome/renderer/extensions/object_backed_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;
[email protected]4f1633f2013-03-09 14:26:2418using extensions::ObjectBackedNativeHandler;
[email protected]3c6babd2012-08-28 03:17:2919
[email protected]1164b862012-05-09 22:38:3720// Native JS functions for doing asserts.
[email protected]4f1633f2013-03-09 14:26:2421class AssertNatives : public ObjectBackedNativeHandler {
[email protected]1164b862012-05-09 22:38:3722 public:
[email protected]9a598442013-06-04 16:39:1223 explicit AssertNatives(extensions::ChromeV8Context* context)
[email protected]4f1633f2013-03-09 14:26:2424 : ObjectBackedNativeHandler(context),
[email protected]2e0e0bc2013-02-04 10:30:3425 assertion_made_(false),
[email protected]1164b862012-05-09 22:38:3726 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.
56class StringSourceMap : public ModuleSystem::SourceMap {
57 public:
58 StringSourceMap() {}
59 virtual ~StringSourceMap() {}
60
[email protected]be9826e62013-02-07 02:00:5861 virtual v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:3762 if (source_map_.count(name) == 0)
63 return v8::Undefined();
64 return v8::String::New(source_map_[name].c_str());
65 }
66
[email protected]be9826e62013-02-07 02:00:5867 virtual bool Contains(const std::string& name) OVERRIDE {
[email protected]1164b862012-05-09 22:38:3768 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]144114942012-12-04 07:23:2380class FailsOnException : public ModuleSystem::ExceptionHandler {
81 public:
[email protected]be9826e62013-02-07 02:00:5882 virtual void HandleUncaughtException() OVERRIDE {
[email protected]144114942012-12-04 07:23:2383 FAIL();
84 }
85};
86
[email protected]1164b862012-05-09 22:38:3787ModuleSystemTest::ModuleSystemTest()
[email protected]48002af2013-05-08 23:06:2488 : isolate_(v8::Isolate::GetCurrent()),
89 handle_scope_(isolate_),
[email protected]9a598442013-06-04 16:39:1290 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]1164b862012-05-09 22:38:3796 source_map_(new StringSourceMap()),
97 should_assertions_be_made_(true) {
[email protected]9a598442013-06-04 16:39:1298 context_->v8_context()->Enter();
[email protected]4f1633f2013-03-09 14:26:2499 assert_natives_ = new AssertNatives(context_.get());
100 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get()));
[email protected]1164b862012-05-09 22:38:37101 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
102 assert_natives_));
[email protected]95ee77da2013-03-19 21:11:11103 module_system_->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:23104 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37105}
106
107ModuleSystemTest::~ModuleSystemTest() {
108 module_system_.reset();
[email protected]9a598442013-06-04 16:39:12109 context_->v8_context()->Exit();
[email protected]1164b862012-05-09 22:38:37110}
111
112void ModuleSystemTest::RegisterModule(const std::string& name,
113 const std::string& code) {
114 source_map_->RegisterModule(name, code);
115}
116
[email protected]11844fa2012-05-10 00:35:59117void ModuleSystemTest::RegisterModule(const std::string& name,
118 int resource_id) {
119 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40120 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59121 source_map_->RegisterModule(name, code);
122}
123
124void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
125 const std::string& code) {
126 RegisterModule(name, code);
[email protected]95ee77da2013-03-19 21:11:11127 module_system_->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59128}
129
[email protected]1164b862012-05-09 22:38:37130void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37131 // 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
137void ModuleSystemTest::ExpectNoAssertionsMade() {
138 should_assertions_be_made_ = false;
139}
140
141v8::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}