mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 21:45:14 -04:00
#1212 add example of mocked network
This commit is contained in:
parent
04779c1099
commit
1ef34df9f3
@ -134,8 +134,15 @@ 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 "io.mockk:mockk-android:1.9"
|
||||
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
|
||||
androidTestImplementation "org.assertj:assertj-core:3.11.1"
|
||||
androidTestImplementation ("org.simpleframework:simple-xml:2.7.1"){
|
||||
exclude module: 'stax'
|
||||
exclude module: 'stax-api'
|
||||
exclude module: 'xpp3'
|
||||
}
|
||||
}
|
||||
|
||||
// Set custom app import directory
|
||||
|
@ -2,11 +2,16 @@ package org.kiwix.kiwixmobile
|
||||
|
||||
import android.Manifest.permission
|
||||
import android.app.Activity
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
|
||||
import androidx.test.rule.ActivityTestRule
|
||||
import androidx.test.rule.GrantPermissionRule
|
||||
import org.junit.Rule
|
||||
import org.junit.runner.RunWith
|
||||
import org.kiwix.kiwixmobile.di.components.DaggerTestComponent
|
||||
|
||||
abstract class BaseActivityTest<T: Activity> {
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
abstract class BaseActivityTest<T : Activity> {
|
||||
@get:Rule
|
||||
abstract var activityRule: ActivityTestRule<T>
|
||||
@get:Rule
|
||||
@ -16,6 +21,19 @@ abstract class BaseActivityTest<T: Activity> {
|
||||
var writePermissionRule =
|
||||
GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE)
|
||||
|
||||
inline fun <reified T : Activity> activityTestRule() =
|
||||
ActivityTestRule(T::class.java)
|
||||
val context by lazy {
|
||||
getInstrumentation().targetContext.applicationContext
|
||||
}
|
||||
|
||||
inline fun <reified T : Activity> activityTestRule(noinline beforeActivityAction: (() -> Unit)? = null) =
|
||||
object : ActivityTestRule<T>(T::class.java) {
|
||||
override fun beforeActivityLaunched() {
|
||||
super.beforeActivityLaunched()
|
||||
beforeActivityAction?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
protected fun testComponent() = DaggerTestComponent.builder()
|
||||
.context(context)
|
||||
.build()
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.kiwix.kiwixmobile
|
||||
|
||||
import android.R.id
|
||||
import android.app.Instrumentation
|
||||
import android.content.Context
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
@ -7,20 +8,31 @@ import androidx.test.uiautomator.Direction
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
import androidx.test.uiautomator.UiObject2
|
||||
import androidx.test.uiautomator.Until
|
||||
import org.kiwix.kiwixmobile.Findable.Text
|
||||
import org.kiwix.kiwixmobile.Findable.ViewId
|
||||
|
||||
const val WAIT_TIMEOUT_MS = 10000L
|
||||
|
||||
abstract class BaseRobot(
|
||||
val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
|
||||
private val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
|
||||
val context: Context = instrumentation.targetContext,
|
||||
val uiDevice: UiDevice = UiDevice.getInstance(instrumentation)
|
||||
private val uiDevice: UiDevice = UiDevice.getInstance(instrumentation)
|
||||
) {
|
||||
|
||||
protected fun isVisible(findable: Findable) =
|
||||
waitFor(findable) ?: throw RuntimeException(findable.errorMessage(this))
|
||||
internal fun clickNegativeDialogButton() {
|
||||
clickOn(ViewId(id.button2))
|
||||
}
|
||||
|
||||
protected fun waitFor(findable: Findable): UiObject2? =
|
||||
uiDevice.wait(Until.findObject(findable.selector(this)), WAIT_TIMEOUT_MS)
|
||||
internal fun clickPositiveDialogButton() {
|
||||
clickOn(ViewId(id.button1))
|
||||
}
|
||||
|
||||
internal fun pressBack() {
|
||||
uiDevice.pressBack()
|
||||
}
|
||||
|
||||
protected fun isVisible(findable: Findable, timeout: Long = WAIT_TIMEOUT_MS) =
|
||||
waitFor(findable, timeout) ?: throw RuntimeException(findable.errorMessage(this))
|
||||
|
||||
protected fun UiObject2.swipeLeft() {
|
||||
customSwipe(Direction.LEFT)
|
||||
@ -30,15 +42,32 @@ abstract class BaseRobot(
|
||||
customSwipe(Direction.RIGHT)
|
||||
}
|
||||
|
||||
private fun UiObject2.customSwipe(
|
||||
direction: Direction,
|
||||
fl: Float = 1.0f
|
||||
) {
|
||||
this.swipe(direction, fl)
|
||||
}
|
||||
|
||||
protected fun clickOn(findable: Findable) {
|
||||
isVisible(findable).click()
|
||||
}
|
||||
|
||||
protected fun longClickOn(findable: Findable) {
|
||||
isVisible(findable).click(2500L)
|
||||
}
|
||||
|
||||
protected fun clickOnTab(textId: Int) {
|
||||
clickOn(Text(context.getString(textId).toUpperCase()))
|
||||
}
|
||||
|
||||
protected fun waitFor(milliseconds: Long) {
|
||||
waitFor(Text("WILL_NEVER_EXIST"), milliseconds)
|
||||
}
|
||||
|
||||
private fun waitFor(
|
||||
findable: Findable,
|
||||
timeout: Long = WAIT_TIMEOUT_MS
|
||||
): UiObject2? =
|
||||
uiDevice.wait(Until.findObject(findable.selector(this)), timeout)
|
||||
|
||||
private fun UiObject2.customSwipe(
|
||||
direction: Direction,
|
||||
fl: Float = 1.0f
|
||||
) {
|
||||
swipe(direction, fl)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,139 @@
|
||||
package org.kiwix.kiwixmobile
|
||||
|
||||
import okhttp3.mockwebserver.Dispatcher
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import okhttp3.mockwebserver.MockWebServer
|
||||
import okhttp3.mockwebserver.RecordedRequest
|
||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
|
||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
|
||||
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity
|
||||
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.FileElement
|
||||
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Pieces
|
||||
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Url
|
||||
import org.simpleframework.xml.core.Persister
|
||||
import java.io.StringWriter
|
||||
import java.util.LinkedList
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
class KiwixMockServer {
|
||||
val mockWebServer = MockWebServer().apply {
|
||||
start(8080)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
mockWebServer.shutdown()
|
||||
}
|
||||
|
||||
fun enqueue(libraryNetworkEntity: LibraryNetworkEntity) {
|
||||
mockWebServer.enqueue(MockResponse().apply {
|
||||
setResponseCode(200)
|
||||
setBody(libraryNetworkEntity.asXmlString())
|
||||
})
|
||||
}
|
||||
|
||||
fun enqueueForEvery(
|
||||
pathsToResponses: Map<String, Any>
|
||||
) {
|
||||
mockWebServer.setDispatcher(object : Dispatcher() {
|
||||
override fun dispatch(request: RecordedRequest) =
|
||||
pathsToResponses[request.path]?.let { successfulResponse(it) }
|
||||
?: MockResponse().throttleBody(1L, 1L, TimeUnit.SECONDS).setBody("0123456789")
|
||||
})
|
||||
}
|
||||
|
||||
private fun successfulResponse(bodyObject: Any) = MockResponse().apply {
|
||||
setResponseCode(200)
|
||||
setBody(bodyObject.asXmlString())
|
||||
}
|
||||
}
|
||||
|
||||
fun Any.asXmlString() = StringWriter().let {
|
||||
Persister().write(this, it)
|
||||
it.toString()
|
||||
}
|
||||
|
||||
fun metaLinkNetworkEntity() = MetaLinkNetworkEntity().apply {
|
||||
file = fileElement()
|
||||
}
|
||||
|
||||
fun fileElement(
|
||||
urls: List<Url> = listOf(
|
||||
url()
|
||||
),
|
||||
name: String = "name",
|
||||
hashes: Map<String, String> = mapOf("hash" to "hash"),
|
||||
pieces: Pieces = pieces()
|
||||
) = FileElement().apply {
|
||||
this.name = name
|
||||
this.urls = urls
|
||||
this.hashes = hashes
|
||||
this.pieces = pieces
|
||||
}
|
||||
|
||||
fun pieces(
|
||||
hashType: String = "hashType",
|
||||
pieceHashes: List<String> = listOf("hash")
|
||||
) = Pieces().apply {
|
||||
this.hashType = hashType
|
||||
this.pieceHashes = pieceHashes
|
||||
}
|
||||
|
||||
fun url(
|
||||
value: String = "http://localhost:8080/relevantUrl.zim.meta4",
|
||||
location: String = "location"
|
||||
) = Url().apply {
|
||||
this.location = location
|
||||
this.value = value
|
||||
}
|
||||
|
||||
fun book(
|
||||
id: String = "id",
|
||||
title: String = "title",
|
||||
description: String = "description",
|
||||
language: String = "eng",
|
||||
creator: String = "creator",
|
||||
publisher: String = "publisher",
|
||||
date: String = "date",
|
||||
url: String = "http://localhost:8080/url",
|
||||
articleCount: String = "mediaCount",
|
||||
mediaCount: String = "mediaCount",
|
||||
size: String = "1024",
|
||||
name: String = "name",
|
||||
favIcon: String = "favIcon"
|
||||
) =
|
||||
Book().apply {
|
||||
this.id = id
|
||||
this.title = title
|
||||
this.description = description
|
||||
this.language = language
|
||||
this.creator = creator
|
||||
this.publisher = publisher
|
||||
this.date = date
|
||||
this.url = url
|
||||
this.articleCount = articleCount
|
||||
this.mediaCount = mediaCount
|
||||
this.size = size
|
||||
this.bookName = name
|
||||
this.favicon = favIcon
|
||||
}
|
||||
|
||||
fun libraryNetworkEntity(books: List<Book> = emptyList()) = LibraryNetworkEntity().apply {
|
||||
book = LinkedList(books)
|
||||
}
|
@ -1,58 +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.di.components;
|
||||
|
||||
import android.content.Context;
|
||||
import dagger.BindsInstance;
|
||||
import dagger.Component;
|
||||
import javax.inject.Singleton;
|
||||
import org.kiwix.kiwixmobile.data.DataModule;
|
||||
import org.kiwix.kiwixmobile.di.modules.ApplicationModule;
|
||||
import org.kiwix.kiwixmobile.di.modules.JNIModule;
|
||||
import org.kiwix.kiwixmobile.di.modules.TestNetworkModule;
|
||||
import org.kiwix.kiwixmobile.tests.NetworkTest;
|
||||
import org.kiwix.kiwixmobile.tests.ZimTest;
|
||||
import org.kiwix.kiwixmobile.utils.TestNetworkInterceptor;
|
||||
|
||||
/**
|
||||
* Created by mhutti1 on 13/04/17.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Component(modules = {
|
||||
ApplicationModule.class,
|
||||
TestNetworkModule.class,
|
||||
JNIModule.class,
|
||||
DataModule.class
|
||||
})
|
||||
public interface TestComponent extends ApplicationComponent {
|
||||
|
||||
@Component.Builder
|
||||
interface Builder {
|
||||
|
||||
@BindsInstance Builder context(Context context);
|
||||
|
||||
TestComponent build();
|
||||
}
|
||||
|
||||
void inject(ZimTest zimTest);
|
||||
|
||||
void inject(NetworkTest networkTest);
|
||||
|
||||
void inject(TestNetworkInterceptor testNetworkInterceptor);
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.di.components
|
||||
|
||||
import android.content.Context
|
||||
import dagger.BindsInstance
|
||||
import dagger.Component
|
||||
import org.kiwix.kiwixmobile.data.DataModule
|
||||
import org.kiwix.kiwixmobile.di.modules.ApplicationModule
|
||||
import org.kiwix.kiwixmobile.di.modules.JNIModule
|
||||
import org.kiwix.kiwixmobile.di.modules.TestNetworkModule
|
||||
import org.kiwix.kiwixmobile.tests.NetworkTest
|
||||
import org.kiwix.kiwixmobile.tests.ZimTest
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Created by mhutti1 on 13/04/17.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Component(modules = [ApplicationModule::class, TestNetworkModule::class, JNIModule::class, DataModule::class])
|
||||
interface TestComponent : ApplicationComponent {
|
||||
|
||||
@Component.Builder
|
||||
interface Builder {
|
||||
|
||||
@BindsInstance fun context(context: Context): Builder
|
||||
fun build(): TestComponent
|
||||
}
|
||||
|
||||
fun inject(zimTest: ZimTest)
|
||||
fun inject(networkTest: NetworkTest)
|
||||
}
|
@ -1,83 +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.di.modules;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import java.io.IOException;
|
||||
import javax.inject.Singleton;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import org.kiwix.kiwixmobile.data.remote.KiwixService;
|
||||
import org.kiwix.kiwixmobile.utils.TestNetworkInterceptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* Created by mhutti1 on 14/04/17.
|
||||
*/
|
||||
|
||||
@Module
|
||||
public class TestNetworkModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
OkHttpClient provideOkHttpClient() {
|
||||
return new OkHttpClient().newBuilder()
|
||||
.followRedirects(true)
|
||||
.followSslRedirects(true)
|
||||
.addInterceptor(new TestNetworkInterceptor())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
KiwixService provideKiwixService(OkHttpClient okHttpClient, MockWebServer mockWebServer) {
|
||||
return KiwixService.ServiceCreator.newHacklistService(okHttpClient,
|
||||
mockWebServer.url("/").toString());
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
MockWebServer provideMockWebServer() {
|
||||
MockWebServer mockWebServer = new MockWebServer();
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
mockWebServer.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
|
||||
return mockWebServer;
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
ConnectivityManager provideConnectivityManager(Context context) {
|
||||
ConnectivityManager connectivityManager = Mockito.mock(ConnectivityManager.class);
|
||||
NetworkInfo networkInfo = Mockito.mock(NetworkInfo.class);
|
||||
doReturn(true).when(networkInfo).isConnected();
|
||||
doReturn(networkInfo).when(connectivityManager).getActiveNetworkInfo();
|
||||
return connectivityManager;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.di.modules
|
||||
|
||||
import dagger.Module
|
||||
import okhttp3.OkHttpClient
|
||||
import org.kiwix.kiwixmobile.data.remote.KiwixService
|
||||
|
||||
/**
|
||||
* Created by mhutti1 on 14/04/17.
|
||||
*/
|
||||
|
||||
@Module
|
||||
class TestNetworkModule : NetworkModule() {
|
||||
internal override fun provideKiwixService(okHttpClient: OkHttpClient): KiwixService =
|
||||
KiwixService.ServiceCreator.newHacklistService(
|
||||
okHttpClient,
|
||||
"http://localhost:8080/"
|
||||
)
|
||||
}
|
@ -17,19 +17,17 @@
|
||||
*/
|
||||
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 IntroActivityTest : BaseActivityTest<IntroActivity>() {
|
||||
@get:Rule
|
||||
override var activityRule = activityTestRule<IntroActivity>()
|
||||
|
||||
@Test
|
||||
fun viewIsSwipeableAnd() {
|
||||
fun viewIsSwipeableAndNavigatesToMain() {
|
||||
intro {
|
||||
swipeLeft()
|
||||
swipeRight()
|
||||
|
@ -0,0 +1,66 @@
|
||||
package org.kiwix.kiwixmobile.manage
|
||||
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.kiwix.kiwixmobile.BaseActivityTest
|
||||
import org.kiwix.kiwixmobile.KiwixApplication
|
||||
import org.kiwix.kiwixmobile.KiwixMockServer
|
||||
import org.kiwix.kiwixmobile.book
|
||||
import org.kiwix.kiwixmobile.libraryNetworkEntity
|
||||
import org.kiwix.kiwixmobile.metaLinkNetworkEntity
|
||||
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity
|
||||
|
||||
class ZimManageActivityTest : BaseActivityTest<ZimManageActivity>() {
|
||||
@get:Rule
|
||||
override var activityRule = activityTestRule<ZimManageActivity> {
|
||||
KiwixApplication.setApplicationComponent(testComponent())
|
||||
}
|
||||
|
||||
private val libraryNetworkEntity = libraryNetworkEntity(listOf(book()))
|
||||
private val mockServer = KiwixMockServer().apply {
|
||||
enqueueForEvery(
|
||||
mapOf(
|
||||
"/library/library_zim.xml" to libraryNetworkEntity,
|
||||
"/${libraryNetworkEntity.book[0].url.substringAfterLast("/")}" to metaLinkNetworkEntity()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testZimManageDataFlow() {
|
||||
zimManage {
|
||||
clickOnOnline {
|
||||
clickOnSearch()
|
||||
searchFor(libraryNetworkEntity(listOf(book(title = "zzzzz"))))
|
||||
waitForEmptyView()
|
||||
searchFor(libraryNetworkEntity)
|
||||
pressBack()
|
||||
pressBack()
|
||||
clickOn(libraryNetworkEntity)
|
||||
}
|
||||
clickOnDownloading {
|
||||
clickStop()
|
||||
clickNegativeDialogButton()
|
||||
clickStop()
|
||||
clickPositiveDialogButton()
|
||||
}
|
||||
clickOnOnline {
|
||||
clickOn(libraryNetworkEntity)
|
||||
}
|
||||
clickOnDownloading {
|
||||
waitForEmptyView()
|
||||
}
|
||||
clickOnDevice {
|
||||
longClickOn(libraryNetworkEntity)
|
||||
clickCloseActionMode()
|
||||
longClickOn(libraryNetworkEntity)
|
||||
clickDelete()
|
||||
clickNegativeDialogButton()
|
||||
longClickOn(libraryNetworkEntity)
|
||||
clickDelete()
|
||||
clickPositiveDialogButton()
|
||||
waitForEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.manage
|
||||
|
||||
import org.kiwix.kiwixmobile.BaseRobot
|
||||
import org.kiwix.kiwixmobile.Findable.Text
|
||||
import org.kiwix.kiwixmobile.Findable.ViewId
|
||||
import org.kiwix.kiwixmobile.R
|
||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
|
||||
|
||||
fun zimManage(func: ZimManageRobot.() -> Unit) = ZimManageRobot().apply(func)
|
||||
|
||||
class ZimManageRobot : BaseRobot() {
|
||||
init {
|
||||
isVisible(ViewId(R.id.manageViewPager))
|
||||
}
|
||||
|
||||
fun clickOnOnline(func: LibraryRobot.() -> Unit): LibraryRobot {
|
||||
clickOnTab(R.string.remote_zims)
|
||||
return library(func)
|
||||
}
|
||||
|
||||
fun clickOnDownloading(func: DownloadRobot.() -> Unit): DownloadRobot {
|
||||
clickOnTab(R.string.zim_downloads)
|
||||
return download(func)
|
||||
}
|
||||
|
||||
fun clickOnDevice(func: DeviceRobot.() -> Unit): DeviceRobot {
|
||||
clickOnTab(R.string.local_zims)
|
||||
return device(func)
|
||||
}
|
||||
|
||||
private fun library(func: LibraryRobot.() -> Unit) = LibraryRobot().apply(func)
|
||||
inner class LibraryRobot : BaseRobot() {
|
||||
init {
|
||||
isVisible(ViewId(R.id.libraryList))
|
||||
}
|
||||
|
||||
fun clickText(text: String) {
|
||||
clickOn(Text(text))
|
||||
}
|
||||
|
||||
fun clickOn(libraryNetworkEntity: LibraryNetworkEntity) {
|
||||
clickOn(Text(libraryNetworkEntity.book[0].title))
|
||||
}
|
||||
|
||||
fun clickOnSearch() {
|
||||
clickOn(ViewId(R.id.action_search))
|
||||
}
|
||||
|
||||
fun searchFor(libraryNetworkEntity: LibraryNetworkEntity) {
|
||||
isVisible(ViewId(R.id.search_src_text)).text= libraryNetworkEntity.book[0].title
|
||||
}
|
||||
|
||||
fun waitForEmptyView() {
|
||||
isVisible(ViewId(R.id.libraryErrorText))
|
||||
}
|
||||
}
|
||||
|
||||
private fun download(func: DownloadRobot.() -> Unit) = DownloadRobot().apply(func)
|
||||
inner class DownloadRobot : BaseRobot() {
|
||||
init {
|
||||
isVisible(ViewId(R.id.zim_downloader_list))
|
||||
}
|
||||
|
||||
fun clickStop() {
|
||||
clickOn(ViewId(R.id.stop))
|
||||
}
|
||||
|
||||
fun waitForEmptyView() {
|
||||
isVisible(ViewId(R.id.download_management_no_downloads), 11000L)
|
||||
}
|
||||
}
|
||||
|
||||
private fun device(func: DeviceRobot.() -> Unit) = DeviceRobot().apply(func)
|
||||
inner class DeviceRobot : BaseRobot() {
|
||||
init {
|
||||
isVisible(ViewId(R.id.zimfilelist))
|
||||
}
|
||||
|
||||
fun longClickOn(libraryNetworkEntity: LibraryNetworkEntity) {
|
||||
longClickOn(Text(libraryNetworkEntity.book[0].title))
|
||||
}
|
||||
|
||||
fun clickCloseActionMode() {
|
||||
clickOn(ViewId(R.id.action_mode_close_button))
|
||||
}
|
||||
|
||||
fun clickDelete() {
|
||||
clickOn(ViewId(R.id.zim_file_delete_item))
|
||||
}
|
||||
|
||||
fun waitForEmptyView() {
|
||||
isVisible(ViewId(R.id.file_management_no_files))
|
||||
}
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ import static org.kiwix.kiwixmobile.utils.StandardActions.enterSettings;
|
||||
|
||||
@LargeTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class SettingsTest {
|
||||
public class SettingsActivityTest {
|
||||
@Rule
|
||||
public ActivityTestRule<MainActivity> activityTestRule =
|
||||
new ActivityTestRule<>(MainActivity.class);
|
@ -1,31 +0,0 @@
|
||||
package org.kiwix.kiwixmobile.tests.activities;
|
||||
|
||||
import android.Manifest;
|
||||
import androidx.test.filters.LargeTest;
|
||||
import androidx.test.rule.ActivityTestRule;
|
||||
import androidx.test.rule.GrantPermissionRule;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
|
||||
|
||||
@LargeTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class SettingsActivityTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<KiwixSettingsActivity> mActivityTestRule = new ActivityTestRule<>(
|
||||
KiwixSettingsActivity.class);
|
||||
@Rule
|
||||
public GrantPermissionRule readPermissionRule =
|
||||
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||
@Rule
|
||||
public GrantPermissionRule writePermissionRule =
|
||||
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||
|
||||
@Test
|
||||
public void SettingsActivitySimple() {
|
||||
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package org.kiwix.kiwixmobile.tests.activities;
|
||||
|
||||
import android.Manifest;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.filters.LargeTest;
|
||||
import androidx.test.rule.ActivityTestRule;
|
||||
import androidx.test.rule.GrantPermissionRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity;
|
||||
|
||||
@LargeTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ZimManageActivityTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<ZimManageActivity> mActivityTestRule = new ActivityTestRule<>(
|
||||
ZimManageActivity.class);
|
||||
@Rule
|
||||
public GrantPermissionRule readPermissionRule =
|
||||
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||
@Rule
|
||||
public GrantPermissionRule writePermissionRule =
|
||||
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||
|
||||
@Test
|
||||
public void ZimManageActivitySimple() {
|
||||
|
||||
}
|
||||
}
|
@ -1,50 +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.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.inject.Inject;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import org.kiwix.kiwixmobile.KiwixApplication;
|
||||
import org.kiwix.kiwixmobile.di.components.TestComponent;
|
||||
|
||||
/**
|
||||
* Created by mhutti1 on 18/04/17.
|
||||
*/
|
||||
|
||||
public class TestNetworkInterceptor implements Interceptor {
|
||||
|
||||
@Inject MockWebServer mockWebServer;
|
||||
|
||||
public TestNetworkInterceptor() {
|
||||
((TestComponent) KiwixApplication.getInstance().getApplicationComponent()).inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request originalRequest = chain.request();
|
||||
|
||||
Request compressedRequest = originalRequest.newBuilder()
|
||||
.url(mockWebServer.url("/").toString())
|
||||
.build();
|
||||
return chain.proceed(compressedRequest);
|
||||
}
|
||||
}
|
@ -39,11 +39,15 @@ public abstract class BaseActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
AndroidInjection.inject(this);
|
||||
injection();
|
||||
super.onCreate(savedInstanceState);
|
||||
LanguageUtils.handleLocaleChange(this, sharedPreferenceUtil);
|
||||
}
|
||||
|
||||
protected void injection() {
|
||||
AndroidInjection.inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resources.Theme getTheme() {
|
||||
Resources.Theme theme = super.getTheme();
|
||||
|
@ -21,6 +21,7 @@ import android.content.Context;
|
||||
import dagger.BindsInstance;
|
||||
import dagger.Component;
|
||||
import javax.inject.Singleton;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.kiwix.kiwixmobile.KiwixApplication;
|
||||
import org.kiwix.kiwixmobile.data.DataModule;
|
||||
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
||||
@ -32,6 +33,7 @@ import org.kiwix.kiwixmobile.main.KiwixWebView;
|
||||
import org.kiwix.kiwixmobile.search.AutoCompleteAdapter;
|
||||
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
|
||||
import org.kiwix.kiwixmobile.zim_manager.DownloadNotificationClickedReceiver;
|
||||
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity;
|
||||
|
||||
@Singleton
|
||||
@Component(modules = {
|
||||
@ -48,8 +50,8 @@ public interface ApplicationComponent {
|
||||
@BindsInstance Builder context(Context context);
|
||||
|
||||
ApplicationComponent build();
|
||||
}
|
||||
|
||||
}
|
||||
ActivityComponent.Builder activityComponent();
|
||||
|
||||
void inject(KiwixApplication application);
|
||||
@ -65,4 +67,6 @@ public interface ApplicationComponent {
|
||||
void inject(AutoCompleteAdapter autoCompleteAdapter);
|
||||
|
||||
void inject(DownloadNotificationClickedReceiver downloadNotificationClickedReceiver);
|
||||
|
||||
void inject(@NotNull ZimManageActivity zimManageActivity);
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import org.simpleframework.xml.Text;
|
||||
public class MetaLinkNetworkEntity {
|
||||
|
||||
@Element
|
||||
private FileElement file;
|
||||
public FileElement file;
|
||||
|
||||
public List<Url> getUrls() {
|
||||
return file.urls;
|
||||
@ -47,15 +47,15 @@ public class MetaLinkNetworkEntity {
|
||||
@Root(strict = false)
|
||||
public static class FileElement {
|
||||
@Attribute
|
||||
private String name;
|
||||
public String name;
|
||||
@ElementList(inline = true, entry = "url")
|
||||
private List<Url> urls;
|
||||
public List<Url> urls;
|
||||
@Element
|
||||
private long size;
|
||||
@ElementMap(entry = "hash", key = "type", attribute = true, inline = true)
|
||||
private Map<String, String> hashes;
|
||||
public Map<String, String> hashes;
|
||||
@Element
|
||||
private Pieces pieces;
|
||||
public Pieces pieces;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@ -88,26 +88,26 @@ public class MetaLinkNetworkEntity {
|
||||
}
|
||||
}
|
||||
|
||||
private static class Pieces {
|
||||
public static class Pieces {
|
||||
@Attribute
|
||||
private int length;
|
||||
|
||||
@Attribute(name = "type")
|
||||
private String hashType;
|
||||
public String hashType;
|
||||
|
||||
@ElementList(inline = true, entry = "hash")
|
||||
private List<String> pieceHashes;
|
||||
public List<String> pieceHashes;
|
||||
}
|
||||
|
||||
public static class Url {
|
||||
@Attribute
|
||||
private String location;
|
||||
public String location;
|
||||
|
||||
@Attribute
|
||||
private int priority;
|
||||
|
||||
@Text
|
||||
private String value;
|
||||
public String value;
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
|
@ -34,6 +34,7 @@ import io.reactivex.schedulers.Schedulers
|
||||
import kotlinx.android.synthetic.main.zim_manager.manageViewPager
|
||||
import kotlinx.android.synthetic.main.zim_manager.tabs
|
||||
import kotlinx.android.synthetic.main.zim_manager.toolbar
|
||||
import org.kiwix.kiwixmobile.KiwixApplication
|
||||
import org.kiwix.kiwixmobile.R
|
||||
import org.kiwix.kiwixmobile.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao
|
||||
@ -61,6 +62,9 @@ class ZimManageActivity : BaseActivity() {
|
||||
|
||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||
@Inject lateinit var languagesDao: NewLanguagesDao
|
||||
override fun injection() {
|
||||
KiwixApplication.getApplicationComponent().inject(this)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
@ -12,7 +12,6 @@
|
||||
android:id="@+id/libraryErrorText"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/get_library_over_network"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
|
Loading…
x
Reference in New Issue
Block a user