/*
* Copyright (C) 2015 The Android Open Source Project
*
* 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 androidx.test.espresso.web.action;
import static androidx.test.espresso.matcher.ViewMatchers.isJavascriptEnabled;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.util.concurrent.Futures.transform;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import androidx.test.espresso.PerformException;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.remote.Bindable;
import androidx.test.espresso.web.model.Atom;
import androidx.test.espresso.web.model.ElementReference;
import androidx.test.espresso.web.model.Evaluation;
import androidx.test.espresso.web.model.WindowReference;
import com.google.common.base.Function;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;
import org.hamcrest.Matcher;
/**
* A ViewAction which causes the provided Atom to be evaluated within a webview.
*
*
It is not recommended to use AtomAction directly.
*
*
Instead {@see androidx.test.espresso.web.sugar.Web} for examples of how to interact
* with a WebView's content through Atoms.
*
*
If you must use AtomAction directly, take care to remember that they are Stateful (unlike most
* ViewActions) and the caller must call {@link #get()} to ensure that the action has completed.
*
* @param The type the specific Atom returns.
*/
public final class AtomAction implements ViewAction, Bindable {
private static final String TAG = "AtomAction";
private static final String ID = TAG;
private static final String EVALUATION_ERROR_KEY = "evaluation_error_key";
private final SettableFuture futureEval = SettableFuture.create();
final Atom atom;
@Nullable final WindowReference window;
@Nullable final ElementReference element;
private IAtomActionResultPropagator atomActionResultPropagator =
new IAtomActionResultPropagator.Stub() {
@Override
public void setResult(Evaluation evaluation) throws RemoteException {
futureEval.set(evaluation);
}
@Override
public void setError(Bundle bundle) throws RemoteException {
Throwable evalError = (Throwable) bundle.getSerializable(EVALUATION_ERROR_KEY);
futureEval.setException(evalError);
}
};
/**
* Creates an AtomAction.
*
* @param atom the atom to execute
* @param window (optional/nullable) the window context to execute on.
* @param element (optional/nullable) the element to execute on.
*/
public AtomAction(
Atom atom, @Nullable WindowReference window, @Nullable ElementReference element) {
this.atom = checkNotNull(atom);
this.window = window;
this.element = element;
}
@Override
public Matcher getConstraints() {
return isJavascriptEnabled();
}
@Override
public String getDescription() {
return String.format("Evaluate Atom: %s in window: %s with element: %s", atom, window, element);
}
@Override
public void perform(UiController controller, View view) {
WebView webView = (WebView) view;
if (Build.VERSION.SDK_INT >= 23 && !webView.isHardwareAccelerated()) {
throw new PerformException.Builder()
.withViewDescription(webView.toString())
.withCause(
new RuntimeException("Hardware acceleration is not supported on current device"))
.build();
}
List