View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.util.Iterator;
18  
19  import org.htmlunit.SimpleWebTestCase;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.xml.sax.helpers.AttributesImpl;
24  
25  /**
26   * Tests for {@link DefaultElementFactory}.
27   *
28   * @author <a href="mailto:marvin.java@gmail.com">Marcos Vinicius B. de Souza</a>
29   * @author Marc Guillemot
30   * @author Ahmed Ashour
31   * @since 1.2
32   */
33  @RunWith(BrowserRunner.class)
34  public class DefaultElementFactoryTest extends SimpleWebTestCase {
35  
36      /**
37       * Test that the attribute order is the same as the provided one.
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void attributeOrder() throws Exception {
42          // Construct the test page.
43          final String html = DOCTYPE_HTML
44                  + "<html><head><title>test page</title></head>\n"
45                  + "<body><div>test message</div></body></html>";
46  
47          // Load the test page.
48          final HtmlPage htmlPage = loadPage(html);
49  
50          // Creates the attributes of the 'anchor'.
51          final AttributesImpl atts = new AttributesImpl();
52          atts.addAttribute(null, "href", "href", null, "http://www.google.com");
53          atts.addAttribute(null, "tabindex", "tabindex", null, "2");
54          atts.addAttribute(null, "accesskey", "accesskey", null, "F");
55  
56          // Access the factory.
57          final DefaultElementFactory defaultElementFactory = new DefaultElementFactory();
58  
59          // Create a anchor element
60          final HtmlAnchor anchor = (HtmlAnchor) defaultElementFactory.createElement(htmlPage, "a", atts);
61  
62          verifyAttributes(anchor);
63      }
64  
65      /**
66       * @param anchor the anchor which attributes should be checked
67       */
68      private static void verifyAttributes(final HtmlAnchor anchor) {
69          // Get the attributes iterator
70          final Iterator<DomAttr> attributeEntriesIterator = anchor.getAttributesMap().values().iterator();
71  
72          // Verify if the attributes are in ascending order of name.
73          DomAttr htmlAttr = attributeEntriesIterator.next();
74          assertEquals("href", htmlAttr.getNodeName());
75          assertEquals("http://www.google.com", htmlAttr.getValue());
76  
77          htmlAttr = attributeEntriesIterator.next();
78          assertEquals("tabindex", htmlAttr.getNodeName());
79          assertEquals("2", htmlAttr.getValue());
80  
81          htmlAttr = attributeEntriesIterator.next();
82          assertEquals("accesskey", htmlAttr.getNodeName());
83          assertEquals("F", htmlAttr.getValue());
84      }
85  
86      /**
87       * Test the order of attributes.
88       * @throws Exception if the test fails
89       */
90      @Test
91      public void attributeOrderLive() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html><body>\n"
94              + "<a href='http://www.google.com' tabindex='2' accesskey='F'>foo</a>\n"
95              + "</body></html>";
96          final HtmlPage page = loadPage(html);
97          final HtmlAnchor anchor = page.getAnchorByText("foo");
98  
99          verifyAttributes(anchor);
100     }
101 }