Convert KiwixSplashActivityTest.java into kotlin

This commit is contained in:
MohitMali 2022-10-20 12:50:37 +05:30 committed by Kelson
parent 0c3048737a
commit 39eca98451
2 changed files with 118 additions and 104 deletions

View File

@ -1,104 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.splash;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import com.adevinta.android.barista.interaction.BaristaSleepInteractions;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.main.KiwixMainActivity;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.assertEquals;
import static org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_SHOW_INTRO;
import static org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class KiwixSplashActivityTest {
private ActivityTestRule<KiwixMainActivity> activityTestRule =
new ActivityTestRule<>(KiwixMainActivity.class, true, false);
@Rule
public GrantPermissionRule readPermissionRule =
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE);
@Rule
public GrantPermissionRule writePermissionRule =
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
private Context context;
@Before
public void setUp() {
Intents.init();
context = getInstrumentation().getTargetContext();
}
@Test
public void testFirstRun() {
shouldShowIntro(true);
activityTestRule.launchActivity(new Intent());
BaristaSleepInteractions.sleep(TEST_PAUSE_MS);
onView(withId(R.id.get_started)).check(matches(isDisplayed()));
// Verify that the value of the "intro shown" boolean inside the SharedPreferences Database is not changed until the "Get started" button is pressed
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
assertEquals(true, preferences.getBoolean(PREF_SHOW_INTRO, true));
}
@Test
public void testNormalRun() {
shouldShowIntro(false);
activityTestRule.launchActivity(new Intent());
BaristaSleepInteractions.sleep(TEST_PAUSE_MS);
intended(hasComponent(KiwixMainActivity.class.getCanonicalName()));
}
@After
public void endTest() {
Intents.release();
}
private void shouldShowIntro(boolean value) {
SharedPreferences.Editor preferencesEditor =
PreferenceManager.getDefaultSharedPreferences(context).edit();
preferencesEditor.putBoolean(PREF_SHOW_INTRO, value).apply();
}
}

View File

@ -0,0 +1,118 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.splash
import android.Manifest
import android.content.Context
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.testutils.TestUtils
@LargeTest
@RunWith(AndroidJUnit4::class)
class KiwixSplashActivityTest {
private val activityScenario: ActivityScenario<KiwixMainActivity> =
ActivityScenario.launch(KiwixMainActivity::class.java)
@Rule
var readPermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
@Rule
var writePermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
private var context: Context? = null
@Before
fun setUp() {
Intents.init()
context = InstrumentationRegistry.getInstrumentation().targetContext
}
@Test
fun testFirstRun() {
shouldShowIntro(true)
activityScenario.recreate()
activityScenario.onActivity {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
Espresso.onView(ViewMatchers.withId(R.id.get_started))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
// Verify that the value of the "intro shown" boolean inside
// the SharedPreferences Database is not changed until
// the "Get started" button is pressed
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
Assert.assertEquals(
true,
preferences.getBoolean(
SharedPreferenceUtil.PREF_SHOW_INTRO,
true
)
)
}
}
@Test
fun testNormalRun() {
shouldShowIntro(false)
activityScenario.recreate()
activityScenario.onActivity {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
Intents.intended(
IntentMatchers.hasComponent(
KiwixMainActivity::class.java.canonicalName
)
)
}
}
@After
fun endTest() {
Intents.release()
}
private fun shouldShowIntro(value: Boolean) {
val preferencesEditor = PreferenceManager.getDefaultSharedPreferences(
context
).edit()
preferencesEditor.putBoolean(
SharedPreferenceUtil.PREF_SHOW_INTRO,
value
).commit()
}
}