Skip to content

Commit

Permalink
Fixing and refactoring Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorstenBandel committed Apr 18, 2022
1 parent 09d96c8 commit 8162648
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 174 deletions.
2 changes: 2 additions & 0 deletions Paintroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation "androidx.test.uiautomator:uiautomator:2.2.0"
testImplementation "androidx.test:core-ktx:1.4.0"
implementation 'com.android.support.test.espresso:espresso-idling-resource:3.1.0'
}

tasks.withType(Javadoc).all {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class FileFromOtherSourceIntegrationTest {
public ScreenshotOnFailRule screenshotOnFailRule = new ScreenshotOnFailRule();

@ClassRule
public static GrantPermissionRule grantPermissionRule = EspressoUtils.grantPermissionRulesVersionCheck();
public static GrantPermissionRule grantPermissionRule = EspressoUtils.INSTANCE.grantPermissionRulesVersionCheck();

private ContentResolver resolver;
private MainActivity activity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

import androidx.annotation.ArrayRes;
import androidx.annotation.ColorInt;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.idling.CountingIdlingResource;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

Expand Down Expand Up @@ -99,6 +101,8 @@ public void testLandscapeMode() {

@Test
public void testTools() {
CountingIdlingResource idlingResource = activityTestRule.getActivity().getIdlingResource();
IdlingRegistry.getInstance().register(idlingResource);
setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
for (ToolType toolType : ToolType.values()) {
if (toolType == ToolType.IMPORTPNG
Expand Down Expand Up @@ -129,6 +133,7 @@ public void testTools() {
onView(withId(R.id.pocketpaint_layout_tool_specific_options))
.check(matches(not(isDisplayed())));
}
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
Expand Down Expand Up @@ -409,6 +414,8 @@ public void testFullscreenLandscapeOrientationChangeWithBrush() {

@Test
public void testFullscreenPortraitOrientationChangeWithShape() {
CountingIdlingResource idlingResource = activityTestRule.getActivity().getIdlingResource();
IdlingRegistry.getInstance().register(idlingResource);
onToolBarView()
.performSelectTool(ToolType.SHAPE);

Expand All @@ -424,6 +431,7 @@ public void testFullscreenPortraitOrientationChangeWithShape() {
onToolBarView()
.performOpenToolOptionsView()
.performCloseToolOptionsView();
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.graphics.Canvas
import android.graphics.Color
import android.net.Uri
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.intent.Intents
Expand All @@ -36,6 +37,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule
import java.io.File
import org.catrobat.paintroid.MainActivity
import org.catrobat.paintroid.R
import org.catrobat.paintroid.common.MAX_LAYERS
Expand All @@ -59,7 +61,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File

@RunWith(AndroidJUnit4::class)
class LayerIntegrationTest {
Expand Down Expand Up @@ -595,6 +596,8 @@ class LayerIntegrationTest {

@Test
fun testHideThenUnhideLayer() {
val idlingResource = launchActivityRule.activity.idlingResource
IdlingRegistry.getInstance().register(idlingResource)
LayerMenuViewInteraction.onLayerMenuView()
.performOpen()
.performAddLayer()
Expand All @@ -614,6 +617,7 @@ class LayerIntegrationTest {
.performToggleLayerVisibility(0)
.performClose()
DrawingSurfaceInteraction.onDrawingSurfaceView().checkPixelColor(Color.BLACK, 1f, 1f)
IdlingRegistry.getInstance().unregister(idlingResource)
}

@Test
Expand Down Expand Up @@ -642,11 +646,13 @@ class LayerIntegrationTest {

@Test
fun testLoadLargeBitmapAndAddMaxLayerMenu() {
val idlingResource = launchActivityRule.activity.idlingResource
val intent = Intent().apply {
data = createTestImageFile()
}
Intents.init()
val intentResult = ActivityResult(Activity.RESULT_OK, intent)
IdlingRegistry.getInstance().register(idlingResource)

Intents.intending(IntentMatchers.anyIntent()).respondWith(intentResult)
TopBarViewInteraction.onTopBarView()
Expand All @@ -664,6 +670,7 @@ class LayerIntegrationTest {
.performAddLayer()
.performAddLayer()
.checkLayerCount(MAX_LAYERS)
IdlingRegistry.getInstance().unregister(idlingResource)
}

private fun createTestImageFile(): Uri {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class MoreOptionsIntegrationTest {
public ScreenshotOnFailRule screenshotOnFailRule = new ScreenshotOnFailRule();

@ClassRule
public static GrantPermissionRule grantPermissionRule = EspressoUtils.grantPermissionRulesVersionCheck();
public static GrantPermissionRule grantPermissionRule = EspressoUtils.INSTANCE.grantPermissionRulesVersionCheck();

@Before
public void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class ToolOnBackPressedIntegrationTest {
public ScreenshotOnFailRule screenshotOnFailRule = new ScreenshotOnFailRule();

@ClassRule
public static GrantPermissionRule grantPermissionRule = EspressoUtils.grantPermissionRulesVersionCheck();
public static GrantPermissionRule grantPermissionRule = EspressoUtils.INSTANCE.grantPermissionRulesVersionCheck();

private File saveFile = null;
private ToolReference toolReference;
Expand Down Expand Up @@ -208,7 +208,7 @@ public void testToolOptionsGoBackWhenBackPressed() {
}

@Test
public void testBrushToolBackPressedFromCatroidAndUsePicture() throws SecurityException, IllegalArgumentException {
public void testBrushToolBackPressedFromCatroidAndUsePicture() throws SecurityException, IllegalArgumentException, InterruptedException {
onDrawingSurfaceView()
.perform(touchAt(DrawingSurfaceLocationProvider.MIDDLE));

Expand All @@ -223,7 +223,7 @@ public void testBrushToolBackPressedFromCatroidAndUsePicture() throws SecurityEx
launchActivityRule.getActivity().model.setOpenedFromCatroid(true);

Espresso.pressBackUnconditionally();

Thread.sleep(100);
assertTrue(launchActivityRule.getActivity().isFinishing());
assertTrue(saveFile.exists());
assertThat(saveFile.length(), is(greaterThan(0L)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.catrobat.paintroid.test.espresso;

import org.catrobat.paintroid.MainActivity;
import org.catrobat.paintroid.test.espresso.util.EspressoUtils;
import org.catrobat.paintroid.test.utils.ScreenshotOnFailRule;
import org.catrobat.paintroid.tools.ToolType;
import org.junit.Before;
Expand All @@ -30,7 +31,6 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import static org.catrobat.paintroid.test.espresso.util.EspressoUtils.waitForToast;
import static org.catrobat.paintroid.test.espresso.util.wrappers.BottomNavigationViewInteraction.onBottomNavigationView;
import static org.catrobat.paintroid.test.espresso.util.wrappers.ToolBarViewInteraction.onToolBarView;

Expand All @@ -56,7 +56,7 @@ public void testToolSelectionToast() {
onToolBarView()
.performSelectTool(toolType);

waitForToast(withText(toolType.getNameResource()), 1000);
EspressoUtils.INSTANCE.waitForToast(withText(toolType.getNameResource()), 1000);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class MoreOptionsIntegrationTest {
public ScreenshotOnFailRule screenshotOnFailRule = new ScreenshotOnFailRule();

@ClassRule
public static GrantPermissionRule grantPermissionRule = EspressoUtils.grantPermissionRulesVersionCheck();
public static GrantPermissionRule grantPermissionRule = EspressoUtils.INSTANCE.grantPermissionRulesVersionCheck();

@Before
public void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class OpenedFromPocketCodeNewImageTest {
public ScreenshotOnFailRule screenshotOnFailRule = new ScreenshotOnFailRule();

@ClassRule
public static GrantPermissionRule grantPermissionRule = EspressoUtils.grantPermissionRulesVersionCheck();
public static GrantPermissionRule grantPermissionRule = EspressoUtils.INSTANCE.grantPermissionRulesVersionCheck();

private File imageFile = null;
private MainActivity activity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.action.ViewActions;
import androidx.test.espresso.idling.CountingIdlingResource;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

Expand Down Expand Up @@ -718,18 +720,20 @@ public void testColorViewsAreDisplayed() {

@Test
public void alphaValueIsSetInSliderWhenChangedInSeekBar() {
CountingIdlingResource idlingResource = launchActivityRule.getActivity().getIdlingResource();
IdlingRegistry.getInstance().register(idlingResource);
onColorPickerView()
.performOpenColorPicker();
onView(allOf(withId(R.id.color_picker_tab_icon), withBackground(R.drawable.ic_color_picker_tab_rgba))).perform(click());

// set color to value #7F000000, alpha seekbar 49%
onView(withId(R.id.color_picker_color_rgb_seekbar_alpha)).perform(touchCenterMiddle());
onView(allOf(withId(R.id.color_picker_tab_icon), withBackground(R.drawable.ic_color_picker_tab_preset))).perform(scrollTo(), click());
onColorPickerView()
.onPositiveButton()
.perform(click());
onToolProperties()
.checkMatchesColor(Color.parseColor("#7F000000"));
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
Expand All @@ -738,7 +742,6 @@ public void alphaValueIsSetInSeekBarWhenChangedInSlider() {
.performOpenColorPicker();
onView(allOf(withId(R.id.color_picker_tab_icon), withBackground(R.drawable.ic_color_picker_tab_preset))).perform(click());

// set color to value #80000000, alpha seekbar 50%
onView(withId(R.id.color_alpha_slider)).perform(scrollTo(), touchCenterMiddle());

onView(allOf(withId(R.id.color_picker_tab_icon), withBackground(R.drawable.ic_color_picker_tab_rgba))).perform(click());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.idling.CountingIdlingResource;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

Expand Down Expand Up @@ -184,6 +186,8 @@ public void testCheckmarkLineFeatureChangeColor() {

@Test
public void testCheckmarkLineFeatureChangeShape() {
CountingIdlingResource idlingResource = launchActivityRule.getActivity().getIdlingResource();
IdlingRegistry.getInstance().register(idlingResource);
onToolProperties().setColor(Color.BLACK);

onDrawingSurfaceView()
Expand Down Expand Up @@ -217,10 +221,13 @@ public void testCheckmarkLineFeatureChangeShape() {

onDrawingSurfaceView()
.checkPixelColor(Color.BLACK, BitmapLocationProvider.MIDDLE);
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
public void testCheckmarkLineFeatureChangeStrokeWidth() {
CountingIdlingResource idlingResource = launchActivityRule.getActivity().getIdlingResource();
IdlingRegistry.getInstance().register(idlingResource);
onToolProperties().setColor(Color.BLACK);

onDrawingSurfaceView()
Expand Down Expand Up @@ -252,6 +259,7 @@ public void testCheckmarkLineFeatureChangeStrokeWidth() {

onDrawingSurfaceView()
.checkPixelColor(Color.BLACK, BitmapLocationProvider.MIDDLE);
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
Expand Down Expand Up @@ -350,6 +358,8 @@ public void testConnectedLinesFeatureRedrawingLine() {

@Test
public void testClickingUndoOnceOnConnectedLines() {
CountingIdlingResource idlingResource = launchActivityRule.getActivity().getIdlingResource();
IdlingRegistry.getInstance().register(idlingResource);
onToolProperties().setColor(Color.BLACK);

onDrawingSurfaceView()
Expand Down Expand Up @@ -390,6 +400,7 @@ public void testClickingUndoOnceOnConnectedLines() {
.checkPixelColor(Color.BLACK, BitmapLocationProvider.HALFWAY_RIGHT_MIDDLE);

onTopBarView().performClickCheckmark();
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.catrobat.paintroid.MainActivity;
import org.catrobat.paintroid.R;
import org.catrobat.paintroid.test.espresso.rtl.util.RtlActivityTestRule;
import org.catrobat.paintroid.test.espresso.util.EspressoUtils;
import org.catrobat.paintroid.test.utils.ScreenshotOnFailRule;
import org.catrobat.paintroid.tools.ToolType;
import org.junit.Rule;
Expand All @@ -41,7 +42,6 @@
import androidx.test.rule.ActivityTestRule;

import static org.catrobat.paintroid.test.espresso.rtl.util.RtlUiTestUtils.checkTextDirection;
import static org.catrobat.paintroid.test.espresso.util.EspressoUtils.getConfiguration;
import static org.catrobat.paintroid.test.espresso.util.UiMatcher.atPosition;
import static org.catrobat.paintroid.test.espresso.util.UiMatcher.hasTypeFace;
import static org.catrobat.paintroid.test.espresso.util.wrappers.ToolBarViewInteraction.onToolBarView;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class TextToolFontListTestArabic {

@Test
public void testTextFontFaceOfFontSpinnerArabic() {
assertEquals(View.LAYOUT_DIRECTION_RTL, getConfiguration().getLayoutDirection());
assertEquals(View.LAYOUT_DIRECTION_RTL, EspressoUtils.INSTANCE.getConfiguration().getLayoutDirection());
assertTrue(checkTextDirection(Locale.getDefault().getDisplayName()));

onToolBarView()
Expand Down
Loading

0 comments on commit 8162648

Please sign in to comment.