Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of settings page #62

Merged
merged 8 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'com.android.application'
id 'com.google.android.gms.oss-licenses-plugin'
id 'kotlin-android'
id 'jacoco'
}
Expand All @@ -11,6 +12,14 @@ jacoco {
}

android {
signingConfigs {
release {
storeFile file('key.jks')
storePassword '123456'
keyPassword '123456'
keyAlias 'key0'
}
}
compileSdk 31

defaultConfig {
Expand All @@ -21,12 +30,14 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.debug
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
testCoverageEnabled true
Expand Down Expand Up @@ -69,10 +80,11 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.preference:preference-ktx:1.2.0'

androidTestImplementation 'org.jetbrains.kotlin:kotlin-test'
androidTestImplementation(platform("org.junit:junit-bom:5.8.2"))
androidTestImplementation("org.junit.jupiter:junit-jupiter")
// androidTestImplementation 'org.jetbrains.kotlin:kotlin-test'
// androidTestImplementation(platform("org.junit:junit-bom:5.8.2"))
// androidTestImplementation("org.junit.jupiter:junit-jupiter")

implementation 'org.jetbrains.kotlin:kotlin-test'
implementation(platform("org.junit:junit-bom:5.8.2"))
Expand Down Expand Up @@ -106,6 +118,10 @@ dependencies {
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0-native-mt'

testImplementation 'org.mockito:mockito-core:4.4.0'

implementation 'com.google.android.material:material:1.5.0'

implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'
}


Expand Down
20 changes: 20 additions & 0 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "ch.epfl.sweng.rps",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0",
"outputFile": "app-release.apk"
}
],
"elementType": "File"
}
27 changes: 13 additions & 14 deletions app/src/androidTest/java/ch/epfl/sweng/rps/GameButtonsTest.kt
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
package ch.epfl.sweng.rps

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

@RunWith(AndroidJUnit4::class)
@SmallTest
class GameButtonsTest {
@get:Rule
val testRule = ActivityScenarioRule(MainActivity::class.java)

@Test
fun pressedRock(){
fun pressedRock() {
checkPressedButton(R.id.rockRB)
}

@Test
fun pressedPaper(){
fun pressedPaper() {
checkPressedButton(R.id.paperRB)
}

@Test
fun pressedScissors() {
checkPressedButton(R.id.scissorsRB)
}
private fun checkPressedButton(radioButtonId: Int){
onView(withId(R.id.button_play_one_offline_game)).perform(click())

private fun checkPressedButton(radioButtonId: Int) {
onView(withId(R.id.button_play_1_games_offline)).perform(click())
onView(withId(radioButtonId)).perform(click())
onView(withId(radioButtonId)).check(matches(isChecked()))
}

@Test
fun pressedRockPaperRock(){
onView(withId(R.id.button_play_one_offline_game)).perform(click())
fun pressedRockPaperRock() {
onView(withId(R.id.button_play_1_games_offline)).perform(click())
onView(withId(R.id.rockRB)).perform(click())
onView(withId(R.id.paperRB)).perform(click())
onView(withId(R.id.rockRB)).perform(click())
onView(withId(R.id.rockRB)).check(matches(isChecked()))
onView(withId(R.id.paperRB)).check(matches(not(isChecked())))
onView(withId(R.id.scissorsRB)).check(matches(not(isChecked())))
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package ch.epfl.sweng.rps


import android.view.Gravity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerActions
import androidx.test.espresso.contrib.DrawerMatchers.isClosed
import androidx.test.espresso.contrib.NavigationViewActions
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
Expand Down
67 changes: 39 additions & 28 deletions app/src/androidTest/java/ch/epfl/sweng/rps/ProfileFragmentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,60 @@ package ch.epfl.sweng.rps

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerActions
import androidx.test.espresso.contrib.DrawerMatchers
import androidx.test.espresso.contrib.NavigationViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.rule.ActivityTestRule
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import ch.epfl.sweng.rps.models.User
import org.junit.Before
import org.junit.Rule
import org.junit.Test


class ProfileFragmentTest {
val i: Intent = Intent()
val b: Bundle = Bundle()
val data = mapOf<String, String>(
"email" to "[email protected]",
"display_name" to "asdino",
"uid" to "123",
"privacy" to User.Privacy.PUBLIC.toString()
)

@Before
fun setUpIntent() {
data.forEach { b.putString(it.key, it.value) }
i.putExtra("User", b)

private val bundle = run {
val b: Bundle = Bundle()
val data = mapOf(
"email" to "[email protected]",
"display_name" to "asdino",
"uid" to "123",
"privacy" to User.Privacy.PUBLIC.toString()
)
data.forEach { (k, v) -> b.putString(k, v) }
b
}


private fun createIntent(): Intent {
val i: Intent = Intent(
InstrumentationRegistry.getInstrumentation().targetContext,
MainActivity::class.java
)
i.putExtra("User", bundle)
return i
}

@get:Rule
val testRule = ActivityTestRule(MainActivity::class.java, false, false)
val testRule = ActivityScenarioRule<MainActivity>(createIntent())

@Test
fun testFields() {
testRule.launchActivity(i)
onView(withId(R.id.nav_profile)).perform(click())
onView(withId(R.id.TextDisplayName)).check(matches(withText(b.getString("display_name"))))
onView(withId(R.id.TextEmail)).check(matches(withText(b.getString("email"))))
onView(withId(R.id.TextPrivacy)).check(matches(withText(b.getString("privacy"))))
onView(withId(R.id.TextDisplayName)).check(matches(withText(bundle.getString("display_name"))))
onView(withId(R.id.TextEmail)).check(matches(withText(bundle.getString("email"))))
onView(withId(R.id.TextPrivacy)).check(matches(withText(bundle.getString("privacy"))))
}

@Test
fun tapSettings() {
onView(withId(R.id.nav_profile)).perform(click())
onView(withId(R.id.profile_appbar_settings_btn)).perform(click())
onView(withId(R.id.settings)).check(matches(isDisplayed()))
pressBack()
onView(withId(R.id.profile_appbar_settings_btn)).check(matches(isDisplayed()))
}
}
105 changes: 105 additions & 0 deletions app/src/androidTest/java/ch/epfl/sweng/rps/SettingsPageTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ch.epfl.sweng.rps

import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.Lifecycle
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.platform.app.InstrumentationRegistry.getInstrumentation
import ch.epfl.sweng.rps.TestUtils.getActivityInstance
import ch.epfl.sweng.rps.TestUtils.waitFor
import ch.epfl.sweng.rps.ui.settings.SettingsActivity
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import org.hamcrest.Matchers.instanceOf
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.runner.RunWith
import java.util.concurrent.FutureTask


@RunWith(AndroidJUnit4::class)
class SettingsPageTest {
@get:Rule
val scenarioRule = ActivityScenarioRule(SettingsActivity::class.java)

private fun computeThemeMap(): List<Map.Entry<String, String>> {
val targetContext = getInstrumentation().targetContext
val entries = targetContext.resources.getStringArray(R.array.theme_entries)
val values = targetContext.resources.getStringArray(R.array.theme_values)
val l = entries.zip(values)
return (l + l).toMap().entries.toList()
}

private val themeIdToAppCompatThemeId = mapOf(
"light" to AppCompatDelegate.MODE_NIGHT_NO,
"dark" to AppCompatDelegate.MODE_NIGHT_YES,
"system" to AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
)

private fun getAppCompatThemeFromThemeId(themeId: String): Int {
return themeIdToAppCompatThemeId[themeId]!!
}

private fun getThemeIdFromAppCompatTheme(appCompatThemeId: Int): String {
val filtered = themeIdToAppCompatThemeId.entries.filter { it.value == appCompatThemeId }
if (filtered.isEmpty()) {
throw IllegalArgumentException("No theme id found for appCompatThemeId $appCompatThemeId")
}
return filtered.first().key
}


private fun getCurrentNightMode(): Int {
val activity = getActivityInstance<SettingsActivity>()

val futureResult = FutureTask { AppCompatDelegate.getDefaultNightMode() }

activity.runOnUiThread(futureResult)

return futureResult.get()
}

@Test
fun testSettingsPage() {
onView(withId(R.id.settings)).check(matches(isDisplayed()))

// the activity's onCreate, onStart and onResume methods have been called at this point
scenarioRule.scenario.moveToState(Lifecycle.State.STARTED)
// the activity's onPause method has been called at this point
scenarioRule.scenario.moveToState(Lifecycle.State.RESUMED)

for (entry in computeThemeMap()) {
Log.i("SettingsPageTest", "Testing theme ${entry.key}")
onView(withText(R.string.theme_mode)).perform(click())
onView(withText(entry.key)).perform(click())
onView(isRoot()).perform(waitFor(1000))

val appCompatThemeId = getCurrentNightMode()

assertEquals(
getAppCompatThemeFromThemeId(entry.value),
appCompatThemeId,
"Theme should be ${entry.value} (${getAppCompatThemeFromThemeId(entry.value)}) after clicking ${entry.key}, but is $appCompatThemeId (${
getThemeIdFromAppCompatTheme(appCompatThemeId)
})"
)

}
}


@Test
fun testSettingsPageLicense() {
onView(withId(R.id.settings)).check(matches(isDisplayed()))
onView(withText(R.string.license_title)).perform(click())

val currentActivity = getActivityInstance<OssLicensesMenuActivity>()
assertThat(currentActivity, instanceOf(OssLicensesMenuActivity::class.java))
onView(withText(R.string.oss_license_title)).check(matches(isDisplayed()))
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package ch.epfl.sweng.rps

import android.view.Gravity
import android.view.View
import androidx.test.espresso.Espresso
import androidx.test.espresso.PerformException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.contrib.DrawerActions
import androidx.test.espresso.contrib.DrawerMatchers
import androidx.test.espresso.contrib.NavigationViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.util.HumanReadables
import androidx.test.espresso.util.TreeIterables
import androidx.test.ext.junit.rules.ActivityScenarioRule
import org.hamcrest.Matcher
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.TimeoutException


Expand Down
Loading