/tags/fest-swing-1.0/src/test/java/org/fest/swing/driver/BasicJTableCellReaderTest.java

http://fest.googlecode.com/ · Java · 251 lines · 198 code · 32 blank · 21 comment · 0 complexity · 8e0a7bc36f1cb10062b1f7747daa9b80 MD5 · raw file

  1. /*
  2. * Created on Apr 12, 2008
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. *
  14. * Copyright @2008-2009 the original author or authors.
  15. */
  16. package org.fest.swing.driver;
  17. import java.awt.Color;
  18. import java.awt.Component;
  19. import java.awt.Font;
  20. import javax.swing.*;
  21. import javax.swing.table.DefaultTableModel;
  22. import org.testng.annotations.AfterMethod;
  23. import org.testng.annotations.BeforeClass;
  24. import org.testng.annotations.BeforeMethod;
  25. import org.testng.annotations.Test;
  26. import org.fest.swing.annotation.RunsInCurrentThread;
  27. import org.fest.swing.annotation.RunsInEDT;
  28. import org.fest.swing.core.Robot;
  29. import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
  30. import org.fest.swing.edt.GuiQuery;
  31. import org.fest.swing.edt.GuiTask;
  32. import org.fest.swing.test.data.BooleanProvider;
  33. import org.fest.swing.test.swing.CustomCellRenderer;
  34. import org.fest.swing.test.swing.TestWindow;
  35. import static org.fest.assertions.Assertions.assertThat;
  36. import static org.fest.swing.core.BasicRobot.robotWithNewAwtHierarchy;
  37. import static org.fest.swing.edt.GuiActionRunner.execute;
  38. import static org.fest.swing.query.ComponentBackgroundQuery.backgroundOf;
  39. import static org.fest.swing.query.ComponentFontQuery.fontOf;
  40. import static org.fest.swing.query.ComponentForegroundQuery.foregroundOf;
  41. import static org.fest.swing.test.core.TestGroups.GUI;
  42. import static org.fest.util.Arrays.array;
  43. /**
  44. * Tests for <code>{@link BasicJTableCellReader}</code>.
  45. *
  46. * @author Alex Ruiz
  47. * @author Yvonne Wang
  48. */
  49. @Test(groups = GUI)
  50. public class BasicJTableCellReaderTest {
  51. private Robot robot;
  52. private JTable table;
  53. private BasicJTableCellReader reader;
  54. @BeforeClass public void setUpOnce() {
  55. FailOnThreadViolationRepaintManager.install();
  56. }
  57. @BeforeMethod public void setUp() {
  58. robot = robotWithNewAwtHierarchy();
  59. MyWindow window = MyWindow.createNew();
  60. table = window.table;
  61. reader = new BasicJTableCellReader();
  62. robot.showWindow(window);
  63. }
  64. @AfterMethod public void tearDown() {
  65. robot.cleanUp();
  66. }
  67. public void shouldReturnModelValueToStringIfRendererNotRecognized() {
  68. setModelData(table, new Object[][] { array(new Jedi("Yoda")) }, array("Names"));
  69. setJToolBarAsCellRenderer(table);
  70. robot.waitForIdle();
  71. String value = valueAt(reader, table, 0, 0);
  72. assertThat(value).isEqualTo("Yoda");
  73. }
  74. @RunsInEDT
  75. private static void setModelData(final JTable table, final Object[][] data, final Object[] columnNames) {
  76. execute(new GuiTask() {
  77. protected void executeInEDT() {
  78. DefaultTableModel model = new DefaultTableModel(data, columnNames);
  79. table.setModel(model);
  80. }
  81. });
  82. }
  83. @RunsInEDT
  84. private static void setJToolBarAsCellRenderer(final JTable table) {
  85. execute(new GuiTask() {
  86. protected void executeInEDT() {
  87. setCellRendererComponent(table, new JToolBar());
  88. }
  89. });
  90. }
  91. public void shouldReturnFontFromRenderer() {
  92. JLabel label = setJLabelAsCellRenderer(table);
  93. robot.waitForIdle();
  94. Font font = fontAt(reader, table, 0, 0);
  95. assertThat(font).isEqualTo(fontOf(label));
  96. }
  97. @RunsInEDT
  98. private static Font fontAt(final BasicJTableCellReader reader, final JTable table, final int row, final int column) {
  99. return execute(new GuiQuery<Font>() {
  100. protected Font executeInEDT() {
  101. return reader.fontAt(table, row, column);
  102. }
  103. });
  104. }
  105. public void shouldReturnBackgroundColorFromRenderer() {
  106. JLabel label = setJLabelAsCellRenderer(table);
  107. robot.waitForIdle();
  108. Color background = backgroundAt(reader, table, 0, 0);
  109. assertThat(background).isEqualTo(backgroundOf(label));
  110. }
  111. @RunsInEDT
  112. private static Color backgroundAt(final BasicJTableCellReader reader, final JTable table, final int row,
  113. final int column) {
  114. return execute(new GuiQuery<Color>() {
  115. protected Color executeInEDT() {
  116. return reader.backgroundAt(table, row, column);
  117. }
  118. });
  119. }
  120. public void shouldReturnForegroundColorFromRenderer() {
  121. JLabel label = setJLabelAsCellRenderer(table);
  122. robot.waitForIdle();
  123. Color foreground = foregroundAt(reader, table, 0, 0);
  124. assertThat(foreground).isEqualTo(foregroundOf(label));
  125. }
  126. @RunsInEDT
  127. private static Color foregroundAt(final BasicJTableCellReader reader, final JTable table, final int row,
  128. final int column) {
  129. return execute(new GuiQuery<Color>() {
  130. protected Color executeInEDT() {
  131. return reader.foregroundAt(table, row, column);
  132. }
  133. });
  134. }
  135. public void shouldReturnTextFromCellRendererIfRendererIsJLabel() {
  136. setJLabelAsCellRenderer(table);
  137. robot.waitForIdle();
  138. String value = valueAt(reader, table, 0, 0);
  139. assertThat(value).isEqualTo("Hello");
  140. }
  141. @RunsInEDT
  142. private static JLabel setJLabelAsCellRenderer(final JTable table) {
  143. return execute(new GuiQuery<JLabel>() {
  144. protected JLabel executeInEDT() {
  145. JLabel label = new JLabel("Hello");
  146. setCellRendererComponent(table, label);
  147. return label;
  148. }
  149. });
  150. }
  151. public void shouldReturnSelectionFromCellRendererIfRendererIsJComboBox() {
  152. setJComboBoxAsCellRenderer(table, 1);
  153. robot.waitForIdle();
  154. String value = valueAt(reader, table, 0, 0);
  155. assertThat(value).isEqualTo("Two");
  156. }
  157. public void shouldReturnNullIfRendererIsJComboBoxWithoutSelection() {
  158. setJComboBoxAsCellRenderer(table, -1);
  159. robot.waitForIdle();
  160. String value = valueAt(reader, table, 0, 0);
  161. assertThat(value).isNull();
  162. }
  163. @RunsInEDT
  164. private static void setJComboBoxAsCellRenderer(final JTable table, final int comboBoxSelectedIndex) {
  165. execute(new GuiTask() {
  166. protected void executeInEDT() {
  167. JComboBox comboBox = new JComboBox(array("One", "Two"));
  168. comboBox.setSelectedIndex(comboBoxSelectedIndex);
  169. setCellRendererComponent(table, comboBox);
  170. }
  171. });
  172. }
  173. @Test(dataProvider = "booleans", dataProviderClass = BooleanProvider.class)
  174. public void shouldReturnIsSelectedIfRendererIsJCheckBox(boolean selected) {
  175. setJCheckBoxAsCellRenderer(table, "Hello", selected);
  176. robot.waitForIdle();
  177. String value = valueAt(reader, table, 0, 0);
  178. assertThat(value).isEqualTo(String.valueOf(selected));
  179. }
  180. @RunsInEDT
  181. private static void setJCheckBoxAsCellRenderer(final JTable table, final String text, final boolean selected) {
  182. execute(new GuiTask() {
  183. protected void executeInEDT() {
  184. JCheckBox checkBox = new JCheckBox(text, selected);
  185. setCellRendererComponent(table, checkBox);
  186. }
  187. });
  188. }
  189. @RunsInCurrentThread
  190. private static void setCellRendererComponent(JTable table, Component renderer) {
  191. CustomCellRenderer cellRenderer = new CustomCellRenderer(renderer);
  192. table.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
  193. }
  194. @RunsInEDT
  195. private static String valueAt(final BasicJTableCellReader reader, final JTable table, final int row, final
  196. int column) {
  197. return execute(new GuiQuery<String>() {
  198. protected String executeInEDT() {
  199. return reader.valueAt(table, row, column);
  200. }
  201. });
  202. }
  203. private static class MyWindow extends TestWindow {
  204. private static final long serialVersionUID = 1L;
  205. @RunsInEDT
  206. static MyWindow createNew() {
  207. return execute(new GuiQuery<MyWindow>() {
  208. protected MyWindow executeInEDT() {
  209. return new MyWindow();
  210. }
  211. });
  212. }
  213. final JTable table = new JTable(1, 1);
  214. private MyWindow() {
  215. super(BasicJTableCellReaderTest.class);
  216. addComponents(table);
  217. }
  218. }
  219. }