|
| 1 | +/* |
| 2 | + * Copyright 2020, The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.android.testing.androidtestorchestratorsample; |
| 18 | + |
| 19 | +import junit.framework.TestSuite; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | + |
| 26 | +import androidx.test.core.app.ActivityScenario; |
| 27 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 28 | +import androidx.test.filters.LargeTest; |
| 29 | +import androidx.test.runner.AndroidJUnitRunner; |
| 30 | + |
| 31 | +import static androidx.test.core.app.ApplicationProvider.getApplicationContext; |
| 32 | +import static androidx.test.espresso.Espresso.onView; |
| 33 | +import static androidx.test.espresso.action.ViewActions.click; |
| 34 | +import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard; |
| 35 | +import static androidx.test.espresso.action.ViewActions.typeText; |
| 36 | +import static androidx.test.espresso.assertion.ViewAssertions.matches; |
| 37 | +import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 38 | +import static androidx.test.espresso.matcher.ViewMatchers.withText; |
| 39 | + |
| 40 | +/** |
| 41 | + * JUnit4 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner} with the |
| 42 | + * Android Test Orchestrator. |
| 43 | + * This class uses the JUnit4 syntax for tests. |
| 44 | + * <p> |
| 45 | + * With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test |
| 46 | + * suite. The {@link AndroidRunnerBuilder} which extends JUnit's |
| 47 | + * {@link AllDefaultPossibilitiesBuilder} will create a single {@link |
| 48 | + * TestSuite} from all tests and run them. |
| 49 | + */ |
| 50 | +@RunWith(AndroidJUnit4.class) |
| 51 | +@LargeTest |
| 52 | +public class CalculatorInstrumentationTest { |
| 53 | + |
| 54 | + /** |
| 55 | + * Use {@link ActivityScenario} to create and launch of the activity. |
| 56 | + */ |
| 57 | + @Before |
| 58 | + public void launchActivity() { |
| 59 | + ActivityScenario.launch(CalculatorActivity.class); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void noOperandShowsComputationError() { |
| 64 | + final String expectedResult = getApplicationContext().getString(R.string.computationError); |
| 65 | + onView(withId(R.id.operation_add_btn)).perform(click()); |
| 66 | + onView(withId(R.id.operation_result_text_view)).check(matches(withText(expectedResult))); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void typeOperandsAndPerformAddOperation() { |
| 71 | + performOperation(R.id.operation_add_btn, "16.0", "16.0", "32.0"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void typeOperandsAndPerformSubOperation() { |
| 76 | + performOperation(R.id.operation_sub_btn, "32.0", "16.0", "16.0"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void typeOperandsAndPerformDivOperation() { |
| 81 | + performOperation(R.id.operation_div_btn, "128.0", "16.0", "8.0"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void divZeroForOperandTwoShowsError() { |
| 86 | + final String expectedResult = getApplicationContext().getString(R.string.computationError); |
| 87 | + performOperation(R.id.operation_div_btn, "128.0", "0.0", expectedResult); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void typeOperandsAndPerformMulOperation() { |
| 92 | + performOperation(R.id.operation_mul_btn, "16.0", "16.0", "256.0"); |
| 93 | + } |
| 94 | + |
| 95 | + private void performOperation(int btnOperationResId, String operandOne, |
| 96 | + String operandTwo, String expectedResult) { |
| 97 | + // Type the two operands in the EditText fields |
| 98 | + onView(withId(R.id.operand_one_edit_text)).perform(typeText(operandOne), |
| 99 | + closeSoftKeyboard()); |
| 100 | + onView(withId(R.id.operand_two_edit_text)).perform(typeText(operandTwo), |
| 101 | + closeSoftKeyboard()); |
| 102 | + |
| 103 | + // Click on a given operation button |
| 104 | + onView(withId(btnOperationResId)).perform(click()); |
| 105 | + |
| 106 | + // Check the expected test is displayed in the Ui |
| 107 | + onView(withId(R.id.operation_result_text_view)).check(matches(withText(expectedResult))); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments