diff --git a/app/build.gradle b/app/build.gradle index 0487103..fdce3c6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/week9fm5cc5/AsciiInstrumentedTest.java b/app/src/androidTest/java/com/example/week9fm5cc5/AsciiInstrumentedTest.java new file mode 100644 index 0000000..c8577ef --- /dev/null +++ b/app/src/androidTest/java/com/example/week9fm5cc5/AsciiInstrumentedTest.java @@ -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 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 childAtPosition( + final Matcher parentMatcher, final int position) { + + return new TypeSafeMatcher() { + @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)); + } + }; + } +} diff --git a/app/src/androidTest/java/com/example/week9fm5cc5/ReplaceCharactersInstrumentedTest.java b/app/src/androidTest/java/com/example/week9fm5cc5/ReplaceCharactersInstrumentedTest.java new file mode 100644 index 0000000..a201f71 --- /dev/null +++ b/app/src/androidTest/java/com/example/week9fm5cc5/ReplaceCharactersInstrumentedTest.java @@ -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 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 childAtPosition( + final Matcher parentMatcher, final int position) { + + return new TypeSafeMatcher() { + @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)); + } + }; + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2680c12..37334d1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,20 +6,34 @@ android:layout_height="match_parent" tools:context=".MainActivity"> + +