001 /* 002 * Created on Dec 19, 2009 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2009-2010 the original author or authors. 015 */ 016 package org.fest.swing.fixture; 017 018 import java.util.regex.Pattern; 019 020 import javax.swing.JProgressBar; 021 022 import org.fest.swing.core.*; 023 import org.fest.swing.driver.JProgressBarDriver; 024 import org.fest.swing.exception.ComponentLookupException; 025 import org.fest.swing.exception.WaitTimedOutError; 026 import org.fest.swing.timing.Timeout; 027 028 /** 029 * Understands functional testing of <code>{@link JProgressBar}</code>s: 030 * <ul> 031 * <li>state verification</li> 032 * <li>property value query</li> 033 * </ul> 034 * 035 * @author Alex Ruiz 036 * 037 * @since 1.2 038 */ 039 public class JProgressBarFixture extends ComponentFixture<JProgressBar> implements StateVerificationFixture, 040 JComponentFixture, TextDisplayFixture { 041 042 private JProgressBarDriver driver; 043 044 /** 045 * Creates a new <code>{@link JProgressBarFixture}</code>. 046 * @param robot performs simulation of user events on the given <code>JProgressBar</code>. 047 * @param target the <code>JProgressBar</code> to be managed by this fixture. 048 * @throws NullPointerException if <code>robot</code> is <code>null</code>. 049 * @throws NullPointerException if <code>target</code> is <code>null</code>. 050 */ 051 public JProgressBarFixture(Robot robot, JProgressBar target) { 052 super(robot, target); 053 createDriver(); 054 } 055 056 /** 057 * Creates a new <code>{@link JProgressBarFixture}</code>. 058 * @param robot performs simulation of user events on a <code>JProgressBar</code>. 059 * @param labelName the name of the <code>JProgressBar</code> to find using the given <code>Robot</code>. 060 * @throws NullPointerException if <code>robot</code> is <code>null</code>. 061 * @throws ComponentLookupException if a matching <code>JProgressBar</code> could not be found. 062 * @throws ComponentLookupException if more than one matching <code>JProgressBar</code> is found. 063 */ 064 public JProgressBarFixture(Robot robot, String labelName) { 065 super(robot, labelName, JProgressBar.class); 066 createDriver(); 067 } 068 069 private void createDriver() { 070 driver(new JProgressBarDriver(robot)); 071 } 072 073 /** 074 * Sets the <code>{@link JProgressBarDriver}</code> to be used by this fixture. 075 * @param newDriver the new <code>JProgressBarDriver</code>. 076 * @throws NullPointerException if the given driver is <code>null</code>. 077 */ 078 protected final void driver(JProgressBarDriver newDriver) { 079 validateNotNull(newDriver); 080 driver = newDriver; 081 } 082 083 /** 084 * Asserts that the value of this fixture's <code>{@link JProgressBar}</code> is equal to the given one. 085 * @param value the expected value. 086 * @return this fixture. 087 * @throws AssertionError if the value of this fixture's <code>JProgressBar</code> is not equal to the given one. 088 */ 089 public JProgressBarFixture requireValue(int value) { 090 driver.requireValue(target, value); 091 return this; 092 } 093 094 /** 095 * Asserts that this fixture's <code>{@link JProgressBar}</code> is in determinate mode. 096 * @return this fixture. 097 * @throws AssertionError if this fixture's <code>JProgressBar</code> is not in determinate mode. 098 */ 099 public JProgressBarFixture requireDeterminate() { 100 driver.requireDeterminate(target); 101 return this; 102 } 103 104 /** 105 * Asserts that this fixture's <code>{@link JProgressBar}</code> is in indeterminate mode. 106 * @return this fixture. 107 * @throws AssertionError if this fixture's <code>JProgressBar</code> is not in indeterminate mode. 108 */ 109 public JProgressBarFixture requireIndeterminate() { 110 driver.requireIndeterminate(target); 111 return this; 112 } 113 114 /** 115 * Returns the text of this fixture's <code>{@link JProgressBar}</code>. 116 * @return the text of this fixture's <code>JProgressBar</code>. 117 */ 118 public String text() { 119 return driver.textOf(target); 120 } 121 122 /** 123 * Asserts that the text of this fixture's <code>{@link JProgressBar}</code> is equal to the specified 124 * <code>String</code>. 125 * @param expected the text to match. 126 * @return this fixture. 127 * @throws AssertionError if the text of this fixture's <code>JProgressBar</code> is not equal to the given one. 128 */ 129 public JProgressBarFixture requireText(String expected) { 130 driver.requireText(target, expected); 131 return this; 132 } 133 134 /** 135 * Asserts that the text of this fixture's <code>{@link JProgressBar}</code> matches the given regular expression 136 * pattern. 137 * @param pattern the regular expression pattern to match. 138 * @return this fixture. 139 * @throws AssertionError if the text of this fixture's <code>JProgressBar</code> does not match the given regular 140 * expression pattern. 141 * @throws NullPointerException if the given regular expression pattern is <code>null</code>. 142 */ 143 public JProgressBarFixture requireText(Pattern pattern) { 144 driver.requireText(target, pattern); 145 return this; 146 } 147 148 /** 149 * Asserts that the toolTip in this fixture's <code>{@link JProgressBar}</code> matches the given value. 150 * @param expected the given value. It can be a regular expression. 151 * @return this fixture. 152 * @throws AssertionError if the toolTip in this fixture's <code>JProgressBar</code> does not match the given value. 153 */ 154 public JProgressBarFixture requireToolTip(String expected) { 155 driver.requireToolTip(target, expected); 156 return this; 157 } 158 159 /** 160 * Asserts that the toolTip in this fixture's <code>{@link JProgressBar}</code> matches the given regular expression 161 * pattern. 162 * @param pattern the regular expression pattern to match. 163 * @return this fixture. 164 * @throws NullPointerException if the given regular expression pattern is <code>null</code>. 165 * @throws AssertionError if the toolTip in this fixture's <code>JProgressBar</code> does not match the given regular 166 * expression pattern. 167 */ 168 public JProgressBarFixture requireToolTip(Pattern pattern) { 169 driver.requireToolTip(target, pattern); 170 return this; 171 } 172 173 /** 174 * Asserts that this fixture's <code>{@link JProgressBar}</code> is enabled. 175 * @return this fixture. 176 * @throws AssertionError if this fixture's <code>JProgressBar</code> is disabled. 177 */ 178 public JProgressBarFixture requireEnabled() { 179 driver.requireEnabled(target); 180 return this; 181 } 182 183 /** 184 * Asserts that this fixture's <code>{@link JProgressBar}</code> is enabled. 185 * @param timeout the time this fixture will wait for the component to be enabled. 186 * @return this fixture. 187 * @throws org.fest.swing.exception.WaitTimedOutError if this fixture's <code>JProgressBar</code> is never enabled. 188 */ 189 public JProgressBarFixture requireEnabled(Timeout timeout) { 190 driver.requireEnabled(target, timeout); 191 return this; 192 } 193 194 /** 195 * Asserts that this fixture's <code>{@link JProgressBar}</code> is disabled. 196 * @return this fixture. 197 * @throws AssertionError if this fixture's <code>JProgressBar</code> is enabled. 198 */ 199 public JProgressBarFixture requireDisabled() { 200 driver.requireDisabled(target); 201 return this; 202 } 203 204 /** 205 * Asserts that this fixture's <code>{@link JProgressBar}</code> is visible. 206 * @return this fixture. 207 * @throws AssertionError if this fixture's <code>JProgressBar</code> is not visible. 208 */ 209 public JProgressBarFixture requireVisible() { 210 driver.requireVisible(target); 211 return this; 212 } 213 214 /** 215 * Asserts that this fixture's <code>{@link JProgressBar}</code> is not visible. 216 * @return this fixture. 217 * @throws AssertionError if this fixture's <code>JProgressBar</code> is visible. 218 */ 219 public JProgressBarFixture requireNotVisible() { 220 driver.requireNotVisible(target); 221 return this; 222 } 223 224 /** 225 * Returns the client property stored in this fixture's <code>{@link JProgressBar}</code>, under the given key. 226 * @param key the key to use to retrieve the client property. 227 * @return the value of the client property stored under the given key, or <code>null</code> if the property was 228 * not found. 229 * @throws NullPointerException if the given key is <code>null</code>. 230 */ 231 public Object clientProperty(Object key) { 232 return driver.clientProperty(target, key); 233 } 234 235 /** 236 * Waits until the value of this fixture's <code>{@link JProgressBar}</code> is equal to the given value. 237 * @param value the expected value. 238 * @return this fixture. 239 * @throws IllegalArgumentException if the given value is less than the <code>JProgressBar</code>'s minimum value. 240 * @throws IllegalArgumentException if the given value is greater than the <code>JProgressBar</code>'s maximum value. 241 * @throws WaitTimedOutError if the value of the <code>JProgressBar</code> does not reach the expected value within 242 * 30 seconds. 243 */ 244 public JProgressBarFixture waitUntilValueIs(int value) { 245 driver.waitUntilValueIs(target, value); 246 return this; 247 } 248 249 /** 250 * Waits until the value of this fixture's <code>{@link JProgressBar}</code> is equal to the given value. 251 * @param value the expected value. 252 * @param timeout the amount of time to wait. 253 * @return this fixture. 254 * @throws IllegalArgumentException if the given value is less than the <code>JProgressBar</code>'s minimum value. 255 * @throws IllegalArgumentException if the given value is greater than the <code>JProgressBar</code>'s maximum value. 256 * @throws NullPointerException if the given timeout is <code>null</code>. 257 * @throws WaitTimedOutError if the value of the <code>JProgressBar</code> does not reach the expected value within 258 * the specified timeout. 259 */ 260 public JProgressBarFixture waitUntilValueIs(int value, Timeout timeout) { 261 driver.waitUntilValueIs(target, value, timeout); 262 return this; 263 } 264 265 /** 266 * Waits until the value of this fixture's <code>{@link JProgressBar}</code> is in determinate mode. 267 * @return this fixture. 268 * @throws WaitTimedOutError if the <code>JProgressBar</code> does not reach determinate mode within 30 seconds. 269 */ 270 public JProgressBarFixture waitUntilIsDeterminate() { 271 driver.waitUntilIsDeterminate(target); 272 return this; 273 } 274 275 /** 276 * Waits until the value of this fixture's <code>{@link JProgressBar}</code> is in determinate mode. 277 * @param timeout the amount of time to wait. 278 * @return this fixture. 279 * @throws NullPointerException if the given timeout is <code>null</code>. 280 * @throws WaitTimedOutError if the <code>JProgressBar</code> does not reach determinate mode within the specified 281 * timeout. 282 */ 283 public JProgressBarFixture waitUntilIsDeterminate(Timeout timeout) { 284 driver.waitUntilIsDeterminate(target, timeout); 285 return this; 286 } 287 }