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