• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_
6 #define CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_
7 
8 #include "chrome/renderer/extensions/chrome_v8_context.h"
9 #include "extensions/renderer/module_system.h"
10 #include "extensions/renderer/scoped_persistent.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "v8/include/v8.h"
13 
14 // Test fixture for testing JS that makes use of the module system.
15 //
16 // Typically tests will look like:
17 //
18 // TEST_F(MyModuleSystemTest, TestStuff) {
19 //   ModuleSystem::NativesEnabledScope natives_enabled(module_system_.get());
20 //   RegisterModule("test", "requireNative('assert').AssertTrue(true);");
21 //   module_system_->Require("test");
22 // }
23 //
24 // By default a test will fail if no method in the native module 'assert' is
25 // called. This behaviour can be overridden by calling ExpectNoAssertionsMade().
26 //
27 // TODO(kalman): move this back into chrome/renderer/extensions.
28 class ModuleSystemTest : public testing::Test {
29  public:
30   ModuleSystemTest();
31   virtual ~ModuleSystemTest();
32 
33   virtual void TearDown() OVERRIDE;
34 
35  protected:
36   // Register a named JS module in the module system.
37   void RegisterModule(const std::string& name, const std::string& code);
38 
39   // Register a named JS module with source retrieved from a ResourceBundle.
40   void RegisterModule(const std::string& name, int resource_id);
41 
42   // Register a named JS module in the module system and tell the module system
43   // to use it to handle any requireNative() calls for native modules with that
44   // name.
45   void OverrideNativeHandler(const std::string& name, const std::string& code);
46 
47   // Registers |file_name| from chrome/test/data/extensions as a module name
48   // |module_name|.
49   void RegisterTestFile(const std::string& module_name,
50                         const std::string& file_name);
51 
52   // Make the test fail if any asserts are called. By default a test will fail
53   // if no asserts are called.
54   void ExpectNoAssertionsMade();
55 
56   // Create an empty object in the global scope with name |name|.
57   v8::Handle<v8::Object> CreateGlobal(const std::string& name);
58 
59   v8::Isolate* isolate_;
60   v8::HandleScope handle_scope_;
61   scoped_ptr<extensions::ChromeV8Context> context_;
62   class AssertNatives;
63   AssertNatives* assert_natives_;
64   class StringSourceMap;
65   scoped_ptr<StringSourceMap> source_map_;
66   bool should_assertions_be_made_;
67 
68  private:
69   DISALLOW_COPY_AND_ASSIGN(ModuleSystemTest);
70 };
71 
72 #endif  // CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_
73