Add ICU message format support
[chromium-blink-merge.git] / ios / web / web_state / js / common_js_unittest.mm
blob2044cbf4d504738a3610b8c5b291504ab2fef6a9
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::WebTestWithUIWebViewWebController>
35     CommonJSUIWebViewTest;
37 // Concrete test fixture to test core.js using WKWebView-based web controller.
38 typedef CommonJsTest<web::WebTestWithWKWebViewWebController>
39     CommonJSWKWebViewTest;
41 WEB_TEST_F(CommonJSUIWebViewTest, CommonJSWKWebViewTest, Foo) {
42   this->LoadHtml(@"<html><body>"
43                   "<input type='text' name='firstname'>"
44                   "<input type='text' name='lastname'>"
45                   "<input type='email' name='email'>"
46                   "<input type='tel' name='phone'>"
47                   "<input type='url' name='blog'>"
48                   "<input type='number' name='expected number of clicks'>"
49                   "<input type='password' name='pwd'>"
50                   "<input type='checkbox' name='vehicle' value='Bike'>"
51                   "<input type='checkbox' name='vehicle' value='Car'>"
52                   "<input type='checkbox' name='vehicle' value='Rocket'>"
53                   "<input type='radio' name='boolean' value='true'>"
54                   "<input type='radio' name='boolean' value='false'>"
55                   "<input type='radio' name='boolean' value='other'>"
56                   "<select name='state'>"
57                   "  <option value='CA'>CA</option>"
58                   "  <option value='MA'>MA</option>"
59                   "</select>"
60                   "<select name='cars' multiple>"
61                   "  <option value='volvo'>Volvo</option>"
62                   "  <option value='saab'>Saab</option>"
63                   "  <option value='opel'>Opel</option>"
64                   "  <option value='audi'>Audi</option>"
65                   "</select>"
66                   "<input type='submit' name='submit' value='Submit'>"
67                   "</body></html>");
69   static const struct TextFieldTestElement testElements[] = {
70       {"firstname", 0, true},
71       {"lastname", 0, true},
72       {"email", 0, true},
73       {"phone", 0, true},
74       {"blog", 0, true},
75       {"expected number of clicks", 0, true},
76       {"pwd", 0, true},
77       {"vehicle", 0, false},
78       {"vehicle", 1, false},
79       {"vehicle", 2, false},
80       {"boolean", 0, false},
81       {"boolean", 1, false},
82       {"boolean", 2, false},
83       {"state", 0, false},
84       {"cars", 0, false},
85       {"submit", 0, false}};
86   for (size_t i = 0; i < arraysize(testElements); ++i) {
87     TextFieldTestElement element = testElements[i];
88     NSString* result =
89         this->RunJavaScript(base::SysUTF8ToNSString(base::StringPrintf(
90             "__gCrWeb.common.isTextField("
91             "window.document.getElementsByName('%s')[%u])",
92             element.element_name, element.element_index)));
93     EXPECT_NSEQ(element.expected_is_text_field ? @"true" : @"false", result)
94         << element.element_name << " with index " << element.element_index
95         << " isTextField(): " << element.expected_is_text_field;
96   }
99 }  // namespace