diff options
author | Kevin Jin <kjin@google.com> | 2015-02-13 15:48:11 -0800 |
---|---|---|
committer | Kevin Jin <kjin@google.com> | 2015-02-13 15:48:11 -0800 |
commit | 9e96b8cb7c7d23167852a9fab0e734072c6758fd (patch) | |
tree | 4f3cb84a6385b7d58477c3d6e27f3c19009f7fad /src | |
parent | 1a0f019f6b874d59b5be2d63ceaf5bf2caa86f80 (diff) | |
download | droiddriver-9e96b8cb7c7d23167852a9fab0e734072c6758fd.tar.gz |
Better message for ActivityUtils.setRunningActivitySupplier
Allow running activity to be null when AUT is not on top.
Change version to 0.9.1.
Change-Id: I1573ca75611520607ea9f513f952db8dfe0d8da8
Diffstat (limited to 'src')
4 files changed, 55 insertions, 6 deletions
diff --git a/src/com/google/android/droiddriver/base/DefaultPoller.java b/src/com/google/android/droiddriver/base/DefaultPoller.java index c6d69c5..d4d178b 100644 --- a/src/com/google/android/droiddriver/base/DefaultPoller.java +++ b/src/com/google/android/droiddriver/base/DefaultPoller.java @@ -20,6 +20,7 @@ import android.os.SystemClock; import com.google.android.droiddriver.DroidDriver; import com.google.android.droiddriver.Poller; +import com.google.android.droiddriver.exceptions.NoRunningActivityException; import com.google.android.droiddriver.exceptions.TimeoutException; import com.google.android.droiddriver.finders.Finder; @@ -66,9 +67,16 @@ public class DefaultPoller implements Poller { long end = SystemClock.uptimeMillis() + timeoutMillis; while (true) { try { - driver.refreshUiElementTree(); + try { + driver.refreshUiElementTree(); + } catch (NoRunningActivityException nrae) { + if (checker == GONE) { + return null; + } + throw nrae; + } return checker.check(driver, finder); - } catch (UnsatisfiedConditionException e) { + } catch (UnsatisfiedConditionException uce) { // fall through to poll } diff --git a/src/com/google/android/droiddriver/exceptions/NoRunningActivityException.java b/src/com/google/android/droiddriver/exceptions/NoRunningActivityException.java new file mode 100644 index 0000000..af72665 --- /dev/null +++ b/src/com/google/android/droiddriver/exceptions/NoRunningActivityException.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2013 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 com.google.android.droiddriver.exceptions; + +/** + * Thrown when the running activity cannot be determined. This can happen when, for example, + * InstrumentationDriver is in use and the running activity does not belong to AUT. + */ +@SuppressWarnings("serial") +public class NoRunningActivityException extends DroidDriverException { + + public NoRunningActivityException(String message) { + super(message); + } + + public NoRunningActivityException(Throwable cause) { + super(cause); + } + + public NoRunningActivityException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java b/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java index 6fd3ae7..bd05b8d 100644 --- a/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java +++ b/src/com/google/android/droiddriver/instrumentation/InstrumentationDriver.java @@ -25,7 +25,7 @@ import com.google.android.droiddriver.actions.InputInjector; import com.google.android.droiddriver.base.BaseDroidDriver; import com.google.android.droiddriver.base.DroidDriverContext; import com.google.android.droiddriver.exceptions.DroidDriverException; -import com.google.android.droiddriver.exceptions.TimeoutException; +import com.google.android.droiddriver.exceptions.NoRunningActivityException; import com.google.android.droiddriver.util.ActivityUtils; import com.google.android.droiddriver.util.Logs; @@ -81,7 +81,6 @@ public class InstrumentationDriver extends BaseDroidDriver<View, ViewElement> { rootView = ActivityUtils.getRunningActivity().getWindow().getDecorView(); } catch (Throwable e) { exception = e; - Logs.log(Log.ERROR, e); } } } @@ -105,8 +104,8 @@ public class InstrumentationDriver extends BaseDroidDriver<View, ViewElement> { } long remainingMillis = end - SystemClock.uptimeMillis(); if (remainingMillis < 0) { - throw new TimeoutException(String.format( - "Timed out after %d milliseconds waiting for foreground activity", timeoutMillis)); + throw new NoRunningActivityException(String.format( + "Cannot find the running activity after %d milliseconds", timeoutMillis)); } SystemClock.sleep(Math.min(250, remainingMillis)); } diff --git a/src/com/google/android/droiddriver/util/ActivityUtils.java b/src/com/google/android/droiddriver/util/ActivityUtils.java index c0f553f..da1480b 100644 --- a/src/com/google/android/droiddriver/util/ActivityUtils.java +++ b/src/com/google/android/droiddriver/util/ActivityUtils.java @@ -18,6 +18,7 @@ package com.google.android.droiddriver.util; import android.app.Activity; +import com.google.android.droiddriver.exceptions.UnrecoverableException; import com.google.android.droiddriver.instrumentation.InstrumentationDriver; /** @@ -53,6 +54,10 @@ public class ActivityUtils { * @return the currently running activity, or null if no activity has focus. */ public static synchronized Activity getRunningActivity() { + if (runningActivitySupplier == null) { + throw new UnrecoverableException("If you don't use DroidDriver TestRunner, you need to call" + + " ActivityUtils.setRunningActivitySupplier appropriately"); + } return runningActivitySupplier.get(); } } |