Visitar URL original
[pigeon] added android unittests (#327) · flutter/packages@ad3368b · GitHub
Skip to content

Commit ad3368b

Browse files
authored
[pigeon] added android unittests (#327)
1 parent 70c8866 commit ad3368b

29 files changed

Lines changed: 562 additions & 3 deletions

File tree

‎packages/pigeon/CONTRIBUTING.md‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,3 @@ This is how `dart:mirrors` gets access to the supplied Pigeon file.
7272
reached the limitations of using dart:mirrors for parsing the Dart files.
7373
That package has been deprecated and it doesn't support null-safe annotations.
7474
We should migrate to using the Dart Analyzer as the front-end parser.
75-
* Integration tests for Android - Right now we have integration tests for Dart
76-
and iOS. We should add some for Android. It is limiting the speed at which
77-
we can bring in external contributor's contributions to Android / Java.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:pigeon/pigeon.dart';
6+
7+
class SetRequest {
8+
int? value;
9+
}
10+
11+
class NestedRequest {
12+
String? context;
13+
SetRequest? request;
14+
}
15+
16+
@HostApi()
17+
abstract class Api {
18+
void setValue(SetRequest request);
19+
}
20+
21+
@HostApi()
22+
abstract class NestedApi {
23+
void setValueWithContext(NestedRequest request);
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
**/ios/Flutter/.last_build_id
26+
.dart_tool/
27+
.flutter-plugins
28+
.flutter-plugins-dependencies
29+
.packages
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Web related
35+
lib/generated_plugin_registrant.dart
36+
37+
# Symbolication related
38+
app.*.symbols
39+
40+
# Obfuscation related
41+
app.*.map.json
42+
43+
# Android Studio will place build artifacts here
44+
/android/app/debug
45+
/android/app/profile
46+
/android/app/release
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 891511d58f6550ce9e9b03b8d7c6a602caa97488
8+
channel: master
9+
10+
project_type: app
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# android_unit_tests
2+
3+
Unit-tests for Pigeon generated Java code. See [../../run_tests.sh](../../run_tests.sh).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
gradle-wrapper.jar
2+
/.gradle
3+
/captures/
4+
/gradlew
5+
/gradlew.bat
6+
/local.properties
7+
GeneratedPluginRegistrant.java
8+
9+
# Remember to never publicly share your keystore.
10+
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
11+
key.properties
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
def localProperties = new Properties()
2+
def localPropertiesFile = rootProject.file('local.properties')
3+
if (localPropertiesFile.exists()) {
4+
localPropertiesFile.withReader('UTF-8') { reader ->
5+
localProperties.load(reader)
6+
}
7+
}
8+
9+
def flutterRoot = localProperties.getProperty('flutter.sdk')
10+
if (flutterRoot == null) {
11+
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12+
}
13+
14+
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
15+
if (flutterVersionCode == null) {
16+
flutterVersionCode = '1'
17+
}
18+
19+
def flutterVersionName = localProperties.getProperty('flutter.versionName')
20+
if (flutterVersionName == null) {
21+
flutterVersionName = '1.0'
22+
}
23+
24+
apply plugin: 'com.android.application'
25+
apply plugin: 'kotlin-android'
26+
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27+
28+
android {
29+
compileSdkVersion 30
30+
31+
sourceSets {
32+
main.java.srcDirs += 'src/main/kotlin'
33+
}
34+
35+
defaultConfig {
36+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
37+
applicationId "com.example.android_unit_tests"
38+
minSdkVersion 16
39+
targetSdkVersion 30
40+
versionCode flutterVersionCode.toInteger()
41+
versionName flutterVersionName
42+
}
43+
44+
buildTypes {
45+
release {
46+
// TODO: Add your own signing config for the release build.
47+
// Signing with the debug keys for now, so `flutter run --release` works.
48+
signingConfig signingConfigs.debug
49+
}
50+
}
51+
52+
testOptions {
53+
unitTests.returnDefaultValues = true
54+
}
55+
}
56+
57+
flutter {
58+
source '../..'
59+
}
60+
61+
dependencies {
62+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
63+
testImplementation 'junit:junit:4.+'
64+
testImplementation "org.mockito:mockito-core:3.+"
65+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.example.android_unit_tests">
3+
<!-- Flutter needs it to communicate with the running application
4+
to allow setting breakpoints, to provide hot reload, etc.
5+
-->
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
</manifest>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.example.android_unit_tests">
3+
<application
4+
android:label="android_unit_tests"
5+
android:icon="@mipmap/ic_launcher">
6+
<activity
7+
android:name=".MainActivity"
8+
android:launchMode="singleTop"
9+
android:theme="@style/LaunchTheme"
10+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
11+
android:hardwareAccelerated="true"
12+
android:windowSoftInputMode="adjustResize">
13+
<!-- Specifies an Android theme to apply to this Activity as soon as
14+
the Android process has started. This theme is visible to the user
15+
while the Flutter UI initializes. After that, this theme continues
16+
to determine the Window background behind the Flutter UI. -->
17+
<meta-data
18+
android:name="io.flutter.embedding.android.NormalTheme"
19+
android:resource="@style/NormalTheme"
20+
/>
21+
<!-- Displays an Android View that continues showing the launch screen
22+
Drawable until Flutter paints its first frame, then this splash
23+
screen fades out. A splash screen is useful to avoid any visual
24+
gap between the end of Android's launch screen and the painting of
25+
Flutter's first frame. -->
26+
<meta-data
27+
android:name="io.flutter.embedding.android.SplashScreenDrawable"
28+
android:resource="@drawable/launch_background"
29+
/>
30+
<intent-filter>
31+
<action android:name="android.intent.action.MAIN"/>
32+
<category android:name="android.intent.category.LAUNCHER"/>
33+
</intent-filter>
34+
</activity>
35+
<!-- Don't delete the meta-data below.
36+
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
37+
<meta-data
38+
android:name="flutterEmbedding"
39+
android:value="2" />
40+
</application>
41+
</manifest>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Autogenerated from Pigeon (v0.2.0), do not edit directly.
2+
// See also: https://pub.dev/packages/pigeon
3+
4+
package com.example.android_unit_tests;
5+
6+
import io.flutter.plugin.common.BasicMessageChannel;
7+
import io.flutter.plugin.common.BinaryMessenger;
8+
import io.flutter.plugin.common.StandardMessageCodec;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/** Generated class from Pigeon. */
13+
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression"})
14+
public class Pigeon {
15+
16+
/** Generated class from Pigeon that represents data sent in messages. */
17+
public static class SetRequest {
18+
private Long value;
19+
20+
public Long getValue() {
21+
return value;
22+
}
23+
24+
public void setValue(Long setterArg) {
25+
this.value = setterArg;
26+
}
27+
28+
Map<String, Object> toMap() {
29+
Map<String, Object> toMapResult = new HashMap<>();
30+
toMapResult.put("value", value);
31+
return toMapResult;
32+
}
33+
34+
static SetRequest fromMap(Map<String, Object> map) {
35+
SetRequest fromMapResult = new SetRequest();
36+
Object value = map.get("value");
37+
fromMapResult.value =
38+
(value == null) ? null : ((value instanceof Integer) ? (Integer) value : (Long) value);
39+
return fromMapResult;
40+
}
41+
}
42+
43+
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
44+
public interface Api {
45+
void setValue(SetRequest arg);
46+
47+
/** Sets up an instance of `Api` to handle messages through the `binaryMessenger`. */
48+
static void setup(BinaryMessenger binaryMessenger, Api api) {
49+
{
50+
BasicMessageChannel<Object> channel =
51+
new BasicMessageChannel<>(
52+
binaryMessenger, "dev.flutter.pigeon.Api.setValue", new StandardMessageCodec());
53+
if (api != null) {
54+
channel.setMessageHandler(
55+
(message, reply) -> {
56+
Map<String, Object> wrapped = new HashMap<>();
57+
try {
58+
@SuppressWarnings("ConstantConditions")
59+
SetRequest input = SetRequest.fromMap((Map<String, Object>) message);
60+
api.setValue(input);
61+
wrapped.put("result", null);
62+
} catch (Error | RuntimeException exception) {
63+
wrapped.put("error", wrapError(exception));
64+
}
65+
reply.reply(wrapped);
66+
});
67+
} else {
68+
channel.setMessageHandler(null);
69+
}
70+
}
71+
}
72+
}
73+
74+
private static Map<String, Object> wrapError(Throwable exception) {
75+
Map<String, Object> errorMap = new HashMap<>();
76+
errorMap.put("message", exception.toString());
77+
errorMap.put("code", exception.getClass().getSimpleName());
78+
errorMap.put("details", null);
79+
return errorMap;
80+
}
81+
}

0 commit comments

Comments
 (0)