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.canvas;
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  
23  /**
24   * Unit tests for {@link CanvasGradient}.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class CanvasGradientTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts("function")
36      public void functions() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><head><script>\n"
39              + LOG_TITLE_FUNCTION
40              + "function test() {\n"
41              + "  var canvas = document.getElementById('myCanvas');\n"
42              + "  var ctx = canvas.getContext('2d');\n"
43              + "  var gradient = ctx.createRadialGradient(100, 100, 100, 100, 100, 0);\n"
44              + "  log(typeof gradient.addColorStop);\n"
45              + "}\n"
46              + "</script>\n"
47              + "</head>\n"
48              + "<body onload='test()'><canvas id='myCanvas'></canvas></body>\n"
49              + "</html>";
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if an error occurs
55       */
56      @Test
57      @Alerts("[object CanvasGradient]")
58      public void addColorStop() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><head><script>\n"
61              + LOG_TITLE_FUNCTION
62              + "function test() {\n"
63              + "  var canvas = document.getElementById('myCanvas');\n"
64              + "  var ctx = canvas.getContext('2d');\n"
65              + "  var gradient = ctx.createRadialGradient(100, 100, 100, 100, 100, 0);\n"
66              + "  gradient.addColorStop(0, 'green');\n"
67              + "  gradient.addColorStop(1, 'white');\n"
68              + "  log(gradient);\n"
69              + "}\n"
70              + "</script>\n"
71              + "</head>\n"
72              + "<body onload='test()'><canvas id='myCanvas'></canvas></body>\n"
73              + "</html>";
74  
75          loadPageVerifyTitle2(html);
76      }
77  }