#1212 code cleanup and extending ZMAT to go to languageActivity

This commit is contained in:
Sean Mac Gillicuddy 2019-08-09 15:49:58 +01:00
parent b3e5a46ae7
commit 774d11377e
23 changed files with 125 additions and 100 deletions

View File

@ -33,7 +33,7 @@ inline fun <reified T : BaseRobot> T.applyWithViewHierarchyPrinting(
try {
function()
} catch (runtimeException: RuntimeException) {
uiDevice.takeScreenshot(File(context.filesDir,"${System.currentTimeMillis()}.png"))
uiDevice.takeScreenshot(File(context.filesDir, "${System.currentTimeMillis()}.png"))
InstrumentationRegistry.getInstrumentation().runOnMainSync {
throw RuntimeException(
combineMessages(
@ -65,14 +65,14 @@ private fun getViewHierarchy(v: View, desc: StringBuilder, margin: Int) {
private fun getViewMessage(v: View, marginOffset: Int) =
"${numSpaces(marginOffset)}[${v.javaClass.simpleName}]${resourceId(v)}${text(v)}" +
"${contentDescription(v)}${visibility(v)}\n"
"${contentDescription(v)}${visibility(v)}\n"
fun visibility(v: View) = " visibility:" +
when (v.visibility) {
View.VISIBLE -> "visible"
View.INVISIBLE -> "invisible"
else -> "gone"
}
when (v.visibility) {
View.VISIBLE -> "visible"
View.INVISIBLE -> "invisible"
else -> "gone"
}
fun contentDescription(view: View) =
view.contentDescription?.let {
@ -83,7 +83,7 @@ fun contentDescription(view: View) =
fun text(v: View) =
if (v is TextView)
if (v.text.isNotEmpty()) " text:${v.text}"
else ""
else ""
else ""
private fun resourceId(view: View) =

View File

@ -2,6 +2,7 @@ package org.kiwix.kiwixmobile
import android.Manifest.permission
import android.app.Activity
import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.rule.ActivityTestRule
@ -15,13 +16,13 @@ abstract class BaseActivityTest<T : Activity> {
@get:Rule
abstract var activityRule: ActivityTestRule<T>
@get:Rule
var readPermissionRule =
var readPermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(permission.READ_EXTERNAL_STORAGE)
@get:Rule
var writePermissionRule =
var writePermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE)
val context by lazy {
val context: Context by lazy {
getInstrumentation().targetContext.applicationContext
}

View File

@ -8,7 +8,7 @@ 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.ContentDesc
import org.kiwix.kiwixmobile.Findable.StringId.ContentDesc
import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId

View File

@ -1,53 +1,53 @@
package org.kiwix.kiwixmobile
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.test.uiautomator.By
import androidx.test.uiautomator.BySelector
interface Findable {
fun selector(baseRobot: BaseRobot): BySelector
fun errorMessage(baseRobot: BaseRobot): String
sealed class Findable {
abstract fun selector(baseRobot: BaseRobot): BySelector
abstract fun errorMessage(baseRobot: BaseRobot): String
class ViewId(val viewId: Int) : Findable {
class ViewId(@IdRes private val resId: Int) : Findable() {
override fun errorMessage(baseRobot: BaseRobot) =
"No view found with Id ${resourceName(baseRobot)}"
override fun selector(baseRobot: BaseRobot)=
override fun selector(baseRobot: BaseRobot): BySelector =
By.res(resourceName(baseRobot))
private fun resourceName(baseRobot: BaseRobot) =
baseRobot.context.resources.getResourceName(viewId)
baseRobot.context.resources.getResourceName(resId)
}
class TextId(val textId: Int) : Findable {
override fun errorMessage(baseRobot: BaseRobot) = "No view found with text ${text(baseRobot)}"
override fun selector(baseRobot: BaseRobot) =
By.text(text(baseRobot))
private fun text(baseRobot: BaseRobot) = baseRobot.context.getString(textId)
}
class TextContains(val textId: Int) : Findable {
override fun errorMessage(baseRobot: BaseRobot) = "No view found containing ${text(baseRobot)}"
override fun selector(baseRobot: BaseRobot) =
By.textContains(text(baseRobot))
private fun text(baseRobot: BaseRobot) = baseRobot.context.getString(textId)
}
class Text(val text: String) : Findable {
class Text(private val text: String) : Findable() {
override fun errorMessage(baseRobot: BaseRobot) = "No view found with text $text"
override fun selector(baseRobot: BaseRobot) = By.text(text)
override fun selector(baseRobot: BaseRobot): BySelector = By.text(text)
}
class ContentDesc(val textId: Int) : Findable {
override fun selector(baseRobot: BaseRobot) = By.desc(text(baseRobot))
sealed class StringId(@StringRes private val resId: Int) : Findable() {
override fun errorMessage(baseRobot: BaseRobot) = "No view found with content description ${text(baseRobot)}"
fun text(baseRobot: BaseRobot): String = baseRobot.context.getString(resId)
private fun text(baseRobot: BaseRobot) = baseRobot.context.getString(textId)
class ContentDesc(@StringRes resId: Int) : StringId(resId) {
override fun selector(baseRobot: BaseRobot): BySelector = By.desc(text(baseRobot))
override fun errorMessage(baseRobot: BaseRobot) =
"No view found with content description ${text(baseRobot)}"
}
class TextContains(@StringRes resId: Int) : StringId(resId) {
override fun selector(baseRobot: BaseRobot): BySelector = By.textContains(text(baseRobot))
override fun errorMessage(baseRobot: BaseRobot) =
"No view found containing ${text(baseRobot)}"
}
class TextId(@StringRes resId: Int) : StringId(resId) {
override fun selector(baseRobot: BaseRobot): BySelector = By.text(text(baseRobot))
override fun errorMessage(baseRobot: BaseRobot) = "No view found with text ${text(baseRobot)}"
}
}
}

View File

@ -33,7 +33,7 @@ import java.util.concurrent.TimeUnit
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class KiwixMockServer {
val mockWebServer = MockWebServer().apply {
private val mockWebServer = MockWebServer().apply {
start(8080)
}
@ -53,7 +53,7 @@ class KiwixMockServer {
) {
mockWebServer.setDispatcher(object : Dispatcher() {
override fun dispatch(request: RecordedRequest) =
pathsToResponses[request.path]?.let { successfulResponse(it) }
pathsToResponses[request.path]?.let(::successfulResponse)
?: MockResponse().throttleBody(1L, 1L, TimeUnit.SECONDS).setBody("0123456789")
})
}
@ -66,7 +66,7 @@ class KiwixMockServer {
fun Any.asXmlString() = StringWriter().let {
Persister().write(this, it)
it.toString()
"$it"
}
fun metaLinkNetworkEntity() = MetaLinkNetworkEntity().apply {
@ -130,8 +130,8 @@ fun book(
this.articleCount = articleCount
this.mediaCount = mediaCount
this.size = size
this.bookName = name
this.favicon = favIcon
bookName = name
favicon = favIcon
}
fun libraryNetworkEntity(books: List<Book> = emptyList()) = LibraryNetworkEntity().apply {

View File

@ -40,5 +40,4 @@ class HelpActivityTest : BaseActivityTest<HelpActivity>() {
clickOnSendFeedback()
}
}
}

View File

@ -18,10 +18,9 @@
package org.kiwix.kiwixmobile.help
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.TextId
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.R.id
import org.kiwix.kiwixmobile.R.string
@ -34,36 +33,36 @@ class HelpRobot : BaseRobot() {
}
fun clickOnWhatDoesKiwixDo() {
clickOn(TextId(R.string.help_2))
clickOn(TextId(string.help_2))
}
fun assertWhatDoesKiwixDoIsExpanded() {
isVisible(
Text(
helpTextFormat(
R.string.help_3,
R.string.help_4
)
Text(
helpTextFormat(
string.help_3,
string.help_4
)
)
)
}
fun clickOnWhereIsContent() {
clickOn(TextId(R.string.help_5))
clickOn(TextId(string.help_5))
}
fun assertWhereIsContentIsExpanded() {
isVisible(
Text(
helpTextFormat(
string.help_6,
string.help_7,
string.help_8,
string.help_9,
string.help_10,
string.help_11
)
Text(
helpTextFormat(
string.help_6,
string.help_7,
string.help_8,
string.help_9,
string.help_10,
string.help_11
)
)
)
}
@ -73,25 +72,24 @@ class HelpRobot : BaseRobot() {
fun assertLargeZimsIsExpanded() {
isVisible(
Text(
helpTextFormat(
R.string.help_13,
R.string.help_14,
R.string.help_15,
R.string.help_16,
R.string.help_17,
R.string.help_18,
R.string.help_19
)
Text(
helpTextFormat(
string.help_13,
string.help_14,
string.help_15,
string.help_16,
string.help_17,
string.help_18,
string.help_19
)
)
)
}
fun clickOnSendFeedback() {
clickOn(ViewId(R.id.activity_help_feedback_text_view))
clickOn(ViewId(id.activity_help_feedback_text_view))
}
private fun helpTextFormat(vararg stringIds: Int) =
stringIds.fold("", { acc, i -> "$acc${context.getString(i)}\n" })
}

View File

@ -21,7 +21,6 @@ import org.junit.Rule
import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest
class IntroActivityTest : BaseActivityTest<IntroActivity>() {
@get:Rule
override var activityRule = activityTestRule<IntroActivity>()

View File

@ -1,19 +1,17 @@
package org.kiwix.kiwixmobile.intro
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.TextId
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.R.id
import org.kiwix.kiwixmobile.R.string
import org.kiwix.kiwixmobile.main.MainRobot
import org.kiwix.kiwixmobile.main.main
fun intro(func: IntroRobot.() -> Unit) = IntroRobot().apply(func)
class IntroRobot() : BaseRobot() {
class IntroRobot : BaseRobot() {
private val getStarted = ViewId(id.get_started)
private val getStarted = ViewId(R.id.get_started)
private val viewPager = ViewId(R.id.view_pager)
init {
@ -22,9 +20,8 @@ class IntroRobot() : BaseRobot() {
fun swipeLeft() {
isVisible(viewPager).swipeLeft()
isVisible(TextId(string.save_books_offline))
isVisible(TextId(R.string.save_books_offline))
isVisible(TextId(R.string.download_books_message))
}
fun swipeRight() {

View File

@ -0,0 +1,14 @@
package org.kiwix.kiwixmobile.language
import applyWithViewHierarchyPrinting
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
fun language(func: LanguageRobot.() -> Unit) = LanguageRobot().applyWithViewHierarchyPrinting(func)
class LanguageRobot : BaseRobot() {
init {
isVisible(ViewId(R.id.language_recycler_view))
}
}

View File

@ -19,7 +19,6 @@ package org.kiwix.kiwixmobile.main
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.R.id
fun main(func: MainRobot.() -> Unit) = MainRobot().apply(func)

View File

@ -63,6 +63,7 @@ class ZimManageActivityTest : BaseActivityTest<ZimManageActivity>() {
clickPositiveDialogButton()
waitForEmptyView()
}
}
clickOnOnline { }
} clickOnLanguageIcon { }
}
}

View File

@ -19,13 +19,16 @@ package org.kiwix.kiwixmobile.zim_manager
import applyWithViewHierarchyPrinting
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.language.LanguageRobot
import org.kiwix.kiwixmobile.language.language
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
fun zimManage(func: ZimManageRobot.() -> Unit) =
ZimManageRobot().applyWithViewHierarchyPrinting(func)
ZimManageRobot().applyWithViewHierarchyPrinting(func)
class ZimManageRobot : BaseRobot() {
init {
@ -47,6 +50,12 @@ class ZimManageRobot : BaseRobot() {
return device(func)
}
infix fun clickOnLanguageIcon(function: LanguageRobot.() -> Unit): LanguageRobot {
TextId(R.string.remote_zims)
clickOn(ViewId(R.id.select_language))
return language(function)
}
private fun library(func: LibraryRobot.() -> Unit) = LibraryRobot().apply(func)
inner class LibraryRobot : BaseRobot() {
init {

View File

@ -29,6 +29,7 @@ import org.kiwix.kiwixmobile.di.modules.ApplicationModule;
import org.kiwix.kiwixmobile.di.modules.JNIModule;
import org.kiwix.kiwixmobile.di.modules.NetworkModule;
import org.kiwix.kiwixmobile.downloader.DownloadService;
import org.kiwix.kiwixmobile.language.LanguageActivity;
import org.kiwix.kiwixmobile.main.KiwixWebView;
import org.kiwix.kiwixmobile.search.AutoCompleteAdapter;
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
@ -46,12 +47,11 @@ public interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance Builder context(Context context);
ApplicationComponent build();
}
ActivityComponent.Builder activityComponent();
void inject(KiwixApplication application);
@ -69,4 +69,6 @@ public interface ApplicationComponent {
void inject(DownloadNotificationClickedReceiver downloadNotificationClickedReceiver);
void inject(@NotNull ZimManageActivity zimManageActivity);
void inject(@NotNull LanguageActivity languageActivity);
}

View File

@ -10,8 +10,9 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.activity_language.language_progressbar
import kotlinx.android.synthetic.main.activity_language.recycler_view
import kotlinx.android.synthetic.main.activity_language.language_recycler_view
import kotlinx.android.synthetic.main.activity_language.toolbar
import org.kiwix.kiwixmobile.KiwixApplication
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.base.BaseActivity
import org.kiwix.kiwixmobile.extensions.viewModel
@ -42,6 +43,10 @@ class LanguageActivity : BaseActivity() {
HeaderDelegate()
)
override fun injection() {
KiwixApplication.getApplicationComponent().inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_language)
@ -52,7 +57,7 @@ class LanguageActivity : BaseActivity() {
it.setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp)
it.setTitle(R.string.select_languages)
}
recycler_view.run {
language_recycler_view.run {
adapter = languageAdapter
layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
setHasFixedSize(true)

View File

@ -35,7 +35,7 @@ import java.util.ArrayList
object FileUtils {
val saveFilePath =
private val saveFilePath =
"${Environment.getExternalStorageDirectory()}${File.separator}Android$" +
"{File.separator}obb${File.separator}${BuildConfig.APPLICATION_ID}"

View File

@ -39,7 +39,7 @@ import javax.inject.Inject
class Fat32Checker @Inject constructor(sharedPreferenceUtil: SharedPreferenceUtil) {
private val _fileSystemStates: BehaviorProcessor<FileSystemState> = BehaviorProcessor.create()
val fileSystemStates: Flowable<FileSystemState> = _fileSystemStates.distinctUntilChanged()
var fileObserver: FileObserver? = null
private var fileObserver: FileObserver? = null
private val requestCheckSystemFileType = BehaviorProcessor.createDefault(Unit)
init {

View File

@ -4,7 +4,7 @@ import java.text.DecimalFormat
import kotlin.math.log10
import kotlin.math.pow
inline class KiloByte(val kilobyteString: String?) {
inline class KiloByte(private val kilobyteString: String?) {
val humanReadable
get() = kilobyteString?.toLongOrNull()?.let {
val units = arrayOf("KB", "MB", "GB", "TB")

View File

@ -55,6 +55,7 @@ class ZimManageActivity : BaseActivity() {
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject lateinit var languagesDao: NewLanguagesDao
override fun injection() {
KiwixApplication.getApplicationComponent().inject(this)
}

View File

@ -59,7 +59,7 @@ class ZimFileSelectFragment : BaseFragment() {
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
private var actionMode: ActionMode? = null
val disposable = CompositeDisposable()
private val disposable = CompositeDisposable()
private val zimManageViewModel by lazy {
activity!!.viewModel<ZimManageViewModel>(viewModelFactory)

View File

@ -9,7 +9,7 @@ import org.kiwix.kiwixmobile.BuildConfig
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
class ShareFiles(val selectedBooks: List<BookOnDisk>) : SideEffect<Unit> {
class ShareFiles(private val selectedBooks: List<BookOnDisk>) : SideEffect<Unit> {
override fun invokeWith(activity: Activity) {
val selectedFileShareIntent = Intent()
selectedFileShareIntent.action = Intent.ACTION_SEND_MULTIPLE

View File

@ -33,5 +33,5 @@ class AdapterDelegateManager<T> {
throw RuntimeException("No delegate registered for $item")
}
var delegates: SparseArrayCompat<AdapterDelegate<T>> = SparseArrayCompat()
private var delegates: SparseArrayCompat<AdapterDelegate<T>> = SparseArrayCompat()
}

View File

@ -24,7 +24,7 @@
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:id="@+id/language_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"