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.
5 #include "chrome/test/base/extension_js_browser_test.h"
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"
17 ExtensionJSBrowserTest::ExtensionJSBrowserTest() : libs_loaded_(false) {
20 ExtensionJSBrowserTest::~ExtensionJSBrowserTest() {
23 void ExtensionJSBrowserTest::WaitForExtension(const char* extension_id
,
24 const base::Closure
& load_cb
) {
25 load_waiter_
.reset(new ExtensionLoadWaiterOneShot());
26 load_waiter_
->WaitForExtension(extension_id
, load_cb
);
29 bool ExtensionJSBrowserTest::RunJavascriptTestF(bool is_async
,
30 const std::string
& test_fixture
,
31 const std::string
& test_name
) {
32 EXPECT_TRUE(load_waiter_
->browser_context());
33 if (!load_waiter_
->browser_context())
35 ConstValueVector args
;
36 args
.push_back(new base::StringValue(test_fixture
));
37 args
.push_back(new base::StringValue(test_name
));
38 std::vector
<base::string16
> scripts
;
40 BuildJavascriptLibraries(&scripts
);
43 scripts
.push_back(BuildRunTestJSCall(is_async
, "RUN_TEST_F", args
));
45 base::string16 script_16
= JoinString(scripts
, '\n');
46 std::string script
= base::UTF16ToUTF8(script_16
);
49 extensions::browsertest_util::ExecuteScriptInBackgroundPage(
50 Profile::FromBrowserContext(load_waiter_
->browser_context()),
51 load_waiter_
->extension_id(),
54 base::JSONReader reader
;
55 scoped_ptr
<base::Value
> value_result
;
56 value_result
.reset(reader
.Read(result
));
57 CHECK_EQ(base::Value::TYPE_DICTIONARY
, value_result
->GetType());
58 base::DictionaryValue
* dict_value
=
59 static_cast<base::DictionaryValue
*>(value_result
.get());
61 std::string test_result_message
;
62 CHECK(dict_value
->GetBoolean("result", &test_result
));
63 CHECK(dict_value
->GetString("message", &test_result_message
));
64 if (!test_result_message
.empty())
65 ADD_FAILURE() << test_result_message
;