Skip to content

Commit

Permalink
ci: use detox for ui test, opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Jan 10, 2024
1 parent 580bc26 commit 05ea67a
Show file tree
Hide file tree
Showing 24 changed files with 1,692 additions and 1,034 deletions.
83 changes: 83 additions & 0 deletions example/.detoxrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/** @type {Detox.DetoxConfig} */
module.exports = {
testRunner: {
args: {
'$0': 'jest',
config: 'e2e/jest.config.js'
},
jest: {
setupTimeout: 120000
}
},
apps: {
'ios.debug': {
type: 'ios.app',
binaryPath: 'ios/build/Build/Products/Debug-iphonesimulator/ImageMarkerExample.app',
build: 'xcodebuild -workspace ios/ImageMarkerExample.xcworkspace -scheme ImageMarkerExample -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build'
},
'ios.release': {
type: 'ios.app',
binaryPath: 'ios/build/Build/Products/Release-iphonesimulator/ImageMarkerExample.app',
build: 'xcodebuild -workspace ios/ImageMarkerExample.xcworkspace -scheme ImageMarkerExample -configuration Release -sdk iphonesimulator -derivedDataPath ios/build'
},
'android.debug': {
type: 'android.apk',
binaryPath: 'android/app/build/outputs/apk/debug/app-debug.apk',
build: 'cd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug',
reversePorts: [
8081
]
},
'android.release': {
type: 'android.apk',
binaryPath: 'android/app/build/outputs/apk/release/app-release.apk',
build: 'cd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release'
}
},
devices: {
simulator: {
type: 'ios.simulator',
device: {
type: 'iPhone 15'
}
},
attached: {
type: 'android.attached',
device: {
adbName: '.*'
}
},
emulator: {
type: 'android.emulator',
device: {
avdName: 'Pixel_XL_API_34'
}
}
},
configurations: {
'ios.sim.debug': {
device: 'simulator',
app: 'ios.debug'
},
'ios.sim.release': {
device: 'simulator',
app: 'ios.release'
},
'android.att.debug': {
device: 'attached',
app: 'android.debug'
},
'android.att.release': {
device: 'attached',
app: 'android.release'
},
'android.emu.debug': {
device: 'emulator',
app: 'android.debug'
},
'android.emu.release': {
device: 'emulator',
app: 'android.release'
}
}
};
9 changes: 8 additions & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: "com.android.application"
apply plugin: "com.facebook.react"
apply plugin: 'kotlin-android'

def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["kotlin_version"]
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ImageMarkerExample_kotlinVersion"]

import com.android.build.OutputFile

Expand Down Expand Up @@ -103,6 +103,8 @@ android {
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testBuildType System.getProperty('testBuildType', 'debug')
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

splits {
Expand Down Expand Up @@ -137,6 +139,7 @@ android {
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
proguardFile "${rootProject.projectDir}/../node_modules/detox/android/detox/proguard-rules-app.pro"
}
}

Expand Down Expand Up @@ -171,6 +174,10 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
androidTestImplementation('com.wix:detox:+')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation project(':react-native-vector-icons')
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.imagemarkerexample;

import com.wix.detox.Detox;
import com.wix.detox.config.DetoxConfig;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class DetoxTest {
@Rule // (2)
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, false, false);

@Test
public void runDetoxTests() {
DetoxConfig detoxConfig = new DetoxConfig();
detoxConfig.idlePolicyConfig.masterTimeoutSec = 90;
detoxConfig.idlePolicyConfig.idleResourceTimeoutSec = 60;
detoxConfig.rnContextLoadTimeoutSec = (BuildConfig.DEBUG ? 180 : 60);

Detox.runTests(mActivityRule, detoxConfig);
}
}
2 changes: 2 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
20 changes: 19 additions & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["kotlin_version"]
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ImageMarkerExample_kotlinVersion"]

ext {
buildToolsVersion = "34.0.0"
Expand All @@ -21,6 +21,9 @@ buildscript {
maven { url "https://repository.jboss.org/maven2" }
maven { url 'https://maven.google.com' }
maven { url 'https://maven.fabric.io/public' }
maven {
url("$rootDir/../node_modules/detox/Detox-android")
}
}
dependencies {
classpath("com.android.tools.build:gradle")
Expand All @@ -29,4 +32,19 @@ buildscript {
}
}

allprojects {
repositories {
google()
mavenCentral()
jcenter()
maven { url 'https://dl.google.com/dl/android/maven2' }
maven { url "https://repository.jboss.org/maven2" }
maven { url 'https://maven.google.com' }
maven { url 'https://maven.fabric.io/public' }
maven {
url("$rootDir/../node_modules/detox/Detox-android")
}
}
}

apply plugin: "com.facebook.react.rootproject"
2 changes: 1 addition & 1 deletion example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ newArchEnabled=false
# If set to false, you will be using JSC instead.
hermesEnabled=true

kotlin_version=1.8.0
ImageMarkerExample_kotlinVersion=1.8.0
4 changes: 3 additions & 1 deletion example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
rootProject.name = 'ImageMarkerExample'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'
includeBuild('../node_modules/@react-native/gradle-plugin')
includeBuild('../node_modules/react-native-gradle-plugin')
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
5 changes: 5 additions & 0 deletions example/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ module.exports = {
},
],
],
env: {
production: {
plugins: ['react-native-paper/babel'],
},
},
};
90 changes: 90 additions & 0 deletions example/e2e/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { device, element, by, waitFor } from 'detox';
import assert from 'power-assert';

describe('e2e/App.test.js', () => {
beforeAll(async () => {
await device.launchApp();
});

beforeEach(async () => {
await device.reloadReactNative();
});

it('should display correctly', async () => {
await expect(element(by.id('backgroundImageFormatLabel'))).toBeVisible();
await expect(element(by.id('backgroundImageFormatLabel'))).toHaveText(
'background image format:'
);

await expect(element(by.id('backgroundImageFormatBtn'))).toBeVisible();
await expect(element(by.id('backgroundImageFormatBtn'))).toHaveLabel(
'image'
);

await expect(element(by.id('watermarkTypeLabel'))).toBeVisible();
await expect(element(by.id('watermarkTypeLabel'))).toHaveText(
'watermark type:'
);

await expect(element(by.id('watermarkTypeBtn'))).toBeVisible();
await expect(element(by.id('watermarkTypeBtn'))).toHaveLabel('text');

await expect(element(by.id('exportResultFormatLabel'))).toBeVisible();
await expect(element(by.id('exportResultFormatLabel'))).toHaveText(
'export result format:'
);

await expect(element(by.id('exportResultFormatBtn'))).toBeVisible();
await expect(element(by.id('exportResultFormatBtn'))).toHaveLabel('png');

await expect(element(by.id('selectBgBtn'))).toBeVisible();
await expect(element(by.id('selectBgBtn'))).toHaveLabel('select bg');

// await expect(element(by.id('selectWatermarkBtn'))).toBeVisible();
// await expect(element(by.id('selectWatermarkBtn'))).toHaveLabel(
// 'select watermark'
// );

await expect(element(by.id('resultFileSizeLabel'))).toBeVisible();
await expect(element(by.id('resultFilePathLabel'))).toBeVisible();
if (device.getPlatform() === 'ios') {
const resultFileSizeLabel = await element(
by.id('resultFileSizeLabel')
).getAttributes('text');

assert.ok(
/^result file size:\d+(.\d+)?\s(KB|MB)$/.test(resultFileSizeLabel.text)
);
const resultFilePathLabel = await element(
by.id('resultFilePathLabel')
).getAttributes('text');
assert.ok(/^file path:.*\.png$/.test(resultFilePathLabel.text));
} else {
await expect(element(by.id('resultFileSizeLabel'))).toHaveLabel(
/^result file size:\d+(\.\d+)?\s(KB|MB)$/
);
await expect(element(by.id('resultFilePathLabel'))).toBeVisible();
await expect(element(by.id('resultFilePathLabel'))).toHaveText(
/^file path:.*\.png$/
);
}

await expect(element(by.id('resultImage'))).toBeVisible();
});

describe('when click backgroundImageFormatBtn', () => {
it('should display correctly', async () => {
await expect(element(by.id('backgroundImageFormatBtn'))).toHaveLabel(
'image'
);
await element(by.id('backgroundImageFormatBtn')).tap();
await waitFor(element(by.type('RCTModalHostView')))
.toBeVisible()
.withTimeout(2000);
await element(by.label('base64')).tap();
await expect(element(by.id('backgroundImageFormatBtn'))).toHaveLabel(
'base64'
);
});
});
});
12 changes: 12 additions & 0 deletions example/e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
rootDir: '..',
testMatch: ['<rootDir>/e2e/**/*.test.js'],
testTimeout: 120000,
maxWorkers: 1,
globalSetup: 'detox/runners/jest/globalSetup',
globalTeardown: 'detox/runners/jest/globalTeardown',
reporters: ['detox/runners/jest/reporter'],
testEnvironment: 'detox/runners/jest/testEnvironment',
verbose: true,
};
Loading

0 comments on commit 05ea67a

Please sign in to comment.