forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test of an Android platform view that draws a gradient (flutter…
…#153878) Closes flutter#152376. The gradient, for posterity, looks like this: <img src="https://github.com/user-attachments/assets/bed9599a-4e16-499c-af79-b51980095e89" width="150"> Let's see if CI agrees! /cc @johnmccutchan
- Loading branch information
1 parent
fa8f29e
commit 1b4df3b
Showing
8 changed files
with
189 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../src/main/kotlin/com/example/android_driver_test/BlueOrangeGradientPlatformViewFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
@file:Suppress("PackageName") | ||
|
||
package com.example.android_driver_test | ||
|
||
import android.content.Context | ||
import android.graphics.Canvas | ||
import android.graphics.Color | ||
import android.graphics.LinearGradient | ||
import android.graphics.Paint | ||
import android.graphics.Shader | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import io.flutter.plugin.platform.PlatformView | ||
import io.flutter.plugin.platform.PlatformViewFactory | ||
|
||
class BlueOrangeGradientPlatformViewFactory : PlatformViewFactory(null) { | ||
override fun create( | ||
context: Context, | ||
viewId: Int, | ||
args: Any? | ||
): PlatformView = GradientPlatformView(context) | ||
} | ||
|
||
private class GradientPlatformView( | ||
context: Context | ||
) : View(context), | ||
PlatformView { | ||
val paint = Paint() | ||
|
||
init { | ||
layoutParams = | ||
ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
} | ||
|
||
override fun getView(): View = this | ||
|
||
override fun dispose() {} | ||
|
||
override fun onDraw(canvas: Canvas) { | ||
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint) | ||
super.onDraw(canvas) | ||
} | ||
|
||
override fun onSizeChanged( | ||
w: Int, | ||
h: Int, | ||
oldw: Int, | ||
oldh: Int | ||
) { | ||
paint.shader = | ||
LinearGradient( | ||
0f, | ||
0f, | ||
w.toFloat(), | ||
h.toFloat(), | ||
intArrayOf( | ||
Color.rgb(0x41, 0x69, 0xE1), | ||
Color.rgb(0xFF, 0xA5, 0x00) | ||
), | ||
null, | ||
Shader.TileMode.CLAMP | ||
) | ||
super.onSizeChanged(w, h, oldw, oldh) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
dev/integration_tests/android_driver_test/lib/blue_orange_gradient_platform_view_main.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:io' as io; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_driver/driver_extension.dart'; | ||
|
||
void main() { | ||
enableFlutterDriverExtension(); | ||
|
||
if (kIsWeb || !io.Platform.isAndroid) { | ||
throw UnsupportedError('This app should only run on Android devices.'); | ||
} | ||
|
||
runApp(const MainApp()); | ||
} | ||
|
||
final class MainApp extends StatelessWidget { | ||
const MainApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const MaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
home: AndroidView(viewType: 'blue_orange_gradient_platform_view'), | ||
); | ||
} | ||
} |
File renamed without changes.
47 changes: 47 additions & 0 deletions
47
...n_tests/android_driver_test/test_driver/blue_orange_gradient_platform_view_main_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_driver/flutter_driver.dart'; | ||
import 'package:flutter_driver/src/native_driver.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '_flutter_goldens_fork.dart'; | ||
|
||
// TODO(matanlurey): This is done automatically by 'flutter test' but not by | ||
// 'flutter drive'. If we get closer to shipping the native 'flutter drive' | ||
// command, we should look into if 'flutter_test_config.dart', or a similar | ||
// mechanism, can be used to configure this automatically. | ||
void main() async { | ||
await testExecutable(_main); | ||
} | ||
|
||
Future<void> _main() async { | ||
// To generate golden files locally, uncomment the following line. | ||
// autoUpdateGoldenFiles = true; | ||
|
||
late FlutterDriver flutterDriver; | ||
late NativeDriver nativeDriver; | ||
|
||
setUpAll(() async { | ||
flutterDriver = await FlutterDriver.connect(); | ||
nativeDriver = await AndroidNativeDriver.connect(); | ||
}); | ||
|
||
tearDownAll(() async { | ||
await nativeDriver.close(); | ||
await flutterDriver.close(); | ||
}); | ||
|
||
test( | ||
'should screenshot and match a blue (top left) -> orange (bottom right) gradient', | ||
() async { | ||
await flutterDriver.waitFor(find.byType('AndroidView')); | ||
await expectLater( | ||
nativeDriver.screenshot(), | ||
matchesGoldenFile('android_driver_test.BlueOrangeGradient.png'), | ||
); | ||
}, | ||
timeout: Timeout.none, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters