You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SSLCertificateDialogTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.sslcertificate;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.config.InvalidIdentityFileException;
  26. import com.dmdirc.harness.ui.ClassFinder;
  27. import com.dmdirc.harness.ui.TestSSLCertificateDialogModel;
  28. import com.dmdirc.ui.IconManager;
  29. import com.dmdirc.addons.ui_swing.UIUtilities;
  30. import com.dmdirc.config.IdentityManager;
  31. import java.awt.Component;
  32. import java.awt.Dialog;
  33. import java.util.Arrays;
  34. import javax.swing.Icon;
  35. import javax.swing.JLabel;
  36. import javax.swing.border.TitledBorder;
  37. import org.fest.swing.driver.BasicCellRendererReader;
  38. import org.fest.swing.driver.BasicJListCellReader;
  39. import org.fest.swing.edt.GuiActionRunner;
  40. import org.fest.swing.edt.GuiQuery;
  41. import org.fest.swing.fixture.DialogFixture;
  42. import org.fest.swing.junit.testcase.FestSwingJUnitTestCase;
  43. import org.junit.After;
  44. import org.junit.Before;
  45. import org.junit.BeforeClass;
  46. import org.junit.Ignore;
  47. import org.junit.Test;
  48. import static org.junit.Assert.*;
  49. public class SSLCertificateDialogTest extends FestSwingJUnitTestCase {
  50. private DialogFixture window;
  51. @BeforeClass
  52. public static void setUpClass() throws InvalidIdentityFileException {
  53. IdentityManager.load();
  54. UIUtilities.initUISettings();
  55. Main.setUI(new SwingController());
  56. }
  57. @Before
  58. @Override
  59. public void onSetUp() {
  60. }
  61. @After
  62. @Override
  63. public void onTearDown() {
  64. if (window != null) {
  65. window.cleanUp();
  66. }
  67. }
  68. @Test
  69. public void testTicksAndCrosses() {
  70. setupWindow();
  71. assertTrue(Arrays.equals(new String[]{
  72. "first cert",
  73. "second cert",
  74. "invalid cert",
  75. "trusted cert",
  76. "invalid+trusted"
  77. }, window.list().contents()));
  78. assertTrue(Arrays.equals(new String[]{
  79. "nothing",
  80. "nothing",
  81. "cross",
  82. "tick",
  83. "cross"
  84. },window.list().cellReader(new BasicJListCellReader(new CertificateListCellReader()))
  85. .contents()));
  86. }
  87. @Test @Ignore
  88. public void testSelection() {
  89. setupWindow();
  90. window.list().requireSelection("first cert");
  91. for (String cert : window.list().contents()) {
  92. window.list().selectItem(cert);
  93. robot().waitForIdle();
  94. window.list().requireSelection(cert);
  95. assertEquals("Information for " + cert, ((TitledBorder) window
  96. .scrollPane(new ClassFinder<CertificateInfoPanel>(CertificateInfoPanel.class, null))
  97. .target.getBorder()).getTitle());
  98. }
  99. }
  100. protected void setupWindow() {
  101. final Dialog d = GuiActionRunner.execute(new GuiQuery<Dialog>() {
  102. @Override
  103. protected Dialog executeInEDT() throws Throwable {
  104. return new SSLCertificateDialog(null,
  105. new TestSSLCertificateDialogModel());
  106. }
  107. });
  108. robot().waitForIdle();
  109. window = new DialogFixture(robot(), d);
  110. window.show();
  111. }
  112. private static class CertificateListCellReader extends BasicCellRendererReader {
  113. @Override
  114. public String valueFrom(Component c) {
  115. final Icon target = ((JLabel) c).getIcon();
  116. for (String icon : new String[]{"tick", "cross", "nothing"}) {
  117. if (target == IconManager.getIconManager().getIcon(icon)) {
  118. return icon;
  119. }
  120. }
  121. return "?";
  122. }
  123. }
  124. }