fixed tests

This commit is contained in:
Thibault Deckers 2021-07-03 17:17:43 +09:00
parent adc41bf3cd
commit 085990c854
8 changed files with 39 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import 'package:aves/services/window_service.dart';
import 'package:aves/services/services.dart';
import 'package:aves/widgets/common/extensions/build_context.dart';
import 'package:flutter/widgets.dart';
@ -19,6 +19,6 @@ extension ExtraKeepScreenOn on KeepScreenOn {
}
void apply() {
WindowService.keepScreenOn(this == KeepScreenOn.always);
windowService.keepScreenOn(this == KeepScreenOn.always);
}
}

View file

@ -4,7 +4,7 @@ import 'package:aves/model/filters/filters.dart';
import 'package:aves/model/settings/enums.dart';
import 'package:aves/model/settings/screen_on.dart';
import 'package:aves/model/source/enums.dart';
import 'package:aves/services/window_service.dart';
import 'package:aves/services/services.dart';
import 'package:aves/utils/pedantic.dart';
import 'package:collection/collection.dart';
import 'package:firebase_core/firebase_core.dart';
@ -99,7 +99,7 @@ class Settings extends ChangeNotifier {
Future<void> init() async {
_prefs = await SharedPreferences.getInstance();
_isRotationLocked = await WindowService.isRotationLocked();
_isRotationLocked = await windowService.isRotationLocked();
}
// Crashlytics initialization is separated from the main settings initialization
@ -385,7 +385,7 @@ class Settings extends ChangeNotifier {
if (_isRotationLocked != newValue) {
_isRotationLocked = newValue;
if (!_isRotationLocked) {
WindowService.requestOrientation();
windowService.requestOrientation();
}
notifyListeners();
}

View file

@ -6,6 +6,7 @@ import 'package:aves/services/media_store_service.dart';
import 'package:aves/services/metadata_service.dart';
import 'package:aves/services/storage_service.dart';
import 'package:aves/services/time_service.dart';
import 'package:aves/services/window_service.dart';
import 'package:get_it/get_it.dart';
import 'package:path/path.dart' as p;
@ -21,6 +22,7 @@ final MediaStoreService mediaStoreService = getIt<MediaStoreService>();
final MetadataService metadataService = getIt<MetadataService>();
final StorageService storageService = getIt<StorageService>();
final TimeService timeService = getIt<TimeService>();
final WindowService windowService = getIt<WindowService>();
void initPlatformServices() {
getIt.registerLazySingleton<p.Context>(() => p.Context());
@ -33,4 +35,5 @@ void initPlatformServices() {
getIt.registerLazySingleton<MetadataService>(() => PlatformMetadataService());
getIt.registerLazySingleton<StorageService>(() => PlatformStorageService());
getIt.registerLazySingleton<TimeService>(() => PlatformTimeService());
getIt.registerLazySingleton<WindowService>(() => PlatformWindowService());
}

View file

@ -2,10 +2,19 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
class WindowService {
abstract class WindowService {
Future<void> keepScreenOn(bool on);
Future<bool> isRotationLocked();
Future<void> requestOrientation([Orientation? orientation]);
}
class PlatformWindowService implements WindowService {
static const platform = MethodChannel('deckers.thibault/aves/window');
static Future<void> keepScreenOn(bool on) async {
@override
Future<void> keepScreenOn(bool on) async {
try {
await platform.invokeMethod('keepScreenOn', <String, dynamic>{
'on': on,
@ -15,7 +24,8 @@ class WindowService {
}
}
static Future<bool> isRotationLocked() async {
@override
Future<bool> isRotationLocked() async {
try {
final result = await platform.invokeMethod('isRotationLocked');
if (result != null) return result as bool;
@ -25,7 +35,8 @@ class WindowService {
return false;
}
static Future<void> requestOrientation([Orientation? orientation]) async {
@override
Future<void> requestOrientation([Orientation? orientation]) async {
// cf Android `ActivityInfo.ScreenOrientation`
late final int orientationCode;
switch (orientation) {

View file

@ -12,7 +12,6 @@ import 'package:aves/ref/mime_types.dart';
import 'package:aves/services/android_app_service.dart';
import 'package:aves/services/image_op_events.dart';
import 'package:aves/services/services.dart';
import 'package:aves/services/window_service.dart';
import 'package:aves/theme/durations.dart';
import 'package:aves/utils/pedantic.dart';
import 'package:aves/widgets/collection/collection_page.dart';
@ -124,10 +123,10 @@ class EntryActionDelegate with FeedbackMixin, PermissionAwareMixin, SizeAwareMix
Future<void> _rotateScreen(BuildContext context) async {
switch (context.read<MediaQueryData>().orientation) {
case Orientation.landscape:
await WindowService.requestOrientation(Orientation.portrait);
await windowService.requestOrientation(Orientation.portrait);
break;
case Orientation.portrait:
await WindowService.requestOrientation(Orientation.landscape);
await windowService.requestOrientation(Orientation.landscape);
break;
}
}

View file

@ -9,7 +9,6 @@ import 'package:aves/model/settings/enums.dart';
import 'package:aves/model/settings/settings.dart';
import 'package:aves/model/source/collection_lens.dart';
import 'package:aves/services/services.dart';
import 'package:aves/services/window_service.dart';
import 'package:aves/theme/durations.dart';
import 'package:aves/utils/change_notifier.dart';
import 'package:aves/widgets/collection/collection_page.dart';
@ -122,7 +121,7 @@ class _EntryViewerStackState extends State<EntryViewerStack> with SingleTickerPr
WidgetsBinding.instance!.addObserver(this);
WidgetsBinding.instance!.addPostFrameCallback((_) => _initOverlay());
if (settings.keepScreenOn == KeepScreenOn.viewerOnly) {
WindowService.keepScreenOn(true);
windowService.keepScreenOn(true);
}
}
@ -511,9 +510,9 @@ class _EntryViewerStackState extends State<EntryViewerStack> with SingleTickerPr
void _onLeave() {
_showSystemUI();
WindowService.requestOrientation();
windowService.requestOrientation();
if (settings.keepScreenOn == KeepScreenOn.viewerOnly) {
WindowService.keepScreenOn(false);
windowService.keepScreenOn(false);
}
}

View file

@ -0,0 +1,8 @@
import 'package:aves/services/window_service.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
class FakeWindowService extends Fake implements WindowService {
@override
Future<bool> isRotationLocked() => SynchronousFuture(false);
}

View file

@ -15,6 +15,7 @@ import 'package:aves/services/metadata_service.dart';
import 'package:aves/services/services.dart';
import 'package:aves/services/storage_service.dart';
import 'package:aves/services/time_service.dart';
import 'package:aves/services/window_service.dart';
import 'package:aves/utils/android_file_utils.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
@ -27,6 +28,7 @@ import '../fake/metadata_db.dart';
import '../fake/metadata_service.dart';
import '../fake/storage_service.dart';
import '../fake/time_service.dart';
import '../fake/window_service.dart';
void main() {
const testAlbum = '${FakeStorageService.primaryPath}Pictures/test';
@ -44,6 +46,7 @@ void main() {
getIt.registerLazySingleton<MetadataService>(() => FakeMetadataService());
getIt.registerLazySingleton<StorageService>(() => FakeStorageService());
getIt.registerLazySingleton<TimeService>(() => FakeTimeService());
getIt.registerLazySingleton<WindowService>(() => FakeWindowService());
await settings.init();
});