| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 EXTENSIONS_RENDERER_API_TEST_BASE_H_ | |
| 6 #define EXTENSIONS_RENDERER_API_TEST_BASE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "extensions/renderer/module_system_test.h" | |
| 15 #include "extensions/renderer/v8_schema_registry.h" | |
| 16 #include "gin/handle.h" | |
| 17 #include "gin/modules/module_registry.h" | |
| 18 #include "gin/object_template_builder.h" | |
| 19 #include "gin/wrappable.h" | |
| 20 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 21 #include "mojo/public/cpp/system/core.h" | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 class V8SchemaRegistry; | |
| 26 | |
| 27 // An InterfaceProvider that provides access from JS modules to interfaces | |
| 28 // registered by AddInterface() calls. | |
| 29 class TestInterfaceProvider : public gin::Wrappable<TestInterfaceProvider> { | |
| 30 public: | |
| 31 static gin::Handle<TestInterfaceProvider> Create(v8::Isolate* isolate); | |
| 32 ~TestInterfaceProvider() override; | |
| 33 | |
| 34 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
| 35 v8::Isolate* isolate) override; | |
| 36 | |
| 37 template <typename Interface> | |
| 38 void AddInterface( | |
| 39 const base::Callback<void(mojo::InterfaceRequest<Interface>)> | |
| 40 factory_callback) { | |
| 41 factories_.insert(std::make_pair( | |
| 42 Interface::Name_, | |
| 43 base::Bind(ForwardToInterfaceFactory<Interface>, factory_callback))); | |
| 44 } | |
| 45 | |
| 46 // Ignore requests for Interface. | |
| 47 template <typename Interface> | |
| 48 void IgnoreInterfaceRequests() { | |
| 49 factories_.insert(std::make_pair( | |
| 50 Interface::Name_, base::Bind(&TestInterfaceProvider::IgnoreHandle))); | |
| 51 } | |
| 52 | |
| 53 static gin::WrapperInfo kWrapperInfo; | |
| 54 | |
| 55 private: | |
| 56 TestInterfaceProvider(); | |
| 57 | |
| 58 mojo::Handle GetInterface(const std::string& interface_name); | |
| 59 | |
| 60 template <typename Interface> | |
| 61 static void ForwardToInterfaceFactory( | |
| 62 const base::Callback<void(mojo::InterfaceRequest<Interface>)> | |
| 63 factory_callback, | |
| 64 mojo::ScopedMessagePipeHandle handle) { | |
| 65 factory_callback.Run(mojo::MakeRequest<Interface>(std::move(handle))); | |
| 66 } | |
| 67 | |
| 68 static void IgnoreHandle(mojo::ScopedMessagePipeHandle handle); | |
| 69 | |
| 70 std::map<std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)> > | |
| 71 factories_; | |
| 72 }; | |
| 73 | |
| 74 // An environment for unit testing apps/extensions API custom bindings | |
| 75 // implemented on Mojo interfaces. This augments a ModuleSystemTestEnvironment | |
| 76 // with a TestInterfaceProvider and other modules available in a real extensions | |
| 77 // environment. | |
| 78 class ApiTestEnvironment { | |
| 79 public: | |
| 80 explicit ApiTestEnvironment(ModuleSystemTestEnvironment* environment); | |
| 81 ~ApiTestEnvironment(); | |
| 82 void RunTest(const std::string& file_name, const std::string& test_name); | |
| 83 TestInterfaceProvider* interface_provider() { return interface_provider_; } | |
| 84 ModuleSystemTestEnvironment* env() { return env_; } | |
| 85 | |
| 86 private: | |
| 87 void RegisterModules(); | |
| 88 void InitializeEnvironment(); | |
| 89 void RunTestInner(const std::string& test_name, | |
| 90 const base::Closure& quit_closure); | |
| 91 void RunPromisesAgain(); | |
| 92 | |
| 93 ModuleSystemTestEnvironment* env_; | |
| 94 TestInterfaceProvider* interface_provider_; | |
| 95 std::unique_ptr<V8SchemaRegistry> v8_schema_registry_; | |
| 96 }; | |
| 97 | |
| 98 // A base class for unit testing apps/extensions API custom bindings implemented | |
| 99 // on Mojo interfaces. To use: | |
| 100 // 1. Register test Mojo interface implementations on interface_provider(). | |
| 101 // 2. Write JS tests in extensions/test/data/test_file.js. | |
| 102 // 3. Write one C++ test function for each JS test containing | |
| 103 // RunTest("test_file.js", "testFunctionName"). | |
| 104 // See extensions/renderer/api_test_base_unittest.cc and | |
| 105 // extensions/test/data/api_test_base_unittest.js for sample usage. | |
| 106 class ApiTestBase : public ModuleSystemTest { | |
| 107 protected: | |
| 108 ApiTestBase(); | |
| 109 ~ApiTestBase() override; | |
| 110 void SetUp() override; | |
| 111 void RunTest(const std::string& file_name, const std::string& test_name); | |
| 112 | |
| 113 ApiTestEnvironment* api_test_env() { return test_env_.get(); } | |
| 114 TestInterfaceProvider* interface_provider() { | |
| 115 return test_env_->interface_provider(); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 base::MessageLoop message_loop_; | |
| 120 std::unique_ptr<ApiTestEnvironment> test_env_; | |
| 121 }; | |
| 122 | |
| 123 } // namespace extensions | |
| 124 | |
| 125 #endif // EXTENSIONS_RENDERER_API_TEST_BASE_H_ | |
| OLD | NEW |