aboutsummaryrefslogtreecommitdiff
path: root/gnu/testlet/VisualTestlet.java
blob: 69d41cd63b39fd5847aaa03a7213879c1dbf412e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* VisualTestlet.java -- Abstract superclass for visual tests
   Copyright (C) 2006 Roman Kennke (kennke@aicas.com)
This file is part of Mauve.

Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Mauve is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

*/

package gnu.testlet;

import java.awt.Component;
import java.awt.Frame;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * Provides an environment for visual tests. Visual tests must provide a
 * component, instructions and the expected results. The harness provides
 * all three to the tester and ask if the test passed or not.
 *
 * The test component is displayed inside a AWT Frame or a Swing JFrame
 * (depending on the type of the component). This means that the tested
 * Java environment needs to have some basic AWT or Swing functionality. This
 * should be covered by other tests (possibly by java.awt.Robot or so).
 */
public abstract class VisualTestlet
  implements Testlet
{

  /**
   * Starts the test.
   *
   * @param h the test harness
   */
  public void test(TestHarness h)
  {
    // Initialize and show test component.
    Component c = getTestComponent();
    Frame f;
    if (c instanceof JComponent)
      {
        JFrame jFrame = new JFrame();
        jFrame.setContentPane((JComponent) c);
        f = jFrame;
      }
    else
      {
        f = new Frame();
        f.add(c);
      }
    f.pack();
    f.setVisible(true);

    // Print instructions and expected results on console.
    System.out.println("====================================================");
    System.out.print("This is a test that needs human interaction. Please ");
    System.out.print("read the instructions carefully and follow them. ");
    System.out.print("Then check if your results match the expected results. ");
    System.out.print("Type p <ENTER> if the test showed the expected results,");
    System.out.println(" f <ENTER> otherwise.");
    System.out.println("====================================================");
    System.out.println("INSTRUCTIONS:");
    System.out.println(getInstructions());
    System.out.println("====================================================");
    System.out.println("EXPECTED RESULTS:");
    System.out.println(getExpectedResults());
    System.out.println("====================================================");

    // Ask the tester whether the test passes or fails.
    System.out.println("(P)ASS or (F)AIL ?");
    while (true)
      {
        int ch;
        try
          {
            ch = System.in.read();
            if (ch == 'P' || ch == 'p')
              {
                h.check(true);
                break;
              }
            else if (ch == 'f' || ch == 'F')
              {
                h.check(false);
                break;
              }
          }
        catch (IOException ex)
          {
            h.debug(ex);
            h.fail("Unexpected IO problem on console");
          }
      }
  }

  /**
   * Provides the instructions for the test.
   *
   * @return the instructions for the test
   */
  public abstract String getInstructions();

  /**
   * Describes the expected results.
   *
   * @return the expected results
   */
  public abstract String getExpectedResults();

  /**
   * Provides the test component.
   *
   * @return the test component
   */
  public abstract Component getTestComponent();
}