summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api_test_utils.cc
diff options
context:
space:
mode:
authoryoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-23 06:52:41 +0000
committeryoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-23 06:52:41 +0000
commite49e1014287407cd8aefc5a49a9c466d633841eb (patch)
treead2017bcac3cce09ee4e92c73d7e17a6f2a54fc9 /extensions/browser/api_test_utils.cc
parent1063d5e16bb34210d25ffaaa640484f7212fac03 (diff)
downloadchromium_src-e49e1014287407cd8aefc5a49a9c466d633841eb.zip
chromium_src-e49e1014287407cd8aefc5a49a9c466d633841eb.tar.gz
chromium_src-e49e1014287407cd8aefc5a49a9c466d633841eb.tar.bz2
Move DnsApiTest.DnsResolveIPLiteral and DnsApiTest.DnsResolveHostname to app_shell_browsertests.
This results in a significant speedup; tests run in <500ms instead of >2s because the lengthy browser_tests startup is avoided. This clones some aspects of extension_function_test_utils into extensions/browser/api_test_utils.cc. Later CLs will clean up the redundancy. BUG=388893 Review URL: https://codereview.chromium.org/394103004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284872 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/browser/api_test_utils.cc')
-rw-r--r--extensions/browser/api_test_utils.cc132
1 files changed, 132 insertions, 0 deletions
diff --git a/extensions/browser/api_test_utils.cc b/extensions/browser/api_test_utils.cc
new file mode 100644
index 0000000..4c9a58b
--- /dev/null
+++ b/extensions/browser/api_test_utils.cc
@@ -0,0 +1,132 @@
+// 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) {
+ base::Value* result = ParseJSON(data);
+ base::ListValue* list = NULL;
+ result->GetAsList(&list);
+ return list;
+}
+
+// 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