1 /* 2 * Copyright (C) 2013 DroidDriver committers 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.android.droiddriver.base; 18 19 import com.google.android.droiddriver.DroidDriver; 20 import com.google.android.droiddriver.Poller; 21 import com.google.android.droiddriver.UiElement; 22 import com.google.android.droiddriver.actions.InputInjector; 23 import com.google.android.droiddriver.exceptions.ElementNotFoundException; 24 import com.google.android.droiddriver.exceptions.TimeoutException; 25 import com.google.android.droiddriver.finders.ByXPath; 26 import com.google.android.droiddriver.finders.Finder; 27 import com.google.android.droiddriver.util.Logs; 28 29 /** 30 * Base DroidDriver that implements the common operations. 31 */ 32 public abstract class BaseDroidDriver<R, E extends BaseUiElement<R, E>> implements DroidDriver { 33 34 private Poller poller = new DefaultPoller(); 35 private E rootElement; 36 37 @Override find(Finder finder)38 public UiElement find(Finder finder) { 39 Logs.call(this, "find", finder); 40 return finder.find(getRootElement()); 41 } 42 43 @Override has(Finder finder)44 public boolean has(Finder finder) { 45 try { 46 refreshUiElementTree(); 47 find(finder); 48 return true; 49 } catch (ElementNotFoundException enfe) { 50 return false; 51 } 52 } 53 54 @Override has(Finder finder, long timeoutMillis)55 public boolean has(Finder finder, long timeoutMillis) { 56 try { 57 getPoller().pollFor(this, finder, Poller.EXISTS, timeoutMillis); 58 return true; 59 } catch (TimeoutException e) { 60 return false; 61 } 62 } 63 64 @Override on(Finder finder)65 public UiElement on(Finder finder) { 66 Logs.call(this, "on", finder); 67 return getPoller().pollFor(this, finder, Poller.EXISTS); 68 } 69 70 @Override checkExists(Finder finder)71 public void checkExists(Finder finder) { 72 Logs.call(this, "checkExists", finder); 73 getPoller().pollFor(this, finder, Poller.EXISTS); 74 } 75 76 @Override checkGone(Finder finder)77 public void checkGone(Finder finder) { 78 Logs.call(this, "checkGone", finder); 79 getPoller().pollFor(this, finder, Poller.GONE); 80 } 81 82 @Override getPoller()83 public Poller getPoller() { 84 return poller; 85 } 86 87 @Override setPoller(Poller poller)88 public void setPoller(Poller poller) { 89 this.poller = poller; 90 } 91 getInjector()92 public abstract InputInjector getInjector(); 93 newRootElement()94 protected abstract E newRootElement(); 95 96 /** 97 * Returns a new UiElement of type {@code E}. 98 */ newUiElement(R rawElement, E parent)99 protected abstract E newUiElement(R rawElement, E parent); 100 getRootElement()101 public E getRootElement() { 102 if (rootElement == null) { 103 refreshUiElementTree(); 104 } 105 return rootElement; 106 } 107 108 @Override refreshUiElementTree()109 public void refreshUiElementTree() { 110 rootElement = newRootElement(); 111 } 112 113 @Override dumpUiElementTree(String path)114 public boolean dumpUiElementTree(String path) { 115 Logs.call(this, "dumpUiElementTree", path); 116 return ByXPath.dumpDom(path, getRootElement()); 117 } 118 } 119