Skip to content

Commit

Permalink
Add test for BOINCActivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed May 29, 2020
1 parent 7e27d97 commit 67801b6
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* This file is part of BOINC.
* http://boinc.berkeley.edu
* Copyright (C) 2020 University of California
*
* BOINC is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* BOINC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with BOINC. If not, see <http://www.gnu.org/licenses/>.
*/
package edu.berkeley.boinc

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.hamcrest.Matchers.allOf
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
class BOINCActivityTest {
@Rule
@JvmField
var mActivityScenarioRule = ActivityScenarioRule(BOINCActivity::class.java)

@Test
fun boincActivityTest() {
val appCompatImageButton = onView(
allOf(withContentDescription("Navigate up"),
childAtPosition(
allOf(withId(R.id.action_bar),
childAtPosition(
withId(R.id.action_bar_container),
0)),
1),
isDisplayed()))
appCompatImageButton.perform(click())

val textView = onView(
allOf(withId(R.id.title), withText("Tasks"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
0)),
1),
isDisplayed()))
textView.check(matches(withText("Tasks")))

val textView2 = onView(
allOf(withId(R.id.title), withText("Notices"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
1)),
1),
isDisplayed()))
textView2.check(matches(withText("Notices")))

val textView3 = onView(
allOf(withId(R.id.title), withText("Projects"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
2)),
1),
isDisplayed()))
textView3.check(matches(withText("Projects")))

val textView4 = onView(
allOf(withId(R.id.title), withText("Add project"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
3)),
1),
isDisplayed()))
textView4.check(matches(withText("Add project")))

val textView5 = onView(
allOf(withId(R.id.title), withText("Preferences"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
4)),
1),
isDisplayed()))
textView5.check(matches(withText("Preferences")))

val textView6 = onView(
allOf(withId(R.id.title), withText("Help"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
5)),
1),
isDisplayed()))
textView6.check(matches(withText("Help")))

val textView7 = onView(
allOf(withId(R.id.title), withText("Report Issue"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
6)),
1),
isDisplayed()))
textView7.check(matches(withText("Report Issue")))

val textView8 = onView(
allOf(withId(R.id.title), withText("About"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
7)),
1),
isDisplayed()))
textView8.check(matches(withText("About")))

val textView9 = onView(
allOf(withId(R.id.title), withText("Event Log"),
childAtPosition(
allOf(withId(R.id.listitem),
childAtPosition(
withId(R.id.list_slidermenu),
8)),
1),
isDisplayed()))
textView9.check(matches(withText("Event Log")))
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
/*
* This file is part of BOINC.
* http://boinc.berkeley.edu
* Copyright (C) 2020 University of California
*
* BOINC is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* BOINC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with BOINC. If not, see <http://www.gnu.org/licenses/>.
*/
package edu.berkeley.boinc

import android.view.View
import android.view.ViewGroup
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.longClick
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsInstanceOf
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -87,19 +100,4 @@ class SplashActivityToEventLogTest {
isDisplayed()))
textView5.check(matches(withText("GUI MESSAGES")))
}

private fun childAtPosition(parentMatcher: Matcher<View>, position: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}

public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of BOINC.
* http://boinc.berkeley.edu
* Copyright (C) 2020 University of California
*
* BOINC is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* BOINC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with BOINC. If not, see <http://www.gnu.org/licenses/>.
*/
package edu.berkeley.boinc

import android.view.View
import android.view.ViewGroup
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher

fun childAtPosition(parentMatcher: Matcher<View>, position: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}

public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}

0 comments on commit 67801b6

Please sign in to comment.