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/javascript_browser_test.h"
7 #include "base/path_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/chrome_paths.h"
10 #include "content/public/browser/web_ui.h"
11 #include "ui/base/resource/resource_bundle.h"
14 const base::FilePath::CharType
15 JavaScriptBrowserTest::kA11yAuditLibraryJSPath
[] =
16 FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js");
19 const base::FilePath::CharType
JavaScriptBrowserTest::kMockJSPath
[] =
20 FILE_PATH_LITERAL("chrome/third_party/mock4js/mock4js.js");
23 const base::FilePath::CharType
JavaScriptBrowserTest::kWebUILibraryJS
[] =
24 FILE_PATH_LITERAL("test_api.js");
27 const base::FilePath::CharType
JavaScriptBrowserTest::kWebUITestFolder
[] =
28 FILE_PATH_LITERAL("webui");
30 void JavaScriptBrowserTest::AddLibrary(const base::FilePath
& library_path
) {
31 user_libraries_
.push_back(library_path
);
34 JavaScriptBrowserTest::JavaScriptBrowserTest() {
37 JavaScriptBrowserTest::~JavaScriptBrowserTest() {
40 void JavaScriptBrowserTest::SetUpOnMainThread() {
41 base::FilePath test_data_directory
;
42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_directory
));
43 test_data_directory
= test_data_directory
.Append(kWebUITestFolder
);
44 library_search_paths_
.push_back(test_data_directory
);
46 base::FilePath gen_test_data_directory
;
48 PathService::Get(chrome::DIR_GEN_TEST_DATA
, &gen_test_data_directory
));
49 library_search_paths_
.push_back(gen_test_data_directory
);
51 base::FilePath source_root_directory
;
52 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &source_root_directory
));
53 library_search_paths_
.push_back(source_root_directory
);
55 base::FilePath resources_pack_path
;
56 PathService::Get(chrome::FILE_RESOURCES_PACK
, &resources_pack_path
);
57 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
58 resources_pack_path
, ui::SCALE_FACTOR_NONE
);
60 AddLibrary(base::FilePath(kMockJSPath
));
61 AddLibrary(base::FilePath(kWebUILibraryJS
));
64 // TODO(dtseng): Make this return bool (success/failure) and remove ASSERt_TRUE
66 void JavaScriptBrowserTest::BuildJavascriptLibraries(
67 std::vector
<base::string16
>* libraries
) {
68 ASSERT_TRUE(libraries
!= NULL
);
69 std::vector
<base::FilePath
>::iterator user_libraries_iterator
;
70 for (user_libraries_iterator
= user_libraries_
.begin();
71 user_libraries_iterator
!= user_libraries_
.end();
72 ++user_libraries_iterator
) {
73 std::string library_content
;
74 if (user_libraries_iterator
->IsAbsolute()) {
76 base::ReadFileToString(*user_libraries_iterator
, &library_content
))
77 << user_libraries_iterator
->value();
80 std::vector
<base::FilePath
>::iterator library_search_path_iterator
;
81 for (library_search_path_iterator
= library_search_paths_
.begin();
82 library_search_path_iterator
!= library_search_paths_
.end();
83 ++library_search_path_iterator
) {
84 ok
= base::ReadFileToString(
85 base::MakeAbsoluteFilePath(
86 library_search_path_iterator
->Append(*user_libraries_iterator
)),
91 ASSERT_TRUE(ok
) << "User library not found: "
92 << user_libraries_iterator
->value();
94 library_content
.append(";\n");
96 // This magic code puts filenames in stack traces.
97 library_content
.append("//# sourceURL=");
98 library_content
.append(user_libraries_iterator
->BaseName().AsUTF8Unsafe());
99 library_content
.append("\n");
100 libraries
->push_back(base::UTF8ToUTF16(library_content
));
104 base::string16
JavaScriptBrowserTest::BuildRunTestJSCall(
106 const std::string
& function_name
,
107 const ConstValueVector
& test_func_args
) {
108 ConstValueVector arguments
;
109 base::FundamentalValue
* is_async_arg
= new base::FundamentalValue(is_async
);
110 arguments
.push_back(is_async_arg
);
111 base::StringValue
* function_name_arg
= new base::StringValue(function_name
);
112 arguments
.push_back(function_name_arg
);
113 base::ListValue
* baked_argument_list
= new base::ListValue();
114 ConstValueVector::const_iterator arguments_iterator
;
115 for (arguments_iterator
= test_func_args
.begin();
116 arguments_iterator
!= test_func_args
.end();
117 ++arguments_iterator
) {
118 baked_argument_list
->Append((*arguments_iterator
)->DeepCopy());
120 arguments
.push_back(baked_argument_list
);
121 return content::WebUI::GetJavascriptCall(std::string("runTest"),