Skip to content

Commit aa46622

Browse files
[ios_platform_images] Add extension tests (flutter#4925)
Adds test coverage of the UIImage class extension that allows native code to load Flutter assets by name.
1 parent adf7adf commit aa46622

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed
Loading
Loading
Loading

packages/ios_platform_images/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
0DE21BF72447752100097E3A /* textfile in Resources */ = {isa = PBXBuildFile; fileRef = 0DE21BF62447752100097E3A /* textfile */; };
1111
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
12+
33B0E32C2AB348FF0006F0C1 /* UIImage+ios_platform_imagesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 33B0E32B2AB348FF0006F0C1 /* UIImage+ios_platform_imagesTests.m */; };
1213
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
1314
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
1415
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
@@ -48,6 +49,7 @@
4849
0EF1CD9A3A3064B5289EF22E /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
4950
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
5051
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
52+
33B0E32B2AB348FF0006F0C1 /* UIImage+ios_platform_imagesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+ios_platform_imagesTests.m"; sourceTree = "<group>"; };
5153
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
5254
4B56C310C5932F84CD6C17AC /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
5355
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -165,6 +167,7 @@
165167
children = (
166168
F76AC1C0266713D00040C8BC /* IosPlatformImagesTests.m */,
167169
F76AC1C2266713D00040C8BC /* Info.plist */,
170+
33B0E32B2AB348FF0006F0C1 /* UIImage+ios_platform_imagesTests.m */,
168171
);
169172
path = RunnerTests;
170173
sourceTree = "<group>";
@@ -299,10 +302,12 @@
299302
);
300303
inputPaths = (
301304
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
305+
"${BUILT_PRODUCTS_DIR}/integration_test/integration_test.framework",
302306
"${BUILT_PRODUCTS_DIR}/ios_platform_images/ios_platform_images.framework",
303307
);
304308
name = "[CP] Embed Pods Frameworks";
305309
outputPaths = (
310+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/integration_test.framework",
306311
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ios_platform_images.framework",
307312
);
308313
runOnlyForDeploymentPostprocessing = 0;
@@ -385,6 +390,7 @@
385390
isa = PBXSourcesBuildPhase;
386391
buildActionMask = 2147483647;
387392
files = (
393+
33B0E32C2AB348FF0006F0C1 /* UIImage+ios_platform_imagesTests.m in Sources */,
388394
F76AC1C1266713D00040C8BC /* IosPlatformImagesTests.m in Sources */,
389395
);
390396
runOnlyForDeploymentPostprocessing = 0;
@@ -482,7 +488,10 @@
482488
"$(PROJECT_DIR)/Flutter",
483489
);
484490
INFOPLIST_FILE = Runner/Info.plist;
485-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
491+
LD_RUNPATH_SEARCH_PATHS = (
492+
"$(inherited)",
493+
"@executable_path/Frameworks",
494+
);
486495
LIBRARY_SEARCH_PATHS = (
487496
"$(inherited)",
488497
"$(PROJECT_DIR)/Flutter",
@@ -614,7 +623,10 @@
614623
"$(PROJECT_DIR)/Flutter",
615624
);
616625
INFOPLIST_FILE = Runner/Info.plist;
617-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
626+
LD_RUNPATH_SEARCH_PATHS = (
627+
"$(inherited)",
628+
"@executable_path/Frameworks",
629+
);
618630
LIBRARY_SEARCH_PATHS = (
619631
"$(inherited)",
620632
"$(PROJECT_DIR)/Flutter",
@@ -641,7 +653,10 @@
641653
"$(PROJECT_DIR)/Flutter",
642654
);
643655
INFOPLIST_FILE = Runner/Info.plist;
644-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
656+
LD_RUNPATH_SEARCH_PATHS = (
657+
"$(inherited)",
658+
"@executable_path/Frameworks",
659+
);
645660
LIBRARY_SEARCH_PATHS = (
646661
"$(inherited)",
647662
"$(PROJECT_DIR)/Flutter",
@@ -662,7 +677,11 @@
662677
CODE_SIGN_STYLE = Automatic;
663678
DEVELOPMENT_TEAM = S8QB4VV633;
664679
INFOPLIST_FILE = RunnerTests/Info.plist;
665-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
680+
LD_RUNPATH_SEARCH_PATHS = (
681+
"$(inherited)",
682+
"@executable_path/Frameworks",
683+
"@loader_path/Frameworks",
684+
);
666685
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.RunnerTests;
667686
PRODUCT_NAME = "$(TARGET_NAME)";
668687
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/Runner";
@@ -677,7 +696,11 @@
677696
CODE_SIGN_STYLE = Automatic;
678697
DEVELOPMENT_TEAM = S8QB4VV633;
679698
INFOPLIST_FILE = RunnerTests/Info.plist;
680-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
699+
LD_RUNPATH_SEARCH_PATHS = (
700+
"$(inherited)",
701+
"@executable_path/Frameworks",
702+
"@loader_path/Frameworks",
703+
);
681704
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.RunnerTests;
682705
PRODUCT_NAME = "$(TARGET_NAME)";
683706
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/Runner";
@@ -692,7 +715,11 @@
692715
CODE_SIGN_STYLE = Automatic;
693716
DEVELOPMENT_TEAM = S8QB4VV633;
694717
INFOPLIST_FILE = RunnerTests/Info.plist;
695-
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
718+
LD_RUNPATH_SEARCH_PATHS = (
719+
"$(inherited)",
720+
"@executable_path/Frameworks",
721+
"@loader_path/Frameworks",
722+
);
696723
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.RunnerTests;
697724
PRODUCT_NAME = "$(TARGET_NAME)";
698725
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Runner.app/Runner";
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
@import ios_platform_images;
6+
@import XCTest;
7+
8+
// The tests test the UIImage extension which is a public API intended for use from native code
9+
// outside of the plugin (see package README). Any change that requires changing existing tests
10+
// in this file (unless it's just to reflect changes to the test assets) is a BREAKING CHANGE for
11+
// the package.
12+
@interface UIImageExtensionTests : XCTestCase
13+
@end
14+
15+
@implementation UIImageExtensionTests
16+
17+
- (void)testMultiResolutionImageUsesBest {
18+
UIImage *image = [UIImage flutterImageWithName:@"assets/multisize.png"];
19+
XCTAssertNotNil(image);
20+
const double height1x = 125; // The height of assets/multisize.png.
21+
const double height2x = 250; // The height of assets/2.0x/multisize.png.
22+
// Loading assets should get the best available asset for the screen scale when resolution-aware
23+
// assets are available (and the example app has 1x and 2x for this asset). See
24+
// https://docs.flutter.dev/ui/assets/assets-and-images#resolution-aware
25+
if (UIScreen.mainScreen.scale > 1.0) {
26+
XCTAssertEqualWithAccuracy(image.size.height, height2x, 0.00001);
27+
} else {
28+
XCTAssertEqualWithAccuracy(image.size.height, height1x, 0.00001);
29+
}
30+
}
31+
32+
- (void)testSingleResolutionFindsImage {
33+
// When there is no resolution-aware asset, the main asset should be used.
34+
UIImage *image = [UIImage flutterImageWithName:@"assets/monosize.png"];
35+
XCTAssertNotNil(image);
36+
}
37+
38+
- (void)testMissingImageReturnsNil {
39+
UIImage *image = [UIImage flutterImageWithName:@"assets/no_such_image.png"];
40+
XCTAssertNil(image);
41+
}
42+
43+
@end

packages/ios_platform_images/example/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ dev_dependencies:
2525
sdk: flutter
2626

2727
flutter:
28+
assets:
29+
- assets/
2830
uses-material-design: true

0 commit comments

Comments
 (0)