1/*
2 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @summary Check if TrayIcon added by a JVM is not visible
27 *          in another JVM
28 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
29 * @library ../../regtesthelpers/process/
30 * @build ProcessResults ProcessCommunicator
31 * @run main InterJVM
32 */
33
34import test.java.awt.regtesthelpers.process.ProcessCommunicator;
35import test.java.awt.regtesthelpers.process.ProcessResults;
36
37import java.awt.*;
38import java.awt.image.BufferedImage;
39
40public class InterJVM {
41
42    static String NEW_JVM = "-doTest";
43
44    public static void main(String[] args) throws Exception {
45        if (! SystemTray.isSupported()) {
46            System.out.println("SystemTray not supported on the platform under test. " +
47                    "Marking the test passed");
48        } else {
49            if (args == null || args.length == 0) {
50                new InterJVM().addTrayIcon();
51            } else {
52                if (args.length == 1 && NEW_JVM.equals(args[0]))
53                    new InterJVM().doTest();
54            }
55        }
56    }
57
58    void doTest() throws Exception {
59        SystemTray tray = SystemTray.getSystemTray();
60        try {
61            TrayIcon[] icons = tray.getTrayIcons();
62            System.out.println(icons.length);
63            if (icons == null || icons.length != 0)
64                throw new RuntimeException("FAIL: getTrayIcons() returned incorrect " +
65                        "value when two icons are added by a " +
66                        "separate JVM: " + icons.length);
67        } catch (Exception e) {
68            throw new RuntimeException(e);
69        }
70    }
71
72    void addTrayIcon() throws Exception {
73
74        SystemTray tray = SystemTray.getSystemTray();
75        TrayIcon icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB));
76        tray.add(icon);
77
78        new Robot().delay(2000);
79
80        ProcessResults processResults =
81                ProcessCommunicator.executeChildProcess(this.getClass(), new String[]{NEW_JVM});
82
83        if (processResults.getExitValue() != 0)
84            throw new RuntimeException("\n"+processResults.getStdErr());
85    }
86}
87