Skip to content

Commit

Permalink
test(windows): added tests for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
RatakondalaArun authored and MarkOSullivan94 committed Jul 26, 2022
1 parent 9a35e23 commit 6329b7a
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 11 deletions.
28 changes: 18 additions & 10 deletions test/abs/icon_generator_test.mocks.dart
Expand Up @@ -3,7 +3,8 @@
// Do not manually edit this file.

import 'package:flutter_launcher_icons/abs/icon_generator.dart' as _i2;
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart' as _i3;
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart'
as _i3;
import 'package:mockito/mockito.dart' as _i1;

// ignore_for_file: type=lint
Expand All @@ -16,19 +17,22 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types

class _FakeIconGeneratorContext_0 extends _i1.Fake implements _i2.IconGeneratorContext {}
class _FakeIconGeneratorContext_0 extends _i1.Fake
implements _i2.IconGeneratorContext {}

/// A class which mocks [FlutterLauncherIconsConfig].
///
/// See the documentation for Mockito's code generation for more information.
class MockFlutterLauncherIconsConfig extends _i1.Mock implements _i3.FlutterLauncherIconsConfig {
class MockFlutterLauncherIconsConfig extends _i1.Mock
implements _i3.FlutterLauncherIconsConfig {
MockFlutterLauncherIconsConfig() {
_i1.throwOnMissingStub(this);
}

@override
Map<String, dynamic> toJson() =>
(super.noSuchMethod(Invocation.method(#toJson, []), returnValue: <String, dynamic>{}) as Map<String, dynamic>);
(super.noSuchMethod(Invocation.method(#toJson, []),
returnValue: <String, dynamic>{}) as Map<String, dynamic>);
}

/// A class which mocks [IconGenerator].
Expand All @@ -40,14 +44,18 @@ class MockIconGenerator extends _i1.Mock implements _i2.IconGenerator {
}

@override
_i2.IconGeneratorContext get context =>
(super.noSuchMethod(Invocation.getter(#context), returnValue: _FakeIconGeneratorContext_0())
as _i2.IconGeneratorContext);
_i2.IconGeneratorContext get context => (super.noSuchMethod(
Invocation.getter(#context),
returnValue: _FakeIconGeneratorContext_0()) as _i2.IconGeneratorContext);
@override
String get platformName => (super.noSuchMethod(Invocation.getter(#platformName), returnValue: '') as String);
String get platformName =>
(super.noSuchMethod(Invocation.getter(#platformName), returnValue: '')
as String);
@override
void createIcons() => super.noSuchMethod(Invocation.method(#createIcons, []), returnValueForMissingStub: null);
void createIcons() => super.noSuchMethod(Invocation.method(#createIcons, []),
returnValueForMissingStub: null);
@override
bool validateRequirements() =>
(super.noSuchMethod(Invocation.method(#validateRequirments, []), returnValue: false) as bool);
(super.noSuchMethod(Invocation.method(#validateRequirements, []),
returnValue: false) as bool);
}
5 changes: 4 additions & 1 deletion test/all_tests.dart
Expand Up @@ -5,8 +5,9 @@ import 'android_test.dart' as android_test;
import 'flutter_launcher_icons_config_test.dart' as fli_config;
import 'main_test.dart' as main_test;
import 'utils_test.dart' as utils_test;
import 'web/web_template_test.dart' as web_template_test;
import 'web/web_icon_generator_test.dart' as web_icon_gen_test;
import 'web/web_template_test.dart' as web_template_test;
import 'windows/windows_icon_generator_test.dart' as windows_icon_gen_test;

void main() {
group('Flutter launcher icons', () {
Expand All @@ -21,5 +22,7 @@ void main() {
// web
web_template_test.main();
web_icon_gen_test.main();
// windows
windows_icon_gen_test.main();
});
}
39 changes: 39 additions & 0 deletions test/flutter_launcher_icons_config_test.dart
Expand Up @@ -46,6 +46,19 @@ void main() {
'theme_color': '#0175C2',
}),
);
// windows
expect(configs.windowsConfig, isNotNull);
expect(configs.windowsConfig!.generate, isNotNull);
expect(configs.windowsConfig!.iconSize, isNotNull);
expect(configs.windowsConfig!.imagePath, isNotNull);
expect(
configs.windowsConfig!.toJson(),
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
'icon_size': 48,
}),
);
});

test('should return null when invalid filePath is given', () {
Expand Down Expand Up @@ -96,6 +109,19 @@ void main() {
'theme_color': '#0175C2',
}),
);
// windows
expect(configs.windowsConfig, isNotNull);
expect(configs.windowsConfig!.generate, isNotNull);
expect(configs.windowsConfig!.iconSize, isNotNull);
expect(configs.windowsConfig!.imagePath, isNotNull);
expect(
configs.windowsConfig!.toJson(),
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
'icon_size': 48,
}),
);
});

group('should throw', () {
Expand Down Expand Up @@ -146,6 +172,19 @@ void main() {
'theme_color': '#0175C2',
}),
);
// windows
expect(configs.windowsConfig, isNotNull);
expect(configs.windowsConfig!.generate, isNotNull);
expect(configs.windowsConfig!.iconSize, isNotNull);
expect(configs.windowsConfig!.imagePath, isNotNull);
expect(
configs.windowsConfig!.toJson(),
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
'icon_size': 48,
}),
);
});
});
});
Expand Down
37 changes: 37 additions & 0 deletions test/templates.dart
Expand Up @@ -14,6 +14,10 @@ flutter_icons:
theme_color: "#0175C2" # hex_color
apple_mobile_web_app_title: "demo"
apple_mobile_web_app_status_bar_style: "hex_color"
windows:
generate: true
image_path: "app_icon.png"
icon_size: 48
''';

const flavorFLIConfigTemplate = fliConfigTemplate;
Expand All @@ -29,6 +33,31 @@ flutter_icons:
apple_mobile_web_app_status_bar_style: "hex_color"
''';

const fliWindowsConfig = r'''
flutter_icons:
windows:
generate: true
image_path: "app_icon.png"
icon_size: 48
''';

const fliWindowsConfigWithInvalidIconSize = r'''
flutter_icons:
windows:
generate: true
image_path: "app_icon.png"
icon_size: 258
''';

const fliWindowsConfigWithNullImagePath = r'''
flutter_icons:
image_path: null
windows:
generate: true
image_path: null
icon_size: 258
''';

const invlaidfliConfigTemplate = r'''
# flutter_icons
android: true
Expand Down Expand Up @@ -101,6 +130,10 @@ flutter_icons:
theme_color: "#0175C2" # hex_color
apple_mobile_web_app_title: "demo"
apple_mobile_web_app_status_bar_style: "hex_color"
windows:
generate: true
image_path: "app_icon.png"
icon_size: 48
''';

const invalidPubspecTemplate = r'''
Expand Down Expand Up @@ -157,6 +190,10 @@ flutter_icons:
theme_color: "#0175C2" # hex_color
apple_mobile_web_app_title: "demo"
apple_mobile_web_app_status_bar_style: "hex_color"
windows:
generate: true
image_path: "app_icon.png"
icon_size: 48
''';

const webManifestTemplate = r'''
Expand Down
150 changes: 150 additions & 0 deletions test/windows/windows_icon_generator_test.dart
@@ -0,0 +1,150 @@
import 'dart:io';

import 'package:flutter_launcher_icons/abs/icon_generator.dart';
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart';
import 'package:flutter_launcher_icons/logger.dart';
import 'package:flutter_launcher_icons/windows/windows_icon_generator.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import '../templates.dart' as templates;
import 'windows_icon_generator_test.mocks.dart';

@GenerateMocks([FlutterLauncherIconsConfig, WindowsConfig, FLILogger])
void main() {
group('WindowsIconGenerator', () {
late IconGeneratorContext context;
late IconGenerator generator;
late FlutterLauncherIconsConfig mockConfig;
late WindowsConfig mockWindowsConfig;
late String prefixPath;
late File testImageFile;
late MockFLILogger mockLogger;
final assetPath = path.join(Directory.current.path, 'test', 'assets');

group('#validateRequirments', () {
setUpAll(() {
// make sure test file exists before starting test
testImageFile = File(path.join(assetPath, 'app_icon.png'));
expect(testImageFile.existsSync(), isTrue);
});
setUp(() async {
prefixPath = path.join(d.sandbox, 'fli_test');
mockConfig = MockFlutterLauncherIconsConfig();
mockWindowsConfig = MockWindowsConfig();
mockLogger = MockFLILogger();
context = IconGeneratorContext(config: mockConfig, prefixPath: prefixPath, logger: mockLogger);
generator = WindowsIconGenerator(context);
// initilize mock defaults
when(mockLogger.error(argThat(anything))).thenReturn(anything);
when(mockLogger.verbose(argThat(anything))).thenReturn(anything);
when(mockLogger.isVerbose).thenReturn(false);
when(mockConfig.windowsConfig).thenReturn(mockWindowsConfig);
when(mockWindowsConfig.generate).thenReturn(true);
when(mockWindowsConfig.imagePath).thenReturn(path.join(prefixPath, 'app_icon.png'));
when(mockConfig.imagePath).thenReturn(path.join(prefixPath, 'app_icon.png'));
when(mockWindowsConfig.iconSize).thenReturn(48);
});

test('should return false when windows config is not provided', () {
when(mockConfig.windowsConfig).thenReturn(null);
expect(generator.validateRequirements(), isFalse);
verify(mockConfig.windowsConfig).called(equals(1));
});

test('should return false when windowsConfig is not null but windows.generate is false', () {
when(mockConfig.windowsConfig).thenReturn(mockWindowsConfig);
when(mockWindowsConfig.generate).thenReturn(false);
expect(generator.validateRequirements(), isFalse);
verify(mockConfig.windowsConfig).called(equals(1));
verify(mockWindowsConfig.generate).called(equals(1));
});

test('should return false when windows.image_path and imagePath is null', () {
when(mockWindowsConfig.imagePath).thenReturn(null);
when(mockConfig.imagePath).thenReturn(null);
expect(generator.validateRequirements(), isFalse);

verifyInOrder([
mockWindowsConfig.imagePath,
mockConfig.imagePath,
]);
});

test('should return false when windows.icon_size is not between 48 and 256', () {
when(mockWindowsConfig.iconSize).thenReturn(40);
expect(generator.validateRequirements(), isFalse);
verify(mockWindowsConfig.iconSize).called(equals(3));

when(mockWindowsConfig.iconSize).thenReturn(257);
expect(generator.validateRequirements(), isFalse);
verify(mockWindowsConfig.iconSize).called(equals(4));
});

test('should return false when windows dir does not exist', () async {
await d.dir('fli_test', [
d.file('app_icon.png', testImageFile.readAsBytesSync()),
]).create();
await expectLater(
d.dir('fli_test', [
d.file('app_icon.png', anything),
]).validate(),
completes,
);
expect(generator.validateRequirements(), isFalse);
});

test('should return false when image file does not exist', () async {
await d.dir('fli_test', [d.dir('windows')]).create();
await expectLater(
d.dir('fli_test', [d.dir('windows')]).validate(),
completes,
);
expect(generator.validateRequirements(), isFalse);
});
});
});

group('WindowsIconGenerator end-to-end', () {
late IconGeneratorContext context;
late IconGenerator generator;
late FlutterLauncherIconsConfig config;
late String prefixPath;
final assetPath = path.join(Directory.current.path, 'test', 'assets');

setUp(() async {
final imageFile = File(path.join(assetPath, 'app_icon.png'));
expect(imageFile.existsSync(), isTrue);
await d.dir('fli_test', [
d.dir('windows'),
d.file('flutter_launcher_icons.yaml', templates.fliWindowsConfig),
d.file('pubspec.yaml', templates.pubspecTemplate),
d.file('app_icon.png', imageFile.readAsBytesSync()),
]).create();
prefixPath = path.join(d.sandbox, 'fli_test');
config = FlutterLauncherIconsConfig.loadConfigFromPath('flutter_launcher_icons.yaml', prefixPath)!;
context = IconGeneratorContext(config: config, prefixPath: prefixPath, logger: FLILogger(false));
generator = WindowsIconGenerator(context);
});

test('should generate valid icons', () async {
expect(generator.validateRequirements(), isTrue);
generator.createIcons();
await expectLater(
d.dir('fli_test', [
d.dir('windows', [
d.dir('runner', [
d.dir('resources', [
d.file('app_icon.ico', anything),
]),
]),
]),
]).validate(),
completes,
);
});
});
}

0 comments on commit 6329b7a

Please sign in to comment.