Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/test/base/javascript_browsertest.cc

Issue 320753002: Support javascript gtests in an extension background page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: WS Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_browsertest.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
Peter Lundblad 2014/06/08 23:27:22 Shouldn't we keep this in the webui tests? We sho
David Tseng 2014/06/10 05:35:22 Requires changing the generator / test api. Puntin
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 void JavaScriptBrowserTest::SetUpOnMainThread() {
35 base::FilePath test_data_directory;
36 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory));
37 test_data_directory = test_data_directory.Append(kWebUITestFolder);
38 library_search_paths_.push_back(test_data_directory);
39
40 base::FilePath gen_test_data_directory;
41 ASSERT_TRUE(
42 PathService::Get(chrome::DIR_GEN_TEST_DATA, &gen_test_data_directory));
43 library_search_paths_.push_back(gen_test_data_directory);
44
45 base::FilePath source_root_directory;
46 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root_directory));
47 library_search_paths_.push_back(source_root_directory);
48
49 base::FilePath resources_pack_path;
50 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
51 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
52 resources_pack_path, ui::SCALE_FACTOR_NONE);
53
54 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath));
55 AddLibrary(base::FilePath(kMockJSPath));
56 AddLibrary(base::FilePath(kWebUILibraryJS));
57 }
58
59 void JavaScriptBrowserTest::BuildJavascriptLibraries(
60 std::vector<base::string16>* libraries) {
61 ASSERT_TRUE(libraries != NULL);
62 std::vector<base::FilePath>::iterator user_libraries_iterator;
63 for (user_libraries_iterator = user_libraries_.begin();
64 user_libraries_iterator != user_libraries_.end();
65 ++user_libraries_iterator) {
66 std::string library_content;
67 if (user_libraries_iterator->IsAbsolute()) {
68 ASSERT_TRUE(
69 base::ReadFileToString(*user_libraries_iterator, &library_content))
70 << user_libraries_iterator->value();
71 } else {
72 bool ok = false;
73 std::vector<base::FilePath>::iterator library_search_path_iterator;
74 for (library_search_path_iterator = library_search_paths_.begin();
75 library_search_path_iterator != library_search_paths_.end();
76 ++library_search_path_iterator) {
77 ok = base::ReadFileToString(
78 base::MakeAbsoluteFilePath(
79 library_search_path_iterator->Append(*user_libraries_iterator)),
80 &library_content);
81 if (ok)
82 break;
83 }
84 ASSERT_TRUE(ok) << "User library not found: "
85 << user_libraries_iterator->value();
86 }
87 library_content.append(";\n");
88
89 // This magic code puts filenames in stack traces.
90 library_content.append("//# sourceURL=");
91 library_content.append(user_libraries_iterator->BaseName().AsUTF8Unsafe());
92 library_content.append("\n");
93 libraries->push_back(base::UTF8ToUTF16(library_content));
94 }
95 }
96
97 base::string16 JavaScriptBrowserTest::BuildRunTestJSCall(
98 bool is_async,
99 const std::string& function_name,
100 const ConstValueVector& test_func_args) {
101 ConstValueVector arguments;
102 base::FundamentalValue* is_async_arg = new base::FundamentalValue(is_async);
103 arguments.push_back(is_async_arg);
104 base::StringValue* function_name_arg = new base::StringValue(function_name);
105 arguments.push_back(function_name_arg);
106 base::ListValue* baked_argument_list = new base::ListValue();
107 ConstValueVector::const_iterator arguments_iterator;
108 for (arguments_iterator = test_func_args.begin();
109 arguments_iterator != test_func_args.end();
110 ++arguments_iterator) {
111 baked_argument_list->Append((*arguments_iterator)->DeepCopy());
112 }
113 arguments.push_back(baked_argument_list);
114 return content::WebUI::GetJavascriptCall(std::string("runTest"),
115 arguments.get());
116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698