Convert BaseUserInterfaceTest

pull/716/head
Quentin Hibon 5 years ago
parent 0fc9bb57ae
commit f882e18be9

@ -1,133 +0,0 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker 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.
*
* Loop Habit Tracker 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.isoron.uhabits;
import android.content.*;
import androidx.test.uiautomator.*;
import com.linkedin.android.testbutler.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.ui.screens.habits.list.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.inject.*;
import org.junit.*;
import static androidx.test.core.app.ApplicationProvider.*;
import static androidx.test.platform.app.InstrumentationRegistry.*;
import static androidx.test.uiautomator.UiDevice.*;
public class BaseUserInterfaceTest
{
private static final String PKG = "org.isoron.uhabits";
public static final String EMPTY_DESCRIPTION_HABIT_NAME = "Read books";
public static UiDevice device;
private HabitsApplicationComponent component;
private HabitList habitList;
private Preferences prefs;
private HabitFixtures fixtures;
private HabitCardListCache cache;
public static void startActivity(Class cls)
{
Intent intent = new Intent();
intent.setComponent(new ComponentName(PKG, cls.getCanonicalName()));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
}
@Before
public void setUp() throws Exception
{
device = getInstance(getInstrumentation());
TestButler.setup(getApplicationContext());
TestButler.verifyAnimationsDisabled(getApplicationContext());
HabitsApplication app =
(HabitsApplication) getApplicationContext().getApplicationContext();
component = app.getComponent();
habitList = component.getHabitList();
prefs = component.getPreferences();
cache = component.getHabitCardListCache();
fixtures = new HabitFixtures(component.getModelFactory(), habitList);
resetState();
}
@After
public void tearDown() throws Exception
{
for (int i = 0; i < 10; i++) device.pressBack();
TestButler.teardown(getApplicationContext());
}
private void resetState() throws Exception
{
prefs.clear();
prefs.setFirstRun(false);
prefs.updateLastHint(100, DateUtils.getToday());
habitList.removeAll();
cache.refreshAllHabits();
Thread.sleep(1000);
Habit h1 = fixtures.createEmptyHabit();
h1.setName("Wake up early");
h1.setQuestion("Did you wake up early today?");
h1.setDescription("test description 1");
h1.setColor(new PaletteColor(5));
habitList.update(h1);
Habit h2 = fixtures.createShortHabit();
h2.setName("Track time");
h2.setQuestion("Did you track your time?");
h2.setDescription("test description 2");
h2.setColor(new PaletteColor(5));
habitList.update(h2);
Habit h3 = fixtures.createLongHabit();
h3.setName("Meditate");
h3.setQuestion("Did meditate today?");
h3.setDescription("test description 3");
h3.setColor(new PaletteColor(10));
habitList.update(h3);
Habit h4 = fixtures.createEmptyHabit();
h4.setName(EMPTY_DESCRIPTION_HABIT_NAME);
h4.setQuestion("Did you read books today?");
h4.setDescription("");
h4.setColor(new PaletteColor(2));
habitList.update(h4);
}
protected void rotateDevice() throws Exception
{
device.setOrientationLeft();
device.setOrientationNatural();
}
}

@ -0,0 +1,118 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker 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.
*
* Loop Habit Tracker 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.isoron.uhabits
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.linkedin.android.testbutler.TestButler
import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.preferences.Preferences
import org.isoron.uhabits.core.ui.screens.habits.list.HabitCardListCache
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
import org.isoron.uhabits.inject.HabitsApplicationComponent
import org.junit.After
import org.junit.Before
open class BaseUserInterfaceTest {
private lateinit var component: HabitsApplicationComponent
private lateinit var habitList: HabitList
private lateinit var prefs: Preferences
private lateinit var fixtures: HabitFixtures
private lateinit var cache: HabitCardListCache
@Before
@Throws(Exception::class)
fun setUp() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
TestButler.setup(ApplicationProvider.getApplicationContext())
TestButler.verifyAnimationsDisabled(ApplicationProvider.getApplicationContext())
val app =
ApplicationProvider.getApplicationContext<Context>().applicationContext as HabitsApplication
component = app.component
habitList = component.habitList
prefs = component.preferences
cache = component.habitCardListCache
fixtures = HabitFixtures(component.modelFactory, habitList)
resetState()
}
@After
@Throws(Exception::class)
fun tearDown() {
for (i in 0..9) device.pressBack()
TestButler.teardown(ApplicationProvider.getApplicationContext())
}
@Throws(Exception::class)
private fun resetState() {
prefs.clear()
prefs.isFirstRun = false
prefs.updateLastHint(100, getToday())
habitList.removeAll()
cache.refreshAllHabits()
Thread.sleep(1000)
val h1 = fixtures.createEmptyHabit()
h1.name = "Wake up early"
h1.question = "Did you wake up early today?"
h1.description = "test description 1"
h1.color = PaletteColor(5)
habitList.update(h1)
val h2 = fixtures.createShortHabit()
h2.name = "Track time"
h2.question = "Did you track your time?"
h2.description = "test description 2"
h2.color = PaletteColor(5)
habitList.update(h2)
val h3 = fixtures.createLongHabit()
h3.name = "Meditate"
h3.question = "Did meditate today?"
h3.description = "test description 3"
h3.color = PaletteColor(10)
habitList.update(h3)
val h4 = fixtures.createEmptyHabit()
h4.name = EMPTY_DESCRIPTION_HABIT_NAME
h4.question = "Did you read books today?"
h4.description = ""
h4.color = PaletteColor(2)
habitList.update(h4)
}
@Throws(Exception::class)
protected fun rotateDevice() {
device!!.setOrientationLeft()
device!!.setOrientationNatural()
}
companion object {
private const val PKG = "org.isoron.uhabits"
const val EMPTY_DESCRIPTION_HABIT_NAME = "Read books"
lateinit var device: UiDevice
fun startActivity(cls: Class<*>) {
val intent = Intent()
intent.component = ComponentName(PKG, cls.canonicalName!!)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
ApplicationProvider.getApplicationContext<Context>().startActivity(intent)
}
}
}

@ -20,7 +20,7 @@
package org.isoron.uhabits.acceptance.steps
import androidx.test.uiautomator.UiSelector
import org.isoron.uhabits.BaseUserInterfaceTest.device
import org.isoron.uhabits.BaseUserInterfaceTest.Companion.device
import org.isoron.uhabits.acceptance.steps.CommonSteps.clickText
import org.isoron.uhabits.acceptance.steps.ListHabitsSteps.MenuItem.SETTINGS
import org.isoron.uhabits.acceptance.steps.ListHabitsSteps.clickMenu

Loading…
Cancel
Save