Android Tests - Determine if WebView is Zoomed - Pinch Zoom

471 views Asked by At

I've been wandering through the internet trying to find out how to make this work. I have an app that allows the user to disable pinch zoom on an webview. It actually works, but I can't test it...(API 15)

I did:

...
import com.robotium.solo.By;

public class MainActivityTest {

    public MainActivityTest() {
        super(MainActivity.class);
    }
    ...
    public void test_is_user_scalable() {
        getActivity();
        assertTrue(mSolo.waitForText("On Fake Page 1"));
        WebView webview = (WebView) getActivity().findViewById(R.id.activity_main_webview);
        assertFalse(webview.canZoomOut());

        mSolo.pinchToZoom(new PointF(500, 500), new PointF(600, 600), new PointF(200, 200), new PointF(700, 700));

        assertTrue(webview.canZoomOut());
    }
}

Even if I change my third line of the test to this one:

WebView webview = (WebView) mSolo.getView(R.id.activity_main_webview);

I'm still getting the error:

java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'Instr: android.test.InstrumentationTestRunner'. All WebView methods must be called on the same thread.

I can't figure out how to view if the user can or cannot pinch zoom the screen. Any ideas?

1

There are 1 answers

0
Mauricio Moraes On BEST ANSWER

I don't know if this is the best way, but I'll answer my own question because this worked quite well.

Instead of picking the webview and trying to interact with it, I went directly to the view and tried to scroll up, down and sideways. It was only possible when the view was zoomed:

public void test_user_scalable() {
    getActivity();
    assertTrue(mSolo.waitForText("On Fake Page 1"));
    View webview = getActivity().findViewById(R.id.activity_main_webview);
    assertFalse(webview.canScrollVertically(-1));
    assertFalse(webview.canScrollVertically(1));
    assertFalse(webview.canScrollHorizontally(-1));
    assertFalse(webview.canScrollHorizontally(1));

    mSolo.pinchToZoom(new PointF(500, 500), new PointF(600, 600), new PointF(200, 200), new PointF(700, 700));

    assertTrue(webview.canScrollVertically(-1));
    assertTrue(webview.canScrollVertically(1));
    assertTrue(webview.canScrollHorizontally(-1));
    assertTrue(webview.canScrollHorizontally(1));
}