Skip to content

Commit

Permalink
Merge branch 'master' into enable_custom_list_child_interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
gdomingues committed Jan 5, 2021
2 parents 911675d + 87cd5a7 commit fa6bb08
Show file tree
Hide file tree
Showing 74 changed files with 2,005 additions and 356 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: "https://github.com/AdevintaSpain/Barista/issues/344"
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/Feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ about: Suggest an idea for this library

**Description what you'd like to happen:**

A clear description if the feature or behavior you'd like implemented.
A clear description of the feature or behavior you'd like implemented.

**Example espresso code if available**

Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI

on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
tests:
runs-on: macOS-latest

steps:
- uses: actions/checkout@v1
name: Checkout
- uses: reactivecircus/[email protected]
name: Run tests
with:
api-level: 28
profile: Nexus 6
headless: true
disable-animations: true
script: ./gradlew connectedCheck

- uses: actions/cache@v1
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ env:
global:
- secure: "pus/Wv/014aNnUbUDNBpAk12o3w/zEO+/0oPomdnFs5034tsUOk53VbV69qKdWGKtH8Gm9LGBL50GLjSiB9Cxnc66BCPGj1ykBQ8RGGG/KHTDg22fuRVXmrSbEc+SqjwhpyUTpFjTf9RDmR2qlh427RbGofaNmkv7ePX14OHDjfVJW39jVNJv6+Jluyuh98zGpEbKq1SW+kNJsXUJMQjBtgyaQWN0qIjWflZWv4Rn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avMadqSamWv02wQYD+vmeuf58SwZcy6XJJtJZ4M6yVL8msiVpsxpX/ln1jWHFEUKwJRRxKBkTHZo="

before_install:
- yes | sdkmanager "platforms;android-30"

android:
components:
- tools
- tools
- platform-tools
- build-tools-28.0.3
- android-29
- android-30
- extra
- addon
- add-on

licenses:
- 'android-sdk-license-.+'
Expand Down
78 changes: 49 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Barista
**The guy who serves a great Espresso**
**The one who serves a great Espresso**

[![Travis](https://img.shields.io/travis/rust-lang/rust.svg?label=Travis+CI)](https://travis-ci.org/SchibstedSpain/Barista)
[![Travis](https://img.shields.io/travis/rust-lang/rust.svg?label=Travis+CI)](https://travis-ci.org/github/AdevintaSpain/Barista)
[![Download](https://api.bintray.com/packages/schibstedspain/maven/barista/images/download.svg)](https://bintray.com/schibstedspain/maven/barista/_latestVersion)
[![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)](LICENSE.md)

Expand All @@ -27,8 +27,7 @@ Barista makes developing UI test faster, easier and more predictable. Built on t

Import Barista as a testing dependency:
```gradle
androidTestImplementation('com.schibsted.spain:barista:3.2.0') {
exclude group: 'com.android.support'
androidTestImplementation('com.schibsted.spain:barista:3.7.0') {
exclude group: 'org.jetbrains.kotlin' // Only if you already use Kotlin in your project
}
```
Expand Down Expand Up @@ -67,6 +66,11 @@ longClickOn("Next");
clickMenu(R.id.menu_item);
```

#### Open the overflow menu without clicking any item
```java
openMenu();
```

#### Writing into widgets
```java
writeTo(R.id.edittext, "A great text");
Expand All @@ -80,13 +84,13 @@ clickListItem(R.id.list, 4);
clickListItemChild(R.id.list, 3, R.id.row_button);
doOnListItemChild(R.id.list, 5, R.id.edittext, replaceText("Yet another great text"));
scrollListToPosition(R.id.list, 4);
assertListItemCount(R.id.listId, 5)
assertListNotEmpty(R.id.listId)
assertDisplayedAtPosition(R.id.recycler, 0, "text");
assertDisplayedAtPosition(R.id.listId, 0, R.id.text_field, "text");
assertDisplayedAtPosition(R.id.recycler, 0, R.string.hello_world);
assertDisplayedAtPosition(R.id.listId, 0, R.id.text_field, R.string.hello_world);
assertCustomAssertionAtPosition(R.id.recycler, 0, customViewAssertion);
assertListItemCount(R.id.list, 5)
assertListNotEmpty(R.id.list)
assertDisplayedAtPosition(R.id.list, 0, "text");
assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, "text");
assertDisplayedAtPosition(R.id.list, 0, R.string.hello_world);
assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, R.string.hello_world);
assertCustomAssertionAtPosition(R.id.list, 0, customViewAssertion);

clickSpinnerItem(R.id.spinner, 1);
```
Expand Down Expand Up @@ -295,30 +299,31 @@ assertNotContains(R.id.textview, R.string.text);

#### Check text is given color
```java
assertTextColorIs(R.id.someRedText, R.color.red);
assertTextColorIs(R.id.someColorListText, R.color.state_list);
assertTextColorIs(R.id.some_red_text, R.color.red);
assertTextColorIs(R.id.some_color_list_text, R.color.state_list);

// ...or not?
assertTextColorIsNot(R.id.someRedText, R.color.blue);
assertTextColorIsNot(R.id.someColorListText, R.color.another_state_list);
assertTextColorIsNot(R.id.some_red_text, R.color.blue);
assertTextColorIsNot(R.id.some_color_list_text, R.color.another_state_list);
```

#### Check recyclerView item count against expected item count
```java
assertRecyclerViewItemCount(R.id.recycler, 10);
```
`assertTextColorIs` and its variant `assertTextColorIsNot` work with:

#### And another tricky feature
```java
assertThatBackButtonClosesTheApp();
```
#### Is this ImageView showing a drawable?
- *Color int*: `Color.parse("#ff00ff")`
- *Color resource*: `R.color.green`
- *Color attribute*: `R.attr.colorPrimary`

Also Barista can check colors parsed from `declarable-style` custom attribute:
```java
assertHasAnyDrawable(R.id.image_view);
assertHasDrawable(R.id.image_view, R.drawable.ic_barista);
assertTextColorIs(R.id.customTextView, R.styleable.SampleCustomView, R.style.SampleCustomStyle, R.styleable.SampleCustomView_customColor);

// ...or not?
assertHasNoDrawable(R.id.image_view);
assertTextColorIsNot(R.id.customTextView, R.styleable.SampleCustomView, R.style.SampleCustomStyle_Green, R.styleable.SampleCustomView_customColor);
```

#### Check recyclerView item count against expected item count
```java
assertRecyclerViewItemCount(R.id.recycler, 10);
```

#### Is this ProgressBar/SeekBar progress?
Expand All @@ -328,6 +333,11 @@ assertProgressIsMin(R.id.seek_bar)
assertProgressIsMax(R.id.seek_bar)
```

#### And another tricky feature
```java
assertThatBackButtonClosesTheApp();
```

### Custom assertions

If you have a special case not covered by the given assertions API, we encourage you to assert these special cases with our custom assertions API. It's a convenient way to replace plain `Matcher`s with complex assertions. With Barista, you can match any kind of view by knowing its type and passing its `viewId`, `text`, or a `Matcher<View>`. Once you matched it, you will be able to assert all its properties without adding any complex `Matcher` to your project.
Expand Down Expand Up @@ -355,10 +365,17 @@ assertAny<RadioGroup>(R.id.radioGroup, "selected option is the second one") {
}
```

## Barista’s Intents API
## Mocking the Intent results

Mocking the Android Camera Intent is a tricky thing to do. To accomplish it in no time, Barista gives a way to do it in one line: the method `mockAndroidCamera()`. This method does all the magic to mock the result of the camera. One more thing to do: you have to call `Intents.init()` before calling `mockAndroidCamera()`, and `Intents.release()` after doing the action that launches the camera. You could also use `IntentsTestRule` instead of the common `ActivityTestRule` to skip it, but as we recommend the use of `BaristaRule`, it's easier to just call both methods manually when needed.

Here's an example to copy paste:

```java
// Creates a Bitmap on a camera provided URI
Intents.init();
mockAndroidCamera();
clickOn(R.id.launch_camera);
Intents.release();
```

## Runtime Permissions
Expand All @@ -367,6 +384,9 @@ The new Marshmallow permissions system requires checking for permissions at runt
```java
PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.GET_ACCOUNTS);
```
```java
PermissionGranter.allowPermissionOneTime(Manifest.permission.GET_ACCOUNTS);
```

## Useful test rules
Barista includes a set of useful test rules to help you:
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ task clean(type: Delete) {
}

ext.minSdkVersionDeclared = 19
ext.compileSdkVersionDeclared = 29
ext.compileSdkVersionDeclared = 30

ext.supportLibVersion = '27.1.1'
ext.espressoVersion = '3.0.2'
ext.uiAutomatorVersion = '2.1.3'
ext.baristaVersion = '3.2.0'
ext.baristaVersion = '3.7.0'
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m
6 changes: 5 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
lintOptions {
disable 'InvalidPackage'
}
kotlinOptions.freeCompilerArgs += ["-module-name", "barista"]
}

dependencies {
Expand All @@ -27,13 +28,16 @@ dependencies {
implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable-animated:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
api 'androidx.viewpager2:viewpager2:1.0.0'

implementation 'com.google.android.material:material:1.2.0'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

testImplementation 'junit:junit:4.12'
testImplementation 'pl.pragmatists:JUnitParams:1.1.0'
testImplementation 'org.assertj:assertj-core:1.7.0'
testImplementation 'org.mockito:mockito-core:2.13.0'
testImplementation 'org.mockito:mockito-core:2.28.2'
}

publish {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.schibsted.spain.barista.internal.util

import android.content.Context
import android.graphics.BitmapFactory
import androidx.test.InstrumentationRegistry
import androidx.test.core.app.ApplicationProvider
import com.schibsted.spain.barista.test.R
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
Expand All @@ -11,8 +12,8 @@ class BitmapComparatorTest {

@Test
fun returnTrueWhenComparingTheSameDrawable() {
val aBitmap = BitmapFactory.decodeResource(InstrumentationRegistry.getTargetContext().resources, R.drawable.ic_barista)
val theSameBitmap = BitmapFactory.decodeResource(InstrumentationRegistry.getTargetContext().resources, R.drawable.ic_barista)
val aBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_barista)
val theSameBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_barista)

val result = BitmapComparator.compare(aBitmap, theSameBitmap)

Expand All @@ -21,8 +22,8 @@ class BitmapComparatorTest {

@Test
fun returnFalseWhenComparingDifferentDrawables() {
val aBitmap = BitmapFactory.decodeResource(InstrumentationRegistry.getTargetContext().resources, R.drawable.ic_barista)
val aDifferentBitmap = BitmapFactory.decodeResource(InstrumentationRegistry.getTargetContext().resources, R.drawable.ic_launcher)
val aBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_barista)
val aDifferentBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_launcher)

val result = BitmapComparator.compare(aBitmap, aDifferentBitmap)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.schibsted.spain.barista.assertion

import android.view.View
import androidx.annotation.IdRes
import com.google.android.material.chip.ChipGroup
import com.schibsted.spain.barista.assertion.BaristaAssertions.assertAny

object BaristaChipAssertions {

@JvmStatic
fun assertAnyChipSelected(@IdRes viewId: Int) {
assertAny<ChipGroup>(viewId) {
it.checkedChipId != View.NO_ID
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.schibsted.spain.barista.assertion

import android.content.Context
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.google.android.material.textfield.TextInputLayout
import androidx.test.InstrumentationRegistry
import androidx.test.espresso.matcher.ViewMatchers
import android.view.View
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
import com.schibsted.spain.barista.internal.assertAny
import org.hamcrest.Description
import org.hamcrest.Matcher
Expand All @@ -16,7 +17,7 @@ object BaristaErrorAssertions {

@JvmStatic
fun assertError(@IdRes viewId: Int, @StringRes text: Int) {
val resourceString = InstrumentationRegistry.getTargetContext().resources.getString(text)
val resourceString = ApplicationProvider.getApplicationContext<Context>().resources.getString(text)
assertError(viewId, resourceString)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.schibsted.spain.barista.assertion

import android.content.Context
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import androidx.test.InstrumentationRegistry
import androidx.test.espresso.matcher.ViewMatchers.withId
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import androidx.test.core.app.ApplicationProvider
import com.schibsted.spain.barista.internal.assertAny
import org.hamcrest.Description
import org.hamcrest.Matcher
Expand All @@ -18,7 +19,7 @@ object BaristaHintAssertions {

@JvmStatic
fun assertHint(@IdRes viewId: Int, @StringRes text: Int) {
val resourceString = InstrumentationRegistry.getTargetContext().resources.getString(text)
val resourceString = ApplicationProvider.getApplicationContext<Context>().resources.getString(text)
assertHint(viewId, resourceString)
}

Expand Down
Loading

0 comments on commit fa6bb08

Please sign in to comment.