| 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 #include "extensions/renderer/api_binding_js_util.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "extensions/renderer/api_binding_test_util.h" | |
| 9 #include "extensions/renderer/api_bindings_system.h" | |
| 10 #include "extensions/renderer/api_bindings_system_unittest.h" | |
| 11 #include "gin/handle.h" | |
| 12 | |
| 13 namespace extensions { | |
| 14 | |
| 15 class APIBindingJSUtilUnittest : public APIBindingsSystemTest { | |
| 16 protected: | |
| 17 APIBindingJSUtilUnittest() {} | |
| 18 ~APIBindingJSUtilUnittest() override {} | |
| 19 | |
| 20 gin::Handle<APIBindingJSUtil> CreateUtil() { | |
| 21 return gin::CreateHandle( | |
| 22 isolate(), | |
| 23 new APIBindingJSUtil(bindings_system()->type_reference_map(), | |
| 24 bindings_system()->request_handler(), | |
| 25 bindings_system()->event_handler(), | |
| 26 base::Bind(&RunFunctionOnGlobalAndIgnoreResult))); | |
| 27 } | |
| 28 | |
| 29 v8::Local<v8::Object> GetLastErrorParent( | |
| 30 v8::Local<v8::Context> context) override { | |
| 31 return context->Global(); | |
| 32 } | |
| 33 | |
| 34 std::string GetExposedError(v8::Local<v8::Context> context) { | |
| 35 v8::Local<v8::Value> last_error = | |
| 36 GetPropertyFromObject(context->Global(), context, "lastError"); | |
| 37 | |
| 38 // Use ADD_FAILURE() to avoid messing up the return type with ASSERT. | |
| 39 if (last_error.IsEmpty()) { | |
| 40 ADD_FAILURE(); | |
| 41 return std::string(); | |
| 42 } | |
| 43 if (!last_error->IsObject() && !last_error->IsUndefined()) { | |
| 44 ADD_FAILURE(); | |
| 45 return std::string(); | |
| 46 } | |
| 47 | |
| 48 if (last_error->IsUndefined()) | |
| 49 return "[undefined]"; | |
| 50 return GetStringPropertyFromObject(last_error.As<v8::Object>(), context, | |
| 51 "message"); | |
| 52 } | |
| 53 | |
| 54 APILastError* last_error() { | |
| 55 return bindings_system()->request_handler()->last_error(); | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 DISALLOW_COPY_AND_ASSIGN(APIBindingJSUtilUnittest); | |
| 60 }; | |
| 61 | |
| 62 TEST_F(APIBindingJSUtilUnittest, TestSetLastError) { | |
| 63 v8::HandleScope handle_scope(isolate()); | |
| 64 v8::Local<v8::Context> context = MainContext(); | |
| 65 | |
| 66 gin::Handle<APIBindingJSUtil> util = CreateUtil(); | |
| 67 v8::Local<v8::Object> v8_util = util.ToV8().As<v8::Object>(); | |
| 68 | |
| 69 EXPECT_FALSE(last_error()->HasError(context)); | |
| 70 EXPECT_EQ("[undefined]", GetExposedError(context)); | |
| 71 const char kSetLastError[] = "obj.setLastError('a last error');"; | |
| 72 CallFunctionOnObject(context, v8_util, kSetLastError); | |
| 73 EXPECT_TRUE(last_error()->HasError(context)); | |
| 74 EXPECT_EQ("\"a last error\"", GetExposedError(context)); | |
| 75 | |
| 76 CallFunctionOnObject(context, v8_util, | |
| 77 "obj.setLastError('a new last error')"); | |
| 78 EXPECT_TRUE(last_error()->HasError(context)); | |
| 79 EXPECT_EQ("\"a new last error\"", GetExposedError(context)); | |
| 80 } | |
| 81 | |
| 82 TEST_F(APIBindingJSUtilUnittest, TestHasLastError) { | |
| 83 v8::HandleScope handle_scope(isolate()); | |
| 84 v8::Local<v8::Context> context = MainContext(); | |
| 85 | |
| 86 gin::Handle<APIBindingJSUtil> util = CreateUtil(); | |
| 87 v8::Local<v8::Object> v8_util = util.ToV8().As<v8::Object>(); | |
| 88 | |
| 89 EXPECT_FALSE(last_error()->HasError(context)); | |
| 90 EXPECT_EQ("[undefined]", GetExposedError(context)); | |
| 91 const char kHasLastError[] = "return obj.hasLastError();"; | |
| 92 v8::Local<v8::Value> has_error = | |
| 93 CallFunctionOnObject(context, v8_util, kHasLastError); | |
| 94 EXPECT_EQ("false", V8ToString(has_error, context)); | |
| 95 | |
| 96 last_error()->SetError(context, "an error"); | |
| 97 EXPECT_TRUE(last_error()->HasError(context)); | |
| 98 EXPECT_EQ("\"an error\"", GetExposedError(context)); | |
| 99 has_error = CallFunctionOnObject(context, v8_util, kHasLastError); | |
| 100 EXPECT_EQ("true", V8ToString(has_error, context)); | |
| 101 | |
| 102 last_error()->ClearError(context, false); | |
| 103 EXPECT_FALSE(last_error()->HasError(context)); | |
| 104 EXPECT_EQ("[undefined]", GetExposedError(context)); | |
| 105 has_error = CallFunctionOnObject(context, v8_util, kHasLastError); | |
| 106 EXPECT_EQ("false", V8ToString(has_error, context)); | |
| 107 } | |
| 108 | |
| 109 TEST_F(APIBindingJSUtilUnittest, TestRunWithLastError) { | |
| 110 v8::HandleScope handle_scope(isolate()); | |
| 111 v8::Local<v8::Context> context = MainContext(); | |
| 112 | |
| 113 gin::Handle<APIBindingJSUtil> util = CreateUtil(); | |
| 114 v8::Local<v8::Object> v8_util = util.ToV8().As<v8::Object>(); | |
| 115 | |
| 116 EXPECT_FALSE(last_error()->HasError(context)); | |
| 117 EXPECT_EQ("[undefined]", GetExposedError(context)); | |
| 118 | |
| 119 const char kRunWithLastError[] = | |
| 120 "obj.runCallbackWithLastError('last error', function() {\n" | |
| 121 " this.exposedLastError =\n" | |
| 122 " this.lastError ? this.lastError.message : 'undefined';\n" | |
| 123 "}, [1, 'foo']);"; | |
| 124 CallFunctionOnObject(context, v8_util, kRunWithLastError); | |
| 125 | |
| 126 EXPECT_FALSE(last_error()->HasError(context)); | |
| 127 EXPECT_EQ("[undefined]", GetExposedError(context)); | |
| 128 EXPECT_EQ("\"last error\"", | |
| 129 GetStringPropertyFromObject(context->Global(), context, | |
| 130 "exposedLastError")); | |
| 131 } | |
| 132 | |
| 133 TEST_F(APIBindingJSUtilUnittest, TestSendRequestWithOptions) { | |
| 134 v8::HandleScope handle_scope(isolate()); | |
| 135 v8::Local<v8::Context> context = MainContext(); | |
| 136 | |
| 137 gin::Handle<APIBindingJSUtil> util = CreateUtil(); | |
| 138 v8::Local<v8::Object> v8_util = util.ToV8().As<v8::Object>(); | |
| 139 | |
| 140 const char kSendRequestWithNoOptions[] = | |
| 141 "obj.sendRequest('alpha.functionWithCallback',\n" | |
| 142 " ['someString', function() {}], undefined, undefined);"; | |
| 143 CallFunctionOnObject(context, v8_util, kSendRequestWithNoOptions); | |
| 144 ASSERT_TRUE(last_request()); | |
| 145 EXPECT_EQ("alpha.functionWithCallback", last_request()->method_name); | |
| 146 EXPECT_EQ("[\"someString\"]", ValueToString(*last_request()->arguments)); | |
| 147 EXPECT_EQ(binding::RequestThread::UI, last_request()->thread); | |
| 148 reset_last_request(); | |
| 149 | |
| 150 const char kSendRequestForIOThread[] = | |
| 151 "obj.sendRequest('alpha.functionWithCallback',\n" | |
| 152 " ['someOtherString', function() {}], undefined,\n" | |
| 153 " {__proto__: null, forIOThread: true});"; | |
| 154 CallFunctionOnObject(context, v8_util, kSendRequestForIOThread); | |
| 155 ASSERT_TRUE(last_request()); | |
| 156 EXPECT_EQ("alpha.functionWithCallback", last_request()->method_name); | |
| 157 EXPECT_EQ("[\"someOtherString\"]", ValueToString(*last_request()->arguments)); | |
| 158 EXPECT_EQ(binding::RequestThread::IO, last_request()->thread); | |
| 159 reset_last_request(); | |
| 160 | |
| 161 const char kSendRequestForUIThread[] = | |
| 162 "obj.sendRequest('alpha.functionWithCallback',\n" | |
| 163 " ['someOtherString', function() {}], undefined,\n" | |
| 164 " {__proto__: null, forIOThread: false});"; | |
| 165 CallFunctionOnObject(context, v8_util, kSendRequestForUIThread); | |
| 166 ASSERT_TRUE(last_request()); | |
| 167 EXPECT_EQ("alpha.functionWithCallback", last_request()->method_name); | |
| 168 EXPECT_EQ("[\"someOtherString\"]", ValueToString(*last_request()->arguments)); | |
| 169 EXPECT_EQ(binding::RequestThread::UI, last_request()->thread); | |
| 170 reset_last_request(); | |
| 171 | |
| 172 const char kSendRequestWithCustomCallback[] = | |
| 173 R"(obj.sendRequest( | |
| 174 'alpha.functionWithCallback', | |
| 175 ['stringy', function() {}], undefined, | |
| 176 { | |
| 177 __proto__: null, | |
| 178 customCallback: function() { | |
| 179 this.callbackCalled = true; | |
| 180 }, | |
| 181 });)"; | |
| 182 CallFunctionOnObject(context, v8_util, kSendRequestWithCustomCallback); | |
| 183 ASSERT_TRUE(last_request()); | |
| 184 EXPECT_EQ("alpha.functionWithCallback", last_request()->method_name); | |
| 185 EXPECT_EQ("[\"stringy\"]", ValueToString(*last_request()->arguments)); | |
| 186 EXPECT_EQ(binding::RequestThread::UI, last_request()->thread); | |
| 187 bindings_system()->CompleteRequest(last_request()->request_id, | |
| 188 base::ListValue(), std::string()); | |
| 189 EXPECT_EQ("true", GetStringPropertyFromObject(context->Global(), context, | |
| 190 "callbackCalled")); | |
| 191 } | |
| 192 | |
| 193 } // namespace extensions | |
| OLD | NEW |