| 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/javascript_browser_test.h" |
| 6 |
| 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" |
| 12 |
| 13 // static |
| 14 const base::FilePath::CharType |
| 15 JavaScriptBrowserTest::kA11yAuditLibraryJSPath[] = |
| 16 FILE_PATH_LITERAL("third_party/accessibility-audit/axs_testing.js"); |
| 17 |
| 18 // static |
| 19 const base::FilePath::CharType JavaScriptBrowserTest::kMockJSPath[] = |
| 20 FILE_PATH_LITERAL("chrome/third_party/mock4js/mock4js.js"); |
| 21 |
| 22 // static |
| 23 const base::FilePath::CharType JavaScriptBrowserTest::kWebUILibraryJS[] = |
| 24 FILE_PATH_LITERAL("test_api.js"); |
| 25 |
| 26 // static |
| 27 const base::FilePath::CharType JavaScriptBrowserTest::kWebUITestFolder[] = |
| 28 FILE_PATH_LITERAL("webui"); |
| 29 |
| 30 void JavaScriptBrowserTest::AddLibrary(const base::FilePath& library_path) { |
| 31 user_libraries_.push_back(library_path); |
| 32 } |
| 33 |
| 34 JavaScriptBrowserTest::JavaScriptBrowserTest() { |
| 35 } |
| 36 |
| 37 JavaScriptBrowserTest::~JavaScriptBrowserTest() { |
| 38 } |
| 39 |
| 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); |
| 45 |
| 46 base::FilePath gen_test_data_directory; |
| 47 ASSERT_TRUE( |
| 48 PathService::Get(chrome::DIR_GEN_TEST_DATA, &gen_test_data_directory)); |
| 49 library_search_paths_.push_back(gen_test_data_directory); |
| 50 |
| 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); |
| 54 |
| 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); |
| 59 |
| 60 AddLibrary(base::FilePath(kMockJSPath)); |
| 61 AddLibrary(base::FilePath(kWebUILibraryJS)); |
| 62 } |
| 63 |
| 64 // TODO(dtseng): Make this return bool (success/failure) and remove ASSERt_TRUE |
| 65 // calls. |
| 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()) { |
| 75 ASSERT_TRUE( |
| 76 base::ReadFileToString(*user_libraries_iterator, &library_content)) |
| 77 << user_libraries_iterator->value(); |
| 78 } else { |
| 79 bool ok = false; |
| 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)), |
| 87 &library_content); |
| 88 if (ok) |
| 89 break; |
| 90 } |
| 91 ASSERT_TRUE(ok) << "User library not found: " |
| 92 << user_libraries_iterator->value(); |
| 93 } |
| 94 library_content.append(";\n"); |
| 95 |
| 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)); |
| 101 } |
| 102 } |
| 103 |
| 104 base::string16 JavaScriptBrowserTest::BuildRunTestJSCall( |
| 105 bool is_async, |
| 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()); |
| 119 } |
| 120 arguments.push_back(baked_argument_list); |
| 121 return content::WebUI::GetJavascriptCall(std::string("runTest"), |
| 122 arguments.get()); |
| 123 } |
| OLD | NEW |