Make hitting "Enter" submit the add/change profile dialog.
[chromium-blink-merge.git] / chrome / test / base / module_system_test.cc
blob365ea7ff1fa089de2afd0d95a3fd0725c8ec38a7
1 // 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.
5 #include "chrome/test/base/module_system_test.h"
7 #include "base/callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_piece.h"
10 #include "chrome/renderer/extensions/native_handler.h"
11 #include "ui/base/resource/resource_bundle.h"
13 #include <map>
14 #include <string>
16 using extensions::ModuleSystem;
17 using extensions::NativeHandler;
19 // Native JS functions for doing asserts.
20 class AssertNatives : public NativeHandler {
21 public:
22 AssertNatives()
23 : assertion_made_(false),
24 failed_(false) {
25 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
26 base::Unretained(this)));
27 RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse,
28 base::Unretained(this)));
31 bool assertion_made() { return assertion_made_; }
32 bool failed() { return failed_; }
34 v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) {
35 CHECK_EQ(1, args.Length());
36 assertion_made_ = true;
37 failed_ = failed_ || !args[0]->ToBoolean()->Value();
38 return v8::Undefined();
41 v8::Handle<v8::Value> AssertFalse(const v8::Arguments& args) {
42 CHECK_EQ(1, args.Length());
43 assertion_made_ = true;
44 failed_ = failed_ || args[0]->ToBoolean()->Value();
45 return v8::Undefined();
48 private:
49 bool assertion_made_;
50 bool failed_;
53 // Source map that operates on std::strings.
54 class StringSourceMap : public ModuleSystem::SourceMap {
55 public:
56 StringSourceMap() {}
57 virtual ~StringSourceMap() {}
59 v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE {
60 if (source_map_.count(name) == 0)
61 return v8::Undefined();
62 return v8::String::New(source_map_[name].c_str());
65 bool Contains(const std::string& name) OVERRIDE {
66 return source_map_.count(name);
69 void RegisterModule(const std::string& name, const std::string& source) {
70 CHECK_EQ(0u, source_map_.count(name));
71 source_map_[name] = source;
74 private:
75 std::map<std::string, std::string> source_map_;
78 class FailsOnException : public ModuleSystem::ExceptionHandler {
79 public:
80 virtual void HandleUncaughtException() {
81 FAIL();
85 ModuleSystemTest::ModuleSystemTest()
86 : context_(v8::Context::New()),
87 source_map_(new StringSourceMap()),
88 should_assertions_be_made_(true) {
89 context_->Enter();
90 assert_natives_ = new AssertNatives();
91 module_system_.reset(new ModuleSystem(context_, source_map_.get()));
92 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
93 assert_natives_));
94 module_system_->set_exception_handler(
95 scoped_ptr<ModuleSystem::ExceptionHandler>(new FailsOnException));
98 ModuleSystemTest::~ModuleSystemTest() {
99 module_system_.reset();
100 context_->Exit();
101 context_.Dispose();
104 void ModuleSystemTest::RegisterModule(const std::string& name,
105 const std::string& code) {
106 source_map_->RegisterModule(name, code);
109 void ModuleSystemTest::RegisterModule(const std::string& name,
110 int resource_id) {
111 const std::string& code = ResourceBundle::GetSharedInstance().
112 GetRawDataResource(resource_id).as_string();
113 source_map_->RegisterModule(name, code);
116 void ModuleSystemTest::OverrideNativeHandler(const std::string& name,
117 const std::string& code) {
118 RegisterModule(name, code);
119 module_system_->OverrideNativeHandler(name);
122 void ModuleSystemTest::TearDown() {
123 // All tests must assert at least once unless otherwise specified.
124 EXPECT_EQ(should_assertions_be_made_,
125 assert_natives_->assertion_made());
126 EXPECT_FALSE(assert_natives_->failed());
129 void ModuleSystemTest::ExpectNoAssertionsMade() {
130 should_assertions_be_made_ = false;
133 v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
134 v8::HandleScope handle_scope;
135 v8::Handle<v8::Object> object = v8::Object::New();
136 v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()),
137 object);
138 return handle_scope.Close(object);