| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| WithId |
|
| 1.0;1 | ||||
| WithId$1 |
|
| 1.0;1 |
| 1 | 118 | /******************************************************************************* |
| 2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
| 3 | * All rights reserved. This program and the accompanying materials | |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 | |
| 5 | * which accompanies this distribution, and is available at | |
| 6 | * http://www.eclipse.org/legal/epl-v10.html | |
| 7 | * | |
| 8 | * Contributors: | |
| 9 | * Ketan Padegaonkar - initial API and implementation | |
| 10 | * Ketan Padegaonkar - http://swtbot.org/bugzilla/show_bug.cgi?id=126 | |
| 11 | *******************************************************************************/ | |
| 12 | package org.eclipse.swtbot.swt.finder.matchers; | |
| 13 | ||
| 14 | import org.eclipse.swt.widgets.Widget; | |
| 15 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
| 16 | import org.eclipse.swtbot.swt.finder.results.Result; | |
| 17 | import org.hamcrest.Description; | |
| 18 | import org.hamcrest.Factory; | |
| 19 | import org.hamcrest.Matcher; | |
| 20 | ||
| 21 | /** | |
| 22 | * Tells if a particular widget has value for the given key. | |
| 23 | * | |
| 24 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 25 | * @version $Id$ | |
| 26 | * @since 2.0 | |
| 27 | */ | |
| 28 | public class WithId<T extends Widget> extends AbstractMatcher<T> { | |
| 29 | ||
| 30 | /** | |
| 31 | * The key to use while matching widgets. | |
| 32 | */ | |
| 33 | 59 | private final String key; |
| 34 | /** | |
| 35 | * The value to use while matching widgets. | |
| 36 | */ | |
| 37 | private final String value; | |
| 38 | ||
| 39 | /** | |
| 40 | * Matches a widget that has the specified Key/Value pair set as data into it. | |
| 41 | * | |
| 42 | * @see Widget#setData(String, Object) | |
| 43 | * @param key the key | |
| 44 | * @param value the value | |
| 45 | */ | |
| 46 | 2 | WithId(String key, String value) { |
| 47 | 2 | this.key = key; |
| 48 | 2 | this.value = value; |
| 49 | 2 | } |
| 50 | ||
| 51 | protected boolean doMatch(final Object obj) { | |
| 52 | 59 | String data = UIThreadRunnable.syncExec(new Result<String>() { |
| 53 | public String run() { | |
| 54 | 59 | return (String) ((Widget) obj).getData(key); |
| 55 | } | |
| 56 | }); | |
| 57 | 59 | return value.equals(data); |
| 58 | } | |
| 59 | ||
| 60 | public void describeTo(Description description) { | |
| 61 | 0 | description.appendText("with key/value (").appendText(key).appendText("/").appendText(value).appendText(")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Matches a widget that has the specified Key/Value pair set as data into it. | |
| 66 | * | |
| 67 | * @see org.eclipse.swt.widgets.Widget#setData(String, Object) | |
| 68 | * @param key the key | |
| 69 | * @param value the value | |
| 70 | * @return a matcher. | |
| 71 | */ | |
| 72 | @Factory | |
| 73 | public static <T extends Widget> Matcher<T> withId(String key, String value) { | |
| 74 | 2 | return new WithId<T>(key, value); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Matches a widget that has the specified value set for the key | |
| 79 | * {@link org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences#DEFAULT_KEY}. | |
| 80 | * | |
| 81 | * @see org.eclipse.swt.widgets.Widget#setData(String, Object) | |
| 82 | * @param value the value | |
| 83 | * @return a matcher. | |
| 84 | * @since 2.0 | |
| 85 | */ | |
| 86 | @Factory | |
| 87 | public static <T extends Widget> Matcher<T> withId(String value) { | |
| 88 | 0 | return new WithId<T>(org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences.DEFAULT_KEY, value); |
| 89 | } | |
| 90 | ||
| 91 | } |