File: DialogOwnerTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (289 lines) | stat: -rw-r--r-- 9,732 bytes parent folder | download | duplicates (7)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
   @bug 8203796
   @run main/manual DialogOwnerTest
   @summary Test DialogOwner API
*/

import java.util.ArrayList;
import java.util.List;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.DialogOwner;
import javax.print.attribute.standard.DialogTypeSelection;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class DialogOwnerTest extends JPanel {

    static final int NONE      =     0x0;
    static final int PRINT     =     0x1;
    static final int PAGE      =     0x2;
    static final int SWING2D   =     0x4;
    static final int NATIVE2D  =     0x8;
    static final int SERVICEUI =    0x10;

    static final int ONTOP      =   0x20;
    static final int OWNED      =   0x40;

    static PrintService[] services =
        PrintServiceLookup.lookupPrintServices(null, null);

    public static void main(String[] args) {
        if (services.length == 0) {
            System.out.println("No printers, exiting");
            return;
        } else {
           service = PrinterJob.getPrinterJob().getPrintService();
        }
        SwingUtilities.invokeLater(() -> {
            createUI();
        });
        while (!testFinished) {
           try {
                Thread.sleep(1000);
           } catch (InterruptedException e){
           }
        }
        if (!testPassed) {
            throw new RuntimeException("TEST FAILED.");
        }
    }


    static final String otherText =
       "This window is used to test on top behaviour\n" +
       "For tests that are 'Owned' or 'On Top' the dialog\n" +
       "must always stay above this window. Verify this\n " +
       "by moving the dialog so that it partially obscures\n" +
       "this window and then trying to raise this window.";

    static final String instructions =
       " Instructions\n" +
       "This tests that a print dialog stays on top of either another\n" +
       "window, or on top of all windows.\n" +
       "For Owned tests the window titled 'Owner Window' should always \n" +
       "stay behind the print dialog.\n" +
       "For On Top tests all windows should stay behind the owner window.\n" +
       "This test tracks if you have checked all the scenarios and will\n" +
       "not allow the test to pass unless you have visited them all.\n";

    static PrintService service;

    public DialogOwnerTest() {
       super();
       //setLayout(new GridLayout(24, 1));
    }

    static boolean isNative(int flags) {
        return (flags & NATIVE2D) != 0;
    }

    static boolean isCommon(int flags) {
        return (flags & SWING2D) != 0;
    }

    static boolean is2D(int flags) {
        return (flags & SWING2D|NATIVE2D) != 0;
    }

    static boolean isPage(int flags) {
        return (flags & PAGE ) != 0;
    }

    static JFrame frame;
    static JFrame other;
    static JButton pass;
    static ArrayList<JPanel> panelList = new ArrayList<JPanel>();
    static volatile boolean testPassed, testFinished;

    int testCount = 0;
    List<String> testList = new ArrayList<String>();

    static void createUI() {
        other = new JFrame("Owner Window");
        JTextArea otherTextArea = new JTextArea(otherText, 10, 40);
        other.add(otherTextArea);
        other.pack();
        other.setVisible(true);
        other.setLocation(800, 100);

        frame = new JFrame("Test Dialog Owner");
        frame.pack();
        JTextArea instructionsPanel = new JTextArea(instructions, 10, 50);
        instructionsPanel.setEditable(false);
        frame.add("North", instructionsPanel);
        DialogOwnerTest test = new DialogOwnerTest();

        test.addTest("Owned Swing Print", OWNED, frame, PRINT|SWING2D);
        test.addTest("On Top Swing Print", ONTOP, null, PRINT|SWING2D);

        test.addTest("Owned Swing Page", OWNED, frame, PAGE|SWING2D);
        test.addTest("On Top Swing Page", ONTOP, null, PAGE|SWING2D);

        test.addTest("Owned javax.print", OWNED, frame, PRINT|SERVICEUI);
        test.addTest("On Top javax.print", OWNED, null, PRINT|SERVICEUI);

        test.addTest("Owned Native Print", OWNED, frame, PRINT|NATIVE2D);
        test.addTest("On Top Native Print", OWNED, null, PRINT|NATIVE2D);

        test.addTest("Owned Native Page", OWNED, frame, PAGE|NATIVE2D);
        test.addTest("On Top Native Page", OWNED, null, PAGE|NATIVE2D);

        test.setLayout(new GridLayout(panelList.size()+2, 1));

        pass = new JButton("Pass");
        pass.setEnabled(false);
        pass.addActionListener((ActionEvent e) -> {
            if (test.testList.size() > 0) {
                return;
            }
            frame.dispose();
            other.dispose();
            System.out.println("User says test passed.");
            testPassed = true;
            testFinished = true;
        });

        JButton fail = new JButton("Fail");
        fail.addActionListener((ActionEvent e) -> {
            frame.dispose();
            other.dispose();
            System.out.println("User says test failed.");
            testPassed = false;
            testFinished = true;
        });

        JPanel p = new JPanel();
        p.add(pass);
        p.add(fail);
        test.add(p);


        for (JPanel panel : panelList) {
            test.add(panel);
        }

        frame.add("Center", test);
        frame.pack();
        frame.setLocation(0,0);
        frame.setVisible(true);
     }

   boolean isSupported(PrintRequestAttributeSet aset,
                     int ownerFlags, Window owner, int dlgFlags) {

       boolean supported = true;
       DialogOwner ownerAttr = null;
       if (ownerFlags != NONE) {
           if (ownerFlags == ONTOP) {
               ownerAttr = new DialogOwner();
           } else if (ownerFlags == OWNED) {
               ownerAttr = new DialogOwner(owner);
           }
           aset.add(ownerAttr);
        }
        if (is2D(dlgFlags)) {
           DialogTypeSelection dst = null;
           if (isNative(dlgFlags)) {
               dst = DialogTypeSelection.NATIVE;
           } else if (isCommon(dlgFlags)) {
               dst = DialogTypeSelection.COMMON;
           }
           if (dst != null &&
               !service.isAttributeValueSupported(dst, null, aset)) {
               //System.out.println("This DialogType not supported");
               supported = false;
           }
           if (dst != null) {
               aset.add(dst);
           }
           if (ownerAttr != null &&
               !service.isAttributeValueSupported(ownerAttr, null, aset)) {
               //System.out.println("This DialogOwner not supported");
               supported = false;
           }
        }
        return supported;
   }

   void addTest(String title, int ownerFlags, Window owner, int dlgFlags) {

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        if (!isSupported(aset, ownerFlags, owner, dlgFlags)) {
            return;
        }

        // if we are here then this is supportable and worth testing
        // and the attribute set is configured.

        String label = title + " Dialog";
        JButton button = new JButton(label);
        JCheckBox tested = new JCheckBox("Tested");
        tested.setEnabled(false);
        JPanel panel = new JPanel();
        panel.add(tested);
        panel.add(button);
        panelList.add(panel);
        //add(panel);
        testList.add(title);
        if (++testCount != testList.size()) {
            throw new RuntimeException("Test titles must be unique");
        }

        button.addActionListener((ActionEvent e) -> {
            tested.setSelected(true);
            testList.remove(title);
            if (testList.isEmpty()) {
              pass.setEnabled(true);
            }

            if (is2D(dlgFlags)) {
               PrinterJob job = PrinterJob.getPrinterJob();
               if (isPage(dlgFlags)) {
                   job.pageDialog(aset);
               } else {
                   job.printDialog(aset);
               }
            } else {
               GraphicsConfiguration gc = null;
               int x = 0, y = 0;
               ServiceUI.printDialog(gc, x, y, services, services[0], null,aset);
            }
        });
    }
}