Chromium Code Reviews| 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 CHECK(load_waiter_->browser_context()); | |
|
Paweł Hajdan Jr.
2014/06/13 13:45:23
Please don't crash the entire test unless really n
David Tseng
2014/06/13 23:23:38
Done.
| |
| 27 ConstValueVector args; | |
| 28 args.push_back(new base::StringValue(test_fixture)); | |
| 29 args.push_back(new base::StringValue(test_name)); | |
| 30 std::vector<base::string16> scripts; | |
| 31 if (!libs_loaded_) { | |
| 32 BuildJavascriptLibraries(&scripts); | |
| 33 libs_loaded_ = true; | |
| 34 } | |
| 35 scripts.push_back(BuildRunTestJSCall(is_async, "RUN_TEST_F", args)); | |
| 36 | |
| 37 base::string16 script_16 = JoinString(scripts, '\n'); | |
| 38 std::string script = base::UTF16ToUTF8(script_16); | |
| 39 | |
| 40 std::string result = | |
| 41 extensions::browsertest_util::ExecuteScriptInBackgroundPage( | |
| 42 Profile::FromBrowserContext(load_waiter_->browser_context()), | |
| 43 load_waiter_->extension_id(), | |
| 44 script); | |
| 45 | |
| 46 base::JSONReader reader; | |
| 47 scoped_ptr<base::Value> value_result; | |
| 48 value_result.reset(reader.Read(result)); | |
| 49 CHECK_EQ(base::Value::TYPE_DICTIONARY, value_result->GetType()); | |
| 50 base::DictionaryValue* dict_value = | |
| 51 static_cast<base::DictionaryValue*>(value_result.get()); | |
| 52 bool test_result; | |
| 53 std::string test_result_message; | |
| 54 CHECK(dict_value->GetBoolean("result", &test_result)); | |
| 55 CHECK(dict_value->GetString("message", &test_result_message)); | |
| 56 if (!test_result_message.empty()) | |
| 57 ADD_FAILURE() << test_result_message; | |
| 58 return test_result; | |
| 59 } | |
| OLD | NEW |