Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 276ee227 authored by Abodunrinwa Toki's avatar Abodunrinwa Toki Committed by Android (Google) Code Review
Browse files

Merge "Set device orientation for tests."

parents bc2f1a6c c9e924eb
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
/*
 * 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 android.util;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.pm.ActivityInfo;

import android.test.ActivityInstrumentationTestCase2;

import com.android.internal.util.Preconditions;

/**
 * Utilities for manipulating screen orientation.
 */
public final class OrientationUtil {

    private final Activity mActivity;
    private final Instrumentation mInstrumentation;

    private final Runnable mSetToPortrait = new Runnable() {
        @Override
        public void run() {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    };

    private final Runnable mSetToLandscape = new Runnable() {
        @Override
        public void run() {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    };

    public static OrientationUtil initializeAndStartActivityIfNotStarted(
            ActivityInstrumentationTestCase2 testCase) {
        Preconditions.checkNotNull(testCase);
        return new OrientationUtil(testCase.getActivity(), testCase.getInstrumentation());
    }

    private OrientationUtil(Activity activity, Instrumentation instrumentation) {
        mActivity = activity;
        mInstrumentation = instrumentation;
    }

    public void setPortraitOrientation() {
        mInstrumentation.runOnMainSync(mSetToPortrait);
    }

    public void setLandscapeOrientation() {
        mInstrumentation.runOnMainSync(mSetToLandscape);
    }
}
+9 −18
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import com.android.frameworks.coretests.R;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.OrientationUtil;
import android.view.KeyEvent;

/**
@@ -43,14 +44,20 @@ import android.view.KeyEvent;
 */
public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextViewActivity>{

    private OrientationUtil mOrientationUtil;

    public TextViewActivityTest() {
        super(TextViewActivity.class);
    }

    @Override
    public void setUp() {
        mOrientationUtil = OrientationUtil.initializeAndStartActivityIfNotStarted(this);
        mOrientationUtil.setPortraitOrientation();
    }

    @SmallTest
    public void testTypedTextIsOnScreen() throws Exception {
        getActivity();

        final String helloWorld = "Hello world!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -60,8 +67,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testPositionCursorAtTextAtIndex() throws Exception {
        getActivity();

        final String helloWorld = "Hello world!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -74,8 +79,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testLongPressToSelect() throws Exception {
        getActivity();

        final String helloWorld = "Hello Kirk!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -87,8 +90,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testLongPressEmptySpace() throws Exception {
        getActivity();

        final String helloWorld = "Hello big round sun!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -102,8 +103,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testLongPressAndDragToSelect() throws Exception {
        getActivity();

        final String helloWorld = "Hello little handsome boy!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -115,8 +114,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testDoubleTapToSelect() throws Exception {
        getActivity();

        final String helloWorld = "Hello SuetYi!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -128,8 +125,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testDoubleTapAndDragToSelect() throws Exception {
        getActivity();

        final String helloWorld = "Hello young beautiful girl!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -141,8 +136,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testSelectBackwordsByTouch() throws Exception {
        getActivity();

        final String helloWorld = "Hello king of the Jungle!";
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(helloWorld));
@@ -154,8 +147,6 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV

    @SmallTest
    public void testToolbarAppearsAfterSelection() throws Exception {
        getActivity();

        // It'll be nice to check that the toolbar is not visible (or does not exist) here
        // I can't currently find a way to do this. I'll get to it later.