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 } // namespace extensions |
OLD | NEW |