Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
Instrumented tests implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
eminmuhammadi committed Apr 28, 2021
1 parent 0234b13 commit 2a18f00
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 39 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0-beta01'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.example.week9fm5cc5;


import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import androidx.test.espresso.ViewInteraction;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import com.example.week9fm5cc5.helpers.GenerateAscii;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.pressImeActionButton;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class AsciiInstrumentedTest {

private GenerateAscii generateAscii = new GenerateAscii();
private ViewInteraction inputBox = onView(withId(R.id.inputBox));
private ViewInteraction generateAsciiButton = onView(withId(R.id.generateAscii));
private ViewInteraction resultText = onView(withId(R.id.resultText));

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void testEmptyStatement() {
String inputString = "";
Integer sum = generateAscii.sum(inputString);

generateAsciiButton.perform(click());

resultText.check(matches(withText("Sum of ASCII = "+ sum.toString())));
}

@Test
public void testHello() {
String inputString = "hello world";
Integer sum = generateAscii.sum(inputString);

inputBox.perform(click());
inputBox.perform(replaceText(inputString), closeSoftKeyboard());
inputBox.perform(pressImeActionButton());

generateAsciiButton.perform(click());

resultText.check(matches(withText("Sum of ASCII = "+ sum.toString())));
}

@Test
public void testCustomAlphabet() {
String inputString = "çöəğşıü";
Integer sum = generateAscii.sum(inputString);

inputBox.perform(click());
inputBox.perform(replaceText(inputString), closeSoftKeyboard());
inputBox.perform(pressImeActionButton());

generateAsciiButton.perform(click());

resultText.check(matches(withText("Sum of ASCII = "+ sum.toString())));
}

private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {

return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}

@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.example.week9fm5cc5;


import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import androidx.test.espresso.ViewInteraction;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import com.example.week9fm5cc5.helpers.ReplaceCharacters;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.pressImeActionButton;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class ReplaceCharactersInstrumentedTest {

private ReplaceCharacters replaceCharacters = new ReplaceCharacters();
private ViewInteraction inputBox = onView(withId(R.id.inputBox));
private ViewInteraction replaceCharactersButton = onView(withId(R.id.replaceCharacters));
private ViewInteraction resultText = onView(withId(R.id.resultText));

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void testEmptyStatement() {
String inputString = "";
String result = replaceCharacters.generate(inputString);

replaceCharactersButton.perform(click());

resultText.check(matches(withText("Result = \"" + result + "\"")));
}

@Test
public void testHelloWorld() {
String inputString = "Hello World";
String result = replaceCharacters.generate(inputString);

inputBox.perform(click());
inputBox.perform(replaceText(inputString), closeSoftKeyboard());
inputBox.perform(pressImeActionButton());
replaceCharactersButton.perform(click());

resultText.check(matches(withText("Result = \"" + result + "\"")));
}

@Test
public void testLongText() {
String inputString = "In publishing and graphic design, Lorem ipsum is a placeholder text.";
String result = replaceCharacters.generate(inputString);

inputBox.perform(click());
inputBox.perform(replaceText(inputString), closeSoftKeyboard());
inputBox.perform(pressImeActionButton());
replaceCharactersButton.perform(click());

resultText.check(matches(withText("Result = \"" + result + "\"")));
}

@Test
public void testCustomAlphabet() {
String inputString = "çöəğşıü";
String result = replaceCharacters.generate(inputString);

inputBox.perform(click());
inputBox.perform(replaceText(inputString), closeSoftKeyboard());
inputBox.perform(pressImeActionButton());
replaceCharactersButton.perform(click());

resultText.check(matches(withText("Result = \"" + result + "\"")));
}

@Test
public void testWithNumbers() {
String inputString = "0123456789";
String result = replaceCharacters.generate(inputString);

inputBox.perform(click());
inputBox.perform(replaceText(inputString), closeSoftKeyboard());
inputBox.perform(pressImeActionButton());
replaceCharactersButton.perform(click());

resultText.check(matches(withText("Result = \"" + result + "\"")));
}

private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {

return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}

@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
64 changes: 26 additions & 38 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,62 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/inputBox"
android:layout_width="323dp"
android:layout_height="56dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="128dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="107dp"
android:ems="10"
android:hint="@string/inputPlaceholder"
android:importantForAutofill="no"
android:inputType="text"
android:text="@string/input"
app:layout_constraintBottom_toTopOf="@+id/resultText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/generateAscii"
android:layout_width="300dp"
android:layout_height="47dp"
android:layout_marginStart="55dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="55dp"
android:layout_marginRight="55dp"
android:layout_marginBottom="10dp"
android:text="@string/generateAsciiSum"
app:layout_constraintBottom_toTopOf="@+id/replaceCharacters"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resultText" />

<Button
android:id="@+id/replaceCharacters"
android:layout_width="300dp"
android:layout_height="47dp"
android:layout_marginStart="55dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="55dp"
android:layout_marginRight="55dp"
android:layout_marginBottom="52dp"
android:text="@string/replaceCharacters"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/generateAscii" />

<EditText
android:id="@+id/inputBox"
android:hint="@string/inputPlaceholder"
android:layout_width="323dp"
android:layout_height="56dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="107dp"
android:ems="10"
android:importantForAutofill="no"
android:inputType="text"
android:text="@string/input"
app:layout_constraintBottom_toTopOf="@+id/resultText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/generateAscii"
app:layout_constraintVertical_bias="0.186" />

<TextView
android:id="@+id/resultText"
android:layout_width="280dp"
android:layout_height="56dp"
android:layout_marginStart="55dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="55dp"
android:layout_marginRight="55dp"
android:layout_marginBottom="32dp"
android:text="@string/result"
app:layout_constraintBottom_toTopOf="@+id/generateAscii"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_bias="0.476"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/inputBox" />

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
android.enableJetifier=true
android.useDeprecatedNdk=true

0 comments on commit 2a18f00

Please sign in to comment.