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