Skip to content

Commit

Permalink
test(macos): added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RatakondalaArun authored and MarkOSullivan94 committed Aug 27, 2022
1 parent 50f31fb commit 03f22fc
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 15 deletions.
37 changes: 22 additions & 15 deletions test/abs/icon_generator_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Mocks generated by Mockito 5.2.0 from annotations
// Mocks generated by Mockito 5.3.0 from annotations
// in flutter_launcher_icons/test/abs/icon_generator_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:flutter_launcher_icons/abs/icon_generator.dart' as _i2;
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart'
as _i3;
Expand All @@ -16,9 +17,13 @@ import 'package:mockito/mockito.dart' as _i1;
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakeIconGeneratorContext_0 extends _i1.Fake
implements _i2.IconGeneratorContext {}
class _FakeIconGeneratorContext_0 extends _i1.SmartFake
implements _i2.IconGeneratorContext {
_FakeIconGeneratorContext_0(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}

/// A class which mocks [FlutterLauncherIconsConfig].
///
Expand All @@ -38,24 +43,24 @@ class MockFlutterLauncherIconsConfig extends _i1.Mock
.noSuchMethod(Invocation.getter(#removeAlphaIOS), returnValue: false)
as bool);
@override
bool get isNeedingNewIOSIcon =>
(super.noSuchMethod(Invocation.getter(#isNeedingNewIOSIcon),
returnValue: false) as bool);
@override
bool get isNeedingNewAndroidIcon =>
(super.noSuchMethod(Invocation.getter(#isNeedingNewAndroidIcon),
returnValue: false) as bool);
@override
bool get hasAndroidAdaptiveConfig =>
(super.noSuchMethod(Invocation.getter(#hasAndroidAdaptiveConfig),
returnValue: false) as bool);
@override
bool get hasPlatformConfig =>
(super.noSuchMethod(Invocation.getter(#hasPlatformConfig),
returnValue: false) as bool);
@override
bool get isCustomAndroidFile =>
(super.noSuchMethod(Invocation.getter(#isCustomAndroidFile),
returnValue: false) as bool);
@override
bool get hasPlatformConfig =>
(super.noSuchMethod(Invocation.getter(#hasPlatformConfig),
bool get isNeedingNewAndroidIcon =>
(super.noSuchMethod(Invocation.getter(#isNeedingNewAndroidIcon),
returnValue: false) as bool);
@override
bool get isNeedingNewIOSIcon =>
(super.noSuchMethod(Invocation.getter(#isNeedingNewIOSIcon),
returnValue: false) as bool);
@override
Map<String, dynamic> toJson() =>
Expand All @@ -73,8 +78,10 @@ class MockIconGenerator extends _i1.Mock implements _i2.IconGenerator {

@override
_i2.IconGeneratorContext get context => (super.noSuchMethod(
Invocation.getter(#context),
returnValue: _FakeIconGeneratorContext_0()) as _i2.IconGeneratorContext);
Invocation.getter(#context),
returnValue:
_FakeIconGeneratorContext_0(this, Invocation.getter(#context)))
as _i2.IconGeneratorContext);
@override
String get platformName =>
(super.noSuchMethod(Invocation.getter(#platformName), returnValue: '')
Expand Down
5 changes: 5 additions & 0 deletions test/all_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:test/test.dart';
import 'abs/icon_generator_test.dart' as icon_generator_test;
import 'android_test.dart' as android_test;
import 'flutter_launcher_icons_config_test.dart' as fli_config;
import 'macos/macos_icon_generator_test.dart' as macos_icons_gen_test;
import 'macos/macos_icon_template_test.dart' as macos_template_test;
import 'main_test.dart' as main_test;
import 'utils_test.dart' as utils_test;
import 'web/web_icon_generator_test.dart' as web_icon_gen_test;
Expand All @@ -24,5 +26,8 @@ void main() {
web_icon_gen_test.main();
// windows
windows_icon_gen_test.main();
// macos
macos_template_test.main();
macos_icons_gen_test.main();
});
}
35 changes: 35 additions & 0 deletions test/flutter_launcher_icons_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ void main() {
'icon_size': 48,
}),
);
// macos
expect(configs.macOSConfig, isNotNull);
expect(configs.macOSConfig!.generate, isNotNull);
expect(configs.macOSConfig!.imagePath, isNotNull);
expect(
configs.macOSConfig!.toJson(),
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
}),
);
});

test('should return null when invalid filePath is given', () {
Expand Down Expand Up @@ -98,6 +109,8 @@ void main() {
expect(configs.webConfig, isNull);
// windows
expect(configs.windowsConfig, isNull);
// macos
expect(configs.macOSConfig, isNull);
});
});
group('#loadConfigFromPubSpec', () {
Expand Down Expand Up @@ -150,6 +163,17 @@ void main() {
'icon_size': 48,
}),
);
// macos
expect(configs.macOSConfig, isNotNull);
expect(configs.macOSConfig!.generate, isNotNull);
expect(configs.macOSConfig!.imagePath, isNotNull);
expect(
configs.macOSConfig!.toJson(),
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
}),
);
});

group('should throw', () {
Expand Down Expand Up @@ -213,6 +237,17 @@ void main() {
'icon_size': 48,
}),
);
// macos
expect(configs.macOSConfig, isNotNull);
expect(configs.macOSConfig!.generate, isNotNull);
expect(configs.macOSConfig!.imagePath, isNotNull);
expect(
configs.macOSConfig!.toJson(),
equals(<String, dynamic>{
'generate': true,
'image_path': 'app_icon.png',
}),
);
});
});
});
Expand Down
158 changes: 158 additions & 0 deletions test/macos/macos_icon_generator_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
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/macos/macos_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 'macos_icon_generator_test.mocks.dart';

@GenerateNiceMocks([
MockSpec<FlutterLauncherIconsConfig>(),
MockSpec<MacOSConfig>(),
MockSpec<FLILogger>(),
])
void main() {
group('MacOSIconGenerator', () {
late IconGeneratorContext context;
late IconGenerator generator;
late FlutterLauncherIconsConfig mockConfig;
late MacOSConfig mockMacOSConfig;
late String prefixPath;
late File testImageFile;
late MockFLILogger mockLogger;
final assetPath = path.join(Directory.current.path, 'test', 'assets');

group('#validateRequirments', () {
setUpAll(() {
testImageFile = File(path.join(assetPath, 'app_icon.png'));
expect(testImageFile.existsSync(), isTrue);
});
setUp(() {
prefixPath = path.join(d.sandbox, 'fli_test');
mockConfig = MockFlutterLauncherIconsConfig();
mockMacOSConfig = MockMacOSConfig();
mockLogger = MockFLILogger();
context = IconGeneratorContext(config: mockConfig, prefixPath: prefixPath, logger: mockLogger);
generator = MacOSIconGenerator(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.macOSConfig).thenReturn(mockMacOSConfig);
when(mockMacOSConfig.generate).thenReturn(true);
when(mockMacOSConfig.imagePath).thenReturn(path.join(prefixPath, 'app_icon.png'));
when(mockConfig.imagePath).thenReturn(path.join(prefixPath, 'app_icon.png'));
});

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

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

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

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

test('should return false when macos 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('macos')]).create();
await expectLater(
d.dir('fli_test', [d.dir('macos')]).validate(),
completes,
);
expect(generator.validateRequirements(), isFalse);
});
});
});

group('MacOSIconGenerator 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('macos/Runner/Assets.xcassets/AppIcon.appiconset', [
d.file('Contents.json', templates.macOSContentsJsonFile),
]),
d.file('flutter_launcher_icons.yaml', templates.fliConfigTemplate),
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 = MacOSIconGenerator(context);
});

test('should generate valid icons & contents.json file', () async {
expect(generator.validateRequirements(), isTrue);
expect(() => generator.createIcons(), isNot(throwsException));

await expectLater(
d.dir('fli_test', [
d.dir('macos/Runner/Assets.xcassets/AppIcon.appiconset', [
d.file('app_icon_1024.png', anything),
d.file('app_icon_16.png', anything),
d.file('app_icon_32.png', anything),
d.file('app_icon_64.png', anything),
d.file('app_icon_128.png', anything),
d.file('app_icon_256.png', anything),
d.file('app_icon_512.png', anything),
]),
]).validate(),
completes,
reason: 'All icon files are not generated',
);

await expectLater(
d.dir('fli_test', [
d.dir('macos/Runner/Assets.xcassets/AppIcon.appiconset', [
d.file('Contents.json', equals(templates.macOSContentsJsonFile)),
]),
]).validate(),
completes,
reason: 'Contents.json file contents are not equal',
);
});
});
}
Loading

0 comments on commit 03f22fc

Please sign in to comment.