mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-10-04 07:06:58 +02:00
feat(lyrics): keep the screen awake while viewing lyrics
This commit is contained in:
1 parent
a1cec6de51
commit
8aa8d598d8
14 files changed
+276
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:spotiflac_android/models/settings.dart';
|
||||
import 'package:spotiflac_android/providers/settings_provider.dart';
|
||||
import 'package:spotiflac_android/widgets/lyrics_screen_awake.dart';
|
||||
|
||||
class _Settings extends SettingsNotifier {
|
||||
@override
|
||||
AppSettings build() => const AppSettings();
|
||||
|
||||
@override
|
||||
void setKeepScreenOnLyrics(bool enabled) {
|
||||
state = state.copyWith(keepScreenOnLyrics: enabled);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final binding = TestWidgetsFlutterBinding.ensureInitialized();
|
||||
const channel = MethodChannel('com.zarz.spotiflac/backend');
|
||||
late List<bool> requests;
|
||||
|
||||
setUp(() {
|
||||
requests = [];
|
||||
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
|
||||
call,
|
||||
) async {
|
||||
if (call.method == 'setScreenAwake') {
|
||||
requests.add(
|
||||
(call.arguments as Map<Object?, Object?>)['enabled']! as bool,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null);
|
||||
});
|
||||
|
||||
test('screen preference defaults on and survives settings round trips', () {
|
||||
expect(AppSettings.fromJson({}).keepScreenOnLyrics, isTrue);
|
||||
for (final enabled in [false, true]) {
|
||||
final settings = const AppSettings().copyWith(
|
||||
keepScreenOnLyrics: enabled,
|
||||
);
|
||||
expect(
|
||||
AppSettings.fromJson(settings.toJson()).keepScreenOnLyrics,
|
||||
enabled,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('only visible lyrics hold the screen awake and release on exit', (
|
||||
tester,
|
||||
) async {
|
||||
final visible = ValueNotifier(false);
|
||||
addTearDown(visible.dispose);
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [settingsProvider.overrideWith(_Settings.new)],
|
||||
child: MaterialApp(
|
||||
home: ValueListenableBuilder(
|
||||
valueListenable: visible,
|
||||
builder: (context, value, _) =>
|
||||
LyricsScreenAwake(visible: value, child: const SizedBox()),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(requests, isEmpty);
|
||||
visible.value = true;
|
||||
await tester.pump();
|
||||
expect(requests, [true]);
|
||||
await tester.pump();
|
||||
expect(requests, [
|
||||
true,
|
||||
], reason: 'No repeated requests on unchanged frames');
|
||||
visible.value = false;
|
||||
await tester.pump();
|
||||
expect(requests, [true, false]);
|
||||
visible.value = true;
|
||||
await tester.pump();
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
expect(requests, [true, false, true, false]);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'releases in background, under another route, and when disabled',
|
||||
(tester) async {
|
||||
final navigator = GlobalKey<NavigatorState>();
|
||||
final container = ProviderContainer(
|
||||
overrides: [settingsProvider.overrideWith(_Settings.new)],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
await tester.pumpWidget(
|
||||
UncontrolledProviderScope(
|
||||
container: container,
|
||||
child: MaterialApp(
|
||||
navigatorKey: navigator,
|
||||
home: const LyricsScreenAwake(visible: true, child: SizedBox()),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(requests.last, isTrue);
|
||||
binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
||||
await tester.pump();
|
||||
expect(requests.last, isFalse);
|
||||
binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
||||
binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
expect(requests.last, isTrue);
|
||||
navigator.currentState!.push<void>(
|
||||
MaterialPageRoute(builder: (_) => const Scaffold()),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(requests.last, isFalse);
|
||||
navigator.currentState!.pop();
|
||||
await tester.pumpAndSettle();
|
||||
expect(requests.last, isTrue);
|
||||
container.read(settingsProvider.notifier).setKeepScreenOnLyrics(false);
|
||||
await tester.pump();
|
||||
expect(requests.last, isFalse);
|
||||
binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
||||
binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
expect(requests.last, isFalse);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -63,6 +63,7 @@ void main() {
|
||||
.setMockMethodCallHandler(secureStorageChannel, (_) async => null);
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(backendChannel, (call) async {
|
||||
if (call.method == 'setScreenAwake') return null;
|
||||
if (call.method != 'readFileMetadata') {
|
||||
fail('Unexpected platform call: ${call.method}');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user