summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/extensions/utils_unittest.cc
blob: 3a718ddccd14a4477469f25e7566a2054f5a8f7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/stringprintf.h"
#include "chrome/test/base/module_system_test.h"
#include "grit/extensions_renderer_resources.h"

namespace extensions {
namespace {

class UtilsUnittest : public ModuleSystemTest {
 protected:
  void RegisterTestModule(const char* code) {
    RegisterModule("test",
                   base::StringPrintf(
                       "var assert = requireNative('assert');\n"
                       "var AssertTrue = assert.AssertTrue;\n"
                       "var AssertFalse = assert.AssertFalse;\n"
                       "var utils = require('utils');\n"
                       "%s",
                       code));
  }

 private:
  virtual void SetUp() OVERRIDE {
    ModuleSystemTest::SetUp();

    RegisterModule("utils", IDR_UTILS_JS);
    OverrideNativeHandler("schema_registry",
                          "exports.GetSchema = function() {};");
    OverrideNativeHandler("logging",
                          "exports.CHECK = function() {};\n"
                          "exports.WARNING = function() {};");
  }
};

TEST_F(UtilsUnittest, TestNothing) {
  ExpectNoAssertionsMade();
}

TEST_F(UtilsUnittest, SuperClass) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      context_->module_system());
  RegisterTestModule(
      "function SuperClassImpl() {}\n"
      "\n"
      "SuperClassImpl.prototype = {\n"
      "  attrA: 'aSuper',\n"
      "  attrB: 'bSuper',\n"
      "  func: function() { return 'func'; },\n"
      "  superFunc: function() { return 'superFunc'; }\n"
      "};\n"
      "\n"
      "function SubClassImpl() {\n"
      "  SuperClassImpl.call(this);\n"
      "}\n"
      "\n"
      "SubClassImpl.prototype = {\n"
      "  __proto__: SuperClassImpl.prototype,\n"
      "  attrA: 'aSub',\n"
      "  attrC: 'cSub',\n"
      "  func: function() { return 'overridden'; },\n"
      "  subFunc: function() { return 'subFunc'; }\n"
      "};\n"
      "\n"
      "var SuperClass = utils.expose('SuperClass',\n"
      "                              SuperClassImpl,\n"
      "                              { functions: ['func', 'superFunc'],\n"
      "                                properties: ['attrA', 'attrB'] });\n"
      "\n"
      "var SubClass = utils.expose('SubClass',\n"
      "                            SubClassImpl,\n"
      "                            { superclass: SuperClass,\n"
      "                              functions: ['subFunc'],\n"
      "                              properties: ['attrC'] });\n"
      "\n"
      "var supe = new SuperClass();\n"
      "AssertTrue(supe.attrA == 'aSuper');\n"
      "AssertTrue(supe.attrB == 'bSuper');\n"
      "AssertFalse('attrC' in supe);\n"
      "AssertTrue(supe.func() == 'func');\n"
      "AssertTrue('superFunc' in supe);\n"
      "AssertTrue(supe.superFunc() == 'superFunc');\n"
      "AssertFalse('subFunc' in supe);\n"
      "AssertTrue(supe instanceof SuperClass);\n"
      "\n"
      "var sub = new SubClass();\n"
      "AssertTrue(sub.attrA == 'aSub');\n"
      "AssertTrue(sub.attrB == 'bSuper');\n"
      "AssertTrue(sub.attrC == 'cSub');\n"
      "AssertTrue(sub.func() == 'overridden');\n"
      "AssertTrue(sub.superFunc() == 'superFunc');\n"
      "AssertTrue('subFunc' in sub);\n"
      "AssertTrue(sub.subFunc() == 'subFunc');\n"
      "AssertTrue(sub instanceof SuperClass);\n"
      "AssertTrue(sub instanceof SubClass);\n"
      "\n"
      "function SubSubClassImpl() {}\n"
      "SubSubClassImpl.prototype = Object.create(SubClassImpl.prototype);\n"
      "SubSubClassImpl.prototype.subSubFunc = function() { return 'subsub'; }\n"
      "\n"
      "var SubSubClass = utils.expose('SubSubClass',\n"
      "                               SubSubClassImpl,\n"
      "                               { superclass: SubClass,\n"
      "                                 functions: ['subSubFunc'] });\n"
      "var subsub = new SubSubClass();\n"
      "AssertTrue(subsub.attrA == 'aSub');\n"
      "AssertTrue(subsub.attrB == 'bSuper');\n"
      "AssertTrue(subsub.attrC == 'cSub');\n"
      "AssertTrue(subsub.func() == 'overridden');\n"
      "AssertTrue(subsub.superFunc() == 'superFunc');\n"
      "AssertTrue(subsub.subFunc() == 'subFunc');\n"
      "AssertTrue(subsub.subSubFunc() == 'subsub');\n"
      "AssertTrue(subsub instanceof SuperClass);\n"
      "AssertTrue(subsub instanceof SubClass);\n"
      "AssertTrue(subsub instanceof SubSubClass);\n");

  context_->module_system()->Require("test");
}

}  // namespace
}  // namespace extensions