1 /*
2  * Copyright (c) 2004, 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 import java.awt.*;
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27 import java.io.InputStream;
28 
29 /*
30  * @test
31  * @key headful
32  * @bug 4758438
33  * @summary Testcase to check the implementation of RFE 4758438
34  *          The RFE suggests that the GNOME desktop properties
35  *          should be made accessible through the
36  *          Toolkit.getDesktopProperty() API.
37  * @author Girish R (girish.ramachandran@sun.com)
38  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
39  * @run shell rfe4758438.sh
40  */
41 
42 public class rfe4758438 implements PropertyChangeListener {
43 
44     enum PROPS {
45         drag_threshold(
46                 "org.gnome.settings-daemon.peripherals.mouse drag-threshold",
47                 "/desktop/gnome/peripherals/mouse/drag_threshold",
48                 "gnome.Net/DndDragThreshold",
49                 "int",
50                 new String[]{"5", "6"}),
51         double_click(
52                 "org.gnome.settings-daemon.peripherals.mouse double-click",
53                 "/desktop/gnome/peripherals/mouse/double_click",
54                 "gnome.Net/DoubleClickTime",
55                 "int",
56                 new String[]{"200","300"}),
57         cursor_blink(
58                 "org.gnome.desktop.interface cursor-blink",
59                 "/desktop/gnome/interface/cursor_blink",
60                 "gnome.Net/CursorBlink",
61                 "bool",
62                 new String[]{"true","false"}),
63         cursor_blink_time(
64                 "org.gnome.desktop.interface cursor-blink-time",
65                 "/desktop/gnome/interface/cursor_blink_time",
66                 "gnome.Net/CursorBlinkTime",
67                 "int",
68                 new String[]{"1000","1500"}),
69         gtk_theme(
70                 "org.gnome.desktop.interface gtk-theme",
71                 "/desktop/gnome/interface/gtk_theme",
72                 "gnome.Net/ThemeName",
73                 "string",
74                 new String[]{"Crux","Simple"});
75 
76         public final String gsettings;
77         public final String gconftool;
78         public final String java;
79         public final String type;
80         public final String[] values;
81 
PROPS(String gsettings, String gconftool, String java, String type, String[] values)82         PROPS(String gsettings, String gconftool, String java, String type, String[] values){
83             this.gsettings = gsettings;
84             this.gconftool = gconftool;
85             this.java = java;
86             this.type = type;
87             this.values = values;
88         }
89     }
90 
91     static boolean useGsettings;
92     static String tool;
93     Toolkit toolkit = Toolkit.getDefaultToolkit();
94     String changedProperty;
95     Object changedValue;
96     Object lock = new Object();
97 
98     /**
99      * Implementation of PropertyChangeListener method
100      */
propertyChange(PropertyChangeEvent event)101     public void propertyChange(PropertyChangeEvent event) {
102         changedProperty = event.getPropertyName();
103         changedValue = toolkit.getDesktopProperty(changedProperty);
104         System.out.println("Property "+changedProperty+" changed. Changed value: "+changedValue);
105         synchronized(lock) {
106             try {
107                 lock.notifyAll();
108             } catch (Exception e) {
109             }
110         }
111     }
112 
main(String[] args)113     public static void main(String[] args) throws Exception {
114         useGsettings = System.getProperty("useGsettings").equals("true");
115         tool = System.getProperty("tool");
116 
117         String osName = System.getProperty("os.name");
118         if (!"Linux".equals(osName) && !"SunOS".equals(osName))
119             System.out.println("This test need not be run on this platform");
120         else
121             new rfe4758438().doTest();
122     }
123 
doTest()124     void doTest() throws Exception {
125         for (PROPS p : PROPS.values())
126             toolkit.addPropertyChangeListener(p.java, this);
127 
128         for (PROPS p : PROPS.values()) {
129             Thread.sleep(1000);
130             doTest(p);
131         }
132         System.out.println("Test passed");
133     }
134 
135     /**
136      * Do the test for each property. Find the current value
137      * of the property, set the property to a value not equal
138      * to the current value, check if the propertyChange event
139      * is triggered. Reset the property to the actual value.
140      */
doTest(PROPS property)141     void doTest(PROPS property) throws Exception {
142         //Choose the test value which is not same as the current value
143         Object obj = toolkit.getDesktopProperty(property.java);
144         if (obj == null)
145             throw new RuntimeException("No such property available: " + property.java);
146 
147         //For boolean type values, getDesktopProperty method returns Integer objects
148         if (property.type.equals("bool")) {
149             if (obj.equals(new Integer(1))) {
150                 obj = new String("true");
151             } else {
152                 obj = new String("false");
153             }
154         }
155         Object value = property.values[0];
156         if (obj.toString().equals(value)) {
157             value = property.values[1];
158         }
159 
160         //Create the command to execute
161         StringBuffer sb = new StringBuffer(tool);
162         if (useGsettings) {
163             sb.append(" set ");
164             sb.append(property.gsettings);
165             sb.append(" ");
166         } else {
167             sb.append(" --set --type=");
168             sb.append(property.type);
169             sb.append(" ");
170             sb.append(property.gconftool);
171             sb.append(" ");
172         }
173         String tempCommand = sb.toString();
174         sb.append(value.toString());
175 
176         //Initialize the variables and execute the command
177         changedProperty = "";
178         changedValue = null;
179         if (executeCommand(sb.toString()) != 0)
180             throw new RuntimeException("Could not execute the command");
181 
182         synchronized(lock) {
183             try {
184                 lock.wait(5000);
185             } catch (Exception e) {
186             }
187         }
188         if (property.type.equals("bool")) {
189             if (changedValue.equals(new Integer(1))) {
190                 changedValue = new String("true");
191             } else {
192                 changedValue = new String("false");
193             }
194         }
195 
196         //Check if the event got triggered
197         if (!changedProperty.equals(property.java)) {
198             //Reset the property
199             executeCommand(tempCommand + obj.toString());
200             throw new RuntimeException("PropertyChangedEvent did not occur for " + property.java);
201         } else if (!changedValue.toString().equals(value.toString())) {
202             //Reset the property
203             executeCommand(tempCommand + obj.toString());
204             throw new RuntimeException("New value of the property is different from " +
205                                        "the value supplied");
206         }
207 
208         //Reset the property
209         executeCommand(tempCommand + obj.toString());
210     }
211 
212     /**
213      * Uses the gconftool-2 command to change the value of the property.
214      * Gets the output of the command and prints the output
215      */
executeCommand(String command)216     int executeCommand(String command) throws Exception {
217         System.out.println("Executing " + command);
218         Process process = Runtime.getRuntime().exec(command);
219 
220         InputStream is = process.getInputStream();
221         InputStream es = process.getErrorStream();
222         StringBuilder stdout = new StringBuilder();
223         StringBuilder stderr = new StringBuilder();
224 
225         process.waitFor();
226 
227         while (is.available() > 0)
228             stdout.append((char) is.read());
229 
230         while (es.available() > 0)
231             stderr.append((char) es.read());
232 
233         if (stdout.length() > 0)
234             System.out.println(stdout.toString());
235         if (stderr.length() > 0)
236             System.err.println(stderr.toString());
237         return process.exitValue();
238     }
239 }
240