Home | History | Annotate | Download | only in extensions
      1 // Copyright 2013 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 "grit/renderer_resources.h"
      8 
      9 namespace extensions {
     10 namespace {
     11 
     12 class SafeBuiltinsUnittest : public ModuleSystemTest {
     13 };
     14 
     15 TEST_F(SafeBuiltinsUnittest, TestNotOriginalObject) {
     16   ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system_.get());
     17   RegisterModule("test",
     18       "var assert = requireNative('assert');\n"
     19       "Array.foo = 10;\n"
     20       "assert.AssertTrue(!$Array.hasOwnProperty('foo'));\n"
     21   );
     22   module_system_->Require("test");
     23 }
     24 
     25 TEST_F(SafeBuiltinsUnittest, TestSelf) {
     26   ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system_.get());
     27   RegisterModule("test",
     28       "var assert = requireNative('assert');\n"
     29       "Array.foo = 10;\n"
     30       "assert.AssertTrue($Array.self.foo == 10);\n"
     31       "var arr = $Array.self(1);\n"
     32       "assert.AssertTrue(arr.length == 1);\n"
     33       "assert.AssertTrue(arr[0] === undefined);\n"
     34   );
     35   module_system_->Require("test");
     36 }
     37 
     38 TEST_F(SafeBuiltinsUnittest, TestStaticFunction) {
     39   ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system_.get());
     40   RegisterModule("test",
     41       "var assert = requireNative('assert');\n"
     42       "Object.keys = function() {throw new Error()};\n"
     43       "var obj = {a: 10};\n"
     44       "var keys = $Object.keys(obj);\n"
     45       "assert.AssertTrue(keys.length == 1);\n"
     46       "assert.AssertTrue(keys[0] == 'a');\n"
     47   );
     48   module_system_->Require("test");
     49 }
     50 
     51 TEST_F(SafeBuiltinsUnittest, TestInstanceMethod) {
     52   ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system_.get());
     53   RegisterModule("test",
     54       "var assert = requireNative('assert');\n"
     55       "Array.prototype.push = function() {throw new Error();}\n"
     56       "var arr = []\n"
     57       "$Array.push(arr, 1);\n"
     58       "assert.AssertTrue(arr.length == 1);\n"
     59       "assert.AssertTrue(arr[0] == 1);\n"
     60   );
     61   module_system_->Require("test");
     62 }
     63 
     64 // NOTE: JSON is already tested in ExtensionApiTest.Messaging, via
     65 // chrome/test/data/extensions/api_test/messaging/connect/page.js.
     66 
     67 }  // namespace
     68 }  // namespace extensions
     69