Skip to content

Commit

Permalink
Added device param validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcc007 committed Jul 17, 2019
1 parent d228757 commit aa6b8b5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 9 deletions.
50 changes: 50 additions & 0 deletions lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,33 @@ class Config {
@visibleForTesting // config is exported in library
Future<bool> 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;

Expand Down Expand Up @@ -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;
}
59 changes: 50 additions & 9 deletions test/screenshots_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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)}');
}
});
});
}

0 comments on commit aa6b8b5

Please sign in to comment.