OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_BINDINGS_SYSTEM_UNITTEST_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_UNITTEST_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "extensions/renderer/api_binding_test.h" |
| 14 #include "extensions/renderer/api_binding_types.h" |
| 15 #include "extensions/renderer/api_request_handler.h" |
| 16 #include "v8/include/v8.h" |
| 17 |
| 18 namespace base { |
| 19 class DictionaryValue; |
| 20 } |
| 21 |
| 22 namespace extensions { |
| 23 class APIBindingsSystem; |
| 24 |
| 25 // The base class to test the APIBindingsSystem. This allows subclasses to |
| 26 // retrieve API schemas differently. |
| 27 class APIBindingsSystemTest : public APIBindingTest { |
| 28 protected: |
| 29 // A struct representing a "fake" API, including the name and specification. |
| 30 // The specification is expected to be a JSON-serializable string that |
| 31 // specifies the types, methods, and events of an API in the same syntax as |
| 32 // the real extension APIs. |
| 33 struct FakeSpec { |
| 34 const char* name; |
| 35 const char* spec; |
| 36 }; |
| 37 |
| 38 APIBindingsSystemTest(); |
| 39 ~APIBindingsSystemTest() override; |
| 40 void SetUp() override; |
| 41 void TearDown() override; |
| 42 void OnWillDisposeContext(v8::Local<v8::Context> context) override; |
| 43 |
| 44 // Returns the collection of fake APIs to be used in the test, allowing |
| 45 // subclasses to return their own specifications. |
| 46 virtual std::vector<FakeSpec> GetAPIs(); |
| 47 |
| 48 // Returns the object to be used as the parent for the `lastError`. The |
| 49 // default returns an empty JS object (assumes no last errors will be set). |
| 50 virtual v8::Local<v8::Object> GetLastErrorParent( |
| 51 v8::Local<v8::Context> context); |
| 52 |
| 53 // Returns the DictionaryValue representing the schema with the given API |
| 54 // name. |
| 55 const base::DictionaryValue& GetAPISchema(const std::string& api_name); |
| 56 |
| 57 // Callback for event listeners changing. |
| 58 void OnEventListenersChanged(const std::string& event_name, |
| 59 binding::EventListenersChanged changed, |
| 60 v8::Local<v8::Context> context); |
| 61 |
| 62 // Callback for an API request being made. Stores the request in |
| 63 // |last_request_|. |
| 64 void OnAPIRequest(std::unique_ptr<APIRequestHandler::Request> request, |
| 65 v8::Local<v8::Context> context); |
| 66 |
| 67 // Checks that |last_request_| exists and was provided with the |
| 68 // |expected_name| and |expected_arguments|. |
| 69 void ValidateLastRequest(const std::string& expected_name, |
| 70 const std::string& expected_arguments); |
| 71 |
| 72 // Wraps the given |script source| in (function(obj) { ... }) and executes |
| 73 // the result function, passing in |object| for an argument. |
| 74 void CallFunctionOnObject(v8::Local<v8::Context> context, |
| 75 v8::Local<v8::Object> object, |
| 76 const std::string& script_source); |
| 77 |
| 78 const APIRequestHandler::Request* last_request() const { |
| 79 return last_request_.get(); |
| 80 } |
| 81 void reset_last_request() { last_request_.reset(); } |
| 82 APIBindingsSystem* bindings_system() { return bindings_system_.get(); } |
| 83 |
| 84 private: |
| 85 // The API schemas for the fake APIs. |
| 86 std::map<std::string, std::unique_ptr<base::DictionaryValue>> api_schemas_; |
| 87 |
| 88 // The APIBindingsSystem associated with the test. Safe to use across multiple |
| 89 // contexts. |
| 90 std::unique_ptr<APIBindingsSystem> bindings_system_; |
| 91 |
| 92 // The last request to be received from the APIBindingsSystem, or null if |
| 93 // there is none. |
| 94 std::unique_ptr<APIRequestHandler::Request> last_request_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystemTest); |
| 97 }; |
| 98 |
| 99 } // namespace extensions |
| 100 |
| 101 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_UNITTEST_H_ |
OLD | NEW |