Switch to clang for nocompile tests and rebaseline existing results.
[chromium-blink-merge.git] / extensions / renderer / api_test_base.h
blob3684080468d5a55c08092d060468a8ebd6a90879
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 "mojo/bindings/js/handle.h"
21 #include "mojo/public/cpp/bindings/interface_request.h"
22 #include "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 // A base class for unit testing apps/extensions API custom bindings implemented
75 // on Mojo services. To use:
76 // 1. Register test Mojo service implementations on service_provider().
77 // 2. Write JS tests in extensions/test/data/test_file.js.
78 // 3. Write one C++ test function for each JS test containing
79 // RunTest("test_file.js", "testFunctionName").
80 // See extensions/renderer/api_test_base_unittest.cc and
81 // extensions/test/data/api_test_base_unittest.js for sample usage.
82 class ApiTestBase : public ModuleSystemTest {
83 protected:
84 ApiTestBase();
85 ~ApiTestBase() override;
86 void SetUp() override;
87 void RunTest(const std::string& file_name, const std::string& test_name);
88 TestServiceProvider* service_provider() { return service_provider_; }
90 private:
91 void RegisterModules();
92 void InitializeEnvironment();
93 void RunTestInner(const std::string& test_name,
94 const base::Closure& quit_closure);
95 void RunPromisesAgain();
97 base::MessageLoop message_loop_;
98 TestServiceProvider* service_provider_;
99 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
102 } // namespace extensions
104 #endif // EXTENSIONS_RENDERER_API_TEST_BASE_H_