From aa6b8b55ec66ef09353577694c139ed529c96cc4 Mon Sep 17 00:00:00 2001 From: Maurice McCabe Date: Wed, 17 Jul 2019 12:44:07 -0700 Subject: [PATCH] Added device param validation --- lib/src/config.dart | 50 ++++++++++++++++++++++++++++++++ test/screenshots_test.dart | 59 ++++++++++++++++++++++++++++++++------ 2 files changed, 100 insertions(+), 9 deletions(-) diff --git a/lib/src/config.dart b/lib/src/config.dart index bb079bba..13210891 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -5,6 +5,7 @@ import 'dart:io'; import 'package:meta/meta.dart'; import 'image_processor.dart'; +import 'orientation.dart'; import 'screens.dart'; import 'package:yaml/yaml.dart'; import 'utils.dart' as utils; @@ -63,6 +64,33 @@ class Config { @visibleForTesting // config is exported in library Future validate( Screens screens, List allDevices, List allEmulators) async { + // validate params + final deviceNames = utils.getAllConfiguredDeviceNames(configInfo); + for (final devName in deviceNames) { + final deviceInfo = findDeviceInfo(configInfo, devName); + if (deviceInfo != null) { + final orientation = deviceInfo['orientation']; + if (orientation != null && !isValidOrientation(orientation)) { + stderr.writeln( + 'Invalid value for \'orientation\' for device \'$devName\': $orientation'); + stderr.writeln('Valid values:'); + for (final orientation in Orientation.values) { + stderr.writeln(' ${utils.getStringFromEnum(orientation)}'); + } + exit(1); + } + final frame = deviceInfo['frame']; + if (frame != null && !isValidFrame(frame)) { + stderr.writeln( + 'Invalid value for \'frame\' for device \'$devName\': $frame'); + stderr.writeln('Valid values:'); + stderr.writeln(' true'); + stderr.writeln(' false'); + exit(1); + } + } + } + final isDeviceAttached = (device) => device != null; final isEmulatorInstalled = (emulator) => emulator != null; @@ -210,3 +238,25 @@ class Config { simulators.forEach((simulator, _) => stdout.write(' $simulator\n')); } } + +bool isValidOrientation(String orientation) { + return Orientation.values.firstWhere( + (o) => utils.getStringFromEnum(o) == orientation, + orElse: () => null) != + null; +} + +bool isValidFrame(dynamic frame) { + return frame != null && (frame == true || frame == false); +} + +/// Find device info in config for device name. +Map findDeviceInfo(Map configInfo, String deviceName) { + Map deviceInfo; + configInfo['devices'].values.forEach((devices) { + devices.forEach((_deviceName, _deviceInfo) { + if (_deviceName == deviceName) deviceInfo = _deviceInfo; + }); + }); + return deviceInfo; +} diff --git a/test/screenshots_test.dart b/test/screenshots_test.dart index f68391fb..a691444d 100644 --- a/test/screenshots_test.dart +++ b/test/screenshots_test.dart @@ -8,6 +8,7 @@ import 'package:screenshots/src/daemon_client.dart'; import 'package:screenshots/src/globals.dart'; import 'package:screenshots/src/image_processor.dart'; import 'package:screenshots/src/orientation.dart' as orient; +import 'package:screenshots/src/orientation.dart'; import 'package:screenshots/src/screens.dart'; import 'package:screenshots/src/resources.dart' as resources; import 'package:screenshots/src/run.dart' as run; @@ -203,15 +204,6 @@ void main() { '/tmp/screenshots/$kTestScreenshotsDir', 'my_prefix'); }); - test('config guide', () async { - final Screens screens = Screens(); - await screens.init(); - final Config config = Config(configPath: 'test/screenshots_test.yaml'); - final daemonClient = DaemonClient(); - await daemonClient.start; - config.generateConfigGuide(screens, await daemonClient.devices); - }); - test('rooted emulator', () async { final emulatorId = 'Nexus_5X_API_27'; final stagingDir = '/tmp/tmp'; @@ -715,4 +707,53 @@ devices: await run.shutdownAndroidEmulator(daemonClient, deviceId), deviceId); }); }); + + group('config validate', () { + test('config guide', () async { + final Screens screens = Screens(); + await screens.init(); + final Config config = Config(configPath: 'test/screenshots_test.yaml'); + final daemonClient = DaemonClient(); + await daemonClient.start; + config.generateConfigGuide(screens, await daemonClient.devices); + }); + + test('validate device params', () { + final deviceName = 'ios device 1'; + final orientation = 'Portrait'; + final frame = true; + final params = ''' + devices: + ios: + $deviceName: + orientation: $orientation + frame: $frame + ios device 2: + android: + android device 1: + android device 2: + '''; + final configInfo = loadYaml(params); + final deviceNames = utils.getAllConfiguredDeviceNames(configInfo); + for (final devName in deviceNames) { + final deviceInfo = findDeviceInfo(configInfo, devName); + print('deviceInfo=$deviceInfo'); + if (deviceInfo != null) { + expect(deviceInfo['orientation'], orientation); + expect(isValidOrientation(orientation), isTrue); + expect(isValidOrientation('bad orientation'), isFalse); + expect(deviceInfo['frame'], frame); + expect(isValidFrame(frame), isTrue); + expect(isValidFrame('bad frame'), isFalse); + } + } + }); + + test('valid values for params', () { + print(Orientation.values); + for (final orientation in Orientation.values) { + print('${utils.getStringFromEnum(orientation)}'); + } + }); + }); }