fix: automate graduate timetable read and refine blue glass Dock
This commit is contained in:
@@ -2,6 +2,26 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hepan_everyday/features/school/school_login_flow.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'repeated school login bounces have a bounded automatic redirect budget',
|
||||
() {
|
||||
final flow = SchoolLoginFlow();
|
||||
final target = Uri.parse(
|
||||
'https://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
||||
);
|
||||
final login = Uri.parse('https://idas.uestc.edu.cn/authserver/login');
|
||||
final home = Uri.parse('https://yjsjy.uestc.edu.cn/pyxx/home/index');
|
||||
flow.begin(target);
|
||||
for (var i = 0; i < 2; i++) {
|
||||
flow.onLoaded(login, authenticated: false);
|
||||
expect(flow.onLoaded(home, authenticated: true), target);
|
||||
}
|
||||
flow.onLoaded(login, authenticated: false);
|
||||
expect(flow.onLoaded(home, authenticated: true), isNull);
|
||||
flow.begin(target);
|
||||
expect(flow.onLoaded(home, authenticated: true), target);
|
||||
},
|
||||
);
|
||||
test('wait for student authentication then request timetable once', () {
|
||||
final flow = SchoolLoginFlow();
|
||||
final target = Uri.parse(
|
||||
|
||||
@@ -2,6 +2,31 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hepan_everyday/features/school/school_navigation_policy.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'official HTTP graduate child frames can load for same-origin DOM reading',
|
||||
() {
|
||||
expect(
|
||||
SchoolNavigationPolicy.canNavigate(
|
||||
Uri.parse('http://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1'),
|
||||
isMainFrame: false,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
for (final value in [
|
||||
'http://evil.test/pyxx/pygl/xskbcx/index/1',
|
||||
'http://yjsjy.uestc.edu.cn:81/pyxx/pygl/xskbcx/index/1',
|
||||
'http://user@yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
||||
]) {
|
||||
expect(
|
||||
SchoolNavigationPolicy.canNavigate(
|
||||
Uri.parse(value),
|
||||
isMainFrame: false,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
test('secure captcha frames are allowed without authorizing credential injection', () {
|
||||
final captcha = Uri.parse('https://captcha.example.test/challenge');
|
||||
expect(
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// Run with Node: exercises the exact WebView script without executing school HTML scripts.
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const source = fs.readFileSync(path.join(__dirname, '../../lib/features/school/school_page_probe.dart'), 'utf8');
|
||||
const script = source.match(/schoolPageProbeScript = r'''([\s\S]*?)''';/)[1];
|
||||
const home = 'https://yjsjy.uestc.edu.cn/pyxx/home/index';
|
||||
const timetable = 'https://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1';
|
||||
|
||||
function term(id) {
|
||||
const result = {
|
||||
selectedIndex: 1,
|
||||
options: [false, false].map(selected => ({
|
||||
selected,
|
||||
setAttribute() { this.selected = true; },
|
||||
removeAttribute() { this.selected = false; },
|
||||
})),
|
||||
cloneNode() { return term(id); },
|
||||
get outerHTML() {
|
||||
return `<select id="${id}">${this.options.map((o, i) =>
|
||||
`<option value="${i}"${o.selected ? ' selected="selected"' : ''}>Term ${i}</option>`).join('')}</select>`;
|
||||
},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
function doc(url, { password = false, chrome = false, table = false, exams = false, frames = [] } = {}) {
|
||||
return {
|
||||
location: new URL(url),
|
||||
querySelector(selector) {
|
||||
if (selector === 'input[type="password"]') return password ? {} : null;
|
||||
if (selector === '#navbar' || selector === '#main-container') return chrome ? {} : null;
|
||||
if (selector === '#tbl') return table ? {outerHTML: '<table id="tbl">synthetic timetable</table>'} : null;
|
||||
if (selector === '#drpxq') return table ? term('drpxq') : null;
|
||||
if (selector === '#sample-table-1') return exams ? {outerHTML: '<table id="sample-table-1">synthetic exams</table>'} : null;
|
||||
if (selector === '#xqbh') return exams ? term('xqbh') : null;
|
||||
return null;
|
||||
},
|
||||
querySelectorAll(selector) { return selector === 'iframe,frame' ? frames : []; },
|
||||
};
|
||||
}
|
||||
|
||||
function probe(document, logout = () => {}) {
|
||||
return JSON.parse(vm.runInNewContext(script, {
|
||||
URL, document, location: document.location, window: {logout},
|
||||
}));
|
||||
}
|
||||
|
||||
assert.equal(probe(doc(home)).authenticated, true, 'real logout function on known graduate home');
|
||||
assert.equal(probe(doc(timetable, {chrome: true})).authenticated, true, 'graduate shell plus logout');
|
||||
assert.equal(probe(doc('http://yjsjy.uestc.edu.cn/pyxx?targetUrl=-1', {chrome: true})).authenticated, true, 'graduate root shell');
|
||||
assert.equal(probe(doc(home), undefined).authenticated, true); // default function
|
||||
assert.equal(probe(doc(home), null).authenticated, false, 'home URL alone is insufficient');
|
||||
assert.equal(probe(doc('https://yjsjy.uestc.edu.cn/pyxx/jzsso/login')).authenticated, false, 'callback alone is insufficient');
|
||||
|
||||
const top = probe(doc(timetable, {table: true}));
|
||||
assert.equal(top.pages.length, 1);
|
||||
assert.match(top.pages[0].html, /value="1" selected="selected"/);
|
||||
assert.doesNotMatch(top.pages[0].html, /value="0" selected/);
|
||||
|
||||
const inaccessible = {};
|
||||
Object.defineProperty(inaccessible, 'contentDocument', {get() { throw new Error('cross-origin'); }});
|
||||
const embedded = probe(doc(home, {frames: [
|
||||
inaccessible,
|
||||
{contentDocument: doc('https://captcha.example.test/pyxx/timetable', {table: true})},
|
||||
{contentDocument: doc('http://yjsjy.uestc.edu.cn/pyxx/timetable', {table: true})},
|
||||
{contentDocument: doc(timetable, {password: true, table: true})},
|
||||
{contentDocument: doc(timetable, {frames: [{contentDocument: doc(timetable, {table: true})}]})},
|
||||
]}));
|
||||
assert.equal(embedded.pages.length, 1, 'only nested same-origin graduate timetable is captured');
|
||||
assert.equal(embedded.pages[0].url, timetable);
|
||||
assert.equal(probe(doc(home, {password: true, table: true})).pages.length, 0);
|
||||
assert.equal(probe(doc('https://idas.uestc.edu.cn/authserver/login', {table: true})).pages.length, 0);
|
||||
for (const url of [
|
||||
'https://yjsjy.uestc.edu.cn.evil.test/pyxx/home/index',
|
||||
'https://yjsjy.uestc.edu.cn:444/pyxx/home/index',
|
||||
'https://user@yjsjy.uestc.edu.cn/pyxx/home/index',
|
||||
'file:///pyxx/home/index',
|
||||
]) assert.equal(probe(doc(url, {table: true})).pages.length, 0);
|
||||
assert.equal(probe(doc('http://yjsjy.uestc.edu.cn/pyxx/home/index', {table: true})).pages.length, 1);
|
||||
assert.equal(probe(doc(home, {exams: true})).pages[0].kind, 'exams');
|
||||
console.log('School WebView probe: authentication, table selection, frames, password exclusion, origins passed.');
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
|
||||
@@ -8,6 +7,7 @@ import 'package:hepan_everyday/features/school/school_exam_adapter.dart';
|
||||
|
||||
import '../support/fake_school_web.dart';
|
||||
import 'exam_adapter_test.dart' show examHtml;
|
||||
import 'recognized_timetable_test.dart' show fixture;
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -25,6 +25,170 @@ void main() {
|
||||
platform.delegate.finished(url);
|
||||
}
|
||||
|
||||
test('graduate logout function automatically opens timetable without an exit anchor', () async {
|
||||
await session.open();
|
||||
platform.web.javascript = (script) async =>
|
||||
script.contains("typeof window.logout")
|
||||
? schoolPageEvidence(topUrl: platform.web.url!, authenticated: true)
|
||||
: false;
|
||||
navigate('http://yjsjy.uestc.edu.cn/pyxx/home/index');
|
||||
await flushSchoolEvents();
|
||||
expect(platform.web.loads.last, SchoolSession.timetableUrl.toString());
|
||||
});
|
||||
|
||||
test('manual graduate page reads recognized embedded timetable instead of restarting login', () async {
|
||||
await session.open();
|
||||
const home = 'https://yjsjy.uestc.edu.cn/pyxx/home/index';
|
||||
platform.web.url = home;
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: home,
|
||||
html: fixture(),
|
||||
documentUrl: SchoolSession.timetableUrl.toString(),
|
||||
);
|
||||
final draft = await session.readTimetable();
|
||||
expect(draft.courses.length, 4);
|
||||
expect(platform.web.loads.length, 1);
|
||||
});
|
||||
|
||||
test(
|
||||
'closed authentication screen ignores later page-finished callbacks',
|
||||
() async {
|
||||
await session.open();
|
||||
session.setReadingActive(true);
|
||||
session.setReadingActive(false);
|
||||
platform.web.javascript = (_) async =>
|
||||
schoolPageEvidence(topUrl: platform.web.url!, html: fixture());
|
||||
navigate(SchoolSession.timetableUrl.toString());
|
||||
await flushSchoolEvents();
|
||||
expect(session.pendingDraft, isNull);
|
||||
expect(session.finding, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'asynchronous timetable DOM is imported without another navigation',
|
||||
() async {
|
||||
await session.open();
|
||||
session.setReadingActive(true);
|
||||
var ready = false;
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: platform.web.url!,
|
||||
html: ready ? fixture() : null,
|
||||
);
|
||||
navigate(SchoolSession.timetableUrl.toString());
|
||||
await flushSchoolEvents();
|
||||
expect(session.pendingDraft, isNull);
|
||||
expect(session.finding, isTrue);
|
||||
ready = true;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 850));
|
||||
await flushSchoolEvents();
|
||||
expect(session.pendingDraft!.courses.length, 4);
|
||||
expect(platform.web.loads.length, 1);
|
||||
},
|
||||
);
|
||||
|
||||
test('stale script response cannot cancel polling on a newer page', () async {
|
||||
await session.open();
|
||||
session.setReadingActive(true);
|
||||
final old = Completer<Object>();
|
||||
platform.web.javascript = (_) => old.future;
|
||||
navigate('https://yjsjy.uestc.edu.cn/pyxx/home/index');
|
||||
await flushSchoolEvents();
|
||||
var ready = false;
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: platform.web.url!,
|
||||
html: ready ? fixture() : null,
|
||||
);
|
||||
navigate(SchoolSession.timetableUrl.toString());
|
||||
await flushSchoolEvents();
|
||||
old.complete(
|
||||
schoolPageEvidence(
|
||||
topUrl: 'https://yjsjy.uestc.edu.cn/pyxx/home/index',
|
||||
authenticated: true,
|
||||
html: fixture(),
|
||||
),
|
||||
);
|
||||
await flushSchoolEvents();
|
||||
ready = true;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 850));
|
||||
await flushSchoolEvents();
|
||||
expect(session.pendingDraft?.courses.length, 4);
|
||||
});
|
||||
|
||||
test('external document evidence cannot become a school import', () async {
|
||||
await session.open();
|
||||
platform.web.url = SchoolSession.timetableUrl.toString();
|
||||
for (final url in [
|
||||
'https://evil.test/pyxx/pygl/xskbcx/index/1',
|
||||
'https://yjsjy.uestc.edu.cn:444/pyxx/pygl/xskbcx/index/1',
|
||||
'http://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
||||
'https://user@yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
||||
]) {
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: platform.web.url!,
|
||||
documentUrl: url,
|
||||
html: fixture(),
|
||||
);
|
||||
await expectLater(session.readTimetable(), throwsA(isA<Exception>()));
|
||||
}
|
||||
expect(session.pendingDraft, isNull);
|
||||
});
|
||||
|
||||
test('read-current recovery imports a frame and never reloads SSO', () async {
|
||||
await session.open();
|
||||
const home = 'http://yjsjy.uestc.edu.cn/pyxx/home/index';
|
||||
platform.web.url = home;
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: home,
|
||||
documentUrl: home,
|
||||
authenticated: true,
|
||||
html: fixture(),
|
||||
);
|
||||
await session.readCurrentPage();
|
||||
expect(session.pendingDraft?.courses.length, 4);
|
||||
expect(platform.web.loads.length, 1);
|
||||
});
|
||||
|
||||
test(
|
||||
'authenticated graduate root dashboard automatically opens timetable',
|
||||
() async {
|
||||
await session.open();
|
||||
const home = 'http://yjsjy.uestc.edu.cn/pyxx?targetUrl=-1';
|
||||
final decision = await platform.delegate.navigation(
|
||||
const NavigationRequest(url: home, isMainFrame: true),
|
||||
);
|
||||
expect(decision, NavigationDecision.navigate);
|
||||
platform.web.javascript = (_) async =>
|
||||
schoolPageEvidence(topUrl: home, authenticated: true);
|
||||
navigate(home);
|
||||
await flushSchoolEvents();
|
||||
expect(platform.web.loads.last, SchoolSession.timetableUrl.toString());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'obsolete automatic request failure preserves a newer pending import',
|
||||
() async {
|
||||
await session.open();
|
||||
final oldRequest = Completer<void>();
|
||||
platform.web.request = (_) => oldRequest.future;
|
||||
platform.web.javascript = (_) async =>
|
||||
schoolPageEvidence(topUrl: platform.web.url!, authenticated: true);
|
||||
navigate('https://yjsjy.uestc.edu.cn/pyxx/home/index');
|
||||
await flushSchoolEvents();
|
||||
platform.web.javascript = (_) async =>
|
||||
schoolPageEvidence(topUrl: platform.web.url!, html: fixture());
|
||||
navigate(SchoolSession.timetableUrl.toString());
|
||||
await flushSchoolEvents();
|
||||
final fresh = session.pendingDraft;
|
||||
expect(fresh, isNotNull);
|
||||
oldRequest.completeError(StateError('obsolete request failure'));
|
||||
await flushSchoolEvents();
|
||||
expect(session.pendingDraft, same(fresh));
|
||||
expect(session.message, '读取成功,正在保存…');
|
||||
},
|
||||
);
|
||||
|
||||
test('actual session follows service login then reads exam HTML', () async {
|
||||
await session.openExams();
|
||||
expect(platform.web.loads.single, SchoolSession.authUrl.toString());
|
||||
@@ -32,8 +196,14 @@ void main() {
|
||||
await flushSchoolEvents();
|
||||
expect(session.connectionState, SchoolConnectionState.authenticating);
|
||||
expect(session.finding, isFalse);
|
||||
platform.web.javascript = (script) async =>
|
||||
script.contains('outerHTML') ? jsonEncode(examHtml()) : true;
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: platform.web.url!,
|
||||
authenticated: true,
|
||||
html: platform.web.url == SchoolSession.examUrl.toString()
|
||||
? examHtml()
|
||||
: null,
|
||||
kind: 'exams',
|
||||
);
|
||||
navigate('http://yjsjy.uestc.edu.cn/pyxx/home/index');
|
||||
await flushSchoolEvents();
|
||||
expect(platform.web.loads.last, SchoolSession.examUrl.toString());
|
||||
@@ -47,7 +217,11 @@ void main() {
|
||||
'obsolete main-frame error cannot interrupt a newer exam read',
|
||||
() async {
|
||||
await session.openExams();
|
||||
platform.web.javascript = (_) async => jsonEncode(examHtml());
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: platform.web.url!,
|
||||
html: examHtml(),
|
||||
kind: 'exams',
|
||||
);
|
||||
navigate(SchoolSession.examUrl.toString());
|
||||
await flushSchoolEvents();
|
||||
platform.delegate.resourceError(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
|
||||
|
||||
@@ -25,6 +26,7 @@ class FakeSchoolWeb extends PlatformWebViewController {
|
||||
final loads = <String>[];
|
||||
String? url;
|
||||
Future<Object> Function(String)? javascript;
|
||||
Future<void> Function(Uri)? request;
|
||||
@override
|
||||
Future<void> setJavaScriptMode(JavaScriptMode mode) async {}
|
||||
@override
|
||||
@@ -35,6 +37,7 @@ class FakeSchoolWeb extends PlatformWebViewController {
|
||||
Future<void> loadRequest(LoadRequestParams params) async {
|
||||
url = params.uri.toString();
|
||||
loads.add(url!);
|
||||
await request?.call(params.uri);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -100,3 +103,18 @@ Future<void> flushSchoolEvents() async {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
String schoolPageEvidence({
|
||||
required String topUrl,
|
||||
bool authenticated = false,
|
||||
String? html,
|
||||
String? documentUrl,
|
||||
String kind = 'timetable',
|
||||
}) => jsonEncode({
|
||||
'topUrl': topUrl,
|
||||
'authenticated': authenticated,
|
||||
'pages': [
|
||||
if (html != null)
|
||||
{'kind': kind, 'url': documentUrl ?? topUrl, 'html': html},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:timezone/data/latest.dart' as tzdata;
|
||||
import 'package:hepan_everyday/widgets/glass_dock.dart';
|
||||
import 'package:hepan_everyday/app/hepan_app.dart';
|
||||
|
||||
import 'navigation_test.dart' show uiController;
|
||||
|
||||
void main() {
|
||||
setUpAll(tzdata.initializeTimeZones);
|
||||
testWidgets('held cursor enlarges before dragging and returns on release', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(child: GlassDock(selectedIndex: 0, onSelected: (_) {})),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final lens = find
|
||||
.descendant(
|
||||
of: find.byType(GlassDock),
|
||||
matching: find.byType(BackdropFilter),
|
||||
)
|
||||
.last;
|
||||
final resting = tester.getSize(lens);
|
||||
final gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byKey(const Key('dock-cursor'))),
|
||||
);
|
||||
await tester.pump(const Duration(milliseconds: 110));
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
final held = tester.getSize(lens);
|
||||
expect(held.width, greaterThan(resting.width * 1.12));
|
||||
expect(held.height, greaterThan(resting.height * 1.12));
|
||||
await gesture.cancel();
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getSize(lens).width, closeTo(resting.width, 0.5));
|
||||
expect(tester.getSize(lens).height, closeTo(resting.height, 0.5));
|
||||
});
|
||||
testWidgets(
|
||||
'Dock stays visible while sliding down instead of abruptly fading',
|
||||
(tester) async {
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
final c = uiController();
|
||||
await c.initialize();
|
||||
await c.showDemo();
|
||||
await tester.pumpWidget(HepanApp(controller: c));
|
||||
await tester.pumpAndSettle();
|
||||
final dock = find.byType(GlassDock);
|
||||
final before = tester.getRect(dock);
|
||||
final gesture = await tester.startGesture(
|
||||
tester.getCenter(find.text('周三')),
|
||||
);
|
||||
await gesture.moveBy(const Offset(0, -80));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 120));
|
||||
final fading = find.ancestor(
|
||||
of: dock,
|
||||
matching: find.byType(FadeTransition),
|
||||
);
|
||||
final opacity = fading.evaluate().isEmpty
|
||||
? 1.0
|
||||
: tester.widget<FadeTransition>(fading.first).opacity.value;
|
||||
expect(opacity, greaterThan(0.95));
|
||||
expect(tester.getRect(dock).top, greaterThan(before.top));
|
||||
expect(tester.getRect(dock).top, lessThan(844));
|
||||
await gesture.up();
|
||||
await tester.pumpAndSettle();
|
||||
expect(dock.hitTestable(), findsNothing);
|
||||
c.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'dart:ui' as ui;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
test(
|
||||
'glass samples and bends background pixels while keeping the centre stable',
|
||||
() async {
|
||||
const width = 160, height = 100;
|
||||
final sourceRecorder = ui.PictureRecorder();
|
||||
final sourceCanvas = ui.Canvas(sourceRecorder);
|
||||
for (var x = 0; x < width; x++) {
|
||||
sourceCanvas.drawRect(
|
||||
ui.Rect.fromLTWH(x.toDouble(), 0, 1, height.toDouble()),
|
||||
ui.Paint()..color = ui.Color.fromARGB(255, 40 + x, 70, 90),
|
||||
);
|
||||
}
|
||||
final sourcePicture = sourceRecorder.endRecording();
|
||||
final source = await sourcePicture.toImage(width, height);
|
||||
final program = await ui.FragmentProgram.fromAsset(
|
||||
'shaders/liquid_glass.frag',
|
||||
);
|
||||
final shader = program.fragmentShader()
|
||||
..setFloat(0, width.toDouble())
|
||||
..setFloat(1, height.toDouble())
|
||||
..setFloat(2, 0)
|
||||
..setFloat(3, 0)
|
||||
..setFloat(4, width.toDouble())
|
||||
..setFloat(5, height.toDouble())
|
||||
..setImageSampler(0, source);
|
||||
Future<ui.Image> draw(double strength) async {
|
||||
shader.setFloat(6, strength);
|
||||
final recorder = ui.PictureRecorder();
|
||||
ui.Canvas(recorder).drawRect(
|
||||
ui.Rect.fromLTWH(0, 0, width.toDouble(), height.toDouble()),
|
||||
ui.Paint()..shader = shader,
|
||||
);
|
||||
final picture = recorder.endRecording();
|
||||
final image = await picture.toImage(width, height);
|
||||
picture.dispose();
|
||||
return image;
|
||||
}
|
||||
|
||||
final plain = await draw(0), refracted = await draw(1);
|
||||
final a = (await plain.toByteData())!,
|
||||
b = (await refracted.toByteData())!;
|
||||
int red(ByteData data, int x) => data.getUint8((50 * width + x) * 4);
|
||||
expect(red(a, 12), closeTo(52, 1));
|
||||
expect((red(b, 12) - red(a, 12)).abs(), greaterThan(3));
|
||||
expect(red(b, 80), closeTo(red(a, 80), 1));
|
||||
expect(b.getUint8((50 * width + 12) * 4 + 3), 255);
|
||||
shader.dispose();
|
||||
plain.dispose();
|
||||
refracted.dispose();
|
||||
source.dispose();
|
||||
sourcePicture.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user