blob: 56024269e7736c3e23ca20ea7ce0c9898aeeaf90 [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]4f1633f2013-03-09 14:26:2423 explicit AssertNatives(v8::Handle<v8::Context> context)
24 : 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_),
90 context_(v8::Context::New(isolate_)),
[email protected]1164b862012-05-09 22:38:3791 source_map_(new StringSourceMap()),
92 should_assertions_be_made_(true) {
93 context_->Enter();
[email protected]4f1633f2013-03-09 14:26:2494 assert_natives_ = new AssertNatives(context_.get());
95 module_system_.reset(new ModuleSystem(context_.get(), source_map_.get()));
[email protected]1164b862012-05-09 22:38:3796 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
97 assert_natives_));
[email protected]95ee77da2013-03-19 21:11:1198 module_system_->SetExceptionHandlerForTest(
[email protected]144114942012-12-04 07:23:2399 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
[email protected]1164b862012-05-09 22:38:37100}
101
102ModuleSystemTest::~ModuleSystemTest() {
103 module_system_.reset();
104 context_->Exit();
[email protected]1164b862012-05-09 22:38:37105}
106
107void ModuleSystemTest::RegisterModule(const std::string& name,
108 const std::string& code) {
109 source_map_->RegisterModule(name, code);
110}
111
[email protected]11844fa2012-05-10 00:35:59112void ModuleSystemTest::RegisterModule(const std::string& name,
113 int resource_id) {
114 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]4d8bb1a92012-11-01 21:12:40115 GetRawDataResource(resource_id).as_string();
[email protected]11844fa2012-05-10 00:35:59116 source_map_->RegisterModule(name, code);
117}
118
119void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
120 const std::string& code) {
121 RegisterModule(name, code);
[email protected]95ee77da2013-03-19 21:11:11122 module_system_->OverrideNativeHandlerForTest(name);
[email protected]11844fa2012-05-10 00:35:59123}
124
[email protected]1164b862012-05-09 22:38:37125void ModuleSystemTest::TearDown() {
[email protected]1164b862012-05-09 22:38:37126 // All tests must assert at least once unless otherwise specified.
127 EXPECT_EQ(should_assertions_be_made_,
128 assert_natives_->assertion_made());
129 EXPECT_FALSE(assert_natives_->failed());
130}
131
132void ModuleSystemTest::ExpectNoAssertionsMade() {
133 should_assertions_be_made_ = false;
134}
135
136v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
137 v8::HandleScope handle_scope;
138 v8::Handle<v8::Object> object = v8::Object::New();
139 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()),
140 object);
141 return handle_scope.Close(object);
142}