PageRenderTime 73ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/robotium-solo/src/main/java/com/robotium/solo/Solo.java

https://github.com/andreasnilsson/robotium
Java | 2718 lines | 904 code | 448 blank | 1366 comment | 45 complexity | f39fc446cf99dba81071e8147a8719d7 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.robotium.solo;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import junit.framework.Assert;
  5. import android.app.Activity;
  6. import android.app.Instrumentation;
  7. import android.content.pm.ActivityInfo;
  8. import android.graphics.PointF;
  9. import android.os.Environment;
  10. import android.view.KeyEvent;
  11. import android.view.View;
  12. import android.view.WindowManager;
  13. import android.webkit.WebView;
  14. import android.widget.AbsListView;
  15. import android.widget.Button;
  16. import android.widget.CheckBox;
  17. import android.widget.CheckedTextView;
  18. import android.widget.CompoundButton;
  19. import android.widget.DatePicker;
  20. import android.widget.EditText;
  21. import android.widget.ImageButton;
  22. import android.widget.ImageView;
  23. import android.widget.ProgressBar;
  24. import android.widget.RadioButton;
  25. import android.widget.ScrollView;
  26. import android.widget.SlidingDrawer;
  27. import android.widget.Spinner;
  28. import android.widget.TextView;
  29. import android.widget.ListView;
  30. import android.widget.TimePicker;
  31. import android.widget.ToggleButton;
  32. import android.app.Instrumentation.ActivityMonitor;
  33. /**
  34. * Main class for development of Robotium tests.
  35. * Robotium has full support for Views, WebViews, Activities, Dialogs, Menus and Context Menus.
  36. * <br>
  37. * Robotium can be used in conjunction with Android test classes like
  38. * ActivityInstrumentationTestCase2 and SingleLaunchActivityTestCase.
  39. *
  40. *
  41. *
  42. *
  43. * @author Renas Reda, renas.reda@robotium.com
  44. */
  45. public class Solo {
  46. protected final Asserter asserter;
  47. protected final ViewFetcher viewFetcher;
  48. protected final Checker checker;
  49. protected final Clicker clicker;
  50. protected final Presser presser;
  51. protected final Searcher searcher;
  52. protected final ActivityUtils activityUtils;
  53. protected final DialogUtils dialogUtils;
  54. protected final TextEnterer textEnterer;
  55. protected final Rotator rotator;
  56. protected final Scroller scroller;
  57. protected final Sleeper sleeper;
  58. protected final Swiper swiper;
  59. protected final Tapper tapper;
  60. protected final Waiter waiter;
  61. protected final Setter setter;
  62. protected final Getter getter;
  63. protected final WebUtils webUtils;
  64. protected final Sender sender;
  65. protected final ScreenshotTaker screenshotTaker;
  66. protected final Instrumentation instrumentation;
  67. protected final Zoomer zoomer;
  68. protected String webUrl = null;
  69. private final Config config;
  70. public final static int LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // 0
  71. public final static int PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 1
  72. public final static int RIGHT = KeyEvent.KEYCODE_DPAD_RIGHT;
  73. public final static int LEFT = KeyEvent.KEYCODE_DPAD_LEFT;
  74. public final static int UP = KeyEvent.KEYCODE_DPAD_UP;
  75. public final static int DOWN = KeyEvent.KEYCODE_DPAD_DOWN;
  76. public final static int ENTER = KeyEvent.KEYCODE_ENTER;
  77. public final static int MENU = KeyEvent.KEYCODE_MENU;
  78. public final static int DELETE = KeyEvent.KEYCODE_DEL;
  79. public final static int CLOSED = 0;
  80. public final static int OPENED = 1;
  81. /**
  82. * Constructor that takes the Instrumentation object and the start Activity.
  83. *
  84. * @param instrumentation the {@link Instrumentation} instance
  85. * @param activity the start {@link Activity} or {@code null}
  86. * if no Activity is specified
  87. */
  88. public Solo(Instrumentation instrumentation, Activity activity) {
  89. this(new Config(), instrumentation, activity);
  90. }
  91. /**
  92. * Constructor that takes the Instrumentation and Config objects.
  93. *
  94. * @param instrumentation the {@link Instrumentation} instance
  95. * @param config the {@link Config} instance
  96. */
  97. public Solo(Instrumentation instrumentation, Config config) {
  98. this(config, instrumentation, null);
  99. }
  100. /**
  101. * Constructor that takes the Instrumentation, Config and Activity objects.
  102. *
  103. * @param instrumentation the {@link Instrumentation} instance
  104. * @param config the {@link Config} instance
  105. * @param activity the start {@link Activity} or {@code null}
  106. * if no Activity is specified
  107. */
  108. public Solo(Instrumentation instrumentation, Config config, Activity activity) {
  109. this(config, instrumentation, activity);
  110. }
  111. /**
  112. * Private constructor.
  113. *
  114. * @param config the {@link Config} instance. If {@code null} one will be created.
  115. * @param instrumentation the {@link Instrumentation} instance
  116. * @param activity the start {@link Activity} or {@code null}
  117. * if no Activity is specified
  118. */
  119. private Solo(Config config, Instrumentation instrumentation, Activity activity) {
  120. this.config = (config == null) ? new Config(): config;
  121. this.instrumentation = instrumentation;
  122. this.sleeper = new Sleeper();
  123. this.sender = new Sender(instrumentation, sleeper);
  124. this.activityUtils = new ActivityUtils(instrumentation, activity, sleeper);
  125. this.viewFetcher = new ViewFetcher(activityUtils);
  126. this.screenshotTaker = new ScreenshotTaker(config, activityUtils, viewFetcher, sleeper);
  127. this.dialogUtils = new DialogUtils(activityUtils, viewFetcher, sleeper);
  128. this.webUtils = new WebUtils(config, instrumentation,activityUtils,viewFetcher, sleeper);
  129. this.scroller = new Scroller(config, instrumentation, activityUtils, viewFetcher, sleeper);
  130. this.searcher = new Searcher(viewFetcher, webUtils, scroller, sleeper);
  131. this.waiter = new Waiter(activityUtils, viewFetcher, searcher,scroller, sleeper);
  132. this.getter = new Getter(instrumentation, activityUtils, waiter);
  133. this.clicker = new Clicker(activityUtils, viewFetcher,sender, instrumentation, sleeper, waiter, webUtils, dialogUtils);
  134. this.setter = new Setter(activityUtils, getter, clicker, waiter);
  135. this.asserter = new Asserter(activityUtils, waiter);
  136. this.checker = new Checker(viewFetcher, waiter);
  137. this.zoomer = new Zoomer(instrumentation);
  138. this.swiper = new Swiper(instrumentation);
  139. this.tapper = new Tapper(instrumentation);
  140. this.rotator = new Rotator(instrumentation);
  141. this.presser = new Presser(viewFetcher, clicker, instrumentation, sleeper, waiter, dialogUtils);
  142. this.textEnterer = new TextEnterer(instrumentation, clicker, dialogUtils);
  143. initialize();
  144. }
  145. /**
  146. * Config class used to set the scroll behaviour, default timeouts, screenshot filetype and screenshot save path.
  147. * <br> <br>
  148. * Example of usage:
  149. * <pre>
  150. * public void setUp() throws Exception {
  151. * Config config = new Config();
  152. * config.screenshotFileType = ScreenshotFileType.PNG;
  153. * config.screenshotSavePath = Environment.getExternalStorageDirectory() + "/Robotium/";
  154. * config.shouldScroll = false;
  155. * solo = new Solo(getInstrumentation(), config);
  156. * getActivity();
  157. * }
  158. * </pre>
  159. *
  160. * @author Renas Reda, renas.reda@robotium.com
  161. */
  162. public static class Config {
  163. /**
  164. * The timeout length of the get, is, set, assert, enter and click methods. Default length is 10 000 milliseconds.
  165. */
  166. public int timeout_small = 10000;
  167. /**
  168. * The timeout length of the waitFor methods. Default length is 20 000 milliseconds.
  169. */
  170. public int timeout_large = 20000;
  171. /**
  172. * The screenshot save path. Default save path is /sdcard/Robotium-Screenshots/.
  173. */
  174. public String screenshotSavePath = Environment.getExternalStorageDirectory() + "/Robotium-Screenshots/";
  175. /**
  176. * The screenshot file type, JPEG or PNG. Use ScreenshotFileType.JPEG or ScreenshotFileType.PNG. Default file type is JPEG.
  177. */
  178. public ScreenshotFileType screenshotFileType = ScreenshotFileType.JPEG;
  179. /**
  180. * Set to true if the get, is, set, enter, type and click methods should scroll. Default value is true.
  181. */
  182. public boolean shouldScroll = true;
  183. /**
  184. * Set to true if JavaScript should be used to click WebElements. Default value is false.
  185. */
  186. public boolean useJavaScriptToClickWebElements = false;
  187. /**
  188. * The screenshot file type, JPEG or PNG.
  189. *
  190. * @author Renas Reda, renas.reda@robotium.com
  191. *
  192. */
  193. public enum ScreenshotFileType {
  194. JPEG, PNG
  195. }
  196. }
  197. /**
  198. * Constructor that takes the instrumentation object.
  199. *
  200. * @param instrumentation the {@link Instrumentation} instance
  201. */
  202. public Solo(Instrumentation instrumentation) {
  203. this(new Config(), instrumentation, null);
  204. }
  205. /**
  206. * Returns the ActivityMonitor used by Robotium.
  207. *
  208. * @return the ActivityMonitor used by Robotium
  209. */
  210. public ActivityMonitor getActivityMonitor(){
  211. return activityUtils.getActivityMonitor();
  212. }
  213. /**
  214. * Returns the Config used by Robotium.
  215. *
  216. * @return the Config used by Robotium
  217. */
  218. public Config getConfig(){
  219. return config;
  220. }
  221. /**
  222. * Returns an ArrayList of all the View objects located in the focused
  223. * Activity or Dialog.
  224. *
  225. * @return an {@code ArrayList} of the {@link View} objects located in the focused window
  226. */
  227. public ArrayList<View> getViews() {
  228. try {
  229. return viewFetcher.getViews(null, false);
  230. } catch (Exception e) {
  231. e.printStackTrace();
  232. return null;
  233. }
  234. }
  235. /**
  236. * Returns an ArrayList of the View objects contained in the parent View.
  237. *
  238. * @param parent the parent view from which to return the views
  239. * @return an {@code ArrayList} of the {@link View} objects contained in the specified {@code View}
  240. */
  241. public ArrayList<View> getViews(View parent) {
  242. try {
  243. return viewFetcher.getViews(parent, false);
  244. } catch (Exception e) {
  245. e.printStackTrace();
  246. return null;
  247. }
  248. }
  249. /**
  250. * Returns the absolute top parent View of the specified View.
  251. *
  252. * @param view the {@link View} whose top parent is requested
  253. * @return the top parent {@link View}
  254. */
  255. public View getTopParent(View view) {
  256. View topParent = viewFetcher.getTopParent(view);
  257. return topParent;
  258. }
  259. /**
  260. * Waits for the specified text to appear. Default timeout is 20 seconds.
  261. *
  262. * @param text the text to wait for, specified as a regular expression
  263. * @return {@code true} if text is displayed and {@code false} if it is not displayed before the timeout
  264. */
  265. public boolean waitForText(String text) {
  266. return (waiter.waitForText(text) != null);
  267. }
  268. /**
  269. * Waits for the specified text to appear.
  270. *
  271. * @param text the text to wait for, specified as a regular expression
  272. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  273. * @param timeout the the amount of time in milliseconds to wait
  274. * @return {@code true} if text is displayed and {@code false} if it is not displayed before the timeout
  275. */
  276. public boolean waitForText(String text, int minimumNumberOfMatches, long timeout) {
  277. return (waiter.waitForText(text, minimumNumberOfMatches, timeout) != null);
  278. }
  279. /**
  280. * Waits for the specified text to appear.
  281. *
  282. * @param text the text to wait for, specified as a regular expression
  283. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  284. * @param timeout the the amount of time in milliseconds to wait
  285. * @param scroll {@code true} if scrolling should be performed
  286. * @return {@code true} if text is displayed and {@code false} if it is not displayed before the timeout
  287. */
  288. public boolean waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll) {
  289. return (waiter.waitForText(text, minimumNumberOfMatches, timeout, scroll) != null);
  290. }
  291. /**
  292. * Waits for the specified text to appear.
  293. *
  294. * @param text the text to wait for, specified as a regular expression
  295. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  296. * @param timeout the the amount of time in milliseconds to wait
  297. * @param scroll {@code true} if scrolling should be performed
  298. * @param onlyVisible {@code true} if only visible text views should be waited for
  299. * @return {@code true} if text is displayed and {@code false} if it is not displayed before the timeout
  300. */
  301. public boolean waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible) {
  302. return (waiter.waitForText(text, minimumNumberOfMatches, timeout, scroll, onlyVisible, true) != null);
  303. }
  304. /**
  305. * Waits for a View matching the specified resource id. Default timeout is 20 seconds.
  306. *
  307. * @param id the R.id of the {@link View} to wait for
  308. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  309. */
  310. public boolean waitForView(int id){
  311. return waitForView(id, 0, Timeout.getLargeTimeout(), true);
  312. }
  313. /**
  314. * Waits for a View matching the specified resource id.
  315. *
  316. * @param id the R.id of the {@link View} to wait for
  317. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  318. * @param timeout the amount of time in milliseconds to wait
  319. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  320. */
  321. public boolean waitForView(int id, int minimumNumberOfMatches, int timeout){
  322. return waitForView(id, minimumNumberOfMatches, timeout, true);
  323. }
  324. /**
  325. * Waits for a View matching the specified resource id.
  326. *
  327. * @param id the R.id of the {@link View} to wait for
  328. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  329. * @param timeout the amount of time in milliseconds to wait
  330. * @param scroll {@code true} if scrolling should be performed
  331. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  332. */
  333. public boolean waitForView(int id, int minimumNumberOfMatches, int timeout, boolean scroll){
  334. int index = minimumNumberOfMatches-1;
  335. if(index < 1)
  336. index = 0;
  337. return (waiter.waitForView(id, index, timeout, scroll) != null);
  338. }
  339. /**
  340. * Waits for a View matching the specified class. Default timeout is 20 seconds.
  341. *
  342. * @param viewClass the {@link View} class to wait for
  343. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  344. */
  345. public <T extends View> boolean waitForView(final Class<T> viewClass){
  346. return waiter.waitForView(viewClass, 0, Timeout.getLargeTimeout(), true);
  347. }
  348. /**
  349. * Waits for the specified View. Default timeout is 20 seconds.
  350. *
  351. * @param view the {@link View} object to wait for
  352. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  353. */
  354. public <T extends View> boolean waitForView(View view){
  355. return waiter.waitForView(view);
  356. }
  357. /**
  358. * Waits for the specified View.
  359. *
  360. * @param view the {@link View} object to wait for
  361. * @param timeout the amount of time in milliseconds to wait
  362. * @param scroll {@code true} if scrolling should be performed
  363. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  364. */
  365. public <T extends View> boolean waitForView(View view, int timeout, boolean scroll){
  366. boolean checkIsShown = false;
  367. if(!scroll){
  368. checkIsShown = true;
  369. }
  370. View viewToWaitFor = waiter.waitForView(view, timeout, scroll, checkIsShown);
  371. if(viewToWaitFor != null)
  372. return true;
  373. return false;
  374. }
  375. /**
  376. * Waits for a View matching the specified class.
  377. *
  378. * @param viewClass the {@link View} class to wait for
  379. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  380. * @param timeout the amount of time in milliseconds to wait
  381. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  382. */
  383. public <T extends View> boolean waitForView(final Class<T> viewClass, final int minimumNumberOfMatches, final int timeout){
  384. int index = minimumNumberOfMatches-1;
  385. if(index < 1)
  386. index = 0;
  387. return waiter.waitForView(viewClass, index, timeout, true);
  388. }
  389. /**
  390. * Waits for a View matching the specified class.
  391. *
  392. * @param viewClass the {@link View} class to wait for
  393. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  394. * @param timeout the amount of time in milliseconds to wait
  395. * @param scroll {@code true} if scrolling should be performed
  396. * @return {@code true} if the {@link View} is displayed and {@code false} if it is not displayed before the timeout
  397. */
  398. public <T extends View> boolean waitForView(final Class<T> viewClass, final int minimumNumberOfMatches, final int timeout,final boolean scroll){
  399. int index = minimumNumberOfMatches-1;
  400. if(index < 1)
  401. index = 0;
  402. return waiter.waitForView(viewClass, index, timeout, scroll);
  403. }
  404. /**
  405. * Waits for a WebElement matching the specified By object. Default timeout is 20 seconds.
  406. *
  407. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  408. * @return {@code true} if the {@link WebElement} is displayed and {@code false} if it is not displayed before the timeout
  409. */
  410. public boolean waitForWebElement(By by){
  411. return (waiter.waitForWebElement(by, 0, Timeout.getLargeTimeout(), true) != null);
  412. }
  413. /**
  414. * Waits for a WebElement matching the specified By object.
  415. *
  416. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  417. * @param timeout the the amount of time in milliseconds to wait
  418. * @param scroll {@code true} if scrolling should be performed
  419. * @return {@code true} if the {@link WebElement} is displayed and {@code false} if it is not displayed before the timeout
  420. */
  421. public boolean waitForWebElement(By by, int timeout, boolean scroll){
  422. return (waiter.waitForWebElement(by, 0, timeout, scroll) != null);
  423. }
  424. /**
  425. * Waits for a WebElement matching the specified By object.
  426. *
  427. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  428. * @param minimumNumberOfMatches the minimum number of matches that are expected to be found. {@code 0} means any number of matches
  429. * @param timeout the the amount of time in milliseconds to wait
  430. * @param scroll {@code true} if scrolling should be performed
  431. * @return {@code true} if the {@link WebElement} is displayed and {@code false} if it is not displayed before the timeout
  432. */
  433. public boolean waitForWebElement(By by, int minimumNumberOfMatches, int timeout, boolean scroll){
  434. return (waiter.waitForWebElement(by, minimumNumberOfMatches, timeout, scroll) != null);
  435. }
  436. /**
  437. * Waits for a condition to be satisfied.
  438. *
  439. * @param condition the condition to wait for
  440. * @param timeout the amount of time in milliseconds to wait
  441. * @return {@code true} if condition is satisfied and {@code false} if it is not satisfied before the timeout
  442. */
  443. public boolean waitForCondition(Condition condition, final int timeout){
  444. return waiter.waitForCondition(condition, timeout);
  445. }
  446. /**
  447. * Searches for a text in the EditText objects currently displayed and returns true if found. Will automatically scroll when needed.
  448. *
  449. * @param text the text to search for
  450. * @return {@code true} if an {@link EditText} displaying the specified text is found or {@code false} if it is not found
  451. */
  452. public boolean searchEditText(String text) {
  453. return searcher.searchWithTimeoutFor(EditText.class, text, 1, true, false);
  454. }
  455. /**
  456. * Searches for a Button displaying the specified text and returns {@code true} if at least one Button
  457. * is found. Will automatically scroll when needed.
  458. *
  459. * @param text the text to search for. The parameter will be interpreted as a regular expression
  460. * @return {@code true} if a {@link Button} displaying the specified text is found and {@code false} if it is not found
  461. */
  462. public boolean searchButton(String text) {
  463. return searcher.searchWithTimeoutFor(Button.class, text, 0, true, false);
  464. }
  465. /**
  466. * Searches for a Button displaying the specified text and returns {@code true} if at least one Button
  467. * is found. Will automatically scroll when needed.
  468. *
  469. * @param text the text to search for. The parameter will be interpreted as a regular expression
  470. * @param onlyVisible {@code true} if only {@link Button} visible on the screen should be searched
  471. * @return {@code true} if a {@link Button} displaying the specified text is found and {@code false} if it is not found
  472. */
  473. public boolean searchButton(String text, boolean onlyVisible) {
  474. return searcher.searchWithTimeoutFor(Button.class, text, 0, true, onlyVisible);
  475. }
  476. /**
  477. * Searches for a ToggleButton displaying the specified text and returns {@code true} if at least one ToggleButton
  478. * is found. Will automatically scroll when needed.
  479. *
  480. * @param text the text to search for. The parameter will be interpreted as a regular expression
  481. * @return {@code true} if a {@link ToggleButton} displaying the specified text is found and {@code false} if it is not found
  482. */
  483. public boolean searchToggleButton(String text) {
  484. return searcher.searchWithTimeoutFor(ToggleButton.class, text, 0, true, false);
  485. }
  486. /**
  487. * Searches for a Button displaying the specified text and returns {@code true} if the
  488. * searched Button is found a specified number of times. Will automatically scroll when needed.
  489. *
  490. * @param text the text to search for. The parameter will be interpreted as a regular expression
  491. * @param minimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
  492. * matches are expected to be found
  493. * @return {@code true} if a {@link Button} displaying the specified text is found a specified number of times and {@code false}
  494. * if it is not found
  495. */
  496. public boolean searchButton(String text, int minimumNumberOfMatches) {
  497. return searcher.searchWithTimeoutFor(Button.class, text, minimumNumberOfMatches, true, false);
  498. }
  499. /**
  500. * Searches for a Button displaying the specified text and returns {@code true} if the
  501. * searched Button is found a specified number of times. Will automatically scroll when needed.
  502. *
  503. * @param text the text to search for. The parameter will be interpreted as a regular expression
  504. * @param minimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
  505. * matches are expected to be found
  506. * @param onlyVisible {@code true} if only {@link Button} visible on the screen should be searched
  507. * @return {@code true} if a {@link Button} displaying the specified text is found a specified number of times and {@code false}
  508. * if it is not found
  509. */
  510. public boolean searchButton(String text, int minimumNumberOfMatches, boolean onlyVisible) {
  511. return searcher.searchWithTimeoutFor(Button.class, text, minimumNumberOfMatches, true, onlyVisible);
  512. }
  513. /**
  514. * Searches for a ToggleButton displaying the specified text and returns {@code true} if the
  515. * searched ToggleButton is found a specified number of times. Will automatically scroll when needed.
  516. *
  517. * @param text the text to search for. The parameter will be interpreted as a regular expression
  518. * @param minimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
  519. * matches are expected to be found
  520. * @return {@code true} if a {@link ToggleButton} displaying the specified text is found a specified number of times and {@code false}
  521. * if it is not found
  522. */
  523. public boolean searchToggleButton(String text, int minimumNumberOfMatches) {
  524. return searcher.searchWithTimeoutFor(ToggleButton.class, text, minimumNumberOfMatches, true, false);
  525. }
  526. /**
  527. * Searches for the specified text and returns {@code true} if at least one item
  528. * is found displaying the expected text. Will automatically scroll when needed.
  529. *
  530. * @param text the text to search for. The parameter will be interpreted as a regular expression
  531. * @return {@code true} if the search string is found and {@code false} if it is not found
  532. */
  533. public boolean searchText(String text) {
  534. return searcher.searchWithTimeoutFor(TextView.class, text, 0, true, false);
  535. }
  536. /**
  537. * Searches for the specified text and returns {@code true} if at least one item
  538. * is found displaying the expected text. Will automatically scroll when needed.
  539. *
  540. * @param text the text to search for. The parameter will be interpreted as a regular expression
  541. * @param onlyVisible {@code true} if only texts visible on the screen should be searched
  542. * @return {@code true} if the search string is found and {@code false} if it is not found
  543. */
  544. public boolean searchText(String text, boolean onlyVisible) {
  545. return searcher.searchWithTimeoutFor(TextView.class, text, 0, true, onlyVisible);
  546. }
  547. /**
  548. * Searches for the specified text and returns {@code true} if the searched text is found a specified
  549. * number of times. Will automatically scroll when needed.
  550. *
  551. * @param text the text to search for. The parameter will be interpreted as a regular expression
  552. * @param minimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
  553. * matches are expected to be found
  554. * @return {@code true} if text is found a specified number of times and {@code false} if the text
  555. * is not found
  556. */
  557. public boolean searchText(String text, int minimumNumberOfMatches) {
  558. return searcher.searchWithTimeoutFor(TextView.class, text, minimumNumberOfMatches, true, false);
  559. }
  560. /**
  561. * Searches for the specified text and returns {@code true} if the searched text is found a specified
  562. * number of times.
  563. *
  564. * @param text the text to search for. The parameter will be interpreted as a regular expression.
  565. * @param minimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
  566. * matches are expected to be found
  567. * @param scroll {@code true} if scrolling should be performed
  568. * @return {@code true} if text is found a specified number of times and {@code false} if the text
  569. * is not found
  570. */
  571. public boolean searchText(String text, int minimumNumberOfMatches, boolean scroll) {
  572. return searcher.searchWithTimeoutFor(TextView.class, text, minimumNumberOfMatches, scroll, false);
  573. }
  574. /**
  575. * Searches for the specified text and returns {@code true} if the searched text is found a specified
  576. * number of times.
  577. *
  578. * @param text the text to search for. The parameter will be interpreted as a regular expression.
  579. * @param minimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
  580. * matches are expected to be found
  581. * @param scroll {@code true} if scrolling should be performed
  582. * @param onlyVisible {@code true} if only texts visible on the screen should be searched
  583. * @return {@code true} if text is found a specified number of times and {@code false} if the text
  584. * is not found
  585. */
  586. public boolean searchText(String text, int minimumNumberOfMatches, boolean scroll, boolean onlyVisible) {
  587. return searcher.searchWithTimeoutFor(TextView.class, text, minimumNumberOfMatches, scroll, onlyVisible);
  588. }
  589. /**
  590. * Sets the Orientation (Landscape/Portrait) for the current Activity.
  591. *
  592. * @param orientation the orientation to set. <code>Solo.</code>{@link #LANDSCAPE} for landscape or
  593. * <code>Solo.</code>{@link #PORTRAIT} for portrait.
  594. */
  595. public void setActivityOrientation(int orientation)
  596. {
  597. activityUtils.setActivityOrientation(orientation);
  598. }
  599. /**
  600. * Returns the current Activity.
  601. *
  602. * @return the current Activity
  603. */
  604. public Activity getCurrentActivity() {
  605. return activityUtils.getCurrentActivity(false);
  606. }
  607. /**
  608. * Asserts that the Activity matching the specified name is active.
  609. *
  610. * @param message the message to display if the assert fails
  611. * @param name the name of the {@link Activity} that is expected to be active. Example is: {@code "MyActivity"}
  612. */
  613. public void assertCurrentActivity(String message, String name)
  614. {
  615. asserter.assertCurrentActivity(message, name);
  616. }
  617. /**
  618. * Asserts that the Activity matching the specified class is active.
  619. *
  620. * @param message the message to display if the assert fails
  621. * @param activityClass the class of the Activity that is expected to be active. Example is: {@code MyActivity.class}
  622. */
  623. @SuppressWarnings("unchecked")
  624. public void assertCurrentActivity(String message, @SuppressWarnings("rawtypes") Class activityClass)
  625. {
  626. asserter.assertCurrentActivity(message, activityClass);
  627. }
  628. /**
  629. * Asserts that the Activity matching the specified name is active, with the possibility to
  630. * verify that the expected Activity is a new instance of the Activity.
  631. *
  632. * @param message the message to display if the assert fails
  633. * @param name the name of the Activity that is expected to be active. Example is: {@code "MyActivity"}
  634. * @param isNewInstance {@code true} if the expected {@link Activity} is a new instance of the {@link Activity}
  635. */
  636. public void assertCurrentActivity(String message, String name, boolean isNewInstance)
  637. {
  638. asserter.assertCurrentActivity(message, name, isNewInstance);
  639. }
  640. /**
  641. * Asserts that the Activity matching the specified class is active, with the possibility to
  642. * verify that the expected Activity is a new instance of the Activity.
  643. *
  644. * @param message the message to display if the assert fails
  645. * @param activityClass the class of the Activity that is expected to be active. Example is: {@code MyActivity.class}
  646. * @param isNewInstance {@code true} if the expected {@link Activity} is a new instance of the {@link Activity}
  647. */
  648. @SuppressWarnings("unchecked")
  649. public void assertCurrentActivity(String message, @SuppressWarnings("rawtypes") Class activityClass,
  650. boolean isNewInstance) {
  651. asserter.assertCurrentActivity(message, activityClass, isNewInstance);
  652. }
  653. /**
  654. * Asserts that the available memory is not considered low by the system.
  655. */
  656. public void assertMemoryNotLow()
  657. {
  658. asserter.assertMemoryNotLow();
  659. }
  660. /**
  661. * Waits for a Dialog to open. Default timeout is 20 seconds.
  662. *
  663. * @return {@code true} if the {@link android.app.Dialog} is opened before the timeout and {@code false} if it is not opened
  664. */
  665. public boolean waitForDialogToOpen() {
  666. return dialogUtils.waitForDialogToOpen(Timeout.getLargeTimeout(), true);
  667. }
  668. /**
  669. * Waits for a Dialog to close. Default timeout is 20 seconds.
  670. *
  671. * @return {@code true} if the {@link android.app.Dialog} is closed before the timeout and {@code false} if it is not closed
  672. */
  673. public boolean waitForDialogToClose() {
  674. return dialogUtils.waitForDialogToClose(Timeout.getLargeTimeout());
  675. }
  676. /**
  677. * Waits for a Dialog to open.
  678. *
  679. * @param timeout the amount of time in milliseconds to wait
  680. * @return {@code true} if the {@link android.app.Dialog} is opened before the timeout and {@code false} if it is not opened
  681. */
  682. public boolean waitForDialogToOpen(long timeout) {
  683. return dialogUtils.waitForDialogToOpen(timeout, true);
  684. }
  685. /**
  686. * Waits for a Dialog to close.
  687. *
  688. * @param timeout the amount of time in milliseconds to wait
  689. * @return {@code true} if the {@link android.app.Dialog} is closed before the timeout and {@code false} if it is not closed
  690. */
  691. public boolean waitForDialogToClose(long timeout) {
  692. return dialogUtils.waitForDialogToClose(timeout);
  693. }
  694. /**
  695. * Simulates pressing the hardware back key.
  696. */
  697. public void goBack()
  698. {
  699. hideSoftKeyboard();
  700. sender.goBack();
  701. }
  702. /**
  703. * Clicks the specified coordinates.
  704. *
  705. * @param x the x coordinate
  706. * @param y the y coordinate
  707. */
  708. public void clickOnScreen(float x, float y) {
  709. sleeper.sleep();
  710. clicker.clickOnScreen(x, y, null);
  711. }
  712. /**
  713. * Clicks the specified coordinates rapidly a specified number of times. Requires API level >= 14.
  714. *
  715. * @param x the x coordinate
  716. * @param y the y coordinate
  717. * @param numberOfClicks the number of clicks to perform
  718. */
  719. public void clickOnScreen(float x, float y, int numberOfClicks) {
  720. if (android.os.Build.VERSION.SDK_INT < 14){
  721. throw new RuntimeException("clickOnScreen(float x, float y, int numberOfClicks) requires API level >= 14");
  722. }
  723. tapper.generateTapGesture(numberOfClicks, new PointF(x, y));
  724. }
  725. /**
  726. * Long clicks the specified coordinates.
  727. *
  728. * @param x the x coordinate
  729. * @param y the y coordinate
  730. */
  731. public void clickLongOnScreen(float x, float y) {
  732. clicker.clickLongOnScreen(x, y, 0, null);
  733. }
  734. /**
  735. * Long clicks the specified coordinates for a specified amount of time.
  736. *
  737. * @param x the x coordinate
  738. * @param y the y coordinate
  739. * @param time the amount of time to long click
  740. */
  741. public void clickLongOnScreen(float x, float y, int time) {
  742. clicker.clickLongOnScreen(x, y, time, null);
  743. }
  744. /**
  745. * Clicks a Button displaying the specified text. Will automatically scroll when needed.
  746. *
  747. * @param text the text displayed by the {@link Button}. The parameter will be interpreted as a regular expression
  748. */
  749. public void clickOnButton(String text) {
  750. clicker.clickOn(Button.class, text);
  751. }
  752. /**
  753. * Clicks an ImageButton matching the specified index.
  754. *
  755. * @param index the index of the {@link ImageButton} to click. 0 if only one is available
  756. */
  757. public void clickOnImageButton(int index) {
  758. clicker.clickOn(ImageButton.class, index);
  759. }
  760. /**
  761. * Clicks a ToggleButton displaying the specified text.
  762. *
  763. * @param text the text displayed by the {@link ToggleButton}. The parameter will be interpreted as a regular expression
  764. */
  765. public void clickOnToggleButton(String text) {
  766. clicker.clickOn(ToggleButton.class, text);
  767. }
  768. /**
  769. * Clicks a MenuItem displaying the specified text.
  770. *
  771. * @param text the text displayed by the MenuItem. The parameter will be interpreted as a regular expression
  772. */
  773. public void clickOnMenuItem(String text)
  774. {
  775. clicker.clickOnMenuItem(text);
  776. }
  777. /**
  778. * Clicks a MenuItem displaying the specified text.
  779. *
  780. * @param text the text displayed by the MenuItem. The parameter will be interpreted as a regular expression
  781. * @param subMenu {@code true} if the menu item could be located in a sub menu
  782. */
  783. public void clickOnMenuItem(String text, boolean subMenu)
  784. {
  785. clicker.clickOnMenuItem(text, subMenu);
  786. }
  787. /**
  788. * Clicks the specified WebElement.
  789. *
  790. * @param webElement the WebElement to click
  791. */
  792. public void clickOnWebElement(WebElement webElement){
  793. if(webElement == null)
  794. Assert.fail("WebElement is null and can therefore not be clicked!");
  795. clicker.clickOnScreen(webElement.getLocationX(), webElement.getLocationY(), null);
  796. }
  797. /**
  798. * Clicks a WebElement matching the specified By object.
  799. *
  800. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  801. */
  802. public void clickOnWebElement(By by){
  803. clickOnWebElement(by, 0, true);
  804. }
  805. /**
  806. * Clicks a WebElement matching the specified By object.
  807. *
  808. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  809. * @param match if multiple objects match, this determines which one to click
  810. */
  811. public void clickOnWebElement(By by, int match){
  812. clickOnWebElement(by, match, true);
  813. }
  814. /**
  815. * Clicks a WebElement matching the specified By object.
  816. *
  817. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  818. * @param match if multiple objects match, this determines which one to click
  819. * @param scroll {@code true} if scrolling should be performed
  820. */
  821. public void clickOnWebElement(By by, int match, boolean scroll){
  822. clicker.clickOnWebElement(by, match, scroll, config.useJavaScriptToClickWebElements);
  823. }
  824. /**
  825. * Presses a MenuItem matching the specified index. Index {@code 0} is the first item in the
  826. * first row, Index {@code 3} is the first item in the second row and
  827. * index {@code 6} is the first item in the third row.
  828. *
  829. * @param index the index of the {@link android.view.MenuItem} to press
  830. */
  831. public void pressMenuItem(int index) {
  832. presser.pressMenuItem(index);
  833. }
  834. /**
  835. * Presses a MenuItem matching the specified index. Supports three rows with a specified amount
  836. * of items. If itemsPerRow equals 5 then index 0 is the first item in the first row,
  837. * index 5 is the first item in the second row and index 10 is the first item in the third row.
  838. *
  839. * @param index the index of the {@link android.view.MenuItem} to press
  840. * @param itemsPerRow the amount of menu items there are per row
  841. */
  842. public void pressMenuItem(int index, int itemsPerRow) {
  843. presser.pressMenuItem(index, itemsPerRow);
  844. }
  845. /**
  846. * Presses the soft keyboard next button.
  847. */
  848. public void pressSoftKeyboardNextButton(){
  849. presser.pressSoftKeyboardSearchOrNextButton(false);
  850. }
  851. /**
  852. * Presses the soft keyboard search button.
  853. */
  854. public void pressSoftKeyboardSearchButton(){
  855. presser.pressSoftKeyboardSearchOrNextButton(true);
  856. }
  857. /**
  858. * Presses a Spinner (drop-down menu) item.
  859. *
  860. * @param spinnerIndex the index of the {@link Spinner} menu to use
  861. * @param itemIndex the index of the {@link Spinner} item to press relative to the currently selected item.
  862. * A Negative number moves up on the {@link Spinner}, positive moves down
  863. */
  864. public void pressSpinnerItem(int spinnerIndex, int itemIndex)
  865. {
  866. presser.pressSpinnerItem(spinnerIndex, itemIndex);
  867. }
  868. /**
  869. * Clicks the specified View.
  870. *
  871. * @param view the {@link View} to click
  872. */
  873. public void clickOnView(View view) {
  874. view = waiter.waitForView(view, Timeout.getSmallTimeout());
  875. clicker.clickOnScreen(view);
  876. }
  877. /**
  878. * Clicks the specified View.
  879. *
  880. * @param view the {@link View} to click
  881. * @param immediately {@code true} if View should be clicked without any wait
  882. */
  883. public void clickOnView(View view, boolean immediately){
  884. if(immediately)
  885. clicker.clickOnScreen(view);
  886. else{
  887. view = waiter.waitForView(view, Timeout.getSmallTimeout());
  888. clicker.clickOnScreen(view);
  889. }
  890. }
  891. /**
  892. * Long clicks the specified View.
  893. *
  894. * @param view the {@link View} to long click
  895. */
  896. public void clickLongOnView(View view) {
  897. view = waiter.waitForView(view, Timeout.getSmallTimeout());
  898. clicker.clickOnScreen(view, true, 0);
  899. }
  900. /**
  901. * Long clicks the specified View for a specified amount of time.
  902. *
  903. * @param view the {@link View} to long click
  904. * @param time the amount of time to long click
  905. */
  906. public void clickLongOnView(View view, int time) {
  907. clicker.clickOnScreen(view, true, time);
  908. }
  909. /**
  910. * Clicks a View or WebElement displaying the specified
  911. * text. Will automatically scroll when needed.
  912. *
  913. * @param text the text to click. The parameter will be interpreted as a regular expression
  914. */
  915. public void clickOnText(String text) {
  916. clicker.clickOnText(text, false, 1, true, 0);
  917. }
  918. /**
  919. * Clicks a View or WebElement displaying the specified text. Will automatically scroll when needed.
  920. *
  921. * @param text the text to click. The parameter will be interpreted as a regular expression
  922. * @param match if multiple objects match the text, this determines which one to click
  923. */
  924. public void clickOnText(String text, int match) {
  925. clicker.clickOnText(text, false, match, true, 0);
  926. }
  927. /**
  928. * Clicks a View or WebElement displaying the specified text.
  929. *
  930. * @param text the text to click. The parameter will be interpreted as a regular expression
  931. * @param match if multiple objects match the text, this determines which one to click
  932. * @param scroll {@code true} if scrolling should be performed
  933. */
  934. public void clickOnText(String text, int match, boolean scroll) {
  935. clicker.clickOnText(text, false, match, scroll, 0);
  936. }
  937. /**
  938. * Long clicks a View or WebElement displaying the specified text. Will automatically scroll when needed.
  939. *
  940. * @param text the text to click. The parameter will be interpreted as a regular expression
  941. */
  942. public void clickLongOnText(String text)
  943. {
  944. clicker.clickOnText(text, true, 1, true, 0);
  945. }
  946. /**
  947. * Long clicks a View or WebElement displaying the specified text. Will automatically scroll when needed.
  948. *
  949. * @param text the text to click. The parameter will be interpreted as a regular expression
  950. * @param match if multiple objects match the text, this determines which one to click
  951. */
  952. public void clickLongOnText(String text, int match)
  953. {
  954. clicker.clickOnText(text, true, match, true, 0);
  955. }
  956. /**
  957. * Long clicks a View or WebElement displaying the specified text.
  958. *
  959. * @param text the text to click. The parameter will be interpreted as a regular expression
  960. * @param match if multiple objects match the text, this determines which one to click
  961. * @param scroll {@code true} if scrolling should be performed
  962. */
  963. public void clickLongOnText(String text, int match, boolean scroll)
  964. {
  965. clicker.clickOnText(text, true, match, scroll, 0);
  966. }
  967. /**
  968. * Long clicks a View or WebElement displaying the specified text.
  969. *
  970. * @param text the text to click. The parameter will be interpreted as a regular expression
  971. * @param match if multiple objects match the text, this determines which one to click
  972. * @param time the amount of time to long click
  973. */
  974. public void clickLongOnText(String text, int match, int time)
  975. {
  976. clicker.clickOnText(text, true, match, true, time);
  977. }
  978. /**
  979. * Long clicks a View displaying the specified text and then selects
  980. * an item from the context menu that appears. Will automatically scroll when needed.
  981. *
  982. * @param text the text to click. The parameter will be interpreted as a regular expression
  983. * @param index the index of the menu item to press. {@code 0} if only one is available
  984. */
  985. public void clickLongOnTextAndPress(String text, int index) {
  986. clicker.clickLongOnTextAndPress(text, index);
  987. }
  988. /**
  989. * Clicks a Button matching the specified index.
  990. *
  991. * @param index the index of the {@link Button} to click. {@code 0} if only one is available
  992. */
  993. public void clickOnButton(int index) {
  994. clicker.clickOn(Button.class, index);
  995. }
  996. /**
  997. * Clicks a RadioButton matching the specified index.
  998. *
  999. * @param index the index of the {@link RadioButton} to click. {@code 0} if only one is available
  1000. */
  1001. public void clickOnRadioButton(int index) {
  1002. clicker.clickOn(RadioButton.class, index);
  1003. }
  1004. /**
  1005. * Clicks a CheckBox matching the specified index.
  1006. *
  1007. * @param index the index of the {@link CheckBox} to click. {@code 0} if only one is available
  1008. */
  1009. public void clickOnCheckBox(int index) {
  1010. clicker.clickOn(CheckBox.class, index);
  1011. }
  1012. /**
  1013. * Clicks an EditText matching the specified index.
  1014. *
  1015. * @param index the index of the {@link EditText} to click. {@code 0} if only one is available
  1016. */
  1017. public void clickOnEditText(int index) {
  1018. clicker.clickOn(EditText.class, index);
  1019. }
  1020. /**
  1021. * Clicks the specified list line and returns an ArrayList of the TextView objects that
  1022. * the list line is displaying. Will use the first ListView it finds.
  1023. *
  1024. * @param line the line to click
  1025. * @return an {@code ArrayList} of the {@link TextView} objects located in the list line
  1026. */
  1027. public ArrayList<TextView> clickInList(int line) {
  1028. return clicker.clickInList(line);
  1029. }
  1030. /**
  1031. * Clicks the specified list line in the ListView matching the specified index and
  1032. * returns an ArrayList of the TextView objects that the list line is displaying.
  1033. *
  1034. * @param line the line to click
  1035. * @param index the index of the list. {@code 0} if only one is available
  1036. * @return an {@code ArrayList} of the {@link TextView} objects located in the list line
  1037. */
  1038. public ArrayList<TextView> clickInList(int line, int index) {
  1039. return clicker.clickInList(line, index, false, 0);
  1040. }
  1041. /**
  1042. * Long clicks the specified list line and returns an ArrayList of the TextView objects that
  1043. * the list line is displaying. Will use the first ListView it finds.
  1044. *
  1045. * @param line the line to click
  1046. * @return an {@code ArrayList} of the {@link TextView} objects located in the list line
  1047. */
  1048. public ArrayList<TextView> clickLongInList(int line){
  1049. return clicker.clickInList(line, 0, true, 0);
  1050. }
  1051. /**
  1052. * Long clicks the specified list line in the ListView matching the specified index and
  1053. * returns an ArrayList of the TextView objects that the list line is displaying.
  1054. *
  1055. * @param line the line to click
  1056. * @param index the index of the list. {@code 0} if only one is available
  1057. * @return an {@code ArrayList} of the {@link TextView} objects located in the list line
  1058. */
  1059. public ArrayList<TextView> clickLongInList(int line, int index){
  1060. return clicker.clickInList(line, index, true, 0);
  1061. }
  1062. /**
  1063. * Long clicks the specified list line in the ListView matching the specified index and
  1064. * returns an ArrayList of the TextView objects that the list line is displaying.
  1065. *
  1066. * @param line the line to click
  1067. * @param index the index of the list. {@code 0} if only one is available
  1068. * @param time the amount of time to long click
  1069. * @return an {@code ArrayList} of the {@link TextView} objects located in the list line
  1070. */
  1071. public ArrayList<TextView> clickLongInList(int line, int index, int time){
  1072. return clicker.clickInList(line, index, true, time);
  1073. }
  1074. /**
  1075. * Clicks an ActionBarItem matching the specified resource id.
  1076. *
  1077. * @param id the R.id of the ActionBar item to click
  1078. */
  1079. public void clickOnActionBarItem(int id){
  1080. clicker.clickOnActionBarItem(id);
  1081. }
  1082. /**
  1083. * Clicks an ActionBar Home/Up button.
  1084. */
  1085. public void clickOnActionBarHomeButton() {
  1086. clicker.clickOnActionBarHomeButton();
  1087. }
  1088. /**
  1089. * Simulate touching the specified location and dragging it to a new location.
  1090. *
  1091. *
  1092. * @param fromX X coordinate of the initial touch, in screen coordinates
  1093. * @param toX X coordinate of the drag destination, in screen coordinates
  1094. * @param fromY Y coordinate of the initial touch, in screen coordinates
  1095. * @param toY Y coordinate of the drag destination, in screen coordinates
  1096. * @param stepCount how many move steps to include in the drag. Less steps results in a faster drag
  1097. */
  1098. public void drag(float fromX, float toX, float fromY, float toY,
  1099. int stepCount) {
  1100. dialogUtils.hideSoftKeyboard(null, false, true);
  1101. scroller.drag(fromX, toX, fromY, toY, stepCount);
  1102. }
  1103. /**
  1104. * Scrolls down the screen.
  1105. *
  1106. * @return {@code true} if more scrolling can be performed and {@code false} if it is at the end of
  1107. * the screen
  1108. */
  1109. @SuppressWarnings("unchecked")
  1110. public boolean scrollDown() {
  1111. waiter.waitForViews(true, AbsListView.class, ScrollView.class, WebView.class);
  1112. return scroller.scroll(Scroller.DOWN);
  1113. }
  1114. /**
  1115. * Scrolls to the bottom of the screen.
  1116. */
  1117. @SuppressWarnings("unchecked")
  1118. public void scrollToBottom() {
  1119. waiter.waitForViews(true, AbsListView.class, ScrollView.class, WebView.class);
  1120. scroller.scroll(Scroller.DOWN, true);
  1121. }
  1122. /**
  1123. * Scrolls up the screen.
  1124. *
  1125. * @return {@code true} if more scrolling can be performed and {@code false} if it is at the top of
  1126. * the screen
  1127. */
  1128. @SuppressWarnings("unchecked")
  1129. public boolean scrollUp(){
  1130. waiter.waitForViews(true, AbsListView.class, ScrollView.class, WebView.class);
  1131. return scroller.scroll(Scroller.UP);
  1132. }
  1133. /**
  1134. * Scrolls to the top of the screen.
  1135. */
  1136. @SuppressWarnings("unchecked")
  1137. public void scrollToTop() {
  1138. waiter.waitForViews(true, AbsListView.class, ScrollView.class, WebView.class);
  1139. scroller.scroll(Scroller.UP, true);
  1140. }
  1141. /**
  1142. * Scrolls down the specified AbsListView.
  1143. *
  1144. * @param list the {@link AbsListView} to scroll
  1145. * @return {@code true} if more scrolling can be performed
  1146. */
  1147. public boolean scrollDownList(AbsListView list) {
  1148. return scroller.scrollList(list, Scroller.DOWN, false);
  1149. }
  1150. /**
  1151. * Scrolls to the bottom of the specified AbsListView.
  1152. *
  1153. * @param list the {@link AbsListView} to scroll
  1154. * @return {@code true} if more scrolling can be performed
  1155. */
  1156. public boolean scrollListToBottom(AbsListView list) {
  1157. return scroller.scrollList(list, Scroller.DOWN, true);
  1158. }
  1159. /**
  1160. * Scrolls up the specified AbsListView.
  1161. *
  1162. * @param list the {@link AbsListView} to scroll
  1163. * @return {@code true} if more scrolling can be performed
  1164. */
  1165. public boolean scrollUpList(AbsListView list) {
  1166. return scroller.scrollList(list, Scroller.UP, false);
  1167. }
  1168. /**
  1169. * Scrolls to the top of the specified AbsListView.
  1170. *
  1171. * @param list the {@link AbsListView} to scroll
  1172. * @return {@code true} if more scrolling can be performed
  1173. */
  1174. public boolean scrollListToTop(AbsListView list) {
  1175. return scroller.scrollList(list, Scroller.UP, true);
  1176. }
  1177. /**
  1178. * Scrolls down a ListView matching the specified index.
  1179. *
  1180. * @param index the index of the {@link ListView} to scroll. {@code 0} if only one list is available
  1181. * @return {@code true} if more scrolling can be performed
  1182. */
  1183. public boolean scrollDownList(int index) {
  1184. return scroller.scrollList(waiter.waitForAndGetView(index, ListView.class), Scroller.DOWN, false);
  1185. }
  1186. /**
  1187. * Scrolls a ListView matching the specified index to the bottom.
  1188. *
  1189. * @param index the index of the {@link ListView} to scroll. {@code 0} if only one list is available
  1190. * @return {@code true} if more scrolling can be performed
  1191. */
  1192. public boolean scrollListToBottom(int index) {
  1193. return scroller.scrollList(waiter.waitForAndGetView(index, ListView.class), Scroller.DOWN, true);
  1194. }
  1195. /**
  1196. * Scrolls up a ListView matching the specified index.
  1197. *
  1198. * @param index the index of the {@link ListView} to scroll. {@code 0} if only one list is available
  1199. * @return {@code true} if more scrolling can be performed
  1200. */
  1201. public boolean scrollUpList(int index) {
  1202. return scroller.scrollList(waiter.waitForAndGetView(index, ListView.class), Scroller.UP, false);
  1203. }
  1204. /**
  1205. * Scrolls a ListView matching the specified index to the top.
  1206. *
  1207. * @param index the index of the {@link ListView} to scroll. {@code 0} if only one list is available
  1208. * @return {@code true} if more scrolling can be performed
  1209. */
  1210. public boolean scrollListToTop(int index) {
  1211. return scroller.scrollList(waiter.waitForAndGetView(index, ListView.class), Scroller.UP, true);
  1212. }
  1213. /**
  1214. * Scroll the specified AbsListView to the specified line.
  1215. *
  1216. * @param absListView the {@link AbsListView} to scroll
  1217. * @param line the line to scroll to
  1218. */
  1219. public void scrollListToLine(AbsListView absListView, int line){
  1220. scroller.scrollListToLine(absListView, line);
  1221. }
  1222. /**
  1223. * Scroll a AbsListView matching the specified index to the specified line.
  1224. *
  1225. * @param index the index of the {@link AbsListView} to scroll
  1226. * @param line the line to scroll to
  1227. */
  1228. public void scrollListToLine(int index, int line){
  1229. scroller.scrollListToLine(waiter.waitForAndGetView(index, AbsListView.class), line);
  1230. }
  1231. /**
  1232. * Scrolls horizontally.
  1233. *
  1234. * @param side the side to scroll; {@link #RIGHT} or {@link #LEFT}
  1235. * @param scrollPosition the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
  1236. * @param stepCount how many move steps to include in the scroll. Less steps results in a faster scroll
  1237. */
  1238. public void scrollToSide(int side, float scrollPosition, int stepCount) {
  1239. switch (side){
  1240. case RIGHT: scroller.scrollToSide(Scroller.Side.RIGHT, scrollPosition, stepCount); break;
  1241. case LEFT: scroller.scrollToSide(Scroller.Side.LEFT, scrollPosition, stepCount); break;
  1242. }
  1243. }
  1244. /**
  1245. * Scrolls horizontally.
  1246. *
  1247. * @param side the side to scroll; {@link #RIGHT} or {@link #LEFT}
  1248. * @param scrollPosition the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
  1249. */
  1250. public void scrollToSide(int side, float scrollPosition) {
  1251. scrollToSide(side, scrollPosition, 20);
  1252. }
  1253. /**
  1254. * Scrolls horizontally.
  1255. *
  1256. * @param side the side to scroll; {@link #RIGHT} or {@link #LEFT}
  1257. */
  1258. public void scrollToSide(int side) {
  1259. scrollToSide(side, 0.75F);
  1260. }
  1261. /**
  1262. * Scrolls a View horizontally.
  1263. *
  1264. * @param view the View to scroll
  1265. * @param side the side to scroll; {@link #RIGHT} or {@link #LEFT}
  1266. * @param scrollPosition the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
  1267. * @param stepCount how many move steps to include in the scroll. Less steps results in a faster scroll
  1268. */
  1269. public void scrollViewToSide(View view, int side, float scrollPosition, int stepCount) {
  1270. waitForView(view);
  1271. sleeper.sleep();
  1272. switch (side){
  1273. case RIGHT: scroller.scrollViewToSide(view, Scroller.Side.RIGHT, scrollPosition, stepCount); break;
  1274. case LEFT: scroller.scrollViewToSide(view, Scroller.Side.LEFT, scrollPosition, stepCount); break;
  1275. }
  1276. }
  1277. /**
  1278. * Scrolls a View horizontally.
  1279. *
  1280. * @param view the View to scroll
  1281. * @param side the side to scroll; {@link #RIGHT} or {@link #LEFT}
  1282. * @param scrollPosition the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
  1283. */
  1284. public void scrollViewToSide(View view, int side, float scrollPosition) {
  1285. scrollViewToSide(view, side, scrollPosition, 20);
  1286. }
  1287. /**
  1288. * Scrolls a View horizontally.
  1289. *
  1290. * @param view the View to scroll
  1291. * @param side the side to scroll; {@link #RIGHT} or {@link #LEFT}
  1292. */
  1293. public void scrollViewToSide(View view, int side) {
  1294. scrollViewToSide(view, side, 0.70F);
  1295. }
  1296. /**
  1297. * Zooms in or out if startPoint1 and startPoint2 are larger or smaller then endPoint1 and endPoint2. Requires API level >= 14.
  1298. *
  1299. * @param startPoint1 First "finger" down on the screen
  1300. * @param startPoint2 Second "finger" down on the screen
  1301. * @param endPoint1 Corresponding ending point of startPoint1
  1302. * @param endPoint2 Corresponding ending point of startPoint2
  1303. */
  1304. public void pinchToZoom(PointF startPoint1, PointF startPoint2, PointF endPoint1, PointF endPoint2)
  1305. {
  1306. if (android.os.Build.VERSION.SDK_INT < 14){
  1307. throw new RuntimeException("pinchToZoom() requires API level >= 14");
  1308. }
  1309. zoomer.generateZoomGesture(startPoint1, startPoint2, endPoint1, endPoint2);
  1310. }
  1311. /**
  1312. * Swipes with two fingers in a linear path determined by starting and ending points. Requires API level >= 14.
  1313. *
  1314. * @param startPoint1 First "finger" down on the screen
  1315. * @param startPoint2 Second "finger" down on the screen
  1316. * @param endPoint1 Corresponding ending point of startPoint1
  1317. * @param endPoint2 Corresponding ending point of startPoint2
  1318. */
  1319. public void swipe(PointF startPoint1, PointF startPoint2, PointF endPoint1, PointF endPoint2)
  1320. {
  1321. if (android.os.Build.VERSION.SDK_INT < 14){
  1322. throw new RuntimeException("swipe() requires API level >= 14");
  1323. }
  1324. swiper.generateSwipeGesture(startPoint1, startPoint2, endPoint1,
  1325. endPoint2);
  1326. }
  1327. /**
  1328. * Draws two semi-circles at the specified centers. Both circles are larger than rotateSmall(). Requires API level >= 14.
  1329. *
  1330. * @param center1 Center of semi-circle drawn from [0, Pi]
  1331. * @param center2 Center of semi-circle drawn from [Pi, 3*Pi]
  1332. */
  1333. public void rotateLarge(PointF center1, PointF center2)
  1334. {
  1335. if (android.os.Build.VERSION.SDK_INT < 14){
  1336. throw new RuntimeException("rotateLarge(PointF center1, PointF center2) requires API level >= 14");
  1337. }
  1338. rotator.generateRotateGesture(Rotator.LARGE, center1, center2);
  1339. }
  1340. /**
  1341. * Draws two semi-circles at the specified centers. Both circles are smaller than rotateLarge(). Requires API level >= 14.
  1342. *
  1343. * @param center1 Center of semi-circle drawn from [0, Pi]
  1344. * @param center2 Center of semi-circle drawn from [Pi, 3*Pi]
  1345. */
  1346. public void rotateSmall(PointF center1, PointF center2)
  1347. {
  1348. if (android.os.Build.VERSION.SDK_INT < 14){
  1349. throw new RuntimeException("rotateSmall(PointF center1, PointF center2) requires API level >= 14");
  1350. }
  1351. rotator.generateRotateGesture(Rotator.SMALL, center1, center2);
  1352. }
  1353. /**
  1354. * Sets the date in a DatePicker matching the specified index.
  1355. *
  1356. * @param index the index of the {@link DatePicker}. {@code 0} if only one is available
  1357. * @param year the year e.g. 2011
  1358. * @param monthOfYear the month which starts from zero e.g. 0 for January
  1359. * @param dayOfMonth the day e.g. 10
  1360. */
  1361. public void setDatePicker(int index, int year, int monthOfYear, int dayOfMonth) {
  1362. setDatePicker(waiter.waitForAndGetView(index, DatePicker.class), year, monthOfYear, dayOfMonth);
  1363. }
  1364. /**
  1365. * Sets the date in the specified DatePicker.
  1366. *
  1367. * @param datePicker the {@link DatePicker} object
  1368. * @param year the year e.g. 2011
  1369. * @param monthOfYear the month which starts from zero e.g. 03 for April
  1370. * @param dayOfMonth the day e.g. 10
  1371. */
  1372. public void setDatePicker(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
  1373. datePicker = (DatePicker) waiter.waitForView(datePicker, Timeout.getSmallTimeout());
  1374. setter.setDatePicker(datePicker, year, monthOfYear, dayOfMonth);
  1375. }
  1376. /**
  1377. * Sets the time in a TimePicker matching the specified index.
  1378. *
  1379. * @param index the index of the {@link TimePicker}. {@code 0} if only one is available
  1380. * @param hour the hour e.g. 15
  1381. * @param minute the minute e.g. 30
  1382. */
  1383. public void setTimePicker(int index, int hour, int minute) {
  1384. setTimePicker(waiter.waitForAndGetView(index, TimePicker.class), hour, minute);
  1385. }
  1386. /**
  1387. * Sets the time in the specified TimePicker.
  1388. *
  1389. * @param timePicker the {@link TimePicker} object
  1390. * @param hour the hour e.g. 15
  1391. * @param minute the minute e.g. 30
  1392. */
  1393. public void setTimePicker(TimePicker timePicker, int hour, int minute) {
  1394. timePicker = (TimePicker) waiter.waitForView(timePicker, Timeout.getSmallTimeout());
  1395. setter.setTimePicker(timePicker, hour, minute);
  1396. }
  1397. /**
  1398. * Sets the progress of a ProgressBar matching the specified index. Examples of ProgressBars are: {@link android.widget.SeekBar} and {@link android.widget.RatingBar}.
  1399. *
  1400. * @param index the index of the {@link ProgressBar}
  1401. * @param progress the progress to set the {@link ProgressBar}
  1402. */
  1403. public void setProgressBar(int index, int progress){
  1404. setProgressBar(waiter.waitForAndGetView(index, ProgressBar.class), progress);
  1405. }
  1406. /**
  1407. * Sets the progress of the specified ProgressBar. Examples of ProgressBars are: {@link android.widget.SeekBar} and {@link android.widget.RatingBar}.
  1408. *
  1409. * @param progressBar the {@link ProgressBar}
  1410. * @param progress the progress to set the {@link ProgressBar}
  1411. */
  1412. public void setProgressBar(ProgressBar progressBar, int progress){
  1413. progressBar = (ProgressBar) waiter.waitForView(progressBar, Timeout.getSmallTimeout());
  1414. setter.setProgressBar(progressBar, progress);
  1415. }
  1416. /**
  1417. * Sets the status of the NavigationDrawer. Examples of status are: {@code Solo.CLOSED} and {@code Solo.OPENED}.
  1418. *
  1419. * @param status the status that the {@link NavigationDrawer} should be set to
  1420. */
  1421. public void setNavigationDrawer(final int status){
  1422. setter.setNavigationDrawer(status);
  1423. }
  1424. /**
  1425. * Sets the status of a SlidingDrawer matching the specified index. Examples of status are: {@code Solo.CLOSED} and {@code Solo.OPENED}.
  1426. *
  1427. * @param index the index of the {@link SlidingDrawer}
  1428. * @param status the status to set the {@link SlidingDrawer}
  1429. */
  1430. public void setSlidingDrawer(int index, int status){
  1431. setSlidingDrawer(waiter.waitForAndGetView(index, SlidingDrawer.class), status);
  1432. }
  1433. /**
  1434. * Sets the status of the specified SlidingDrawer. Examples of status are: {@code Solo.CLOSED} and {@code Solo.OPENED}.
  1435. *
  1436. * @param slidingDrawer the {@link SlidingDrawer}
  1437. * @param status the status to set the {@link SlidingDrawer}
  1438. */
  1439. @SuppressWarnings("deprecation")
  1440. public void setSlidingDrawer(SlidingDrawer slidingDrawer, int status){
  1441. slidingDrawer = (SlidingDrawer) waiter.waitForView(slidingDrawer, Timeout.getSmallTimeout());
  1442. setter.setSlidingDrawer(slidingDrawer, status);
  1443. }
  1444. /**
  1445. * Enters text in an EditText matching the specified index.
  1446. *
  1447. * @param index the index of the {@link EditText}. {@code 0} if only one is available
  1448. * @param text the text to enter in the {@link EditText} field
  1449. */
  1450. public void enterText(int index, String text) {
  1451. textEnterer.setEditText(waiter.waitForAndGetView(index, EditText.class), text);
  1452. }
  1453. /**
  1454. * Enters text in the specified EditText.
  1455. *
  1456. * @param editText the {@link EditText} to enter text in
  1457. * @param text the text to enter in the {@link EditText} field
  1458. */
  1459. public void enterText(EditText editText, String text) {
  1460. editText = (EditText) waiter.waitForView(editText, Timeout.getSmallTimeout());
  1461. textEnterer.setEditText(editText, text);
  1462. }
  1463. /**
  1464. * Enters text in a WebElement matching the specified By object.
  1465. *
  1466. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1467. * @param text the text to enter in the {@link WebElement} field
  1468. */
  1469. public void enterTextInWebElement(By by, String text){
  1470. if(waiter.waitForWebElement(by, 0, Timeout.getSmallTimeout(), false) == null) {
  1471. Assert.fail("WebElement with " + webUtils.splitNameByUpperCase(by.getClass().getSimpleName()) + ": '" + by.getValue() + "' is not found!");
  1472. }
  1473. webUtils.enterTextIntoWebElement(by, text);
  1474. }
  1475. /**
  1476. * Types text in an EditText matching the specified index.
  1477. *
  1478. * @param index the index of the {@link EditText}. {@code 0} if only one is available
  1479. * @param text the text to type in the {@link EditText} field
  1480. */
  1481. public void typeText(int index, String text) {
  1482. textEnterer.typeText(waiter.waitForAndGetView(index, EditText.class), text);
  1483. }
  1484. /**
  1485. * Types text in the specified EditText.
  1486. *
  1487. * @param editText the {@link EditText} to type text in
  1488. * @param text the text to type in the {@link EditText} field
  1489. */
  1490. public void typeText(EditText editText, String text) {
  1491. editText = (EditText) waiter.waitForView(editText, Timeout.getSmallTimeout());
  1492. textEnterer.typeText(editText, text);
  1493. }
  1494. /**
  1495. * Types text in a WebElement matching the specified By object.
  1496. *
  1497. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1498. * @param text the text to enter in the {@link WebElement} field
  1499. */
  1500. public void typeTextInWebElement(By by, String text){
  1501. typeTextInWebElement(by, text, 0);
  1502. }
  1503. /**
  1504. * Types text in a WebElement matching the specified By object.
  1505. *
  1506. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1507. * @param text the text to enter in the {@link WebElement} field
  1508. * @param match if multiple objects match, this determines which one will be typed in
  1509. */
  1510. public void typeTextInWebElement(By by, String text, int match){
  1511. clicker.clickOnWebElement(by, match, true, false);
  1512. dialogUtils.hideSoftKeyboard(null, true, true);
  1513. instrumentation.sendStringSync(text);
  1514. }
  1515. /**
  1516. * Types text in the specified WebElement.
  1517. *
  1518. * @param webElement the WebElement to type text in
  1519. * @param text the text to enter in the {@link WebElement} field
  1520. */
  1521. public void typeTextInWebElement(WebElement webElement, String text){
  1522. clickOnWebElement(webElement);
  1523. dialogUtils.hideSoftKeyboard(null, true, true);
  1524. instrumentation.sendStringSync(text);
  1525. }
  1526. /**
  1527. * Clears the value of an EditText.
  1528. *
  1529. * @param index the index of the {@link EditText} to clear. 0 if only one is available
  1530. */
  1531. public void clearEditText(int index) {
  1532. textEnterer.setEditText(waiter.waitForAndGetView(index, EditText.class), "");
  1533. }
  1534. /**
  1535. * Clears the value of an EditText.
  1536. *
  1537. * @param editText the {@link EditText} to clear
  1538. */
  1539. public void clearEditText(EditText editText) {
  1540. editText = (EditText) waiter.waitForView(editText, Timeout.getSmallTimeout());
  1541. textEnterer.setEditText(editText, "");
  1542. }
  1543. /**
  1544. * Clears text in a WebElement matching the specified By object.
  1545. *
  1546. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1547. */
  1548. public void clearTextInWebElement(By by){
  1549. webUtils.enterTextIntoWebElement(by, "");
  1550. }
  1551. /**
  1552. * Clicks an ImageView matching the specified index.
  1553. *
  1554. * @param index the index of the {@link ImageView} to click. {@code 0} if only one is available
  1555. */
  1556. public void clickOnImage(int index) {
  1557. clicker.clickOn(ImageView.class, index);
  1558. }
  1559. /**
  1560. * Returns an EditText matching the specified index.
  1561. *
  1562. * @param index the index of the {@link EditText}. {@code 0} if only one is available
  1563. * @return an {@link EditText} matching the specified index
  1564. */
  1565. public EditText getEditText(int index) {
  1566. return getter.getView(EditText.class, index);
  1567. }
  1568. /**
  1569. * Returns a Button matching the specified index.
  1570. *
  1571. * @param index the index of the {@link Button}. {@code 0} if only one is available
  1572. * @return a {@link Button} matching the specified index
  1573. */
  1574. public Button getButton(int index) {
  1575. return getter.getView(Button.class, index);
  1576. }
  1577. /**
  1578. * Returns a TextView matching the specified index.
  1579. *
  1580. * @param index the index of the {@link TextView}. {@code 0} if only one is available
  1581. * @return a {@link TextView} matching the specified index
  1582. */
  1583. public TextView getText(int index) {
  1584. return getter.getView(TextView.class, index);
  1585. }
  1586. /**
  1587. * Returns an ImageView matching the specified index.
  1588. *
  1589. * @param index the index of the {@link ImageView}. {@code 0} if only one is available
  1590. * @return an {@link ImageView} matching the specified index
  1591. */
  1592. public ImageView getImage(int index) {
  1593. return getter.getView(ImageView.class, index);
  1594. }
  1595. /**
  1596. * Returns an ImageButton matching the specified index.
  1597. *
  1598. * @param index the index of the {@link ImageButton}. {@code 0} if only one is available
  1599. * @return the {@link ImageButton} matching the specified index
  1600. */
  1601. public ImageButton getImageButton(int index) {
  1602. return getter.getView(ImageButton.class, index);
  1603. }
  1604. /**
  1605. * Returns a TextView displaying the specified text.
  1606. *
  1607. * @param text the text that is displayed, specified as a regular expression
  1608. * @return the {@link TextView} displaying the specified text
  1609. */
  1610. public TextView getText(String text)
  1611. {
  1612. return getter.getView(TextView.class, text, false);
  1613. }
  1614. /**
  1615. * Returns a TextView displaying the specified text.
  1616. *
  1617. * @param text the text that is displayed, specified as a regular expression
  1618. * @param onlyVisible {@code true} if only visible texts on the screen should be returned
  1619. * @return the {@link TextView} displaying the specified text
  1620. */
  1621. public TextView getText(String text, boolean onlyVisible)
  1622. {
  1623. return getter.getView(TextView.class, text, onlyVisible);
  1624. }
  1625. /**
  1626. * Returns a Button displaying the specified text.
  1627. *
  1628. * @param text the text that is displayed, specified as a regular expression
  1629. * @return the {@link Button} displaying the specified text
  1630. */
  1631. public Button getButton(String text)
  1632. {
  1633. return getter.getView(Button.class, text, false);
  1634. }
  1635. /**
  1636. * Returns a Button displaying the specified text.
  1637. *
  1638. * @param text the text that is displayed, specified as a regular expression
  1639. * @param onlyVisible {@code true} if only visible buttons on the screen should be returned
  1640. * @return the {@link Button} displaying the specified text
  1641. */
  1642. public Button getButton(String text, boolean onlyVisible)
  1643. {
  1644. return getter.getView(Button.class, text, onlyVisible);
  1645. }
  1646. /**
  1647. * Returns an EditText displaying the specified text.
  1648. *
  1649. * @param text the text that is displayed, specified as a regular expression
  1650. * @return the {@link EditText} displaying the specified text
  1651. */
  1652. public EditText getEditText(String text)
  1653. {
  1654. return getter.getView(EditText.class, text, false);
  1655. }
  1656. /**
  1657. * Returns an EditText displaying the specified text.
  1658. *
  1659. * @param text the text that is displayed, specified as a regular expression
  1660. * @param onlyVisible {@code true} if only visible EditTexts on the screen should be returned
  1661. * @return the {@link EditText} displaying the specified text
  1662. */
  1663. public EditText getEditText(String text, boolean onlyVisible)
  1664. {
  1665. return getter.getView(EditText.class, text, onlyVisible);
  1666. }
  1667. /**
  1668. * Returns a View matching the specified resource id.
  1669. *
  1670. * @param id the R.id of the {@link View} to return
  1671. * @return a {@link View} matching the specified id
  1672. */
  1673. public View getView(int id){
  1674. return getView(id, 0);
  1675. }
  1676. /**
  1677. * Returns a View matching the specified resource id and index.
  1678. *
  1679. * @param id the R.id of the {@link View} to return
  1680. * @param index the index of the {@link View}. {@code 0} if only one is available
  1681. * @return a {@link View} matching the specified id and index
  1682. */
  1683. public View getView(int id, int index){
  1684. View viewToReturn = getter.getView(id, index);
  1685. if(viewToReturn == null) {
  1686. int match = index + 1;
  1687. if(match > 1){
  1688. Assert.fail(match + " Views with id: '" + id + "' are not found!");
  1689. }
  1690. else {
  1691. Assert.fail("View with id: '" + id + "' is not found!");
  1692. }
  1693. }
  1694. return viewToReturn;
  1695. }
  1696. /**
  1697. * Returns a View matching the specified resource id.
  1698. *
  1699. * @param id the id of the {@link View} to return
  1700. * @return a {@link View} matching the specified id
  1701. */
  1702. public View getView(String id){
  1703. return getView(id, 0);
  1704. }
  1705. /**
  1706. * Returns a View matching the specified resource id and index.
  1707. *
  1708. * @param id the id of the {@link View} to return
  1709. * @param index the index of the {@link View}. {@code 0} if only one is available
  1710. * @return a {@link View} matching the specified id and index
  1711. */
  1712. public View getView(String id, int index){
  1713. View viewToReturn = getter.getView(id, index);
  1714. if(viewToReturn == null) {
  1715. int match = index + 1;
  1716. if(match > 1){
  1717. Assert.fail(match + " Views with id: '" + id + "' are not found!");
  1718. }
  1719. else {
  1720. Assert.fail("View with id: '" + id + "' is not found!");
  1721. }
  1722. }
  1723. return viewToReturn;
  1724. }
  1725. /**
  1726. * Returns a View matching the specified class and index.
  1727. *
  1728. * @param viewClass the class of the requested view
  1729. * @param index the index of the {@link View}. {@code 0} if only one is available
  1730. * @return a {@link View} matching the specified class and index
  1731. */
  1732. public <T extends View> T getView(Class<T> viewClass, int index){
  1733. return waiter.waitForAndGetView(index, viewClass);
  1734. }
  1735. /**
  1736. * Returns a WebElement matching the specified By object and index.
  1737. *
  1738. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1739. * @param index the index of the {@link WebElement}. {@code 0} if only one is available
  1740. * @return a {@link WebElement} matching the specified index
  1741. */
  1742. public WebElement getWebElement(By by, int index){
  1743. int match = index + 1;
  1744. WebElement webElement = waiter.waitForWebElement(by, match, Timeout.getSmallTimeout(), true);
  1745. if(webElement == null) {
  1746. if(match > 1){
  1747. Assert.fail(match + " WebElements with " + webUtils.splitNameByUpperCase(by.getClass().getSimpleName()) + ": '" + by.getValue() + "' are not found!");
  1748. }
  1749. else {
  1750. Assert.fail("WebElement with " + webUtils.splitNameByUpperCase(by.getClass().getSimpleName()) + ": '" + by.getValue() + "' is not found!");
  1751. }
  1752. }
  1753. return webElement;
  1754. }
  1755. /**
  1756. * Returns the current web page URL.
  1757. *
  1758. * @return the current web page URL
  1759. */
  1760. public String getWebUrl() {
  1761. final WebView webView = waiter.waitForAndGetView(0, WebView.class);
  1762. if(webView == null)
  1763. Assert.fail("WebView is not found!");
  1764. instrumentation.runOnMainSync(new Runnable() {
  1765. public void run() {
  1766. webUrl = webView.getUrl();
  1767. }
  1768. });
  1769. return webUrl;
  1770. }
  1771. /**
  1772. * Returns an ArrayList of the Views currently displayed in the focused Activity or Dialog.
  1773. *
  1774. * @return an {@code ArrayList} of the {@link View} objects currently displayed in the
  1775. * focused window
  1776. */
  1777. public ArrayList<View> getCurrentViews() {
  1778. return viewFetcher.getViews(null, true);
  1779. }
  1780. /**
  1781. * Returns an ArrayList of Views matching the specified class located in the focused Activity or Dialog.
  1782. *
  1783. * @param classToFilterBy return all instances of this class. Examples are: {@code Button.class} or {@code ListView.class}
  1784. * @return an {@code ArrayList} of {@code View}s matching the specified {@code Class} located in the current {@code Activity}
  1785. */
  1786. public <T extends View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy) {
  1787. return viewFetcher.getCurrentViews(classToFilterBy, true);
  1788. }
  1789. /**
  1790. * Returns an ArrayList of Views matching the specified class located in the focused Activity or Dialog.
  1791. *
  1792. * @param classToFilterBy return all instances of this class. Examples are: {@code Button.class} or {@code ListView.class}
  1793. * @param includeSubclasses include instances of the subclasses in the {@code ArrayList} that will be returned
  1794. * @return an {@code ArrayList} of {@code View}s matching the specified {@code Class} located in the current {@code Activity}
  1795. */
  1796. public <T extends View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy, boolean includeSubclasses) {
  1797. return viewFetcher.getCurrentViews(classToFilterBy, includeSubclasses);
  1798. }
  1799. /**
  1800. * Returns an ArrayList of Views matching the specified class located under the specified parent.
  1801. *
  1802. * @param classToFilterBy return all instances of this class. Examples are: {@code Button.class} or {@code ListView.class}
  1803. * @param parent the parent {@code View} for where to start the traversal
  1804. * @return an {@code ArrayList} of {@code View}s matching the specified {@code Class} located under the specified {@code parent}
  1805. */
  1806. public <T extends View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy, View parent) {
  1807. return viewFetcher.getCurrentViews(classToFilterBy, true, parent);
  1808. }
  1809. /**
  1810. * Returns an ArrayList of Views matching the specified class located under the specified parent.
  1811. *
  1812. * @param classToFilterBy return all instances of this class. Examples are: {@code Button.class} or {@code ListView.class}
  1813. * @param includeSubclass include instances of subclasses in {@code ArrayList} that will be returned
  1814. * @param parent the parent {@code View} for where to start the traversal
  1815. * @return an {@code ArrayList} of {@code View}s matching the specified {@code Class} located under the specified {@code parent}
  1816. */
  1817. public <T extends View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy, boolean includeSubclass, View parent) {
  1818. return viewFetcher.getCurrentViews(classToFilterBy, includeSubclass, parent);
  1819. }
  1820. /**
  1821. * Returns an ArrayList of all the WebElements displayed in the active WebView.
  1822. *
  1823. * @return an {@code ArrayList} of all the {@link WebElement} objects currently displayed in the active WebView
  1824. */
  1825. public ArrayList<WebElement> getWebElements(){
  1826. return webUtils.getWebElements(false);
  1827. }
  1828. /**
  1829. * Returns an ArrayList of all the WebElements displayed in the active WebView matching the specified By object.
  1830. *
  1831. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1832. * @return an {@code ArrayList} of all the {@link WebElement} objects displayed in the active WebView
  1833. */
  1834. public ArrayList<WebElement> getWebElements(By by){
  1835. return webUtils.getWebElements(by, false);
  1836. }
  1837. /**
  1838. * Returns an ArrayList of the currently displayed WebElements in the active WebView.
  1839. *
  1840. * @return an {@code ArrayList} of the {@link WebElement} objects displayed in the active WebView
  1841. */
  1842. public ArrayList<WebElement> getCurrentWebElements(){
  1843. return webUtils.getWebElements(true);
  1844. }
  1845. /**
  1846. * Returns an ArrayList of the currently displayed WebElements in the active WebView matching the specified By object.
  1847. *
  1848. * @param by the By object. Examples are: {@code By.id("id")} and {@code By.name("name")}
  1849. * @return an {@code ArrayList} of the {@link WebElement} objects currently displayed in the active WebView
  1850. */
  1851. public ArrayList<WebElement> getCurrentWebElements(By by){
  1852. return webUtils.getWebElements(by, true);
  1853. }
  1854. /**
  1855. * Checks if a RadioButton matching the specified index is checked.
  1856. *
  1857. * @param index of the {@link RadioButton} to check. {@code 0} if only one is available
  1858. * @return {@code true} if {@link RadioButton} is checked and {@code false} if it is not checked
  1859. */
  1860. public boolean isRadioButtonChecked(int index)
  1861. {
  1862. return checker.isButtonChecked(RadioButton.class, index);
  1863. }
  1864. /**
  1865. * Checks if a RadioButton displaying the specified text is checked.
  1866. *
  1867. * @param text the text that the {@link RadioButton} displays, specified as a regular expression
  1868. * @return {@code true} if a {@link RadioButton} matching the specified text is checked and {@code false} if it is not checked
  1869. */
  1870. public boolean isRadioButtonChecked(String text)
  1871. {
  1872. return checker.isButtonChecked(RadioButton.class, text);
  1873. }
  1874. /**
  1875. * Checks if a CheckBox matching the specified index is checked.
  1876. *
  1877. * @param index of the {@link CheckBox} to check. {@code 0} if only one is available
  1878. * @return {@code true} if {@link CheckBox} is checked and {@code false} if it is not checked
  1879. */
  1880. public boolean isCheckBoxChecked(int index)
  1881. {
  1882. return checker.isButtonChecked(CheckBox.class, index);
  1883. }
  1884. /**
  1885. * Checks if a ToggleButton displaying the specified text is checked.
  1886. *
  1887. * @param text the text that the {@link ToggleButton} displays, specified as a regular expression
  1888. * @return {@code true} if a {@link ToggleButton} matching the specified text is checked and {@code false} if it is not checked
  1889. */
  1890. public boolean isToggleButtonChecked(String text)
  1891. {
  1892. return checker.isButtonChecked(ToggleButton.class, text);
  1893. }
  1894. /**
  1895. * Checks if a ToggleButton matching the specified index is checked.
  1896. *
  1897. * @param index of the {@link ToggleButton} to check. {@code 0} if only one is available
  1898. * @return {@code true} if {@link ToggleButton} is checked and {@code false} if it is not checked
  1899. */
  1900. public boolean isToggleButtonChecked(int index)
  1901. {
  1902. return checker.isButtonChecked(ToggleButton.class, index);
  1903. }
  1904. /**
  1905. * Checks if a CheckBox displaying the specified text is checked.
  1906. *
  1907. * @param text the text that the {@link CheckBox} displays, specified as a regular expression
  1908. * @return {@code true} if a {@link CheckBox} displaying the specified text is checked and {@code false} if it is not checked
  1909. */
  1910. public boolean isCheckBoxChecked(String text)
  1911. {
  1912. return checker.isButtonChecked(CheckBox.class, text);
  1913. }
  1914. /**
  1915. * Checks if the specified text is checked.
  1916. *
  1917. * @param text the text that the {@link CheckedTextView} or {@link CompoundButton} objects display, specified as a regular expression
  1918. * @return {@code true} if the specified text is checked and {@code false} if it is not checked
  1919. */
  1920. @SuppressWarnings("unchecked")
  1921. public boolean isTextChecked(String text){
  1922. waiter.waitForViews(false, CheckedTextView.class, CompoundButton.class);
  1923. if(viewFetcher.getCurrentViews(CheckedTextView.class, true).size() > 0 && checker.isCheckedTextChecked(text))
  1924. return true;
  1925. if(viewFetcher.getCurrentViews(CompoundButton.class, true).size() > 0 && checker.isButtonChecked(CompoundButton.class, text))
  1926. return true;
  1927. return false;
  1928. }
  1929. /**
  1930. * Checks if the specified text is selected in any Spinner located in the current screen.
  1931. *
  1932. * @param text the text that is expected to be selected, specified as a regular expression
  1933. * @return {@code true} if the specified text is selected in any {@link Spinner} and false if it is not
  1934. */
  1935. public boolean isSpinnerTextSelected(String text)
  1936. {
  1937. return checker.isSpinnerTextSelected(text);
  1938. }
  1939. /**
  1940. * Checks if the specified text is selected in a Spinner matching the specified index.
  1941. *
  1942. * @param index the index of the spinner to check. {@code 0} if only one spinner is available
  1943. * @param text the text that is expected to be selected, specified as a regular expression
  1944. * @return {@code true} if the specified text is selected in the specified {@link Spinner} and false if it is not
  1945. */
  1946. public boolean isSpinnerTextSelected(int index, String text)
  1947. {
  1948. return checker.isSpinnerTextSelected(index, text);
  1949. }
  1950. /**
  1951. * Hides the soft keyboard.
  1952. */
  1953. public void hideSoftKeyboard() {
  1954. dialogUtils.hideSoftKeyboard(null, true, false);
  1955. }
  1956. /**
  1957. * Unlocks the screen.
  1958. */
  1959. public void unlockScreen(){
  1960. instrumentation.runOnMainSync(new Runnable() {
  1961. @Override
  1962. public void run() {
  1963. activityUtils.getCurrentActivity(false).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  1964. }
  1965. });
  1966. }
  1967. /**
  1968. * Sends a key: Right, Left, Up, Down, Enter, Menu or Delete.
  1969. *
  1970. * @param key the key to be sent. Use {@code Solo.}{@link #RIGHT}, {@link #LEFT}, {@link #UP}, {@link #DOWN},
  1971. * {@link #ENTER}, {@link #MENU}, {@link #DELETE}
  1972. */
  1973. public void sendKey(int key)
  1974. {
  1975. sender.sendKeyCode(key);
  1976. }
  1977. /**
  1978. * Returns to an Activity matching the specified name.
  1979. *
  1980. * @param name the name of the {@link Activity} to return to. Example is: {@code "MyActivity"}
  1981. */
  1982. public void goBackToActivity(String name) {
  1983. activityUtils.goBackToActivity(name);
  1984. }
  1985. /**
  1986. * Waits for an Activity matching the specified name. Default timeout is 20 seconds.
  1987. *
  1988. * @param name the name of the {@code Activity} to wait for. Example is: {@code "MyActivity"}
  1989. * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
  1990. */
  1991. public boolean waitForActivity(String name){
  1992. return waiter.waitForActivity(name, Timeout.getLargeTimeout());
  1993. }
  1994. /**
  1995. * Waits for an Activity matching the specified name.
  1996. *
  1997. * @param name the name of the {@link Activity} to wait for. Example is: {@code "MyActivity"}
  1998. * @param timeout the amount of time in milliseconds to wait
  1999. * @return {@code true} if {@link Activity} appears before the timeout and {@code false} if it does not
  2000. */
  2001. public boolean waitForActivity(String name, int timeout)
  2002. {
  2003. return waiter.waitForActivity(name, timeout);
  2004. }
  2005. /**
  2006. * Waits for an Activity matching the specified class. Default timeout is 20 seconds.
  2007. *
  2008. * @param activityClass the class of the {@code Activity} to wait for. Example is: {@code MyActivity.class}
  2009. * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
  2010. */
  2011. public boolean waitForActivity(Class<? extends Activity> activityClass){
  2012. return waiter.waitForActivity(activityClass, Timeout.getLargeTimeout());
  2013. }
  2014. /**
  2015. * Waits for an Activity matching the specified class.
  2016. *
  2017. * @param activityClass the class of the {@code Activity} to wait for. Example is: {@code MyActivity.class}
  2018. * @param timeout the amount of time in milliseconds to wait
  2019. * @return {@code true} if {@link Activity} appears before the timeout and {@code false} if it does not
  2020. */
  2021. public boolean waitForActivity(Class<? extends Activity> activityClass, int timeout)
  2022. {
  2023. return waiter.waitForActivity(activityClass, timeout);
  2024. }
  2025. /**
  2026. * Wait for the activity stack to be empty.
  2027. *
  2028. * @param timeout the amount of time in milliseconds to wait
  2029. * @return {@code true} if activity stack is empty before the timeout and {@code false} if it is not
  2030. */
  2031. public boolean waitForEmptyActivityStack(int timeout)
  2032. {
  2033. return waiter.waitForCondition(
  2034. new Condition(){
  2035. @Override
  2036. public boolean isSatisfied() {
  2037. return activityUtils.isActivityStackEmpty();
  2038. }
  2039. }, timeout);
  2040. }
  2041. /**
  2042. * Waits for a Fragment matching the specified tag. Default timeout is 20 seconds.
  2043. *
  2044. * @param tag the name of the tag
  2045. * @return {@code true} if fragment appears and {@code false} if it does not appear before the timeout
  2046. */
  2047. public boolean waitForFragmentByTag(String tag){
  2048. return waiter.waitForFragment(tag, 0, Timeout.getLargeTimeout());
  2049. }
  2050. /**
  2051. * Waits for a Fragment matching the specified tag.
  2052. *
  2053. * @param tag the name of the tag
  2054. * @param timeout the amount of time in milliseconds to wait
  2055. * @return {@code true} if fragment appears and {@code false} if it does not appear before the timeout
  2056. */
  2057. public boolean waitForFragmentByTag(String tag, int timeout){
  2058. return waiter.waitForFragment(tag, 0, timeout);
  2059. }
  2060. /**
  2061. * Waits for a Fragment matching the specified resource id. Default timeout is 20 seconds.
  2062. *
  2063. * @param id the R.id of the fragment
  2064. * @return {@code true} if fragment appears and {@code false} if it does not appear before the timeout
  2065. */
  2066. public boolean waitForFragmentById(int id){
  2067. return waiter.waitForFragment(null, id, Timeout.getLargeTimeout());
  2068. }
  2069. /**
  2070. * Waits for a Fragment matching the specified resource id.
  2071. *
  2072. * @param id the R.id of the fragment
  2073. * @param timeout the amount of time in milliseconds to wait
  2074. * @return {@code true} if fragment appears and {@code false} if it does not appear before the timeout
  2075. */
  2076. public boolean waitForFragmentById(int id, int timeout){
  2077. return waiter.waitForFragment(null, id, timeout);
  2078. }
  2079. /**
  2080. * Waits for the specified log message to appear. Default timeout is 20 seconds.
  2081. * Requires read logs permission (android.permission.READ_LOGS) in AndroidManifest.xml of the application under test.
  2082. *
  2083. * @param logMessage the log message to wait for
  2084. * @return {@code true} if log message appears and {@code false} if it does not appear before the timeout
  2085. *
  2086. * @see clearLog()
  2087. */
  2088. public boolean waitForLogMessage(String logMessage){
  2089. return waiter.waitForLogMessage(logMessage, Timeout.getLargeTimeout());
  2090. }
  2091. /**
  2092. * Waits for the specified log message to appear.
  2093. * Requires read logs permission (android.permission.READ_LOGS) in AndroidManifest.xml of the application under test.
  2094. *
  2095. * @param logMessage the log message to wait for
  2096. * @param timeout the amount of time in milliseconds to wait
  2097. * @return {@code true} if log message appears and {@code false} if it does not appear before the timeout
  2098. *
  2099. * @see clearLog()
  2100. */
  2101. public boolean waitForLogMessage(String logMessage, int timeout){
  2102. return waiter.waitForLogMessage(logMessage, timeout);
  2103. }
  2104. /**
  2105. * Clears the log.
  2106. */
  2107. public void clearLog(){
  2108. waiter.clearLog();
  2109. }
  2110. /**
  2111. * Returns a localized String matching the specified resource id.
  2112. *
  2113. * @param id the R.id of the String
  2114. * @return the localized String
  2115. */
  2116. public String getString(int id)
  2117. {
  2118. return getter.getString(id);
  2119. }
  2120. /**
  2121. * Returns a localized String matching the specified resource id.
  2122. *
  2123. * @param id the id of the String
  2124. * @return the localized String
  2125. */
  2126. public String getString(String id)
  2127. {
  2128. return getter.getString(id);
  2129. }
  2130. /**
  2131. * Robotium will sleep for the specified time.
  2132. *
  2133. * @param time the time in milliseconds that Robotium should sleep
  2134. */
  2135. public void sleep(int time)
  2136. {
  2137. sleeper.sleep(time);
  2138. }
  2139. /**
  2140. *
  2141. * Finalizes the Solo object and removes the ActivityMonitor.
  2142. *
  2143. * @see #finishOpenedActivities() finishOpenedActivities() to close the activities that have been active
  2144. */
  2145. public void finalize() throws Throwable {
  2146. activityUtils.finalize();
  2147. }
  2148. /**
  2149. * The Activities that are alive are finished. Usually used in tearDown().
  2150. */
  2151. public void finishOpenedActivities(){
  2152. activityUtils.finishOpenedActivities();
  2153. }
  2154. /**
  2155. * Takes a screenshot and saves it in the {@link Config} objects save path (default set to: /sdcard/Robotium-Screenshots/).
  2156. * Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
  2157. */
  2158. public void takeScreenshot(){
  2159. takeScreenshot(null);
  2160. }
  2161. /**
  2162. * Takes a screenshot and saves it with the specified name in the {@link Config} objects save path (default set to: /sdcard/Robotium-Screenshots/).
  2163. * Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
  2164. *
  2165. * @param name the name to give the screenshot
  2166. */
  2167. public void takeScreenshot(String name){
  2168. takeScreenshot(name, 100);
  2169. }
  2170. /**
  2171. * Takes a screenshot and saves the image with the specified name in the {@link Config} objects save path (default set to: /sdcard/Robotium-Screenshots/).
  2172. * Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
  2173. *
  2174. * @param name the name to give the screenshot
  2175. * @param quality the compression rate. From 0 (compress for lowest size) to 100 (compress for maximum quality)
  2176. */
  2177. public void takeScreenshot(String name, int quality){
  2178. screenshotTaker.takeScreenshot(name, quality);
  2179. }
  2180. /**
  2181. * Takes a screenshot sequence and saves the images with the specified name prefix in the {@link Config} objects save path (default set to: /sdcard/Robotium-Screenshots/).
  2182. *
  2183. * The name prefix is appended with "_" + sequence_number for each image in the sequence,
  2184. * where numbering starts at 0.
  2185. *
  2186. * Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
  2187. *
  2188. * At present multiple simultaneous screenshot sequences are not supported.
  2189. * This method will throw an exception if stopScreenshotSequence() has not been
  2190. * called to finish any prior sequences.
  2191. * Calling this method is equivalend to calling startScreenshotSequence(name, 80, 400, 100);
  2192. *
  2193. * @param name the name prefix to give the screenshot
  2194. */
  2195. public void startScreenshotSequence(String name) {
  2196. startScreenshotSequence(name,
  2197. 80, // quality
  2198. 400, // 400 ms frame delay
  2199. 100); // max frames
  2200. }
  2201. /**
  2202. * Takes a screenshot sequence and saves the images with the specified name prefix in the {@link Config} objects save path (default set to: /sdcard/Robotium-Screenshots/).
  2203. *
  2204. * The name prefix is appended with "_" + sequence_number for each image in the sequence,
  2205. * where numbering starts at 0.
  2206. *
  2207. * Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in the
  2208. * AndroidManifest.xml of the application under test.
  2209. *
  2210. * Taking a screenshot will take on the order of 40-100 milliseconds of time on the
  2211. * main UI thread. Therefore it is possible to mess up the timing of tests if
  2212. * the frameDelay value is set too small.
  2213. *
  2214. * At present multiple simultaneous screenshot sequences are not supported.
  2215. * This method will throw an exception if stopScreenshotSequence() has not been
  2216. * called to finish any prior sequences.
  2217. *
  2218. * @param name the name prefix to give the screenshot
  2219. * @param quality the compression rate. From 0 (compress for lowest size) to 100 (compress for maximum quality)
  2220. * @param frameDelay the time in milliseconds to wait between each frame
  2221. * @param maxFrames the maximum number of frames that will comprise this sequence
  2222. */
  2223. public void startScreenshotSequence(String name, int quality, int frameDelay, int maxFrames) {
  2224. screenshotTaker.startScreenshotSequence(name, quality, frameDelay, maxFrames);
  2225. }
  2226. /**
  2227. * Causes a screenshot sequence to end.
  2228. *
  2229. * If this method is not called to end a sequence and a prior sequence is still in
  2230. * progress, startScreenshotSequence() will throw an exception.
  2231. */
  2232. public void stopScreenshotSequence() {
  2233. screenshotTaker.stopScreenshotSequence();
  2234. }
  2235. /**
  2236. * Initialize timeout using 'adb shell setprop' or use setLargeTimeout() and setSmallTimeout(). Will fall back to the default values set by {@link Config}.
  2237. */
  2238. private void initialize(){
  2239. Timeout.setLargeTimeout(initializeTimeout("solo_large_timeout", config.timeout_large));
  2240. Timeout.setSmallTimeout(initializeTimeout("solo_small_timeout", config.timeout_small));
  2241. }
  2242. /**
  2243. * Parse a timeout value set using adb shell.
  2244. *
  2245. * There are two options to set the timeout. Set it using adb shell (requires root access):
  2246. * <br><br>
  2247. * 'adb shell setprop solo_large_timeout milliseconds'
  2248. * <br>
  2249. * 'adb shell setprop solo_small_timeout milliseconds'
  2250. * <br>
  2251. * Example: adb shell setprop solo_small_timeout 10000
  2252. * <br><br>
  2253. * Set the values directly using setLargeTimeout() and setSmallTimeout
  2254. *
  2255. * @param property name of the property to read the timeout from
  2256. * @param defaultValue default value for the timeout
  2257. * @return timeout in milliseconds
  2258. */
  2259. @SuppressWarnings({ "rawtypes", "unchecked" })
  2260. private static int initializeTimeout(String property, int defaultValue) {
  2261. try {
  2262. Class clazz = Class.forName("android.os.SystemProperties");
  2263. Method method = clazz.getDeclaredMethod("get", String.class);
  2264. String value = (String) method.invoke(null, property);
  2265. return Integer.parseInt(value);
  2266. } catch (Exception e) {
  2267. return defaultValue;
  2268. }
  2269. }
  2270. }