Commit b737caa2 authored by vabr's avatar vabr Committed by Commit bot

Move web_js_test.h upstream

This creates a copy of the downstream web_js_test.h upstream in
ios/web/public/test and puts it into the ios_web_test_support build target
(+equivalent in GN).

The upstream web_js_test.h is an almost-identical copy of the downstream version, with 2 differences:
  (1) git cl format is required upstream, which changed indenting in a single location.
  (2) namespace web was introduced to match the style of the target directory.

BUG=627775

Review-Url: https://codereview.chromium.org/2150513002
Cr-Commit-Position: refs/heads/master@{#405217}
parent be1bd6dc
...@@ -318,6 +318,7 @@ source_set("test_support") { ...@@ -318,6 +318,7 @@ source_set("test_support") {
deps = [ deps = [
":web", ":web",
"//base",
"//base/test:test_support", "//base/test:test_support",
"//ios/testing:ocmock_support", "//ios/testing:ocmock_support",
"//ios/third_party/gcdwebserver", "//ios/third_party/gcdwebserver",
...@@ -368,6 +369,7 @@ source_set("test_support") { ...@@ -368,6 +369,7 @@ source_set("test_support") {
"public/test/test_web_thread_bundle.h", "public/test/test_web_thread_bundle.h",
"public/test/test_web_view_content_view.h", "public/test/test_web_view_content_view.h",
"public/test/test_web_view_content_view.mm", "public/test/test_web_view_content_view.mm",
"public/test/web_js_test.h",
"public/test/web_test.h", "public/test/web_test.h",
"public/test/web_test.mm", "public/test/web_test.mm",
"public/test/web_test_suite.h", "public/test/web_test_suite.h",
......
...@@ -459,6 +459,7 @@ ...@@ -459,6 +459,7 @@
'target_name': 'ios_web_test_support', 'target_name': 'ios_web_test_support',
'type': 'static_library', 'type': 'static_library',
'dependencies': [ 'dependencies': [
'../../base/base.gyp:base',
'../../base/base.gyp:test_support_base', '../../base/base.gyp:test_support_base',
'../../ios/testing/ios_testing.gyp:ocmock_support', '../../ios/testing/ios_testing.gyp:ocmock_support',
'../../ios/third_party/gcdwebserver/gcdwebserver.gyp:gcdwebserver', '../../ios/third_party/gcdwebserver/gcdwebserver.gyp:gcdwebserver',
...@@ -512,6 +513,7 @@ ...@@ -512,6 +513,7 @@
'public/test/test_web_thread_bundle.h', 'public/test/test_web_thread_bundle.h',
'public/test/test_web_view_content_view.h', 'public/test/test_web_view_content_view.h',
'public/test/test_web_view_content_view.mm', 'public/test/test_web_view_content_view.mm',
'public/test/web_js_test.h',
'public/test/web_test.h', 'public/test/web_test.h',
'public/test/web_test.mm', 'public/test/web_test.mm',
'public/test/web_test_suite.h', 'public/test/web_test_suite.h',
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_INTERNAL_CHROME_BROWSER_WEB_WEB_JS_TEST_H_
#define IOS_INTERNAL_CHROME_BROWSER_WEB_WEB_JS_TEST_H_
#import <Foundation/Foundation.h>
#import "base/mac/bundle_locations.h"
#import "base/mac/scoped_nsobject.h"
#include "testing/gtest_mac.h"
class GURL;
namespace web {
// Base fixture mixin for testing JavaScripts.
template <class WebTestT>
class WebJsTest : public WebTestT {
public:
WebJsTest(NSArray* java_script_paths)
: java_script_paths_([java_script_paths retain]) {}
protected:
// Loads |html| and inject JavaScripts at |javaScriptPaths_|.
void LoadHtmlAndInject(NSString* html) {
WebTestT::LoadHtml(html);
Inject();
}
// Returns a NSString representation of the JavaScript's evaluation results;
// the JavaScript is passed in as a |format| and its arguments.
NSString* EvaluateJavaScriptWithFormat(NSString* format, ...)
__attribute__((format(__NSString__, 2, 3)));
// Helper method that EXPECTs the |java_script| evaluation results on each
// element obtained by scripts in |get_element_javas_cripts|; the expected
// result is the corresponding entry in |expected_results|.
void EvaluateJavaScriptOnElementsAndCheck(NSString* java_script,
NSArray* get_element_java_scripts,
NSArray* expected_results);
// Helper method that EXPECTs the |java_script| evaluation results on each
// element obtained by JavaScripts in |get_element_java_scripts|. The
// expected results are boolean and are true only for elements in
// |get_element_java_scripts_expecting_true| which is subset of
// |get_element_java_scripts|.
void EvaluateBooleanJavaScriptOnElementsAndCheck(
NSString* java_script,
NSArray* get_element_java_scripts,
NSArray* get_element_java_scripts_expecting_true);
private:
// Injects JavaScript at |java_script_paths_|.
void Inject();
base::scoped_nsobject<NSArray> java_script_paths_;
};
template <class WebTestT>
void WebJsTest<WebTestT>::Inject() {
// Wait until main web injection has occured.
NSString* beacon = nil;
do {
beacon = WebTestT::EvaluateJavaScriptAsString(@"typeof __gCrWeb");
} while (![beacon isEqualToString:@"object"]);
for (NSString* java_script_path in java_script_paths_.get()) {
NSString* path =
[base::mac::FrameworkBundle() pathForResource:java_script_path
ofType:@"js"];
WebTestT::EvaluateJavaScriptAsString([NSString
stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil]);
}
}
template <class WebTestT>
NSString* WebJsTest<WebTestT>::EvaluateJavaScriptWithFormat(NSString* format,
...) {
va_list args;
va_start(args, format);
base::scoped_nsobject<NSString> java_script(
[[NSString alloc] initWithFormat:format arguments:args]);
va_end(args);
return WebTestT::EvaluateJavaScriptAsString(java_script);
}
template <class WebTestT>
void WebJsTest<WebTestT>::EvaluateJavaScriptOnElementsAndCheck(
NSString* java_script,
NSArray* get_element_java_scripts,
NSArray* expected_results) {
for (NSUInteger i = 0; i < get_element_java_scripts.count; ++i) {
EXPECT_NSEQ(
expected_results[i],
EvaluateJavaScriptWithFormat(java_script, get_element_java_scripts[i]));
}
}
template <class WebTestT>
void WebJsTest<WebTestT>::EvaluateBooleanJavaScriptOnElementsAndCheck(
NSString* java_script,
NSArray* get_element_java_scripts,
NSArray* get_element_java_scripts_expecting_true) {
for (NSUInteger index = 0; index < get_element_java_scripts.count; ++index) {
NSString* get_element_java_script = get_element_java_scripts[index];
NSString* expected = [get_element_java_scripts_expecting_true
containsObject:get_element_java_script]
? @"true"
: @"false";
EXPECT_NSEQ(expected, EvaluateJavaScriptWithFormat(java_script,
get_element_java_script))
<< [NSString stringWithFormat:@"%@ on %@ should return %@", java_script,
get_element_java_script, expected];
}
}
} // namespace web
#endif // IOS_INTERNAL_CHROME_BROWSER_WEB_WEB_JS_TEST_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment