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

Unified Diff: chrome/test/base/extension_js_browser_test.cc

Issue 320753002: Support javascript gtests in an extension background page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixup comment Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/base/extension_js_browser_test.cc
diff --git a/chrome/test/base/extension_js_browser_test.cc b/chrome/test/base/extension_js_browser_test.cc
new file mode 100644
index 0000000000000000000000000000000000000000..76d305bef13e89985ce907a5166330ee308bdafa
--- /dev/null
+++ b/chrome/test/base/extension_js_browser_test.cc
@@ -0,0 +1,54 @@
+// 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 "chrome/test/base/extension_js_browser_test.h"
+
+#include "base/json/json_reader.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "chrome/browser/extensions/browsertest_util.h"
+#include "chrome/browser/profiles/profile.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/test/browser_test_utils.h"
+
+void ExtensionJSBrowserTest::WaitFor(const char* extension_id) {
+ load_waiter_.WaitFor(extension_id);
+}
+
+bool ExtensionJSBrowserTest::RunJavascriptTestF(bool is_async,
+ const std::string& test_fixture,
+ const std::string& test_name) {
+ CHECK(load_waiter_.browser_context());
+ ConstValueVector args;
+ args.push_back(new base::StringValue(test_fixture));
+ args.push_back(new base::StringValue(test_name));
+ std::vector<base::string16> scripts;
+ if (!libs_loaded_) {
+ BuildJavascriptLibraries(&scripts);
+ libs_loaded_ = true;
+ }
+ scripts.push_back(BuildRunTestJSCall(is_async, "RUN_TEST_F", args));
+
+ base::string16 script_16 = JoinString(scripts, '\n');
+ std::string script = base::UTF16ToUTF8(script_16);
+
+ std::string result =
+ extensions::browsertest_util::ExecuteScriptInBackgroundPage(
+ Profile::FromBrowserContext(load_waiter_.browser_context()),
+ load_waiter_.extension_id(),
+ script);
+
+ base::JSONReader reader;
+ scoped_ptr<base::Value> value_result;
+ value_result.reset(reader.Read(result));
+ CHECK_EQ(base::Value::TYPE_DICTIONARY, value_result->GetType());
+ base::DictionaryValue* dict_value =
+ static_cast<base::DictionaryValue*>(value_result.get());
+ bool test_result;
+ std::string test_result_message;
+ CHECK(dict_value->GetBoolean("result", &test_result));
+ CHECK(dict_value->GetString("message", &test_result_message));
+ return test_result;
+}

Powered by Google App Engine
This is Rietveld 408576698