Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / renderer / api_test_base.h
blob9e6feab4474e4346114d64de6da73f11df3ede49
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.
5 #ifndef EXTENSIONS_RENDERER_API_TEST_BASE_H_
6 #define EXTENSIONS_RENDERER_API_TEST_BASE_H_
8 #include <map>
9 #include <string>
10 #include <utility>
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 "third_party/mojo/src/mojo/edk/js/handle.h"
21 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
22 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
24 namespace extensions {
26 class V8SchemaRegistry;
28 // A ServiceProvider that provides access from JS modules to services registered
29 // by AddService() calls.
30 class TestServiceProvider : public gin::Wrappable<TestServiceProvider> {
31 public:
32 static gin::Handle<TestServiceProvider> Create(v8::Isolate* isolate);
33 ~TestServiceProvider() override;
35 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
36 v8::Isolate* isolate) override;
38 template <typename Interface>
39 void AddService(const base::Callback<void(mojo::InterfaceRequest<Interface>)>
40 service_factory) {
41 service_factories_.insert(std::make_pair(
42 Interface::Name_,
43 base::Bind(ForwardToServiceFactory<Interface>, service_factory)));
46 // Ignore requests for the Interface service.
47 template <typename Interface>
48 void IgnoreServiceRequests() {
49 service_factories_.insert(std::make_pair(
50 Interface::Name_, base::Bind(&TestServiceProvider::IgnoreHandle)));
53 static gin::WrapperInfo kWrapperInfo;
55 private:
56 TestServiceProvider();
58 mojo::Handle ConnectToService(const std::string& service_name);
60 template <typename Interface>
61 static void ForwardToServiceFactory(
62 const base::Callback<void(mojo::InterfaceRequest<Interface>)>
63 service_factory,
64 mojo::ScopedMessagePipeHandle handle) {
65 service_factory.Run(mojo::MakeRequest<Interface>(handle.Pass()));
68 static void IgnoreHandle(mojo::ScopedMessagePipeHandle handle);
70 std::map<std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)> >
71 service_factories_;
74 // An environment for unit testing apps/extensions API custom bindings
75 // implemented on Mojo services. This augments a ModuleSystemTestEnvironment
76 // with a TestServiceProvider 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 TestServiceProvider* service_provider() { return service_provider_; }
84 ModuleSystemTestEnvironment* env() { return env_; }
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();
93 ModuleSystemTestEnvironment* env_;
94 TestServiceProvider* service_provider_;
95 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
98 // A base class for unit testing apps/extensions API custom bindings implemented
99 // on Mojo services. To use:
100 // 1. Register test Mojo service implementations on service_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);
113 ApiTestEnvironment* api_test_env() { return test_env_.get(); }
114 TestServiceProvider* service_provider() {
115 return test_env_->service_provider();
118 private:
119 base::MessageLoop message_loop_;
120 scoped_ptr<ApiTestEnvironment> test_env_;
123 } // namespace extensions
125 #endif // EXTENSIONS_RENDERER_API_TEST_BASE_H_