Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardyeats: BasicSample #1

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions BasicSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 25
Expand Down Expand Up @@ -54,7 +56,11 @@ dependencies {
compile 'android.arch.lifecycle:extensions:' + rootProject.archLifecycleVersion;
compile 'android.arch.persistence.room:runtime:' + rootProject.archRoomVersion;
annotationProcessor "android.arch.lifecycle:compiler:" + rootProject.archLifecycleVersion;
annotationProcessor "android.arch.persistence.room:compiler:" + rootProject.archRoomVersion;
kapt "android.arch.persistence.room:compiler:" + rootProject.archRoomVersion

kapt 'com.android.databinding:compiler:3.0.0-beta2'
compile 'io.reactivex.rxjava2:rxjava:2.1.2'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

testCompile 'junit:junit:4.12'

Expand All @@ -72,5 +78,9 @@ dependencies {
// Force usage of dependencies in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:' + rootProject.supportLibVersion;
androidTestCompile 'com.android.support:support-v4:' + rootProject.supportLibVersion;
androidTestCompile 'com.android.support:recyclerview-v7:' + rootProject.supportLibVersion;
androidTestCompile 'com.android.support:recyclerview-v7:' + rootProject.supportLibVersion
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version";
}
repositories {
mavenCentral()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2017, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.persistence


import android.arch.lifecycle.ViewModelProviders
import android.support.test.espresso.Espresso
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.IdlingResource
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.contrib.RecyclerViewActions
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.rule.ActivityTestRule
import android.support.v7.widget.RecyclerView
import android.view.View
import com.example.android.persistence.viewmodel.ProductListViewModel
import org.hamcrest.core.IsNot.not
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.atomic.AtomicBoolean

class MainActivityTest {

@Rule
var mActivityRule = ActivityTestRule(
MainActivity::class.java)

private val idlingRes = SimpleIdlingResource()

@Before
fun idlingResourceSetup() {

Espresso.registerIdlingResources(idlingRes)
// There's always
idlingRes.isIdleNow = false

val productListViewModel = productListViewModel

// Subscribe to ProductListViewModel's products list observable to figure out when the
// app is idle.
productListViewModel.products.observeForever { productEntities ->
if (productEntities != null) {
idlingRes.isIdleNow = true
}
}
}

@Test
fun clickOnFirstItem_opensComments() {
// When clicking on the first product
onView(withContentDescription(R.string.cd_products_list))
.perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()))

// Then the second screen with the comments should appear.
onView(withContentDescription(R.string.cd_comments_list))
.check(matches(isDisplayed()))

// Then the second screen with the comments should appear.
onView(withContentDescription(R.string.cd_product_name))
.check(matches(not<View>(withText(""))))

}

/** Gets the ViewModel for the current fragment */
private val productListViewModel: ProductListViewModel
get() {
val activity = mActivityRule.activity

val productListFragment = activity.supportFragmentManager
.findFragmentByTag(ProductListFragment.TAG)

return ViewModelProviders.of(productListFragment)
.get(ProductListViewModel::class.java)
}

private class SimpleIdlingResource : IdlingResource {

// written from main thread, read from any thread.
@Volatile private var mResourceCallback: IdlingResource.ResourceCallback? = null

private val mIsIdleNow = AtomicBoolean(true)

fun setIdleNow(idleNow: Boolean) {
mIsIdleNow.set(idleNow)
if (idleNow) {
mResourceCallback!!.onTransitionToIdle()
}
}

override fun getName(): String {
return "Simple idling resource"
}

override fun isIdleNow(): Boolean {
return mIsIdleNow.get()
}

override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback) {
mResourceCallback = callback
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.persistence

import android.os.Bundle

import android.arch.lifecycle.LifecycleActivity
import com.example.android.persistence.model.Product

class MainActivity : LifecycleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)

// Add product list fragment if this is first creation
if(savedInstanceState == null)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, ProductListFragment(), ProductListFragment.TAG).commit()
}

/** Shows the product detail fragment */
fun show(product: Product) {
supportFragmentManager
.beginTransaction()
.addToBackStack("product")
.replace(R.id.fragment_container,
ProductFragment.forProduct(product.id), null).commit()
}
}
Loading