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 AddLibrary(base::FilePath(kMockJSPath
));
56 AddLibrary(base::FilePath(kWebUILibraryJS
));
59 // TODO(dtseng): Make this return bool (success/failure) and remove ASSERt_TRUE
61 void JavaScriptBrowserTest::BuildJavascriptLibraries(
62 std::vector
<base::string16
>* libraries
) {
63 ASSERT_TRUE(libraries
!= NULL
);
64 std::vector
<base::FilePath
>::iterator user_libraries_iterator
;
65 for (user_libraries_iterator
= user_libraries_
.begin();
66 user_libraries_iterator
!= user_libraries_
.end();
67 ++user_libraries_iterator
) {
68 std::string library_content
;
69 if (user_libraries_iterator
->IsAbsolute()) {
71 base::ReadFileToString(*user_libraries_iterator
, &library_content
))
72 << user_libraries_iterator
->value();
75 std::vector
<base::FilePath
>::iterator library_search_path_iterator
;
76 for (library_search_path_iterator
= library_search_paths_
.begin();
77 library_search_path_iterator
!= library_search_paths_
.end();
78 ++library_search_path_iterator
) {
79 ok
= base::ReadFileToString(
80 base::MakeAbsoluteFilePath(
81 library_search_path_iterator
->Append(*user_libraries_iterator
)),
86 ASSERT_TRUE(ok
) << "User library not found: "
87 << user_libraries_iterator
->value();
89 library_content
.append(";\n");
91 // This magic code puts filenames in stack traces.
92 library_content
.append("//# sourceURL=");
93 library_content
.append(user_libraries_iterator
->BaseName().AsUTF8Unsafe());
94 library_content
.append("\n");
95 libraries
->push_back(base::UTF8ToUTF16(library_content
));
99 base::string16
JavaScriptBrowserTest::BuildRunTestJSCall(
101 const std::string
& function_name
,
102 const ConstValueVector
& test_func_args
) {
103 ConstValueVector arguments
;
104 base::FundamentalValue
* is_async_arg
= new base::FundamentalValue(is_async
);
105 arguments
.push_back(is_async_arg
);
106 base::StringValue
* function_name_arg
= new base::StringValue(function_name
);
107 arguments
.push_back(function_name_arg
);
108 base::ListValue
* baked_argument_list
= new base::ListValue();
109 ConstValueVector::const_iterator arguments_iterator
;
110 for (arguments_iterator
= test_func_args
.begin();
111 arguments_iterator
!= test_func_args
.end();
112 ++arguments_iterator
) {
113 baked_argument_list
->Append((*arguments_iterator
)->DeepCopy());
115 arguments
.push_back(baked_argument_list
);
116 return content::WebUI::GetJavascriptCall(std::string("runTest"),