Flutter Engine
The Flutter Engine
GoldenTestManager.m
Go to the documentation of this file.
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
6#import <XCTest/XCTest.h>
7
8@interface GoldenTestManager ()
9
10@property(readwrite, strong, nonatomic) GoldenImage* goldenImage;
11
12@end
13
14@implementation GoldenTestManager
15
16NSDictionary* launchArgsMap;
17const double kDefaultRmseThreshold = 0.5;
18
19- (instancetype)initWithLaunchArg:(NSString*)launchArg {
20 self = [super init];
21 if (self) {
22 // The launchArgsMap should match the one in the `PlatformVieGoldenTestManager`.
23 static NSDictionary<NSString*, NSString*>* launchArgsMap;
24 static dispatch_once_t onceToken;
25 dispatch_once(&onceToken, ^{
26 launchArgsMap = @{
27 @"--platform-view" : @"platform_view",
28 @"--platform-view-multiple" : @"platform_view_multiple",
29 @"--platform-view-multiple-background-foreground" :
30 @"platform_view_multiple_background_foreground",
31 @"--platform-view-cliprect" : @"platform_view_cliprect",
32 @"--platform-view-cliprrect" : @"platform_view_cliprrect",
33 @"--platform-view-large-cliprrect" : @"platform_view_large_cliprrect",
34 @"--platform-view-clippath" : @"platform_view_clippath",
35 @"--platform-view-cliprrect-with-transform" : @"platform_view_cliprrect_with_transform",
36 @"--platform-view-large-cliprrect-with-transform" :
37 @"platform_view_large_cliprrect_with_transform",
38 @"--platform-view-cliprect-with-transform" : @"platform_view_cliprect_with_transform",
39 @"--platform-view-clippath-with-transform" : @"platform_view_clippath_with_transform",
40 @"--platform-view-transform" : @"platform_view_transform",
41 @"--platform-view-opacity" : @"platform_view_opacity",
42 @"--platform-view-with-other-backdrop-filter" : @"platform_view_with_other_backdrop_filter",
43 @"--two-platform-views-with-other-backdrop-filter" :
44 @"two_platform_views_with_other_backdrop_filter",
45 @"--platform-view-with-negative-backdrop-filter" :
46 @"platform_view_with_negative_backdrop_filter",
47 @"--platform-view-rotate" : @"platform_view_rotate",
48 @"--non-full-screen-flutter-view-platform-view" :
49 @"non_full_screen_flutter_view_platform_view",
50 @"--bogus-font-text" : @"bogus_font_text",
51 @"--spawn-engine-works" : @"spawn_engine_works",
52 @"--platform-view-cliprect-after-moved" : @"platform_view_cliprect_after_moved",
53 @"--two-platform-view-clip-rect" : @"two_platform_view_clip_rect",
54 @"--two-platform-view-clip-rrect" : @"two_platform_view_clip_rrect",
55 @"--two-platform-view-clip-path" : @"two_platform_view_clip_path",
56 @"--app-extension" : @"app_extension",
57 @"--darwin-system-font" : @"darwin_system_font",
58 };
59 });
60 _identifier = launchArgsMap[launchArg];
61
62 NSString* impeller = @"";
63 NSNumber* enableImpeller = [[NSBundle bundleWithIdentifier:@"dev.flutter.ScenariosUITests"]
64 objectForInfoDictionaryKey:@"FLTEnableImpeller"];
65 if (enableImpeller != nil) {
66 impeller = enableImpeller.boolValue ? @"impeller_" : @"";
67 } else {
68 NSLog(@"FLTEnableImpeller was nil");
69 }
70 NSLog(@"impeller = '%@'", impeller);
71
72 NSString* prefix = [NSString stringWithFormat:@"golden_%@_%@", _identifier, impeller];
73 _goldenImage = [[GoldenImage alloc] initWithGoldenNamePrefix:prefix];
74 _launchArg = launchArg;
75 }
76 return self;
77}
78
79- (void)checkGoldenForTest:(XCTestCase*)test rmesThreshold:(double)rmesThreshold {
80 XCUIScreenshot* screenshot = [[XCUIScreen mainScreen] screenshot];
81 if (!_goldenImage.image) {
82 XCTAttachment* attachment = [XCTAttachment attachmentWithScreenshot:screenshot];
83 attachment.name = [_goldenImage.goldenName stringByAppendingString:@"_new.png"];
84 attachment.lifetime = XCTAttachmentLifetimeKeepAlways;
85 [test addAttachment:attachment];
86 // Instead of XCTFail because that definition changed between Xcode 11 and 12 whereas this impl
87 // is stable.
88 _XCTPrimitiveFail(test,
89 @"This test will fail - no golden named %@ found. "
90 @"Follow the steps in the README to add a new golden.",
91 _goldenImage.goldenName);
92 }
93
94 if (![_goldenImage compareGoldenToImage:screenshot.image rmesThreshold:rmesThreshold]) {
95 XCTAttachment* screenshotAttachment = [XCTAttachment attachmentWithImage:screenshot.image];
96 screenshotAttachment.name = [_goldenImage.goldenName stringByAppendingString:@"_actual.png"];
97 screenshotAttachment.lifetime = XCTAttachmentLifetimeKeepAlways;
98 [test addAttachment:screenshotAttachment];
99
100 _XCTPrimitiveFail(test,
101 @"Goldens do not match. Follow the steps in the "
102 @"README to update golden named %@ if needed.",
103 _goldenImage.goldenName);
104 }
105}
106
107@end
int64_t _identifier
const double kDefaultRmseThreshold
NSDictionary * launchArgsMap
GoldenImage * goldenImage