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 io.appium.droiddriver.base;
18 
19 import android.util.Log;
20 
21 import io.appium.droiddriver.DroidDriver;
22 import io.appium.droiddriver.Poller;
23 import io.appium.droiddriver.UiElement;
24 import io.appium.droiddriver.actions.InputInjector;
25 import io.appium.droiddriver.exceptions.ElementNotFoundException;
26 import io.appium.droiddriver.exceptions.TimeoutException;
27 import io.appium.droiddriver.finders.ByXPath;
28 import io.appium.droiddriver.finders.Finder;
29 import io.appium.droiddriver.util.Logs;
30 
31 /**
32  * Base DroidDriver that implements the common operations.
33  */
34 public abstract class BaseDroidDriver<R, E extends BaseUiElement<R, E>> implements DroidDriver {
35 
36   private Poller poller = new DefaultPoller();
37   private E rootElement;
38 
39   @Override
find(Finder finder)40   public UiElement find(Finder finder) {
41     Logs.call(Log.VERBOSE, this, "find", finder);
42     return finder.find(getRootElement());
43   }
44 
45   @Override
has(Finder finder)46   public boolean has(Finder finder) {
47     try {
48       refreshUiElementTree();
49       find(finder);
50       return true;
51     } catch (ElementNotFoundException enfe) {
52       return false;
53     }
54   }
55 
56   @Override
has(Finder finder, long timeoutMillis)57   public boolean has(Finder finder, long timeoutMillis) {
58     try {
59       getPoller().pollFor(this, finder, Poller.EXISTS, timeoutMillis);
60       return true;
61     } catch (TimeoutException e) {
62       return false;
63     }
64   }
65 
66   @Override
on(Finder finder)67   public UiElement on(Finder finder) {
68     Logs.call(this, "on", finder);
69     return getPoller().pollFor(this, finder, Poller.EXISTS);
70   }
71 
72   @Override
checkExists(Finder finder)73   public void checkExists(Finder finder) {
74     Logs.call(this, "checkExists", finder);
75     getPoller().pollFor(this, finder, Poller.EXISTS);
76   }
77 
78   @Override
checkGone(Finder finder)79   public void checkGone(Finder finder) {
80     Logs.call(this, "checkGone", finder);
81     getPoller().pollFor(this, finder, Poller.GONE);
82   }
83 
84   @Override
getPoller()85   public Poller getPoller() {
86     return poller;
87   }
88 
89   @Override
setPoller(Poller poller)90   public void setPoller(Poller poller) {
91     this.poller = poller;
92   }
93 
getInjector()94   public abstract InputInjector getInjector();
95 
newRootElement()96   protected abstract E newRootElement();
97 
98   /**
99    * Returns a new UiElement of type {@code E}.
100    */
newUiElement(R rawElement, E parent)101   protected abstract E newUiElement(R rawElement, E parent);
102 
getRootElement()103   public E getRootElement() {
104     if (rootElement == null) {
105       refreshUiElementTree();
106     }
107     return rootElement;
108   }
109 
110   @Override
refreshUiElementTree()111   public void refreshUiElementTree() {
112     rootElement = newRootElement();
113   }
114 
115   @Override
dumpUiElementTree(String path)116   public boolean dumpUiElementTree(String path) {
117     Logs.call(this, "dumpUiElementTree", path);
118     return ByXPath.dumpDom(path, getRootElement());
119   }
120 }
121