Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / api_test_utils.cc
blobdd04bb7948cc6a9007fdcf473fa5abb09b86c14f
1 // Copyright 2014 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.
5 #include "extensions/browser/api_test_utils.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/test/test_utils.h"
12 #include "extensions/browser/extension_function.h"
13 #include "extensions/browser/extension_function_dispatcher.h"
14 #include "extensions/common/extension_builder.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using extensions::ExtensionFunctionDispatcher;
19 namespace {
21 class TestFunctionDispatcherDelegate
22 : public ExtensionFunctionDispatcher::Delegate {
23 public:
24 TestFunctionDispatcherDelegate() {}
25 virtual ~TestFunctionDispatcherDelegate() {}
27 // NULL implementation.
28 private:
29 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate);
32 base::Value* ParseJSON(const std::string& data) {
33 return base::JSONReader::Read(data);
36 base::ListValue* ParseList(const std::string& data) {
37 base::Value* result = ParseJSON(data);
38 base::ListValue* list = NULL;
39 result->GetAsList(&list);
40 return list;
43 // This helps us be able to wait until an UIThreadExtensionFunction calls
44 // SendResponse.
45 class SendResponseDelegate
46 : public UIThreadExtensionFunction::DelegateForTests {
47 public:
48 SendResponseDelegate() : should_post_quit_(false) {}
50 virtual ~SendResponseDelegate() {}
52 void set_should_post_quit(bool should_quit) {
53 should_post_quit_ = should_quit;
56 bool HasResponse() { return response_.get() != NULL; }
58 bool GetResponse() {
59 EXPECT_TRUE(HasResponse());
60 return *response_.get();
63 virtual void OnSendResponse(UIThreadExtensionFunction* function,
64 bool success,
65 bool bad_message) OVERRIDE {
66 ASSERT_FALSE(bad_message);
67 ASSERT_FALSE(HasResponse());
68 response_.reset(new bool);
69 *response_ = success;
70 if (should_post_quit_) {
71 base::MessageLoopForUI::current()->Quit();
75 private:
76 scoped_ptr<bool> response_;
77 bool should_post_quit_;
80 } // namespace
82 namespace extensions {
84 namespace api_test_utils {
86 base::Value* RunFunctionWithDelegateAndReturnSingleResult(
87 UIThreadExtensionFunction* function,
88 const std::string& args,
89 content::BrowserContext* context,
90 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) {
91 return RunFunctionWithDelegateAndReturnSingleResult(
92 function, args, context, dispatcher.Pass(), NONE);
95 base::Value* RunFunctionWithDelegateAndReturnSingleResult(
96 UIThreadExtensionFunction* function,
97 const std::string& args,
98 content::BrowserContext* context,
99 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
100 RunFunctionFlags flags) {
101 scoped_refptr<ExtensionFunction> function_owner(function);
102 // Without a callback the function will not generate a result.
103 function->set_has_callback(true);
104 RunFunction(function, args, context, dispatcher.Pass(), flags);
105 EXPECT_TRUE(function->GetError().empty())
106 << "Unexpected error: " << function->GetError();
107 const base::Value* single_result = NULL;
108 if (function->GetResultList() != NULL &&
109 function->GetResultList()->Get(0, &single_result)) {
110 return single_result->DeepCopy();
112 return NULL;
115 base::Value* RunFunctionAndReturnSingleResult(
116 UIThreadExtensionFunction* function,
117 const std::string& args,
118 content::BrowserContext* context) {
119 return RunFunctionAndReturnSingleResult(function, args, context, NONE);
122 base::Value* RunFunctionAndReturnSingleResult(
123 UIThreadExtensionFunction* function,
124 const std::string& args,
125 content::BrowserContext* context,
126 RunFunctionFlags flags) {
127 TestFunctionDispatcherDelegate delegate;
128 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
129 new ExtensionFunctionDispatcher(context, &delegate));
131 return RunFunctionWithDelegateAndReturnSingleResult(
132 function, args, context, dispatcher.Pass(), flags);
135 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
136 const std::string& args,
137 content::BrowserContext* context) {
138 return RunFunctionAndReturnError(function, args, context, NONE);
141 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
142 const std::string& args,
143 content::BrowserContext* context,
144 RunFunctionFlags flags) {
145 TestFunctionDispatcherDelegate delegate;
146 scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
147 new ExtensionFunctionDispatcher(context, &delegate));
148 scoped_refptr<ExtensionFunction> function_owner(function);
149 // Without a callback the function will not generate a result.
150 function->set_has_callback(true);
151 RunFunction(function, args, context, dispatcher.Pass(), flags);
152 EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
153 return function->GetError();
156 bool RunFunction(UIThreadExtensionFunction* function,
157 const std::string& args,
158 content::BrowserContext* context,
159 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
160 RunFunctionFlags flags) {
161 scoped_ptr<base::ListValue> parsed_args(ParseList(args));
162 EXPECT_TRUE(parsed_args.get())
163 << "Could not parse extension function arguments: " << args;
164 return RunFunction(
165 function, parsed_args.Pass(), context, dispatcher.Pass(), flags);
168 bool RunFunction(UIThreadExtensionFunction* function,
169 scoped_ptr<base::ListValue> args,
170 content::BrowserContext* context,
171 scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher,
172 RunFunctionFlags flags) {
173 SendResponseDelegate response_delegate;
174 function->set_test_delegate(&response_delegate);
175 function->SetArgs(args.get());
177 CHECK(dispatcher);
178 function->set_dispatcher(dispatcher->AsWeakPtr());
180 function->set_browser_context(context);
181 function->set_include_incognito(flags & INCLUDE_INCOGNITO);
182 function->Run()->Execute();
184 // If the RunAsync of |function| didn't already call SendResponse, run the
185 // message loop until they do.
186 if (!response_delegate.HasResponse()) {
187 response_delegate.set_should_post_quit(true);
188 content::RunMessageLoop();
191 EXPECT_TRUE(response_delegate.HasResponse());
192 return response_delegate.GetResponse();
195 } // namespace api_test_utils
196 } // namespace extensions