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