diff options
author | Dejan Gloszic | 2006-01-05 00:11:41 +0000 |
---|---|---|
committer | Dejan Gloszic | 2006-01-05 00:11:41 +0000 |
commit | f19e9f7a2623eacc1ce71d2b28ed69e394e45fc2 (patch) | |
tree | 4e42437d98fd1ce9d14374178714574ef3a5ffc2 /org.eclipse.ua.tests/base/org/eclipse/ua/tests/util | |
parent | bfda4e065254d5b116b524c247bbee0b76351d56 (diff) | |
download | eclipse.platform.ua-f19e9f7a2623eacc1ce71d2b28ed69e394e45fc2.tar.gz eclipse.platform.ua-f19e9f7a2623eacc1ce71d2b28ed69e394e45fc2.tar.xz eclipse.platform.ua-f19e9f7a2623eacc1ce71d2b28ed69e394e45fc2.zip |
*** empty log message ***
Diffstat (limited to 'org.eclipse.ua.tests/base/org/eclipse/ua/tests/util')
7 files changed, 507 insertions, 0 deletions
diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/DisplayUtil.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/DisplayUtil.java new file mode 100644 index 000000000..cd7738b90 --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/DisplayUtil.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import org.eclipse.swt.widgets.Display; + +/* + * Utility methods for working with Displays. + */ +public class DisplayUtil { + + /* + * Flushes and events in the UI thread queue. + */ + public static void flush() { + while(Display.getDefault().readAndDispatch()) { + } + } +} diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/FileUtil.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/FileUtil.java new file mode 100644 index 000000000..f0cf48862 --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/FileUtil.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +/* + * Utility methods for working with files. + */ +public class FileUtil { + + public static String getContents(String path) throws IOException { + BufferedReader in = new BufferedReader(new FileReader(path)); + StringBuffer buf = new StringBuffer(); + String line = null; + while ((line = in.readLine()) != null) { + buf.append(line); + buf.append("\n"); + } + in.close(); + return buf.toString(); + } +} diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/Keyboard.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/Keyboard.java new file mode 100644 index 000000000..501599e77 --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/Keyboard.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; + +/* + * Allows pressing of keys programmatically. + */ +public class Keyboard { + + /* + * Press and hold a key down until keyUp called. + */ + public static void keyDown(char c, int modifiers) { + DisplayUtil.flush(); + Event event = new Event(); + event.type = SWT.KeyDown; + event.character = c; + event.stateMask = modifiers; + Display.getDefault().post(event); + DisplayUtil.flush(); + } + + /* + * Press and hold a key down until keyUp called. + */ + public static void keyDown(int keyCode, int modifiers) { + DisplayUtil.flush(); + Event event = new Event(); + event.type = SWT.KeyDown; + event.keyCode = keyCode; + event.stateMask = modifiers; + Display.getDefault().post(event); + DisplayUtil.flush(); + } + + /* + * Release a key that was previously called with keyDown. + */ + public static void keyUp(int keyCode, int modifiers) { + DisplayUtil.flush(); + Event event = new Event(); + event.type = SWT.KeyUp; + event.keyCode = keyCode; + event.stateMask = modifiers; + Display.getDefault().post(event); + DisplayUtil.flush(); + } + + /* + * Release a key that was previously called with keyDown. + */ + public static void keyUp(char c, int modifiers) { + DisplayUtil.flush(); + Event event = new Event(); + event.type = SWT.KeyUp; + event.character = c; + event.stateMask = modifiers; + Display.getDefault().post(event); + DisplayUtil.flush(); + } + + /* + * Press and release a key, programmatically. + */ + public static void press(int keyCode) { + press(keyCode, SWT.NONE); + } + + /* + * Press and release a key, programmatically. + */ + public static void press(char c) { + press(c, SWT.NONE); + } + + /* + * Press and release a key with the given modifiers (e.g. Alt, Ctrl). + */ + public static void press(int keyCode, int modifiers) { + /* + * First press each modifier. + */ + int[] array = separateModifiers(modifiers); + for (int i=0;i<array.length;++i) { + keyDown(array[i], SWT.NONE); + } + + /* + * Press and release the key. + */ + keyDown(keyCode, modifiers); + keyUp(keyCode, modifiers); + + /* + * Release each modifier. + */ + for (int i=0;i<array.length;++i) { + keyUp(array[i], SWT.NONE); + } + } + + /* + * Press and release a key with the given modifiers (e.g. Alt, Ctrl). + */ + public static void press(char c, int modifiers) { + /* + * First press each modifier. + */ + int[] array = separateModifiers(modifiers); + for (int i=0;i<array.length;++i) { + keyDown(array[i], SWT.NONE); + } + + /* + * Press and release the key. + */ + keyDown(c, modifiers); + keyUp(c, modifiers); + + /* + * Release each modifier. + */ + for (int i=0;i<array.length;++i) { + keyUp(array[i], SWT.NONE); + } + } + + /* + * Given the modifier flags, returns each modifier separately. For example, if you pass in + * (SWT.ALT | SWT.CTRL), it returns { SWT.ALT, SWT.CTRL }, in no particular order. + */ + private static int[] separateModifiers(int modifiers) { + List list = new ArrayList(); + for (int i=0;i<32;++i) { + int currentModifier = (1 << i); + if ((modifiers & currentModifier) != 0) { + list.add(new Integer(currentModifier)); + } + } + int[] array = new int[list.size()]; + for (int i=0;i<array.length;++i) { + array[i] = ((Integer)list.get(i)).intValue(); + } + return array; + } +} diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/ResourceFinder.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/ResourceFinder.java new file mode 100644 index 000000000..51e7cc082 --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/ResourceFinder.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Plugin; + +/* + * Utility methods for finding resources. + */ +public class ResourceFinder { + + /* + * Finds the specified file in the given plugin and returns a URL to it. + */ + public static URL findFile(Plugin plugin, String path) { + String fullLocation = plugin.getBundle().getLocation(); + String location = fullLocation.substring(fullLocation.indexOf('@') + 1); + IPath fullPath = new Path(location).append(path); + File file = fullPath.toFile(); + + /* + * If it's a relative path, append it to the install location. + */ + if (!file.exists()) { + fullPath = new Path(Platform.getInstallLocation().getURL().toString().substring("file:".length()) + fullPath); + file = fullPath.toFile(); + } + + try { + return file.toURL(); + } + catch (MalformedURLException e) { + e.printStackTrace(); + return null; + } + } + + /* + * Finds and returns URLs to all files in the plugin directory under the given + * folder with the given suffix. Can also recursively traverse all subfolders. + */ + public static URL[] findFiles(Plugin plugin, String folder, String suffix, boolean recursive) { + String fullLocation = plugin.getBundle().getLocation(); + String location = fullLocation.substring(fullLocation.indexOf('@') + 1); + IPath path = new Path(location).append(folder); + File file = path.toFile(); + + /* + * If it's a relative path, append it to the install location. + */ + if (!file.exists()) { + path = new Path(Platform.getInstallLocation().getURL().toString().substring("file:".length()) + path); + file = path.toFile(); + } + + File[] files = path.toFile().listFiles(); + return findFiles(files, suffix, recursive); + } + + /* + * Finds and returns URLs to all files in the given list that have the given suffix, and + * recursively traverses subdirectories if requested. + */ + private static URL[] findFiles(File[] files, String suffix, boolean recursive) { + List list = new ArrayList(); + if (files != null) { + for (int i=0;i<files.length;++i) { + if (files[i].isDirectory()) { + if (recursive) { + list.addAll(Arrays.asList(findFiles(files[i].listFiles(), suffix, recursive))); + } + } + else { + try { + URL url = files[i].toURL(); + if (url.toString().endsWith(suffix)) { + list.add(url); + } + } + catch (MalformedURLException e) { + } + } + } + } + + URL[] array = new URL[list.size()]; + list.toArray(array); + return array; + } +} diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/TreeUtil.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/TreeUtil.java new file mode 100644 index 000000000..054d4dcf0 --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/TreeUtil.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; + +/* + * Utility methods for working with Trees. + */ +public class TreeUtil { + + /* + * Fully expands the given tree. + */ + public static void expandTree(Tree tree) { + tree.setFocus(); + TreeItem[] items = tree.getItems(); + for (int i=0;i<items.length;++i) { + tree.setSelection(new TreeItem[] { items[i] }); + Keyboard.press(SWT.ARROW_RIGHT); + } + } +} diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/WidgetChecker.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/WidgetChecker.java new file mode 100644 index 000000000..557702349 --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/WidgetChecker.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import junit.framework.Assert; + +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Item; + +/* + * A utility for checking widget properties. + */ +public class WidgetChecker { + + /* + * Checks if the control is non-null, not disposed, and is visible. Msg is the + * description of the widget (e.g. "The Tree in the cheat sheets dialog"). + */ + public static void checkControl(String msg, Control c) { + Assert.assertNotNull("The following control was unexpectedly null: " + msg, c); + Assert.assertFalse("The following control was unexpectedly disposed: " + msg, c.isDisposed()); + Assert.assertTrue("The following control was unexpectedly not visible: " + msg, c.isVisible()); + } + + /* + * Checks if the item is non-null and not disposed. Msg is the description of the item + * (e.g. "Tree item in the cheat sheets dialog"). + */ + public static void checkItem(String msg, Item c) { + Assert.assertNotNull("The following item was unexpectedly null: " + msg, c); + Assert.assertFalse("The following item was unexpectedly disposed: " + msg, c.isDisposed()); + } +} diff --git a/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/WidgetFinder.java b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/WidgetFinder.java new file mode 100644 index 000000000..dec0cb02d --- /dev/null +++ b/org.eclipse.ua.tests/base/org/eclipse/ua/tests/util/WidgetFinder.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2002, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ua.tests.util; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Tree; + +/* + * A utility for finding widgets inside dialogs. + */ +public class WidgetFinder { + + /* + * Finds all controls of the given type. + */ + public static Control[] findControls(Control c, Class type) { + if (c != null) { + if (c instanceof Composite) { + List allChildren = new ArrayList(); + Control[] children = ((Composite)c).getChildren(); + for (int i=0;i<children.length;++i) { + Control[] controls = findControls(children[i], type); + if (controls != null) { + allChildren.addAll(Arrays.asList(controls)); + } + } + + boolean isAssignable = type.isAssignableFrom(c.getClass()); + Control[] array = new Control[allChildren.size() + (isAssignable ? 1 : 0)]; + allChildren.toArray(array); + if (isAssignable) { + array[array.length - 1] = c; + } + return array; + } + else if (type.isAssignableFrom(c.getClass())) { + return new Control[] { c }; + } + } + return new Control[0]; + } + + /* + * Finds all the Trees under the given Control/Composite. + */ + public static Tree[] findTrees(Control c) { + Control[] controls = findControls(c, Tree.class); + Tree[] trees = new Tree[controls.length]; + System.arraycopy(controls, 0, trees, 0, controls.length); + return trees; + } + + /* + * Finds all the Labels under the given Control/Composite. + */ + public static Label[] findLabels(Control c) { + Control[] controls = findControls(c, Label.class); + Label[] labels = new Label[controls.length]; + System.arraycopy(controls, 0, labels, 0, controls.length); + return labels; + } + + /* + * Finds any widget that has a getText() method that matches the given text. + */ + public static boolean containsText(String text, Control c) { + // don't take \r into account when comparing + text = text.replaceAll("\r", ""); + + Control[] controls = findControls(c, Control.class); + for (int i=0;i<controls.length;++i) { + try { + Method m = controls[i].getClass().getMethod("getText", null); + Object obj = m.invoke(controls[i], null); + if (obj != null && obj instanceof String) { + String widgetText = (String)obj; + widgetText = widgetText.replaceAll("\r", ""); + if (text.equals(widgetText)) { + return true; + } + } + } + catch (Exception e) { + // the method didn't exist; skip this one + } + } + return false; + } +} |