blob: 9a96676a2ffd1e1a320bb226dd98c339be3e2485 [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]c49201a2012-05-24 11:04:5711#include "ui/base/layout.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;
19
[email protected]1164b862012-05-09 22:38:3720// Native JS functions for doing asserts.
21class AssertNatives : public NativeHandler {
22 public:
23 AssertNatives()
24 : assertion_made_(false),
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.
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
79ModuleSystemTest::ModuleSystemTest()
80 : context_(v8::Context::New()),
81 source_map_(new StringSourceMap()),
82 should_assertions_be_made_(true) {
83 context_->Enter();
84 assert_natives_ = new AssertNatives();
85 module_system_.reset(new ModuleSystem(context_, source_map_.get()));
86 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
87 assert_natives_));
88 try_catch_.SetCaptureMessage(true);
89}
90
91ModuleSystemTest::~ModuleSystemTest() {
92 module_system_.reset();
93 context_->Exit();
94 context_.Dispose();
95}
96
97void ModuleSystemTest::RegisterModule(const std::string& name,
98 const std::string& code) {
99 source_map_->RegisterModule(name, code);
100}
101
[email protected]11844fa2012-05-10 00:35:59102void ModuleSystemTest::RegisterModule(const std::string& name,
103 int resource_id) {
104 const std::string& code = ResourceBundle::GetSharedInstance().
[email protected]c49201a2012-05-24 11:04:57105 GetRawDataResource(resource_id,
106 ui::SCALE_FACTOR_NONE).as_string();
[email protected]11844fa2012-05-10 00:35:59107 source_map_->RegisterModule(name, code);
108}
109
110void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
111 const std::string& code) {
112 RegisterModule(name, code);
113 module_system_->OverrideNativeHandler(name);
114}
115
[email protected]1164b862012-05-09 22:38:37116void ModuleSystemTest::TearDown() {
[email protected]1c6189f2012-05-18 06:45:52117 if (try_catch_.HasCaught())
[email protected]452b36f2012-07-12 05:27:54118 ModuleSystem::DumpException(try_catch_);
[email protected]1164b862012-05-09 22:38:37119 EXPECT_FALSE(try_catch_.HasCaught());
120 // All tests must assert at least once unless otherwise specified.
121 EXPECT_EQ(should_assertions_be_made_,
122 assert_natives_->assertion_made());
123 EXPECT_FALSE(assert_natives_->failed());
124}
125
126void ModuleSystemTest::ExpectNoAssertionsMade() {
127 should_assertions_be_made_ = false;
128}
129
130v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
131 v8::HandleScope handle_scope;
132 v8::Handle<v8::Object> object = v8::Object::New();
133 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()),
134 object);
135 return handle_scope.Close(object);
136}