#1212 move automated tests to same package

This commit is contained in:
Sean Mac Gillicuddy 2019-07-15 16:33:10 +01:00
parent 5641a54176
commit dc9008561e
39 changed files with 52 additions and 141 deletions

View File

@ -135,6 +135,7 @@ dependencies {
testImplementation "org.assertj:assertj-core:3.11.1"
testImplementation 'com.jraska.livedata:testing-ktx:1.1.0'
testImplementation 'androidx.arch.core:core-testing:2.0.1'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
// Set custom app import directory

View File

@ -0,0 +1,2 @@
package org.kiwix.kiwixmobile

View File

@ -0,0 +1,2 @@
package org.kiwix.kiwixmobile

View File

@ -0,0 +1,2 @@
package org.kiwix.kiwixmobile

View File

@ -45,7 +45,7 @@ public interface TestComponent extends ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance TestComponent.Builder context(Context context);
@BindsInstance Builder context(Context context);
TestComponent build();
}

View File

@ -0,0 +1,38 @@
/*
* Kiwix Android
* Copyright (C) 2018 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.intro
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.BaseActivityTest
@RunWith(AndroidJUnit4::class)
class IntroActivityKtTest : BaseActivityTest<IntroActivity>() {
@get:Rule
override var activityRule = activityTestRule<IntroActivity>()
@Test
fun viewIsSwipeableAnd() {
intro {
swipeLeft()
swipeRight()
} clickGetStarted { }
}
}

View File

@ -0,0 +1,2 @@
package org.kiwix.kiwixmobile.intro

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kiwix.kiwixmobile.data.local;
package org.kiwix.kiwixmobile.local;
import android.content.Context;
import androidx.test.InstrumentationRegistry;
@ -28,6 +28,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
import org.kiwix.kiwixmobile.data.local.dao.BookDao;
import org.kiwix.kiwixmobile.data.local.entity.BookDatabaseEntity;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kiwix.kiwixmobile.data.local;
package org.kiwix.kiwixmobile.local;
import com.yahoo.squidb.data.AbstractModel;
import com.yahoo.squidb.data.SquidCursor;
@ -24,6 +24,7 @@ import com.yahoo.squidb.sql.Query;
import org.junit.Before;
import org.junit.Test;
import org.kiwix.kiwixmobile.data.ZimContentProvider;
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
import org.kiwix.kiwixmobile.data.local.dao.RecentSearchDao;
import org.kiwix.kiwixmobile.data.local.entity.RecentSearch;
import org.mockito.Mock;

View File

@ -28,10 +28,6 @@ import com.schibsted.spain.barista.interaction.BaristaMenuClickInteractions;
import com.schibsted.spain.barista.interaction.BaristaSleepInteractions;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import okhttp3.mockwebserver.MockResponse;

View File

@ -1,32 +0,0 @@
This is the readme for testing Kiwix Android. As you can see there are already some tests in this directory and others.
The Directory structure works as follows:
* test - unit tests
* androidTest - instrumentation tests
* androidTestKiwix - instrumentation tests only for the Kiwix flavour (no custom apps)
Instrumentation tests mainly utilize [Espresso](https://developer.android.com/training/testing/ui-testing/espresso-testing.html)
although [UI Automator](https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html) is used for some
workarounds such as accepting permissions where out of app interaction is required.
Creating a new test is simple.
1. I advice using the test recorder in Android Studio to get the basic flow of your test down.
2. Note down any interactions which were not recordered.
3. Generate the test using the tool and give it an appropriate name.
4. Reimplement in code the interactions that were not recordered.
5. Listview interactions should be handled delicately view examples in DownloadTest.java
6. Idling resources e.g network and file system should be identified and hooked up to the test suite. There is a static method to call
binding your resource at the start and unbinding at the end. Espresso will pause itself while the resource is active. Call
TestingUtils.bindResource(MyClass.class); and TestingUtils.unbindResource(MyClass.class);
7. Other issues may arise so test.
8. (optional) Mock components of the app, swapping them out with dependency injection. (See NetworkTest.java and ZimTest.java)
Once generated tests with the correct annotations (created by creation tool) should be run automatically.
Once Travis builds an APK it pushes it to [testdroid](https://cloud.testdroid.com/) where it is run on a variety of devices.
The result is passed back to Travis and the build either passes or fails.
There is a possiblity of unrelated issues causing a test to fail e.g network failure, broken device. A rebuild in travis can be
triggered to try again and individual devices can be retried on testdroid.

View File

@ -1,102 +0,0 @@
/*
* Kiwix Android
* Copyright (C) 2018 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.intro;
import android.Manifest;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.intent.Intents;
import androidx.test.filters.LargeTest;
import androidx.test.rule.GrantPermissionRule;
import androidx.test.runner.AndroidJUnit4;
import com.schibsted.spain.barista.interaction.BaristaSleepInteractions;
import com.schibsted.spain.barista.rule.BaristaRule;
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.MainActivity;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.swipeLeft;
import static androidx.test.espresso.action.ViewActions.swipeRight;
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 androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class IntroActivityTest {
@Rule
public BaristaRule<IntroActivity> activityTestRule = BaristaRule.create(IntroActivity.class);
@Rule
public GrantPermissionRule readPermissionRule =
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE);
@Rule
public GrantPermissionRule writePermissionRule =
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
@Before
public void setUp() {
Intents.init();
activityTestRule.launchActivity();
}
@Test
public void testIntroActivity() {
BaristaSleepInteractions.sleep(TEST_PAUSE_MS);
ViewInteraction viewPager = onView(withId(R.id.view_pager));
// Verify that the sliding view is working properly
viewPager.perform(swipeLeft());
onView(allOf(withId(R.id.heading), withText(R.string.save_books_offline), isDisplayed()))
.check(matches(notNullValue()));
onView(allOf(withId(R.id.subheading), withText(R.string.download_books_message), isDisplayed()))
.check(matches(notNullValue()));
BaristaSleepInteractions.sleep(TEST_PAUSE_MS);
viewPager.perform(swipeRight());
onView(allOf(withId(R.id.heading), withText(R.string.welcome_to_the_family), isDisplayed()))
.check(matches(notNullValue()));
onView(allOf(withId(R.id.subheading), withText(R.string.human_kind_knowledge), isDisplayed()))
.check(matches(notNullValue()));
// Verify that the button is there
onView(withId(R.id.get_started)).check(matches(notNullValue()));
onView(withId(R.id.get_started)).check(matches(withText(R.string.get_started)));
onView(withId(R.id.get_started)).perform(click());
// Test the intent generated for MainActivity
intended(hasComponent(MainActivity.class.getName()));
}
@After
public void endTest() {
Intents.release();
}
}