forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_js_unittest.mm
99 lines (88 loc) · 3.88 KB
/
common_js_unittest.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2015 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.
#import <UIKit/UIKit.h>
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "ios/web/public/test/web_test_util.h"
#include "ios/web/test/web_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
// Unit tests for ios/web/web_state/js/resources/common.js.
namespace {
// Struct for isTextField() test data.
struct TextFieldTestElement {
// The element name.
const char* element_name;
// The index of this element in those that have the same name.
const int element_index;
// True if this is expected to be a text field.
const bool expected_is_text_field;
};
// A mixin class for testing with CRWWKWebViewWebController or
// CRWUIWebViewWebController.
template <typename WebTestT>
class CommonJsTest : public WebTestT {};
// Concrete test fixture to test core.js using UIWebView-based web controller.
typedef CommonJsTest<web::WebTestWithUIWebViewWebController>
CommonJSUIWebViewTest;
// Concrete test fixture to test core.js using WKWebView-based web controller.
typedef CommonJsTest<web::WebTestWithWKWebViewWebController>
CommonJSWKWebViewTest;
WEB_TEST_F(CommonJSUIWebViewTest, CommonJSWKWebViewTest, Foo) {
this->LoadHtml(@"<html><body>"
"<input type='text' name='firstname'>"
"<input type='text' name='lastname'>"
"<input type='email' name='email'>"
"<input type='tel' name='phone'>"
"<input type='url' name='blog'>"
"<input type='number' name='expected number of clicks'>"
"<input type='password' name='pwd'>"
"<input type='checkbox' name='vehicle' value='Bike'>"
"<input type='checkbox' name='vehicle' value='Car'>"
"<input type='checkbox' name='vehicle' value='Rocket'>"
"<input type='radio' name='boolean' value='true'>"
"<input type='radio' name='boolean' value='false'>"
"<input type='radio' name='boolean' value='other'>"
"<select name='state'>"
" <option value='CA'>CA</option>"
" <option value='MA'>MA</option>"
"</select>"
"<select name='cars' multiple>"
" <option value='volvo'>Volvo</option>"
" <option value='saab'>Saab</option>"
" <option value='opel'>Opel</option>"
" <option value='audi'>Audi</option>"
"</select>"
"<input type='submit' name='submit' value='Submit'>"
"</body></html>");
static const struct TextFieldTestElement testElements[] = {
{"firstname", 0, true},
{"lastname", 0, true},
{"email", 0, true},
{"phone", 0, true},
{"blog", 0, true},
{"expected number of clicks", 0, true},
{"pwd", 0, true},
{"vehicle", 0, false},
{"vehicle", 1, false},
{"vehicle", 2, false},
{"boolean", 0, false},
{"boolean", 1, false},
{"boolean", 2, false},
{"state", 0, false},
{"cars", 0, false},
{"submit", 0, false}};
for (size_t i = 0; i < arraysize(testElements); ++i) {
TextFieldTestElement element = testElements[i];
NSString* result =
this->RunJavaScript(base::SysUTF8ToNSString(base::StringPrintf(
"__gCrWeb.common.isTextField("
"window.document.getElementsByName('%s')[%u])",
element.element_name, element.element_index)));
EXPECT_NSEQ(element.expected_is_text_field ? @"true" : @"false", result)
<< element.element_name << " with index " << element.element_index
<< " isTextField(): " << element.expected_is_text_field;
}
}
} // namespace