OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/html/HTMLInputElement.h" | 5 #include "core/html/HTMLInputElement.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/frame/FrameHost.h" | 8 #include "core/frame/FrameHost.h" |
9 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
10 #include "core/frame/VisualViewport.h" | 10 #include "core/frame/VisualViewport.h" |
11 #include "core/html/HTMLBodyElement.h" | 11 #include "core/html/HTMLBodyElement.h" |
12 #include "core/html/HTMLFormElement.h" | 12 #include "core/html/HTMLFormElement.h" |
13 #include "core/html/HTMLHtmlElement.h" | 13 #include "core/html/HTMLHtmlElement.h" |
| 14 #include "core/html/HTMLOptionElement.h" |
14 #include "core/html/forms/DateTimeChooser.h" | 15 #include "core/html/forms/DateTimeChooser.h" |
15 #include "core/testing/DummyPageHolder.h" | 16 #include "core/testing/DummyPageHolder.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include <memory> | 18 #include <memory> |
18 | 19 |
19 namespace blink { | 20 namespace blink { |
20 | 21 |
21 TEST(HTMLInputElementTest, create) | 22 class HTMLInputElementTest : public testing::Test { |
| 23 protected: |
| 24 Document& document() { return m_pageHolder->document(); } |
| 25 HTMLInputElement& testElement() |
| 26 { |
| 27 Element* element = document().getElementById("test"); |
| 28 DCHECK(element); |
| 29 return toHTMLInputElement(*element); |
| 30 } |
| 31 |
| 32 private: |
| 33 void SetUp() override |
| 34 { |
| 35 m_pageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 36 } |
| 37 |
| 38 std::unique_ptr<DummyPageHolder> m_pageHolder; |
| 39 }; |
| 40 |
| 41 TEST_F(HTMLInputElementTest, FilteredDataListOptionsNoList) |
22 { | 42 { |
23 Document* document = Document::create(); | 43 document().documentElement()->setInnerHTML("<input id=test>", ASSERT_NO_EXCE
PTION); |
24 HTMLInputElement* input = HTMLInputElement::create(*document, nullptr, /* cr
eatedByParser */ false); | 44 EXPECT_TRUE(testElement().filteredDataListOptions().isEmpty()); |
| 45 |
| 46 document().documentElement()->setInnerHTML("<input id=test list=dl1><datalis
t id=dl1></datalist>", ASSERT_NO_EXCEPTION); |
| 47 EXPECT_TRUE(testElement().filteredDataListOptions().isEmpty()); |
| 48 } |
| 49 |
| 50 TEST_F(HTMLInputElementTest, FilteredDataListOptionsPrefixed) |
| 51 { |
| 52 document().documentElement()->setInnerHTML( |
| 53 "<input id=test value=ABC list=dl2>" |
| 54 "<datalist id=dl2>" |
| 55 "<option>AbC DEF</option>" |
| 56 "<option>VAX</option>" |
| 57 "<option value=ghi>abc</option>" |
| 58 "</datalist>", ASSERT_NO_EXCEPTION); |
| 59 auto options = testElement().filteredDataListOptions(); |
| 60 EXPECT_EQ(1u, options.size()); |
| 61 EXPECT_EQ("AbC DEF", options[0]->value().utf8()); |
| 62 } |
| 63 |
| 64 TEST_F(HTMLInputElementTest, FilteredDataListOptionsForMultipleEmail) |
| 65 { |
| 66 document().documentElement()->setInnerHTML( |
| 67 "<input id=test value='foo@example.com, tkent' list=dl3 type=email multi
ple>" |
| 68 "<datalist id=dl3>" |
| 69 "<option>keishi@chromium.org</option>" |
| 70 "<option>tkent@chromium.org</option>" |
| 71 "</datalist>", ASSERT_NO_EXCEPTION); |
| 72 auto options = testElement().filteredDataListOptions(); |
| 73 EXPECT_EQ(1u, options.size()); |
| 74 EXPECT_EQ("tkent@chromium.org", options[0]->value().utf8()); |
| 75 } |
| 76 |
| 77 TEST_F(HTMLInputElementTest, create) |
| 78 { |
| 79 HTMLInputElement* input = HTMLInputElement::create(document(), nullptr, /* c
reatedByParser */ false); |
25 EXPECT_NE(nullptr, input->userAgentShadowRoot()); | 80 EXPECT_NE(nullptr, input->userAgentShadowRoot()); |
26 | 81 |
27 input = HTMLInputElement::create(*document, nullptr, /* createdByParser */ t
rue); | 82 input = HTMLInputElement::create(document(), nullptr, /* createdByParser */
true); |
28 EXPECT_EQ(nullptr, input->userAgentShadowRoot()); | 83 EXPECT_EQ(nullptr, input->userAgentShadowRoot()); |
29 input->parserSetAttributes(Vector<Attribute>()); | 84 input->parserSetAttributes(Vector<Attribute>()); |
30 EXPECT_NE(nullptr, input->userAgentShadowRoot()); | 85 EXPECT_NE(nullptr, input->userAgentShadowRoot()); |
31 } | 86 } |
32 | 87 |
33 TEST(HTMLInputElementTest, NoAssertWhenMovedInNewDocument) | 88 TEST_F(HTMLInputElementTest, NoAssertWhenMovedInNewDocument) |
34 { | 89 { |
35 Document* documentWithoutFrame = Document::create(); | 90 Document* documentWithoutFrame = Document::create(); |
36 EXPECT_EQ(nullptr, documentWithoutFrame->frameHost()); | 91 EXPECT_EQ(nullptr, documentWithoutFrame->frameHost()); |
37 HTMLHtmlElement* html = HTMLHtmlElement::create(*documentWithoutFrame); | 92 HTMLHtmlElement* html = HTMLHtmlElement::create(*documentWithoutFrame); |
38 html->appendChild(HTMLBodyElement::create(*documentWithoutFrame)); | 93 html->appendChild(HTMLBodyElement::create(*documentWithoutFrame)); |
39 | 94 |
40 // Create an input element with type "range" inside a document without frame
. | 95 // Create an input element with type "range" inside a document without frame
. |
41 toHTMLBodyElement(html->firstChild())->setInnerHTML("<input type='range' />"
, ASSERT_NO_EXCEPTION); | 96 toHTMLBodyElement(html->firstChild())->setInnerHTML("<input type='range' />"
, ASSERT_NO_EXCEPTION); |
42 documentWithoutFrame->appendChild(html); | 97 documentWithoutFrame->appendChild(html); |
43 | 98 |
44 std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(); | 99 std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(); |
45 auto& document = pageHolder->document(); | 100 auto& document = pageHolder->document(); |
46 EXPECT_NE(nullptr, document.frameHost()); | 101 EXPECT_NE(nullptr, document.frameHost()); |
47 | 102 |
48 // Put the input element inside a document with frame. | 103 // Put the input element inside a document with frame. |
49 document.body()->appendChild(documentWithoutFrame->body()->firstChild()); | 104 document.body()->appendChild(documentWithoutFrame->body()->firstChild()); |
50 | 105 |
51 // Remove the input element and all refs to it so it gets deleted before the
document. | 106 // Remove the input element and all refs to it so it gets deleted before the
document. |
52 // The assert in |EventHandlerRegistry::updateEventHandlerTargets()| should
not be triggered. | 107 // The assert in |EventHandlerRegistry::updateEventHandlerTargets()| should
not be triggered. |
53 document.body()->removeChild(document.body()->firstChild()); | 108 document.body()->removeChild(document.body()->firstChild()); |
54 } | 109 } |
55 | 110 |
56 TEST(HTMLInputElementTest, DefaultToolTip) | 111 TEST_F(HTMLInputElementTest, DefaultToolTip) |
57 { | 112 { |
58 Document* document = Document::create(); | 113 HTMLInputElement* inputWithoutForm = HTMLInputElement::create(document(), nu
llptr, false); |
59 HTMLHtmlElement* html = HTMLHtmlElement::create(*document); | |
60 html->appendChild(HTMLBodyElement::create(*document)); | |
61 HTMLInputElement* inputWithoutForm = HTMLInputElement::create(*document, nul
lptr, false); | |
62 inputWithoutForm->setBooleanAttribute(HTMLNames::requiredAttr, true); | 114 inputWithoutForm->setBooleanAttribute(HTMLNames::requiredAttr, true); |
63 toHTMLBodyElement(html->firstChild())->appendChild(inputWithoutForm); | 115 document().body()->appendChild(inputWithoutForm); |
64 document->appendChild(html); | |
65 EXPECT_EQ("<<ValidationValueMissing>>", inputWithoutForm->defaultToolTip()); | 116 EXPECT_EQ("<<ValidationValueMissing>>", inputWithoutForm->defaultToolTip()); |
66 | 117 |
67 HTMLFormElement* form = HTMLFormElement::create(*document); | 118 HTMLFormElement* form = HTMLFormElement::create(document()); |
68 document->body()->appendChild(form); | 119 document().body()->appendChild(form); |
69 HTMLInputElement* inputWithForm = HTMLInputElement::create(*document, nullpt
r, false); | 120 HTMLInputElement* inputWithForm = HTMLInputElement::create(document(), nullp
tr, false); |
70 inputWithForm->setBooleanAttribute(HTMLNames::requiredAttr, true); | 121 inputWithForm->setBooleanAttribute(HTMLNames::requiredAttr, true); |
71 form->appendChild(inputWithForm); | 122 form->appendChild(inputWithForm); |
72 EXPECT_EQ("<<ValidationValueMissing>>", inputWithForm->defaultToolTip()); | 123 EXPECT_EQ("<<ValidationValueMissing>>", inputWithForm->defaultToolTip()); |
73 | 124 |
74 form->setBooleanAttribute(HTMLNames::novalidateAttr, true); | 125 form->setBooleanAttribute(HTMLNames::novalidateAttr, true); |
75 EXPECT_EQ(String(), inputWithForm->defaultToolTip()); | 126 EXPECT_EQ(String(), inputWithForm->defaultToolTip()); |
76 } | 127 } |
77 | 128 |
78 // crbug.com/589838 | 129 // crbug.com/589838 |
79 TEST(HTMLInputElementTest, ImageTypeCrash) | 130 TEST_F(HTMLInputElementTest, ImageTypeCrash) |
80 { | 131 { |
81 Document* document = Document::create(); | 132 HTMLInputElement* input = HTMLInputElement::create(document(), nullptr, fals
e); |
82 HTMLInputElement* input = HTMLInputElement::create(*document, nullptr, false
); | |
83 input->setAttribute(HTMLNames::typeAttr, "image"); | 133 input->setAttribute(HTMLNames::typeAttr, "image"); |
84 input->ensureFallbackContent(); | 134 input->ensureFallbackContent(); |
85 // Make sure ensurePrimaryContent() recreates UA shadow tree, and updating | 135 // Make sure ensurePrimaryContent() recreates UA shadow tree, and updating |
86 // |value| doesn't crash. | 136 // |value| doesn't crash. |
87 input->ensurePrimaryContent(); | 137 input->ensurePrimaryContent(); |
88 input->setAttribute(HTMLNames::valueAttr, "aaa"); | 138 input->setAttribute(HTMLNames::valueAttr, "aaa"); |
89 } | 139 } |
90 | 140 |
91 TEST(HTMLInputElementTest, DateTimeChooserSizeParamRespectsScale) | 141 TEST_F(HTMLInputElementTest, DateTimeChooserSizeParamRespectsScale) |
92 { | 142 { |
93 std::unique_ptr<DummyPageHolder> pageHolder = DummyPageHolder::create(); | 143 document().view()->frame().host()->visualViewport().setScale(2.f); |
94 Document* document = &(pageHolder->document()); | 144 document().body()->setInnerHTML("<input type='date' style='width:200px;heigh
t:50px' />", ASSERT_NO_EXCEPTION); |
95 document->view()->frame().host()->visualViewport().setScale(2.f); | 145 document().view()->updateAllLifecyclePhases(); |
96 document->body()->setInnerHTML("<input type='date' style='width:200px;height
:50px' />", ASSERT_NO_EXCEPTION); | 146 HTMLInputElement* input = toHTMLInputElement(document().body()->firstChild()
); |
97 document->view()->updateAllLifecyclePhases(); | |
98 HTMLInputElement* input = toHTMLInputElement(document->body()->firstChild())
; | |
99 | 147 |
100 DateTimeChooserParameters params; | 148 DateTimeChooserParameters params; |
101 bool success = input->setupDateTimeChooserParameters(params); | 149 bool success = input->setupDateTimeChooserParameters(params); |
102 EXPECT_TRUE(success); | 150 EXPECT_TRUE(success); |
103 EXPECT_EQ("date", params.type); | 151 EXPECT_EQ("date", params.type); |
104 EXPECT_EQ(IntRect(16, 16, 400, 100), params.anchorRectInScreen); | 152 EXPECT_EQ(IntRect(16, 16, 400, 100), params.anchorRectInScreen); |
105 } | 153 } |
106 | 154 |
107 } // namespace blink | 155 } // namespace blink |
OLD | NEW |