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.javascript.host.performance;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.WebDriver;
21  
22  /**
23   * Tests for {@link Performance}.
24   *
25   * @author Ronald Brill
26   */
27  public class PerformanceTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("true")
34      public void same() throws Exception {
35          final String html = DOCTYPE_HTML
36                  + "<html>\n"
37                  + "<head>\n"
38                  + "<script>\n"
39                  + LOG_TITLE_FUNCTION
40                  + "  function test() {\n"
41                  + "    let perf = window.performance;\n"
42                  + "    log(window.performance == perf);\n"
43                  + "  }\n"
44                  + "  test();\n"
45                  + "</script>\n"
46                  + "</head>\n"
47                  + "<body></body>\n"
48                  + "</html>";
49  
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      public void now() throws Exception {
58          final String html = DOCTYPE_HTML
59                  + "<html>\n"
60                  + "<head>\n"
61                  + "<script>\n"
62                  + LOG_TITLE_FUNCTION
63                  + "  function test() {\n"
64                  + "    log(performance.now());\n"
65                  + "    log(performance.now());\n"
66                  + "    log(typeof performance.now());\n"
67                  + "  }\n"
68                  + "  test();\n"
69                  + "</script>\n"
70                  + "</head>\n"
71                  + "<body></body>\n"
72                  + "</html>";
73  
74          final WebDriver driver = loadPage2(html);
75          final String[] title = driver.getTitle().split("§");
76          assertEquals(3, title.length);
77  
78          final String now1 = title[0];
79          assertTrue(Double.parseDouble(now1) > 0);
80  
81          final String now2 = title[1];
82          assertTrue(Double.parseDouble(now2) > Double.parseDouble(now1));
83  
84          assertEquals("number", title[2]);
85      }
86  
87      /**
88       * @throws Exception if the test fails
89       */
90      @Test
91      @Alerts("[object PerformanceTiming]")
92      public void timing() throws Exception {
93          final String html = DOCTYPE_HTML
94                  + "<html>\n"
95                  + "<head>\n"
96                  + "<script>\n"
97                  + LOG_TITLE_FUNCTION
98                  + "  function test() {\n"
99                  + "    var performanceTiming = performance.timing;\n"
100                 + "    log(performanceTiming);\n"
101                 + "  }\n"
102                 + "  test();\n"
103                 + "</script>\n"
104                 + "</head>\n"
105                 + "<body></body>\n"
106                 + "</html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception if the test fails
113      */
114     @Test
115     @Alerts({"function", "function", "function", "function"})
116     public void methods() throws Exception {
117         final String html = DOCTYPE_HTML
118                 + "<html>\n"
119                 + "<body>\n"
120                 + "<script>\n"
121                 + LOG_TITLE_FUNCTION
122                 + "  log(typeof performance.now);\n"
123                 + "  log(typeof performance.getEntries);\n"
124                 + "  log(typeof performance.getEntriesByName);\n"
125                 + "  log(typeof performance.getEntriesByType);\n"
126                 + "</script>\n"
127                 + "</body></html>";
128 
129         loadPageVerifyTitle2(html);
130     }
131 }