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_last_error.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "extensions/renderer/api_binding_test.h" | |
10 #include "extensions/renderer/api_binding_test_util.h" | |
11 #include "gin/converter.h" | |
12 #include "gin/public/context_holder.h" | |
13 | |
14 namespace extensions { | |
15 | |
16 namespace { | |
17 | |
18 std::string GetLastError(v8::Local<v8::Object> parent, | |
19 v8::Local<v8::Context> context) { | |
20 v8::Local<v8::Value> last_error = | |
21 GetPropertyFromObject(parent, context, "lastError"); | |
22 if (last_error.IsEmpty() || !last_error->IsObject()) | |
23 return V8ToString(last_error, context); | |
24 v8::Local<v8::Value> message = | |
25 GetPropertyFromObject(last_error.As<v8::Object>(), context, "message"); | |
26 return V8ToString(message, context); | |
27 } | |
28 | |
29 using ParentList = | |
30 std::vector<std::pair<v8::Local<v8::Context>, v8::Local<v8::Object>>>; | |
31 v8::Local<v8::Object> GetParent(const ParentList& parents, | |
32 v8::Local<v8::Context> context) { | |
33 // This would be simpler with a map<context, object>, but Local<> doesn't | |
34 // define an operator<. | |
35 for (const auto& parent : parents) { | |
36 if (parent.first == context) | |
37 return parent.second; | |
38 } | |
39 return v8::Local<v8::Object>(); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 using APILastErrorTest = APIBindingTest; | |
45 | |
46 // Test basic functionality of the lastError object. | |
47 TEST_F(APILastErrorTest, TestLastError) { | |
48 v8::HandleScope handle_scope(isolate()); | |
49 v8::Local<v8::Context> context = ContextLocal(); | |
50 v8::Local<v8::Object> parent_object = v8::Object::New(isolate()); | |
51 | |
52 ParentList parents = {{context, parent_object}}; | |
53 APILastError last_error(base::Bind(&GetParent, parents)); | |
54 | |
55 EXPECT_EQ("undefined", GetLastError(parent_object, context)); | |
jbroman
2017/02/13 19:41:27
At least once, you might want to check that the pr
Devlin
2017/02/14 04:57:08
Good call; done.
| |
56 | |
57 last_error.SetError(context, "Some last error"); | |
58 EXPECT_EQ("\"Some last error\"", GetLastError(parent_object, context)); | |
59 | |
60 last_error.ClearError(context, false); | |
61 EXPECT_EQ("undefined", GetLastError(parent_object, context)); | |
62 } | |
63 | |
64 // Test throwing an error if the last error wasn't checked. | |
65 TEST_F(APILastErrorTest, ReportIfUnchecked) { | |
66 v8::HandleScope handle_scope(isolate()); | |
67 v8::Local<v8::Context> context = ContextLocal(); | |
68 v8::Local<v8::Object> parent_object = v8::Object::New(isolate()); | |
69 | |
70 ParentList parents = {{context, parent_object}}; | |
71 APILastError last_error(base::Bind(&GetParent, parents)); | |
72 | |
73 { | |
74 v8::TryCatch try_catch(isolate()); | |
75 last_error.SetError(context, "foo"); | |
76 // GetLastError() will count as accessing the error property, so we | |
77 // shouldn't throw an exception. | |
78 EXPECT_EQ("\"foo\"", GetLastError(parent_object, context)); | |
79 last_error.ClearError(context, true); | |
80 EXPECT_FALSE(try_catch.HasCaught()); | |
81 } | |
82 | |
83 { | |
84 v8::TryCatch try_catch(isolate()); | |
85 // This time, we should throw an exception. | |
86 last_error.SetError(context, "A last error"); | |
87 last_error.ClearError(context, true); | |
88 ASSERT_TRUE(try_catch.HasCaught()); | |
89 EXPECT_EQ("Uncaught Error: A last error", | |
90 gin::V8ToString(try_catch.Message()->Get())); | |
91 } | |
92 } | |
93 | |
94 // Test behavior when something else sets a lastError property on the parent | |
95 // object. | |
96 TEST_F(APILastErrorTest, NonLastErrorObject) { | |
97 v8::HandleScope handle_scope(isolate()); | |
98 v8::Local<v8::Context> context = ContextLocal(); | |
99 v8::Local<v8::Object> parent_object = v8::Object::New(isolate()); | |
100 | |
101 ParentList parents = {{context, parent_object}}; | |
102 APILastError last_error(base::Bind(&GetParent, parents)); | |
103 | |
104 auto checked_set = [context](v8::Local<v8::Object> object, | |
105 base::StringPiece key, | |
106 v8::Local<v8::Value> value) { | |
107 v8::Maybe<bool> success = object->Set( | |
108 context, gin::StringToSymbol(context->GetIsolate(), key), value); | |
109 ASSERT_TRUE(success.IsJust()); | |
110 ASSERT_TRUE(success.FromJust()); | |
111 }; | |
112 | |
113 // Set a "fake" lastError property on the parent. | |
114 v8::Local<v8::Object> fake_last_error = v8::Object::New(isolate()); | |
115 checked_set(fake_last_error, "message", | |
116 gin::StringToV8(isolate(), "fake error")); | |
117 checked_set(parent_object, "lastError", fake_last_error); | |
118 | |
119 EXPECT_EQ("\"fake error\"", GetLastError(parent_object, context)); | |
120 | |
121 // The bindings shouldn't mangle an existing property. | |
122 // TODO(devlin): Or should we? If someone sets chrome.runtime.lastError, it | |
lazyboy
2017/02/13 21:54:27
This TODO feels more appropriate in line ~90 in ap
Devlin
2017/02/14 04:57:08
Done.
| |
123 // might be the right course of action to overwrite it. | |
124 last_error.SetError(context, "Real last error"); | |
125 EXPECT_EQ("\"fake error\"", GetLastError(parent_object, context)); | |
126 last_error.ClearError(context, false); | |
127 EXPECT_EQ("\"fake error\"", GetLastError(parent_object, context)); | |
128 } | |
129 | |
130 // Test lastError in multiple different contexts. | |
131 TEST_F(APILastErrorTest, MultipleContexts) { | |
132 v8::HandleScope handle_scope(isolate()); | |
133 v8::Local<v8::Context> context_a = ContextLocal(); | |
134 v8::Local<v8::Context> context_b = v8::Context::New(isolate()); | |
135 gin::ContextHolder holder_b(isolate()); | |
136 holder_b.SetContext(context_b); | |
137 | |
138 v8::Local<v8::Object> parent_a = v8::Object::New(isolate()); | |
139 v8::Local<v8::Object> parent_b = v8::Object::New(isolate()); | |
140 ParentList parents = {{context_a, parent_a}, {context_b, parent_b}}; | |
141 APILastError last_error(base::Bind(&GetParent, parents)); | |
142 | |
143 last_error.SetError(context_a, "Last error a"); | |
144 EXPECT_EQ("\"Last error a\"", GetLastError(parent_a, context_a)); | |
145 EXPECT_EQ("undefined", GetLastError(parent_b, context_b)); | |
146 | |
147 last_error.SetError(context_b, "Last error b"); | |
148 EXPECT_EQ("\"Last error a\"", GetLastError(parent_a, context_a)); | |
149 EXPECT_EQ("\"Last error b\"", GetLastError(parent_b, context_b)); | |
150 | |
151 last_error.ClearError(context_b, false); | |
152 EXPECT_EQ("\"Last error a\"", GetLastError(parent_a, context_a)); | |
153 EXPECT_EQ("undefined", GetLastError(parent_b, context_b)); | |
154 | |
155 last_error.ClearError(context_a, false); | |
156 EXPECT_EQ("undefined", GetLastError(parent_a, context_a)); | |
157 EXPECT_EQ("undefined", GetLastError(parent_b, context_b)); | |
158 } | |
159 | |
160 } // namespace extensions | |
OLD | NEW |