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
3 // found in the LICENSE file.
6 #include "core/html/HTMLSelectElement.h"
8 #include "core/frame/FrameView.h"
9 #include "core/html/HTMLDocument.h"
10 #include "core/html/forms/FormController.h"
11 #include "core/loader/EmptyClients.h"
12 #include "core/testing/DummyPageHolder.h"
13 #include <gtest/gtest.h>
17 class HTMLSelectElementTest
: public::testing::Test
{
19 void SetUp() override
;
20 HTMLDocument
& document() const { return *m_document
; }
23 OwnPtr
<DummyPageHolder
> m_dummyPageHolder
;
24 RefPtrWillBePersistent
<HTMLDocument
> m_document
;
27 void HTMLSelectElementTest::SetUp()
29 Page::PageClients pageClients
;
30 fillWithEmptyClients(pageClients
);
31 m_dummyPageHolder
= DummyPageHolder::create(IntSize(800, 600), &pageClients
);
33 m_document
= toHTMLDocument(&m_dummyPageHolder
->document());
34 m_document
->setMimeType("text/html");
37 TEST_F(HTMLSelectElementTest
, SaveRestoreSelectSingleFormControlState
)
39 document().documentElement()->setInnerHTML(String("<!DOCTYPE HTML><select id='sel'>"
40 "<option value='111' id='0'>111</option>"
41 "<option value='222'>222</option>"
42 "<option value='111' selected id='2'>!666</option>"
43 "<option value='999'>999</option></select>"), ASSERT_NO_EXCEPTION
);
44 document().view()->updateAllLifecyclePhases();
45 Element
* element
= document().getElementById("sel");
46 HTMLFormControlElementWithState
* select
= toHTMLSelectElement(element
);
47 HTMLOptionElement
* opt0
= toHTMLOptionElement(document().getElementById("0"));
48 HTMLOptionElement
* opt2
= toHTMLOptionElement(document().getElementById("2"));
50 // Save the select element state, and then restore again.
51 // Test passes if the restored state is not changed.
52 EXPECT_EQ(2, toHTMLSelectElement(element
)->selectedIndex());
53 EXPECT_FALSE(opt0
->selected());
54 EXPECT_TRUE(opt2
->selected());
55 FormControlState selectState
= select
->saveFormControlState();
56 EXPECT_EQ(2U, selectState
.valueSize());
58 // Clear the selected state, to be restored by restoreFormControlState.
59 toHTMLSelectElement(select
)->setSelectedIndex(-1);
60 ASSERT_FALSE(opt2
->selected());
63 select
->restoreFormControlState(selectState
);
64 EXPECT_EQ(2, toHTMLSelectElement(element
)->selectedIndex());
65 EXPECT_FALSE(opt0
->selected());
66 EXPECT_TRUE(opt2
->selected());
69 TEST_F(HTMLSelectElementTest
, SaveRestoreSelectMultipleFormControlState
)
71 document().documentElement()->setInnerHTML(String("<!DOCTYPE HTML><select id='sel' multiple>"
72 "<option value='111' id='0'>111</option>"
73 "<option value='222'>222</option>"
74 "<option value='111' selected id='2'>!666</option>"
75 "<option value='999' selected id='3'>999</option></select>"), ASSERT_NO_EXCEPTION
);
76 document().view()->updateAllLifecyclePhases();
77 HTMLFormControlElementWithState
* select
= toHTMLSelectElement(document().getElementById("sel"));
79 HTMLOptionElement
* opt0
= toHTMLOptionElement(document().getElementById("0"));
80 HTMLOptionElement
* opt2
= toHTMLOptionElement(document().getElementById("2"));
81 HTMLOptionElement
* opt3
= toHTMLOptionElement(document().getElementById("3"));
83 // Save the select element state, and then restore again.
84 // Test passes if the selected options are not changed.
85 EXPECT_FALSE(opt0
->selected());
86 EXPECT_TRUE(opt2
->selected());
87 EXPECT_TRUE(opt3
->selected());
88 FormControlState selectState
= select
->saveFormControlState();
89 EXPECT_EQ(4U, selectState
.valueSize());
91 // Clear the selected state, to be restored by restoreFormControlState.
92 opt2
->setSelected(false);
93 opt3
->setSelected(false);
94 ASSERT_FALSE(opt2
->selected());
95 ASSERT_FALSE(opt3
->selected());
98 select
->restoreFormControlState(selectState
);
99 EXPECT_FALSE(opt0
->selected());
100 EXPECT_TRUE(opt2
->selected());
101 EXPECT_TRUE(opt3
->selected());
104 TEST_F(HTMLSelectElementTest
, ElementRectRelativeToViewport
)
106 document().documentElement()->setInnerHTML("<select style='position:fixed; top:12.3px; height:24px; -webkit-appearance:none;'><option>o1</select>", ASSERT_NO_EXCEPTION
);
107 document().view()->updateAllLifecyclePhases();
108 HTMLSelectElement
* select
= toHTMLSelectElement(document().body()->firstChild());
110 IntRect bounds
= select
->elementRectRelativeToViewport();
111 EXPECT_EQ(24, bounds
.height());
114 TEST_F(HTMLSelectElementTest
, PopupIsVisible
)
116 document().documentElement()->setInnerHTML("<select><option>o1</option></select>", ASSERT_NO_EXCEPTION
);
117 document().view()->updateAllLifecyclePhases();
118 HTMLSelectElement
* select
= toHTMLSelectElement(document().body()->firstChild());
120 EXPECT_FALSE(select
->popupIsVisible());
122 EXPECT_TRUE(select
->popupIsVisible());
124 EXPECT_FALSE(select
->popupIsVisible());