1
2
3
4
5
6
7
8
9
10
11
12
13
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
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class PerformanceTest extends WebDriverTestCase {
31
32
33
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
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
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
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 }