Chromium Code Reviews| Index: extensions/browser/api_test_utils.cc |
| diff --git a/extensions/browser/api_test_utils.cc b/extensions/browser/api_test_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d38be5d6430db59d8c98e9f7fe91cc5ee477b4ea |
| --- /dev/null |
| +++ b/extensions/browser/api_test_utils.cc |
| @@ -0,0 +1,133 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "extensions/browser/api_test_utils.h" |
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "extensions/browser/extension_function.h" |
| +#include "extensions/browser/extension_function_dispatcher.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +base::Value* ParseJSON(const std::string& data) { |
| + return base::JSONReader::Read(data); |
| +} |
| + |
| +base::ListValue* ParseList(const std::string& data) { |
| + scoped_ptr<base::Value> result(ParseJSON(data)); |
| + if (result.get() && result->IsType(base::Value::TYPE_LIST)) |
| + return static_cast<base::ListValue*>(result.release()); |
| + else |
|
James Cook
2014/07/16 17:15:31
nit: no need for else
Yoyo Zhou
2014/07/18 02:45:31
I rewrote this since I had just copied it over and
|
| + return NULL; |
| +} |
| + |
| +// This helps us be able to wait until an UIThreadExtensionFunction calls |
| +// SendResponse. |
| +class SendResponseDelegate |
| + : public UIThreadExtensionFunction::DelegateForTests { |
| + public: |
| + SendResponseDelegate() : should_post_quit_(false) {} |
| + |
| + virtual ~SendResponseDelegate() {} |
| + |
| + void set_should_post_quit(bool should_quit) { |
| + should_post_quit_ = should_quit; |
| + } |
| + |
| + bool HasResponse() { return response_.get() != NULL; } |
| + |
| + bool GetResponse() { |
| + EXPECT_TRUE(HasResponse()); |
| + return *response_.get(); |
| + } |
| + |
| + virtual void OnSendResponse(UIThreadExtensionFunction* function, |
| + bool success, |
| + bool bad_message) OVERRIDE { |
| + ASSERT_FALSE(bad_message); |
| + ASSERT_FALSE(HasResponse()); |
| + response_.reset(new bool); |
| + *response_ = success; |
| + if (should_post_quit_) { |
| + base::MessageLoopForUI::current()->Quit(); |
| + } |
| + } |
| + |
| + private: |
| + scoped_ptr<bool> response_; |
| + bool should_post_quit_; |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace extensions { |
| + |
| +namespace api_test_utils { |
| + |
| +base::Value* RunFunctionAndReturnSingleResult( |
| + UIThreadExtensionFunction* function, |
| + const std::string& args, |
| + content::BrowserContext* context, |
| + scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher) { |
| + return RunFunctionAndReturnSingleResult( |
| + function, args, context, dispatcher.Pass(), NONE); |
| +} |
| + |
| +base::Value* RunFunctionAndReturnSingleResult( |
| + UIThreadExtensionFunction* function, |
| + const std::string& args, |
| + content::BrowserContext* context, |
| + scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| + RunFunctionFlags flags) { |
| + scoped_refptr<ExtensionFunction> function_owner(function); |
| + // Without a callback the function will not generate a result. |
| + function->set_has_callback(true); |
| + RunFunction(function, args, context, dispatcher.Pass(), flags); |
| + EXPECT_TRUE(function->GetError().empty()) |
| + << "Unexpected error: " << function->GetError(); |
| + const base::Value* single_result = NULL; |
| + if (function->GetResultList() != NULL && |
| + function->GetResultList()->Get(0, &single_result)) { |
| + return single_result->DeepCopy(); |
| + } |
| + return NULL; |
| +} |
| + |
| +bool RunFunction(UIThreadExtensionFunction* function, |
| + const std::string& args, |
| + content::BrowserContext* context, |
| + scoped_ptr<extensions::ExtensionFunctionDispatcher> dispatcher, |
| + RunFunctionFlags flags) { |
| + SendResponseDelegate response_delegate; |
| + function->set_test_delegate(&response_delegate); |
| + scoped_ptr<base::ListValue> parsed_args(ParseList(args)); |
| + EXPECT_TRUE(parsed_args.get()) |
| + << "Could not parse extension function arguments: " << args; |
| + function->SetArgs(parsed_args.get()); |
| + |
| + CHECK(dispatcher); |
| + function->set_dispatcher(dispatcher->AsWeakPtr()); |
| + |
| + function->set_browser_context(context); |
| + function->set_include_incognito(flags & INCLUDE_INCOGNITO); |
| + function->Run()->Execute(); |
| + |
| + // If the RunAsync of |function| didn't already call SendResponse, run the |
| + // message loop until they do. |
| + if (!response_delegate.HasResponse()) { |
| + response_delegate.set_should_post_quit(true); |
| + content::RunMessageLoop(); |
| + } |
| + |
| + EXPECT_TRUE(response_delegate.HasResponse()); |
| + return response_delegate.GetResponse(); |
| +} |
| + |
| +} // namespace api_test_utils |
| +} // namespace extensions |