Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Side by Side Diff: extensions/renderer/api_request_handler_unittest.cc

Issue 2657613005: [Extensions Bindings] Add chrome.runtime.lastError support (Closed)
Patch Set: Remove MessageListener Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/values.h" 6 #include "base/values.h"
7 #include "extensions/renderer/api_binding_test.h" 7 #include "extensions/renderer/api_binding_test.h"
8 #include "extensions/renderer/api_binding_test_util.h" 8 #include "extensions/renderer/api_binding_test_util.h"
9 #include "extensions/renderer/api_request_handler.h" 9 #include "extensions/renderer/api_request_handler.h"
10 #include "gin/converter.h" 10 #include "gin/converter.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 DISALLOW_COPY_AND_ASSIGN(APIRequestHandlerTest); 47 DISALLOW_COPY_AND_ASSIGN(APIRequestHandlerTest);
48 }; 48 };
49 49
50 // Tests adding a request to the request handler, and then triggering the 50 // Tests adding a request to the request handler, and then triggering the
51 // response. 51 // response.
52 TEST_F(APIRequestHandlerTest, AddRequestAndCompleteRequestTest) { 52 TEST_F(APIRequestHandlerTest, AddRequestAndCompleteRequestTest) {
53 v8::HandleScope handle_scope(isolate()); 53 v8::HandleScope handle_scope(isolate());
54 v8::Local<v8::Context> context = ContextLocal(); 54 v8::Local<v8::Context> context = ContextLocal();
55 55
56 APIRequestHandler request_handler( 56 APIRequestHandler request_handler(
57 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this))); 57 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)),
58 APILastError(APILastError::GetParent()));
58 59
59 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); 60 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty());
60 61
61 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); 62 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs);
62 ASSERT_FALSE(function.IsEmpty()); 63 ASSERT_FALSE(function.IsEmpty());
63 64
64 int request_id = request_handler.AddPendingRequest(isolate(), function, 65 int request_id = request_handler.AddPendingRequest(isolate(), function,
65 context, ArgumentList()); 66 context, ArgumentList());
66 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), 67 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(),
67 testing::UnorderedElementsAre(request_id)); 68 testing::UnorderedElementsAre(request_id));
68 69
69 const char kArguments[] = "['foo',1,{'prop1':'bar'}]"; 70 const char kArguments[] = "['foo',1,{'prop1':'bar'}]";
70 std::unique_ptr<base::ListValue> response_arguments = 71 std::unique_ptr<base::ListValue> response_arguments =
71 ListValueFromString(kArguments); 72 ListValueFromString(kArguments);
72 ASSERT_TRUE(response_arguments); 73 ASSERT_TRUE(response_arguments);
73 request_handler.CompleteRequest(request_id, *response_arguments); 74 request_handler.CompleteRequest(request_id, *response_arguments,
75 std::string());
74 76
75 EXPECT_TRUE(did_run_js()); 77 EXPECT_TRUE(did_run_js());
76 EXPECT_EQ(ReplaceSingleQuotes(kArguments), 78 EXPECT_EQ(ReplaceSingleQuotes(kArguments),
77 GetStringPropertyFromObject(context->Global(), context, "result")); 79 GetStringPropertyFromObject(context->Global(), context, "result"));
78 80
79 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); 81 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty());
80 } 82 }
81 83
82 // Tests that trying to run non-existent or invalided requests is a no-op. 84 // Tests that trying to run non-existent or invalided requests is a no-op.
83 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) { 85 TEST_F(APIRequestHandlerTest, InvalidRequestsTest) {
84 v8::HandleScope handle_scope(isolate()); 86 v8::HandleScope handle_scope(isolate());
85 v8::Local<v8::Context> context = ContextLocal(); 87 v8::Local<v8::Context> context = ContextLocal();
86 88
87 APIRequestHandler request_handler( 89 APIRequestHandler request_handler(
88 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this))); 90 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)),
91 APILastError(APILastError::GetParent()));
89 92
90 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); 93 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs);
91 ASSERT_FALSE(function.IsEmpty()); 94 ASSERT_FALSE(function.IsEmpty());
92 95
93 int request_id = request_handler.AddPendingRequest(isolate(), function, 96 int request_id = request_handler.AddPendingRequest(isolate(), function,
94 context, ArgumentList()); 97 context, ArgumentList());
95 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), 98 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(),
96 testing::UnorderedElementsAre(request_id)); 99 testing::UnorderedElementsAre(request_id));
97 100
98 std::unique_ptr<base::ListValue> response_arguments = 101 std::unique_ptr<base::ListValue> response_arguments =
99 ListValueFromString("['foo']"); 102 ListValueFromString("['foo']");
100 ASSERT_TRUE(response_arguments); 103 ASSERT_TRUE(response_arguments);
101 104
102 // Try running with a non-existent request id. 105 // Try running with a non-existent request id.
103 int fake_request_id = 42; 106 int fake_request_id = 42;
104 request_handler.CompleteRequest(fake_request_id, *response_arguments); 107 request_handler.CompleteRequest(fake_request_id, *response_arguments,
108 std::string());
105 EXPECT_FALSE(did_run_js()); 109 EXPECT_FALSE(did_run_js());
106 110
107 // Try running with a request from an invalidated context. 111 // Try running with a request from an invalidated context.
108 request_handler.InvalidateContext(context); 112 request_handler.InvalidateContext(context);
109 request_handler.CompleteRequest(request_id, *response_arguments); 113 request_handler.CompleteRequest(request_id, *response_arguments,
114 std::string());
110 EXPECT_FALSE(did_run_js()); 115 EXPECT_FALSE(did_run_js());
111 } 116 }
112 117
113 TEST_F(APIRequestHandlerTest, MultipleRequestsAndContexts) { 118 TEST_F(APIRequestHandlerTest, MultipleRequestsAndContexts) {
114 v8::HandleScope handle_scope(isolate()); 119 v8::HandleScope handle_scope(isolate());
115 v8::Local<v8::Context> context_a = ContextLocal(); 120 v8::Local<v8::Context> context_a = ContextLocal();
116 v8::Local<v8::Context> context_b = v8::Context::New(isolate()); 121 v8::Local<v8::Context> context_b = v8::Context::New(isolate());
117 gin::ContextHolder holder_b(isolate()); 122 gin::ContextHolder holder_b(isolate());
118 holder_b.SetContext(context_b); 123 holder_b.SetContext(context_b);
119 124
120 APIRequestHandler request_handler( 125 APIRequestHandler request_handler(
121 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this))); 126 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)),
127 APILastError(APILastError::GetParent()));
122 128
123 // By having both different arguments and different behaviors in the 129 // By having both different arguments and different behaviors in the
124 // callbacks, we can easily verify that the right function is called in the 130 // callbacks, we can easily verify that the right function is called in the
125 // right context. 131 // right context.
126 v8::Local<v8::Function> function_a = FunctionFromString( 132 v8::Local<v8::Function> function_a = FunctionFromString(
127 context_a, "(function(res) { this.result = res + 'alpha'; })"); 133 context_a, "(function(res) { this.result = res + 'alpha'; })");
128 v8::Local<v8::Function> function_b = FunctionFromString( 134 v8::Local<v8::Function> function_b = FunctionFromString(
129 context_b, "(function(res) { this.result = res + 'beta'; })"); 135 context_b, "(function(res) { this.result = res + 'beta'; })");
130 136
131 int request_a = request_handler.AddPendingRequest(isolate(), function_a, 137 int request_a = request_handler.AddPendingRequest(isolate(), function_a,
132 context_a, ArgumentList()); 138 context_a, ArgumentList());
133 int request_b = request_handler.AddPendingRequest(isolate(), function_b, 139 int request_b = request_handler.AddPendingRequest(isolate(), function_b,
134 context_b, ArgumentList()); 140 context_b, ArgumentList());
135 141
136 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), 142 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(),
137 testing::UnorderedElementsAre(request_a, request_b)); 143 testing::UnorderedElementsAre(request_a, request_b));
138 144
139 std::unique_ptr<base::ListValue> response_a = 145 std::unique_ptr<base::ListValue> response_a =
140 ListValueFromString("['response_a:']"); 146 ListValueFromString("['response_a:']");
141 ASSERT_TRUE(response_a); 147 ASSERT_TRUE(response_a);
142 148
143 request_handler.CompleteRequest(request_a, *response_a); 149 request_handler.CompleteRequest(request_a, *response_a, std::string());
144 EXPECT_TRUE(did_run_js()); 150 EXPECT_TRUE(did_run_js());
145 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), 151 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(),
146 testing::UnorderedElementsAre(request_b)); 152 testing::UnorderedElementsAre(request_b));
147 153
148 EXPECT_EQ( 154 EXPECT_EQ(
149 ReplaceSingleQuotes("'response_a:alpha'"), 155 ReplaceSingleQuotes("'response_a:alpha'"),
150 GetStringPropertyFromObject(context_a->Global(), context_a, "result")); 156 GetStringPropertyFromObject(context_a->Global(), context_a, "result"));
151 157
152 std::unique_ptr<base::ListValue> response_b = 158 std::unique_ptr<base::ListValue> response_b =
153 ListValueFromString("['response_b:']"); 159 ListValueFromString("['response_b:']");
154 ASSERT_TRUE(response_b); 160 ASSERT_TRUE(response_b);
155 161
156 request_handler.CompleteRequest(request_b, *response_b); 162 request_handler.CompleteRequest(request_b, *response_b, std::string());
157 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); 163 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty());
158 164
159 EXPECT_EQ( 165 EXPECT_EQ(
160 ReplaceSingleQuotes("'response_b:beta'"), 166 ReplaceSingleQuotes("'response_b:beta'"),
161 GetStringPropertyFromObject(context_b->Global(), context_b, "result")); 167 GetStringPropertyFromObject(context_b->Global(), context_b, "result"));
162 } 168 }
163 169
164 TEST_F(APIRequestHandlerTest, CustomCallbackArguments) { 170 TEST_F(APIRequestHandlerTest, CustomCallbackArguments) {
165 v8::HandleScope handle_scope(isolate()); 171 v8::HandleScope handle_scope(isolate());
166 v8::Local<v8::Context> context = ContextLocal(); 172 v8::Local<v8::Context> context = ContextLocal();
167 173
168 APIRequestHandler request_handler( 174 APIRequestHandler request_handler(
169 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this))); 175 base::Bind(&APIRequestHandlerTest::RunJS, base::Unretained(this)),
176 APILastError(APILastError::GetParent()));
170 177
171 ArgumentList custom_callback_args = { 178 ArgumentList custom_callback_args = {
172 gin::StringToV8(isolate(), "to"), gin::StringToV8(isolate(), "be"), 179 gin::StringToV8(isolate(), "to"), gin::StringToV8(isolate(), "be"),
173 }; 180 };
174 181
175 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs); 182 v8::Local<v8::Function> function = FunctionFromString(context, kEchoArgs);
176 ASSERT_FALSE(function.IsEmpty()); 183 ASSERT_FALSE(function.IsEmpty());
177 184
178 int request_id = request_handler.AddPendingRequest( 185 int request_id = request_handler.AddPendingRequest(
179 isolate(), function, context, custom_callback_args); 186 isolate(), function, context, custom_callback_args);
180 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(), 187 EXPECT_THAT(request_handler.GetPendingRequestIdsForTesting(),
181 testing::UnorderedElementsAre(request_id)); 188 testing::UnorderedElementsAre(request_id));
182 189
183 std::unique_ptr<base::ListValue> response_arguments = 190 std::unique_ptr<base::ListValue> response_arguments =
184 ListValueFromString("['or','not','to','be']"); 191 ListValueFromString("['or','not','to','be']");
185 ASSERT_TRUE(response_arguments); 192 ASSERT_TRUE(response_arguments);
186 request_handler.CompleteRequest(request_id, *response_arguments); 193 request_handler.CompleteRequest(request_id, *response_arguments,
194 std::string());
187 195
188 EXPECT_TRUE(did_run_js()); 196 EXPECT_TRUE(did_run_js());
189 EXPECT_EQ(ReplaceSingleQuotes("['to','be','or','not','to','be']"), 197 EXPECT_EQ(ReplaceSingleQuotes("['to','be','or','not','to','be']"),
190 GetStringPropertyFromObject(context->Global(), context, "result")); 198 GetStringPropertyFromObject(context->Global(), context, "result"));
191 199
192 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty()); 200 EXPECT_TRUE(request_handler.GetPendingRequestIdsForTesting().empty());
193 } 201 }
194 202
195 } // namespace extensions 203 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698