| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/test/base/extension_js_browser_test.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/browsertest_util.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/test/browser_test_utils.h" |
| 16 |
| 17 void ExtensionJSBrowserTest::WaitForExtension(const char* extension_id, |
| 18 const base::Closure& load_cb) { |
| 19 load_waiter_.reset(new ExtensionLoadWaiterOneShot()); |
| 20 load_waiter_->WaitForExtension(extension_id, load_cb); |
| 21 } |
| 22 |
| 23 bool ExtensionJSBrowserTest::RunJavascriptTestF(bool is_async, |
| 24 const std::string& test_fixture, |
| 25 const std::string& test_name) { |
| 26 EXPECT_TRUE(load_waiter_->browser_context()); |
| 27 if (!load_waiter_.browser_context()) |
| 28 return false; |
| 29 ConstValueVector args; |
| 30 args.push_back(new base::StringValue(test_fixture)); |
| 31 args.push_back(new base::StringValue(test_name)); |
| 32 std::vector<base::string16> scripts; |
| 33 if (!libs_loaded_) { |
| 34 BuildJavascriptLibraries(&scripts); |
| 35 libs_loaded_ = true; |
| 36 } |
| 37 scripts.push_back(BuildRunTestJSCall(is_async, "RUN_TEST_F", args)); |
| 38 |
| 39 base::string16 script_16 = JoinString(scripts, '\n'); |
| 40 std::string script = base::UTF16ToUTF8(script_16); |
| 41 |
| 42 std::string result = |
| 43 extensions::browsertest_util::ExecuteScriptInBackgroundPage( |
| 44 Profile::FromBrowserContext(load_waiter_->browser_context()), |
| 45 load_waiter_->extension_id(), |
| 46 script); |
| 47 |
| 48 base::JSONReader reader; |
| 49 scoped_ptr<base::Value> value_result; |
| 50 value_result.reset(reader.Read(result)); |
| 51 CHECK_EQ(base::Value::TYPE_DICTIONARY, value_result->GetType()); |
| 52 base::DictionaryValue* dict_value = |
| 53 static_cast<base::DictionaryValue*>(value_result.get()); |
| 54 bool test_result; |
| 55 std::string test_result_message; |
| 56 CHECK(dict_value->GetBoolean("result", &test_result)); |
| 57 CHECK(dict_value->GetString("message", &test_result_message)); |
| 58 if (!test_result_message.empty()) |
| 59 ADD_FAILURE() << test_result_message; |
| 60 return test_result; |
| 61 } |
| OLD | NEW |