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

[js/react_native] Create ONNX Runtime React Native pipeline #10474

Merged
merged 110 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 109 commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
1b92749
Pipeline for ONNX Runtime react native
hanbitmyths Jan 11, 2022
4e2d77b
Fix a test failure
hanbitmyths Jan 12, 2022
7b6d3ac
test with custom built binaries
hanbitmyths Jan 12, 2022
7445506
add onnxruntime-common package back
hanbitmyths Jan 12, 2022
1a2a9eb
don't bob build when bootstrap
hanbitmyths Jan 12, 2022
181283c
revise Android test
hanbitmyths Jan 14, 2022
1390a14
rename example to e2e
hanbitmyths Jan 20, 2022
6fb7107
Merge branch 'master' of https://github.com/microsoft/onnxruntime
hanbitmyths Jan 20, 2022
3d51f4a
Merge branch 'master' into sunghcho/react_native_pipeline
hanbitmyths Jan 20, 2022
e7d844a
remove onnxruntime packages from package.json
hanbitmyths Jan 20, 2022
7764946
remove release-it package
hanbitmyths Jan 20, 2022
0a5bb4b
upgrade gradle version to the same as CI
hanbitmyths Jan 20, 2022
8fc11bd
add a pipeline for react native
hanbitmyths Jan 21, 2022
2467a73
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 22, 2022
4586674
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 22, 2022
ff9ed66
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 24, 2022
4200749
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 24, 2022
f474673
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 24, 2022
f9c1a85
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 24, 2022
d3f99e0
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
883849e
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
a7097b4
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
ffcf21b
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
f5d8581
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
194ffd6
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
85bd2eb
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
d077694
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
c60c578
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
38a4ca9
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 25, 2022
e9e5b2c
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
ac7d885
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
5e348a6
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
abf36d1
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
3db8679
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
d1fe9a0
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
34770c8
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
ac23064
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
9e4a772
android and ios mobile build for react native e2e
hanbitmyths Jan 26, 2022
17c8e67
Merge branch 'sunghcho/react_native_pipeline' of https://github.com/m…
hanbitmyths Jan 26, 2022
3f334e7
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 26, 2022
a249fef
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
a3e3745
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
bef6b8b
use android aar package template
hanbitmyths Jan 27, 2022
563b961
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
037080d
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
f8fdb85
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
6800eaa
use android aar package template
hanbitmyths Jan 27, 2022
28e9764
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
0076c63
publish ios test results
hanbitmyths Jan 27, 2022
b70f8a7
add e2e tests and publish a npm package
hanbitmyths Jan 27, 2022
763a5b7
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
0cab317
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
a577005
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
a59110d
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 27, 2022
ff41ef8
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 28, 2022
7145c51
remove aar from npm package
hanbitmyths Jan 28, 2022
b0ea500
wait for view displayed
hanbitmyths Jan 28, 2022
7b0bdce
Merge branch 'sunghcho/react_native_pipeline' of https://github.com/m…
hanbitmyths Jan 28, 2022
1cb406f
change a waiting logic
hanbitmyths Jan 28, 2022
d5fefcc
increase wait time for app launching
hanbitmyths Jan 28, 2022
afbb50d
give more time to launch an app
hanbitmyths Jan 28, 2022
7031fad
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 28, 2022
c9970a6
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 28, 2022
85d9f58
disable metro server on testing
hanbitmyths Jan 28, 2022
af862d4
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
7911c68
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
f5c4f5c
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
eae4e49
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
72706b1
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
9e438bb
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
aca1f69
test ios simulator launching
hanbitmyths Jan 29, 2022
1b91083
Merge branch 'sunghcho/react_native_pipeline' of https://github.com/m…
hanbitmyths Jan 29, 2022
9b4aba3
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Jan 29, 2022
1292c1f
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 2, 2022
c34204b
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 2, 2022
64728de
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 2, 2022
d24d027
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 2, 2022
f44c221
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 2, 2022
ea00aac
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 2, 2022
ac3240e
fix iOS e2e test
hanbitmyths Feb 3, 2022
aa05d14
Merge branch 'master' of https://github.com/microsoft/onnxruntime
hanbitmyths Feb 3, 2022
bf09f05
merge with master
hanbitmyths Feb 3, 2022
44f33d1
use a publishing version of npm packages
hanbitmyths Feb 3, 2022
0f21d3b
make pretty
hanbitmyths Feb 3, 2022
b7f564d
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 3, 2022
ac7ee6d
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 3, 2022
101b662
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 3, 2022
deb5d63
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 3, 2022
98e16f3
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 3, 2022
38c856c
make only one onnxruntime-common package after packaging
hanbitmyths Feb 3, 2022
6d79cee
make a powershell script of packaging universal
hanbitmyths Feb 4, 2022
1612a3b
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 4, 2022
252cab0
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 4, 2022
358aa6d
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 4, 2022
8e4ae36
Add a warning for file changes during a test
hanbitmyths Feb 4, 2022
9961767
clean up
hanbitmyths Feb 5, 2022
e46bee2
fix lint errors
hanbitmyths Feb 5, 2022
05394a5
fix js npm packaging
hanbitmyths Feb 5, 2022
735e96c
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 5, 2022
78b4a44
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 6, 2022
508a9ff
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 6, 2022
6e3ba5c
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 6, 2022
4df7f94
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 6, 2022
3c1dc6b
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 6, 2022
fc42f35
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 7, 2022
a9ec371
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 7, 2022
a4c054b
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 7, 2022
dd15432
Update mac-react-native-ci-pipeline.yml for Azure Pipelines
hanbitmyths Feb 7, 2022
8c2aab4
resolve comments
hanbitmyths Feb 8, 2022
1ab014d
fix a typo
hanbitmyths Feb 10, 2022
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
2 changes: 1 addition & 1 deletion js/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = {
'@typescript-eslint/naming-convention': 'off'
}
}, {
files: ['react_native/example/src/**/*.ts', 'react_native/example/src/**/*.tsx'], rules: {
files: ['react_native/e2e/src/**/*.ts', 'react_native/e2e/src/**/*.tsx'], rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unnecessary-type-assertion': 'off',
'unicorn/filename-case': 'off',
Expand Down
47 changes: 32 additions & 15 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,10 @@ By default, ONNX Runtime React Native leverages ONNX Runtime Mobile package with

4. Copy `<BUILD_DIRECTORY>/aar_out/MinSizeRel/com/microsoft/onnxruntime/onnxruntime-mobile/<version>/onnxruntime-mobile-<version>.aar` into `<ORT_ROOT>/js/react_native/android/libs` directory.

5. Modify `Onnxruntime_mobileVersion` property in `<ORT_ROOT>/js/react_native/android/build.properties` to consume a locally built package or a newly published package from Maven.

6. To verify, open Android Emulator and run this command from `<ORT_ROOT>/js/react_native/android`
5. To verify, open Android Emulator and run this command from `<ORT_ROOT>/js/react_native/android`

```sh
adb shell am instrument -w ai.onnxruntime.react_native.test/androidx.test.runner.AndroidJUnitRunner
./gradlew connectedDebugAndroidTest
```

3. Build iOS ONNX Runtime package
Expand All @@ -393,38 +391,57 @@ By default, ONNX Runtime React Native leverages ONNX Runtime Mobile package with

```sh
pod install
xcodebuild test -workspace OnnxruntimeModule.xcworkspace -scheme OnnxruntimeModuleTest -destination 'platform=iOS Simulator,name=iPhone 11,OS=15.0'
xcodebuild test -workspace OnnxruntimeModule.xcworkspace -scheme OnnxruntimeModuleTest -destination 'platform=iOS Simulator,OS=latest,name=iPhone 13'
```

4. Test an example for Android and iOS. In Windows, open Android Emulator first.
4. Test Android and iOS apps. In Windows, open Android Emulator first.

`debug.keystore` must be generated ahead for Android example.

```sh
keytool -genkey -v -keystore <ORT_ROOT>/js/react_native/example/android/app/debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -keysize 2048 -validity 999999 -dname "CN=Android Debug,O=Android,C=US"
keytool -genkey -v -keystore <ORT_ROOT>/js/react_native/e2e/android/debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -keysize 2048 -validity 999999 -dname "CN=Android Debug,O=Android,C=US"
```

From `<ORT_ROOT>/js/react_native,

```sh
yarn bootstrap
yarn example ios
yarn example android
```

When testing with a custom built ONNX Runtime Android package, copy `<BUILD_DIRECTORY>/aar_out/MinSizeRel/com/microsoft/onnxruntime/onnxruntime-mobile/<version>/onnxruntime-mobile-<version>.aar` into `<ORT_ROOT>/js/react_native/e2e/node_modules/onnxruntime-react-native/android/libs` directory. Using a custom built ONNX Runtime iOS package, copy `onnxruntime-mobile-c.zip` into `<ORT_ROOT>/js/react_native/local_pods` directory if it's not already done.

From `<ORT_ROOT>/js/react_native/e2e/android`, run e2e Android tests as follows,

```sh
./gradlew :app:connectedDebugAndroidTest
```

From `<ORT_ROOT>/js/react_native/e2e/ios`, run e2e iOS tests as follows,

```sh
xcrun xcodebuild test -workspace OnnxruntimeModuleExample.xcworkspace -scheme OnnxruntimeModuleExample -destination 'platform=iOS Simulator,OS=latest,name=iPhone 13'
```

***`yarn bootstrap` changes `packages.json` and `yarn.lock` files. Once testing is done, restore changes to avoid unwanted commit.***

5. Run Android and iOS apps.

```sh
yarn e2e android
yarn e2e ios
```

### NPM Packaging

1. Update a version using `npm verison <version>` from `<ORT_ROOT>/js/react_native` folder. If it's for a dev, use `npm version <version>-dev.<subversion>`

2. Modify Onnxruntime_mobileVersion property in `<ORT_ROOT>/js/react_native/android/build.properties` to update ONNX Runtime Android package version.

3. Run `yarn prepack` to change `onnxruntime-common` to point to a published npm package
2. Run `yarn prepack-rel` to change `onnxruntime-common` to point to a published npm package

4. Run `npm pack` and verify NPM package contents
3. Run `npm pack` and verify NPM package contents

5. Run `npm publish <tgz> --dry-run` to see how it's going to be published
4. Run `npm publish <tgz> --dry-run` to see how it's going to be published

6. Run `npm publish <tgz>` to publish to npmjs. If it's for a dev, add flag `--tag dev`.
5. Run `npm publish <tgz>` to publish to npmjs. If it's for a dev, add flag `--tag dev`.

### Distribution

Expand Down
2 changes: 1 addition & 1 deletion js/react_native/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android.iml
# Cocoapods
#
ios/Pods/
example/ios/Pods/
e2e/ios/Pods/

# node.js
#
Expand Down
4 changes: 1 addition & 3 deletions js/react_native/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ repositories {
}

dependencies {
def onnxruntimeMobileVersion = project.properties['Onnxruntime_mobileVersion']

// noinspection GradleDynamicVersion
api "com.facebook.react:react-native:+"
api "org.mockito:mockito-core:2.28.2"
Expand All @@ -134,5 +132,5 @@ dependencies {

androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito-inline-extended:2.28.1"

implementation "com.microsoft.onnxruntime:onnxruntime-mobile:$onnxruntimeMobileVersion@aar"
implementation "com.microsoft.onnxruntime:onnxruntime-mobile:latest.integration@aar"
}
1 change: 0 additions & 1 deletion js/react_native/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ android.useAndroidX=true
OnnxruntimeModule_buildToolsVersion=29.0.2
OnnxruntimeModule_compileSdkVersion=29
OnnxruntimeModule_targetSdkVersion=29
Onnxruntime_mobileVersion=1.8.2
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Tue Jan 26 16:57:19 PST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar


# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
Expand Down Expand Up @@ -129,6 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`

JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -84,6 +84,7 @@ set CMD_LINE_ARGS=%*

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ android {
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
splits {
abi {
Expand All @@ -144,16 +146,13 @@ android {
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
storeFile rootProject.file('debug.keystore')
keyAlias 'androiddebugkey'
keyPassword 'android'
storePassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
Expand Down Expand Up @@ -183,7 +182,6 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules


implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
exclude group:'com.facebook.fbjni'
Expand All @@ -204,7 +202,11 @@ dependencies {
implementation jscFlavor
}

implementation project(':onnxruntimereactnative')
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'

implementation project(':onnxruntime-react-native')
}

// Run this once to be able to run the application with BUCK
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.example.reactnativeonnxruntimemodule;

import android.view.View;
import android.widget.TextView;

import androidx.test.espresso.NoMatchingViewException;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static org.hamcrest.Matchers.allOf;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class OnnxruntimeModuleExampleUITests {
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void testExample() {
// Wait for a view displayed
int waitTime = 0;
final int sleepTime = 1000;
do {
try {
ViewInteraction view = onView(allOf(withContentDescription("output"), isDisplayed()));
if (getText(view) != null) {
break;
}
} catch (NoMatchingViewException ne) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ie) {
}
waitTime += sleepTime;
}
} while (waitTime < 180000);

ViewInteraction view = onView(allOf(withContentDescription("output"), isDisplayed()));
Assert.assertEquals(getText(view), "Result: 3");
}

private String getText(ViewInteraction matcher) {
final String[] text = {null};

matcher.perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}

@Override
public String getDescription() {
return "get a text from a TextView";
}

@Override
public void perform(UiController uiController, View view) {
TextView textView = (TextView)view;
text[0] = textView.getText().toString();
}
});

return text[0];
}
}
Loading