OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 #ifndef IOS_INTERNAL_CHROME_BROWSER_WEB_WEB_JS_TEST_H_ | |
6 #define IOS_INTERNAL_CHROME_BROWSER_WEB_WEB_JS_TEST_H_ | |
7 | |
8 #import <Foundation/Foundation.h> | |
9 | |
10 #include "base/mac/bundle_locations.h" | |
sdefresne
2016/07/13 12:50:00
nit: import here too
vabr (Chromium)
2016/07/13 12:54:28
Done.
| |
11 #import "base/mac/scoped_nsobject.h" | |
12 #include "testing/gtest_mac.h" | |
13 | |
14 class GURL; | |
15 | |
16 namespace web { | |
17 | |
18 // Base fixture mixin for testing JavaScripts. | |
19 template <class WebTestT> | |
20 class WebJsTest : public WebTestT { | |
21 public: | |
22 WebJsTest(NSArray* java_script_paths) | |
23 : java_script_paths_([java_script_paths retain]) {} | |
24 | |
25 protected: | |
26 // Loads |html| and inject JavaScripts at |javaScriptPaths_|. | |
27 void LoadHtmlAndInject(NSString* html) { | |
28 WebTestT::LoadHtml(html); | |
29 Inject(); | |
30 } | |
31 | |
32 // Returns a NSString representation of the JavaScript's evaluation results; | |
33 // the JavaScript is passed in as a |format| and its arguments. | |
34 NSString* EvaluateJavaScriptWithFormat(NSString* format, ...) | |
35 __attribute__((format(__NSString__, 2, 3))); | |
36 | |
37 // Helper method that EXPECTs the |java_script| evaluation results on each | |
38 // element obtained by scripts in |get_element_javas_cripts|; the expected | |
39 // result is the corresponding entry in |expected_results|. | |
40 void EvaluateJavaScriptOnElementsAndCheck(NSString* java_script, | |
41 NSArray* get_element_java_scripts, | |
42 NSArray* expected_results); | |
43 | |
44 // Helper method that EXPECTs the |java_script| evaluation results on each | |
45 // element obtained by JavaScripts in |get_element_java_scripts|. The | |
46 // expected results are boolean and are true only for elements in | |
47 // |get_element_java_scripts_expecting_true| which is subset of | |
48 // |get_element_java_scripts|. | |
49 void EvaluateBooleanJavaScriptOnElementsAndCheck( | |
50 NSString* java_script, | |
51 NSArray* get_element_java_scripts, | |
52 NSArray* get_element_java_scripts_expecting_true); | |
53 | |
54 private: | |
55 // Injects JavaScript at |java_script_paths_|. | |
56 void Inject(); | |
57 | |
58 base::scoped_nsobject<NSArray> java_script_paths_; | |
59 }; | |
60 | |
61 template <class WebTestT> | |
62 void WebJsTest<WebTestT>::Inject() { | |
63 // Wait until main web injection has occured. | |
64 NSString* beacon = nil; | |
65 do { | |
66 beacon = WebTestT::EvaluateJavaScriptAsString(@"typeof __gCrWeb"); | |
67 } while (![beacon isEqualToString:@"object"]); | |
68 for (NSString* java_script_path in java_script_paths_.get()) { | |
69 NSString* path = | |
70 [base::mac::FrameworkBundle() pathForResource:java_script_path | |
71 ofType:@"js"]; | |
72 WebTestT::EvaluateJavaScriptAsString([NSString | |
73 stringWithContentsOfFile:path | |
74 encoding:NSUTF8StringEncoding | |
75 error:nil]); | |
76 } | |
77 } | |
78 | |
79 template <class WebTestT> | |
80 NSString* WebJsTest<WebTestT>::EvaluateJavaScriptWithFormat(NSString* format, | |
81 ...) { | |
82 va_list args; | |
83 va_start(args, format); | |
84 base::scoped_nsobject<NSString> java_script( | |
85 [[NSString alloc] initWithFormat:format arguments:args]); | |
86 va_end(args); | |
87 | |
88 return WebTestT::EvaluateJavaScriptAsString(java_script); | |
89 } | |
90 | |
91 template <class WebTestT> | |
92 void WebJsTest<WebTestT>::EvaluateJavaScriptOnElementsAndCheck( | |
93 NSString* java_script, | |
94 NSArray* get_element_java_scripts, | |
95 NSArray* expected_results) { | |
96 for (NSUInteger i = 0; i < get_element_java_scripts.count; ++i) { | |
97 EXPECT_NSEQ( | |
98 expected_results[i], | |
99 EvaluateJavaScriptWithFormat(java_script, get_element_java_scripts[i])); | |
100 } | |
101 } | |
102 | |
103 template <class WebTestT> | |
104 void WebJsTest<WebTestT>::EvaluateBooleanJavaScriptOnElementsAndCheck( | |
105 NSString* java_script, | |
106 NSArray* get_element_java_scripts, | |
107 NSArray* get_element_java_scripts_expecting_true) { | |
108 for (NSUInteger index = 0; index < get_element_java_scripts.count; ++index) { | |
109 NSString* get_element_java_script = get_element_java_scripts[index]; | |
110 NSString* expected = [get_element_java_scripts_expecting_true | |
111 containsObject:get_element_java_script] | |
112 ? @"true" | |
113 : @"false"; | |
114 EXPECT_NSEQ(expected, EvaluateJavaScriptWithFormat(java_script, | |
115 get_element_java_script)) | |
116 << [NSString stringWithFormat:@"%@ on %@ should return %@", java_script, | |
117 get_element_java_script, expected]; | |
118 } | |
119 } | |
120 | |
121 } // namespace web | |
122 | |
123 #endif // IOS_INTERNAL_CHROME_BROWSER_WEB_WEB_JS_TEST_H_ | |
OLD | NEW |