ChartPanelTest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -------------------
  28. * ChartPanelTest.java
  29. * -------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 13-Jul-2004 : Version 1 (DG);
  38. * 12-Jan-2009 : Added test2502355() (DG);
  39. * 08-Jun-2009 : Added testSetMouseWheelEnabled() (DG);
  40. */
  41. package org.jfree.chart;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertTrue;
  44. import static org.junit.Assert.assertFalse;
  45. import java.awt.geom.Rectangle2D;
  46. import java.util.EventListener;
  47. import java.util.List;
  48. import javax.swing.event.CaretListener;
  49. import org.jfree.chart.axis.NumberAxis;
  50. import org.jfree.chart.event.ChartChangeEvent;
  51. import org.jfree.chart.event.ChartChangeListener;
  52. import org.jfree.chart.plot.PlotOrientation;
  53. import org.jfree.chart.plot.XYPlot;
  54. import org.jfree.data.xy.DefaultXYDataset;
  55. import org.junit.Test;
  56. /**
  57. * Tests for the {@link ChartPanel} class.
  58. */
  59. public class ChartPanelTest implements ChartChangeListener, ChartMouseListener {
  60. private List chartChangeEvents = new java.util.ArrayList();
  61. /**
  62. * Receives a chart change event and stores it in a list for later
  63. * inspection.
  64. *
  65. * @param event the event.
  66. */
  67. @Override
  68. public void chartChanged(ChartChangeEvent event) {
  69. this.chartChangeEvents.add(event);
  70. }
  71. /**
  72. * Test that the constructor will accept a null chart.
  73. */
  74. @Test
  75. public void testConstructor1() {
  76. ChartPanel panel = new ChartPanel(null);
  77. assertEquals(null, panel.getChart());
  78. }
  79. /**
  80. * Test that it is possible to set the panel's chart to null.
  81. */
  82. @Test
  83. public void testSetChart() {
  84. JFreeChart chart = new JFreeChart(new XYPlot());
  85. ChartPanel panel = new ChartPanel(chart);
  86. panel.setChart(null);
  87. assertEquals(null, panel.getChart());
  88. }
  89. /**
  90. * Check the behaviour of the getListeners() method.
  91. */
  92. @Test
  93. public void testGetListeners() {
  94. ChartPanel p = new ChartPanel(null);
  95. p.addChartMouseListener(this);
  96. EventListener[] listeners = p.getListeners(ChartMouseListener.class);
  97. assertEquals(1, listeners.length);
  98. assertEquals(this, listeners[0]);
  99. // try a listener type that isn't registered
  100. listeners = p.getListeners(CaretListener.class);
  101. assertEquals(0, listeners.length);
  102. p.removeChartMouseListener(this);
  103. listeners = p.getListeners(ChartMouseListener.class);
  104. assertEquals(0, listeners.length);
  105. // try a null argument
  106. boolean pass = false;
  107. try {
  108. listeners = p.getListeners((Class) null);
  109. }
  110. catch (NullPointerException e) {
  111. pass = true;
  112. }
  113. assertTrue(pass);
  114. // try a class that isn't a listener
  115. pass = false;
  116. try {
  117. listeners = p.getListeners(Integer.class);
  118. }
  119. catch (ClassCastException e) {
  120. pass = true;
  121. }
  122. assertTrue(pass);
  123. }
  124. /**
  125. * Ignores a mouse click event.
  126. *
  127. * @param event the event.
  128. */
  129. @Override
  130. public void chartMouseClicked(ChartMouseEvent event) {
  131. // ignore
  132. }
  133. /**
  134. * Ignores a mouse move event.
  135. *
  136. * @param event the event.
  137. */
  138. @Override
  139. public void chartMouseMoved(ChartMouseEvent event) {
  140. // ignore
  141. }
  142. /**
  143. * Checks that a call to the zoom() method generates just one
  144. * ChartChangeEvent.
  145. */
  146. @Test
  147. public void test2502355_zoom() {
  148. DefaultXYDataset dataset = new DefaultXYDataset();
  149. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  150. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  151. ChartPanel panel = new ChartPanel(chart);
  152. chart.addChangeListener(this);
  153. this.chartChangeEvents.clear();
  154. panel.zoom(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
  155. assertEquals(1, this.chartChangeEvents.size());
  156. }
  157. /**
  158. * Checks that a call to the zoomInBoth() method generates just one
  159. * ChartChangeEvent.
  160. */
  161. @Test
  162. public void test2502355_zoomInBoth() {
  163. DefaultXYDataset dataset = new DefaultXYDataset();
  164. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  165. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  166. ChartPanel panel = new ChartPanel(chart);
  167. chart.addChangeListener(this);
  168. this.chartChangeEvents.clear();
  169. panel.zoomInBoth(1.0, 2.0);
  170. assertEquals(1, this.chartChangeEvents.size());
  171. }
  172. /**
  173. * Checks that a call to the zoomOutBoth() method generates just one
  174. * ChartChangeEvent.
  175. */
  176. @Test
  177. public void test2502355_zoomOutBoth() {
  178. DefaultXYDataset dataset = new DefaultXYDataset();
  179. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  180. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  181. ChartPanel panel = new ChartPanel(chart);
  182. chart.addChangeListener(this);
  183. this.chartChangeEvents.clear();
  184. panel.zoomOutBoth(1.0, 2.0);
  185. assertEquals(1, this.chartChangeEvents.size());
  186. }
  187. /**
  188. * Checks that a call to the restoreAutoBounds() method generates just one
  189. * ChartChangeEvent.
  190. */
  191. @Test
  192. public void test2502355_restoreAutoBounds() {
  193. DefaultXYDataset dataset = new DefaultXYDataset();
  194. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  195. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  196. ChartPanel panel = new ChartPanel(chart);
  197. chart.addChangeListener(this);
  198. this.chartChangeEvents.clear();
  199. panel.restoreAutoBounds();
  200. assertEquals(1, this.chartChangeEvents.size());
  201. }
  202. /**
  203. * Checks that a call to the zoomInDomain() method, for a plot with more
  204. * than one domain axis, generates just one ChartChangeEvent.
  205. */
  206. @Test
  207. public void test2502355_zoomInDomain() {
  208. DefaultXYDataset dataset = new DefaultXYDataset();
  209. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  210. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  211. XYPlot plot = (XYPlot) chart.getPlot();
  212. plot.setDomainAxis(1, new NumberAxis("X2"));
  213. ChartPanel panel = new ChartPanel(chart);
  214. chart.addChangeListener(this);
  215. this.chartChangeEvents.clear();
  216. panel.zoomInDomain(1.0, 2.0);
  217. assertEquals(1, this.chartChangeEvents.size());
  218. }
  219. /**
  220. * Checks that a call to the zoomInRange() method, for a plot with more
  221. * than one range axis, generates just one ChartChangeEvent.
  222. */
  223. @Test
  224. public void test2502355_zoomInRange() {
  225. DefaultXYDataset dataset = new DefaultXYDataset();
  226. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  227. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  228. XYPlot plot = (XYPlot) chart.getPlot();
  229. plot.setRangeAxis(1, new NumberAxis("X2"));
  230. ChartPanel panel = new ChartPanel(chart);
  231. chart.addChangeListener(this);
  232. this.chartChangeEvents.clear();
  233. panel.zoomInRange(1.0, 2.0);
  234. assertEquals(1, this.chartChangeEvents.size());
  235. }
  236. /**
  237. * Checks that a call to the zoomOutDomain() method, for a plot with more
  238. * than one domain axis, generates just one ChartChangeEvent.
  239. */
  240. @Test
  241. public void test2502355_zoomOutDomain() {
  242. DefaultXYDataset dataset = new DefaultXYDataset();
  243. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  244. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  245. XYPlot plot = (XYPlot) chart.getPlot();
  246. plot.setDomainAxis(1, new NumberAxis("X2"));
  247. ChartPanel panel = new ChartPanel(chart);
  248. chart.addChangeListener(this);
  249. this.chartChangeEvents.clear();
  250. panel.zoomOutDomain(1.0, 2.0);
  251. assertEquals(1, this.chartChangeEvents.size());
  252. }
  253. /**
  254. * Checks that a call to the zoomOutRange() method, for a plot with more
  255. * than one range axis, generates just one ChartChangeEvent.
  256. */
  257. @Test
  258. public void test2502355_zoomOutRange() {
  259. DefaultXYDataset dataset = new DefaultXYDataset();
  260. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  261. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  262. XYPlot plot = (XYPlot) chart.getPlot();
  263. plot.setRangeAxis(1, new NumberAxis("X2"));
  264. ChartPanel panel = new ChartPanel(chart);
  265. chart.addChangeListener(this);
  266. this.chartChangeEvents.clear();
  267. panel.zoomOutRange(1.0, 2.0);
  268. assertEquals(1, this.chartChangeEvents.size());
  269. }
  270. /**
  271. * Checks that a call to the restoreAutoDomainBounds() method, for a plot
  272. * with more than one range axis, generates just one ChartChangeEvent.
  273. */
  274. @Test
  275. public void test2502355_restoreAutoDomainBounds() {
  276. DefaultXYDataset dataset = new DefaultXYDataset();
  277. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  278. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  279. XYPlot plot = (XYPlot) chart.getPlot();
  280. plot.setDomainAxis(1, new NumberAxis("X2"));
  281. ChartPanel panel = new ChartPanel(chart);
  282. chart.addChangeListener(this);
  283. this.chartChangeEvents.clear();
  284. panel.restoreAutoDomainBounds();
  285. assertEquals(1, this.chartChangeEvents.size());
  286. }
  287. /**
  288. * Checks that a call to the restoreAutoRangeBounds() method, for a plot
  289. * with more than one range axis, generates just one ChartChangeEvent.
  290. */
  291. @Test
  292. public void test2502355_restoreAutoRangeBounds() {
  293. DefaultXYDataset dataset = new DefaultXYDataset();
  294. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  295. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  296. XYPlot plot = (XYPlot) chart.getPlot();
  297. plot.setRangeAxis(1, new NumberAxis("X2"));
  298. ChartPanel panel = new ChartPanel(chart);
  299. chart.addChangeListener(this);
  300. this.chartChangeEvents.clear();
  301. panel.restoreAutoRangeBounds();
  302. assertEquals(1, this.chartChangeEvents.size());
  303. }
  304. /**
  305. * In version 1.0.13 there is a bug where enabling the mouse wheel handler
  306. * twice would in fact disable it.
  307. */
  308. @Test
  309. public void testSetMouseWheelEnabled() {
  310. DefaultXYDataset dataset = new DefaultXYDataset();
  311. JFreeChart chart = ChartFactory.createXYLineChart("TestChart", "X",
  312. "Y", dataset, PlotOrientation.VERTICAL, false, false, false);
  313. ChartPanel panel = new ChartPanel(chart);
  314. panel.setMouseWheelEnabled(true);
  315. assertTrue(panel.isMouseWheelEnabled());
  316. panel.setMouseWheelEnabled(true);
  317. assertTrue(panel.isMouseWheelEnabled());
  318. panel.setMouseWheelEnabled(false);
  319. assertFalse(panel.isMouseWheelEnabled());
  320. }
  321. }