Skip to content

Commit

Permalink
Merge pull request #29 from wealthfront/view-matcher
Browse files Browse the repository at this point in the history
#19 Use Espresso API to modify views
  • Loading branch information
cmathew committed May 30, 2023
2 parents fa08447 + 4bed80d commit 051950b
Show file tree
Hide file tree
Showing 56 changed files with 939 additions and 669 deletions.
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
@@ -1,4 +1,5 @@

import Versions.appCompatVersion
import Versions.junitVersion
import Versions.recyclerViewVersion
import Versions.robolectricVersion
Expand All @@ -13,6 +14,7 @@ object Plugins {

object Dependencies {
const val recyclerView = "androidx.recyclerview:recyclerview:$recyclerViewVersion"
const val appCompat = "androidx.appcompat:appcompat:$appCompatVersion"
const val screenshotty = "eu.bolt:screenshotty:$screenshottyVersion"

const val robolectric = "org.robolectric:robolectric:$robolectricVersion"
Expand All @@ -24,5 +26,6 @@ object Dependencies {
const val testExtJunit = "androidx.test.ext:junit:1.1.5"
const val testRules = "androidx.test:rules:1.5.0"
const val espressoCore = "androidx.test.espresso:espresso-core:3.5.1"
const val espressoContrib = "androidx.test.espresso:espresso-contrib:3.5.1"
}

1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Expand Up @@ -7,6 +7,7 @@ object Versions {

const val kotlinVersion = "1.7.21"
const val recyclerViewVersion = "1.2.1"
const val appCompatVersion = "1.5.1"
const val screenshottyVersion = "1.0.4"

const val robolectricVersion = "4.10.3"
Expand Down
1 change: 1 addition & 0 deletions screencaptor-test/.gitignore
@@ -0,0 +1 @@
/build
39 changes: 39 additions & 0 deletions screencaptor-test/build.gradle.kts
@@ -0,0 +1,39 @@
plugins {
id(Plugins.androidLibrary)
id(Plugins.kotlinAndroid)
}

dependencies {
implementation(Dependencies.recyclerView)
implementation(Dependencies.espressoCore)
implementation(Dependencies.espressoContrib)
implementation(Dependencies.appCompat)
}

android {
this.compileSdk = Versions.compileSdkVersion

defaultConfig {
this.minSdk = Versions.minSdkVersion
this.targetSdk = Versions.targetSdkVersion
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
kotlinOptions {
jvmTarget = "1.8"
}

testOptions.apply {
animationsDisabled = true
unitTests.apply {
isIncludeAndroidResources = true
isReturnDefaultValues = true
}
}

namespace = "com.wealthfront.screencaptor.test"
}
23 changes: 23 additions & 0 deletions screencaptor-test/src/main/AndroidManifest.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wealthfront.screencaptor"
>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application>
<activity
android:name="com.wealthfront.screencaptor.SampleActivity"
android:exported="false"
android:theme="@style/Theme.AppCompat"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
@@ -0,0 +1,28 @@
package com.wealthfront.screencaptor

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wealthfront.screencaptor.test.R

class MessageAdapter(private val messages: Array<String>) : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.message_row_item, parent, false) as TextView

return MessageViewHolder(view)
}

override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
holder.view.text = messages[position]
}

override fun getItemCount(): Int = messages.size

data class MessageViewHolder(
val view: TextView
) : RecyclerView.ViewHolder(view)
}

@@ -0,0 +1,28 @@
package com.wealthfront.screencaptor

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers
import org.hamcrest.Description

object RecyclerViewMatchers {

fun hasItemWithText(text: String) = object : BoundedMatcher<View, View>(View::class.java) {
override fun describeTo(description: Description) {
description.appendText("Searching for item with: $text")
}

override fun matchesSafely(item: View): Boolean {
if (item is RecyclerView) {
for (i in 0 until item.childCount) {
val childView = item.findViewHolderForAdapterPosition(i)!!.itemView
if (ViewMatchers.withText(text).matches(childView)) {
return true
}
}
}
return false
}
}
}
@@ -0,0 +1,34 @@
package com.wealthfront.screencaptor

import android.app.AlertDialog
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wealthfront.screencaptor.test.R

class SampleActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.screenshot_test)
findViewById<Button>(R.id.showDialog).setOnClickListener {
AlertDialog.Builder(this)
.setMessage("I am a Dialog")
.show()
}

val messageList = findViewById<RecyclerView>(R.id.messageList)
val layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
messageList.layoutManager = layoutManager

val dividerItemDecoration = DividerItemDecoration(this, layoutManager.orientation)
messageList.addItemDecoration(dividerItemDecoration)

val messages = arrayOf("Bulldog", "Corgi", "Mastiff", "Labrador Retriever", "Border Collie")
messageList.adapter = MessageAdapter(messages)
messageList.adapter!!.notifyDataSetChanged()
}
}
6 changes: 6 additions & 0 deletions screencaptor-test/src/main/res/layout/message_row_item.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
72 changes: 72 additions & 0 deletions screencaptor-test/src/main/res/layout/screenshot_test.xml
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>

<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4B0082"
android:scrollbars="vertical"
tools:ignore="HardcodedText"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<ImageView
android:id="@+id/wealthfrontIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
android:src="@drawable/wf_logo"
/>

<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:hint="Welcome to Wealthfront!!!"
android:inputType="text"
android:textColorHint="#FFFFFF"
tools:ignore="Autofill,LabelFor"
/>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some sample data which is really long, so long that it wraps to another line and maybe even three lines"
/>

<Button
android:id="@+id/showDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show dialog"
/>

<ScrollView
android:id="@+id/anotherScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<HorizontalScrollView
android:id="@+id/horizonalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

</androidx.core.widget.NestedScrollView>
18 changes: 7 additions & 11 deletions screencaptor/build.gradle.kts
Expand Up @@ -29,34 +29,30 @@ android {
isReturnDefaultValues = true
}
}

sourceSets {
this.getByName("androidTest"){
this.java.srcDir("src/sharedTest/java")
this.manifest.srcFile("src/sharedTest/AndroidManifest.xml")
}
this.getByName("test"){
this.java.srcDir("src/sharedTest/java")
this.manifest.srcFile("src/sharedTest/AndroidManifest.xml")
}
}
}

dependencies {
implementation(Dependencies.recyclerView)
implementation(Dependencies.screenshotty)
implementation(Dependencies.espressoCore)
implementation(Dependencies.espressoContrib)
implementation(Dependencies.appCompat)

testImplementation(Dependencies.robolectric)
testImplementation(Dependencies.testCore)
testImplementation(Dependencies.testCoreKtx)
testImplementation(Dependencies.junit)
testImplementation(Dependencies.truth)
testImplementation(Dependencies.espressoCore)
testImplementation(Dependencies.espressoContrib)
testImplementation(Dependencies.testRules)
testImplementation(Dependencies.testExtJunit)
testImplementation(project(":screencaptor-test"))

androidTestImplementation(Dependencies.junit)
androidTestImplementation(Dependencies.testExtJunit)
androidTestImplementation(Dependencies.testRules)
androidTestImplementation(Dependencies.espressoCore)
androidTestImplementation(Dependencies.espressoContrib)
androidTestImplementation(project(":screencaptor-test"))
}

0 comments on commit 051950b

Please sign in to comment.