diff options
author | Eric Fitchett <efitchett@google.com> | 2015-12-02 12:20:13 -0500 |
---|---|---|
committer | Eric Fitchett <efitchett@google.com> | 2016-02-16 14:28:32 -0500 |
commit | 71ab195d6034efb023efaf6def8e5246262d2e66 (patch) | |
tree | 78bfd745929d9442c88e2abf60f80b65b4688589 | |
parent | 675040fd1a83ee2505e1109489adcc0d666f61ea (diff) | |
download | droiddriver-71ab195d6034efb023efaf6def8e5246262d2e66.tar.gz |
Added DuoDriver.
Automatically uses the most appropriate DroidDriver implementation.
Uses InstrumentationDriver for activities owned by the application
under instrumentation. Uses UiAutomationDriver for all others.
Change-Id: Iea232a8c3ef590d170fd2ca47c78f515fe0ec57c
5 files changed, 205 insertions, 63 deletions
diff --git a/src/io/appium/droiddriver/base/AbstractDroidDriver.java b/src/io/appium/droiddriver/base/AbstractDroidDriver.java new file mode 100644 index 0000000..bf0df4b --- /dev/null +++ b/src/io/appium/droiddriver/base/AbstractDroidDriver.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2016 DroidDriver committers + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.droiddriver.base; + +import io.appium.droiddriver.DroidDriver; +import io.appium.droiddriver.Poller; +import io.appium.droiddriver.UiElement; +import io.appium.droiddriver.actions.InputInjector; +import io.appium.droiddriver.exceptions.ElementNotFoundException; +import io.appium.droiddriver.exceptions.TimeoutException; +import io.appium.droiddriver.finders.Finder; +import io.appium.droiddriver.util.Logs; + +/** + * Base DroidDriver that implements the common operations. + */ +public abstract class AbstractDroidDriver implements DroidDriver { + + private Poller poller = new DefaultPoller(); + + @Override + public boolean has(Finder finder) { + try { + refreshUiElementTree(); + find(finder); + return true; + } catch (ElementNotFoundException enfe) { + return false; + } + } + + @Override + public boolean has(Finder finder, long timeoutMillis) { + try { + getPoller().pollFor(this, finder, Poller.EXISTS, timeoutMillis); + return true; + } catch (TimeoutException e) { + return false; + } + } + + @Override + public UiElement on(Finder finder) { + Logs.call(this, "on", finder); + return getPoller().pollFor(this, finder, Poller.EXISTS); + } + + @Override + public void checkExists(Finder finder) { + Logs.call(this, "checkExists", finder); + getPoller().pollFor(this, finder, Poller.EXISTS); + } + + @Override + public void checkGone(Finder finder) { + Logs.call(this, "checkGone", finder); + getPoller().pollFor(this, finder, Poller.GONE); + } + + @Override + public Poller getPoller() { + return poller; + } + + @Override + public void setPoller(Poller poller) { + this.poller = poller; + } + + public abstract InputInjector getInjector(); + +}
\ No newline at end of file diff --git a/src/io/appium/droiddriver/base/BaseDroidDriver.java b/src/io/appium/droiddriver/base/BaseDroidDriver.java index e985a38..827bce6 100644 --- a/src/io/appium/droiddriver/base/BaseDroidDriver.java +++ b/src/io/appium/droiddriver/base/BaseDroidDriver.java @@ -17,23 +17,16 @@ package io.appium.droiddriver.base; import android.util.Log; - -import io.appium.droiddriver.DroidDriver; -import io.appium.droiddriver.Poller; import io.appium.droiddriver.UiElement; -import io.appium.droiddriver.actions.InputInjector; -import io.appium.droiddriver.exceptions.ElementNotFoundException; -import io.appium.droiddriver.exceptions.TimeoutException; import io.appium.droiddriver.finders.ByXPath; import io.appium.droiddriver.finders.Finder; import io.appium.droiddriver.util.Logs; /** - * Base DroidDriver that implements the common operations. + * Enhances AbstractDroidDriver to include basic element handling and matching operations. */ -public abstract class BaseDroidDriver<R, E extends BaseUiElement<R, E>> implements DroidDriver { +public abstract class BaseDroidDriver<R, E extends BaseUiElement<R, E>> extends AbstractDroidDriver { - private Poller poller = new DefaultPoller(); private E rootElement; @Override @@ -42,57 +35,6 @@ public abstract class BaseDroidDriver<R, E extends BaseUiElement<R, E>> implemen return finder.find(getRootElement()); } - @Override - public boolean has(Finder finder) { - try { - refreshUiElementTree(); - find(finder); - return true; - } catch (ElementNotFoundException enfe) { - return false; - } - } - - @Override - public boolean has(Finder finder, long timeoutMillis) { - try { - getPoller().pollFor(this, finder, Poller.EXISTS, timeoutMillis); - return true; - } catch (TimeoutException e) { - return false; - } - } - - @Override - public UiElement on(Finder finder) { - Logs.call(this, "on", finder); - return getPoller().pollFor(this, finder, Poller.EXISTS); - } - - @Override - public void checkExists(Finder finder) { - Logs.call(this, "checkExists", finder); - getPoller().pollFor(this, finder, Poller.EXISTS); - } - - @Override - public void checkGone(Finder finder) { - Logs.call(this, "checkGone", finder); - getPoller().pollFor(this, finder, Poller.GONE); - } - - @Override - public Poller getPoller() { - return poller; - } - - @Override - public void setPoller(Poller poller) { - this.poller = poller; - } - - public abstract InputInjector getInjector(); - protected abstract E newRootElement(); /** diff --git a/src/io/appium/droiddriver/base/CompositeDroidDriver.java b/src/io/appium/droiddriver/base/CompositeDroidDriver.java new file mode 100644 index 0000000..c92c5c3 --- /dev/null +++ b/src/io/appium/droiddriver/base/CompositeDroidDriver.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2016 DroidDriver committers + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.droiddriver.base; + +import io.appium.droiddriver.UiDevice; +import io.appium.droiddriver.UiElement; +import io.appium.droiddriver.actions.InputInjector; +import io.appium.droiddriver.finders.Finder; + +/** + * Helper class to ease creation of drivers that defer actions to other drivers. + */ +public abstract class CompositeDroidDriver extends AbstractDroidDriver { + /** + * Determines which DroidDriver should handle the current situation. + * + * @return The DroidDriver instance to use + */ + protected abstract AbstractDroidDriver getApplicableDriver(); + + @Override + public InputInjector getInjector() { + return getApplicableDriver().getInjector(); + } + + @Override + public UiDevice getUiDevice() { + return getApplicableDriver().getUiDevice(); + } + + @Override + public UiElement find(Finder finder) { + return getApplicableDriver().find(finder); + } + + @Override + public void refreshUiElementTree() { + getApplicableDriver().refreshUiElementTree(); + } + + @Override + public boolean dumpUiElementTree(String path) { + return getApplicableDriver().dumpUiElementTree(path); + } +} diff --git a/src/io/appium/droiddriver/duo/DuoDriver.java b/src/io/appium/droiddriver/duo/DuoDriver.java new file mode 100644 index 0000000..d784087 --- /dev/null +++ b/src/io/appium/droiddriver/duo/DuoDriver.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016 DroidDriver committers + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.droiddriver.duo; + +import io.appium.droiddriver.base.AbstractDroidDriver; +import io.appium.droiddriver.base.CompositeDroidDriver; +import io.appium.droiddriver.instrumentation.InstrumentationDriver; +import io.appium.droiddriver.uiautomation.UiAutomationDriver; +import io.appium.droiddriver.util.ActivityUtils; +import io.appium.droiddriver.util.InstrumentationUtils; +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.Instrumentation; + +/** + * Implementation of DroidDriver that attempts to use the best driver for the current activity. + * If the activity is part of the application under instrumentation, the InstrumentationDriver is + * used. Otherwise, the UiAutomationDriver is used. + */ +@TargetApi(18) +public class DuoDriver extends CompositeDroidDriver { + private final String targetApkPackage; + private final UiAutomationDriver uiAutomationDriver; + private final InstrumentationDriver instrumentationDriver; + + public DuoDriver() { + Instrumentation instrumentation = InstrumentationUtils.getInstrumentation(); + targetApkPackage = InstrumentationUtils.getTargetContext().getPackageName(); + uiAutomationDriver = new UiAutomationDriver(instrumentation); + instrumentationDriver = new InstrumentationDriver(instrumentation); + } + + @Override + protected AbstractDroidDriver getApplicableDriver() { + Activity activity = ActivityUtils.getRunningActivity(); + if (activity != null && targetApkPackage.equals( + activity.getApplicationContext().getPackageName())) { + return instrumentationDriver; + } + return uiAutomationDriver; + } +} diff --git a/src/io/appium/droiddriver/helpers/DroidDrivers.java b/src/io/appium/droiddriver/helpers/DroidDrivers.java index 7725bf5..f48a777 100644 --- a/src/io/appium/droiddriver/helpers/DroidDrivers.java +++ b/src/io/appium/droiddriver/helpers/DroidDrivers.java @@ -19,11 +19,10 @@ package io.appium.droiddriver.helpers; import android.annotation.TargetApi; import android.app.Instrumentation; import android.os.Build; - import io.appium.droiddriver.DroidDriver; +import io.appium.droiddriver.duo.DuoDriver; import io.appium.droiddriver.exceptions.DroidDriverException; import io.appium.droiddriver.instrumentation.InstrumentationDriver; -import io.appium.droiddriver.uiautomation.UiAutomationDriver; import io.appium.droiddriver.util.InstrumentationUtils; /** @@ -83,7 +82,7 @@ public class DroidDrivers { // If "dd.driver" is not specified, return default. if (hasUiAutomation()) { checkUiAutomation(); - return new UiAutomationDriver(instrumentation); + return new DuoDriver(); } return new InstrumentationDriver(instrumentation); } |