Skip to content

Commit 5ed49d8

Browse files
ref: Inject dependencies for ViewHierarchyProvider (#5460)
Instead of retrieving the dependencies in the SentryViewHierarchyProvider via the SentryDependencyContainer, clearly specify them in the initializer, which is better for testability and readability. Another benefit is that the SentryViewHierarchyProviderTests don't need to call SentryDependencyContainer.reset.
1 parent 476d5be commit 5ed49d8

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ - (SentryViewHierarchyProvider *)viewHierarchyProvider SENTRY_THREAD_SANITIZER_D
297297
{
298298
# if SENTRY_HAS_UIKIT
299299

300-
SENTRY_LAZY_INIT(_viewHierarchyProvider, [[SentryViewHierarchyProvider alloc] init]);
300+
SENTRY_LAZY_INIT(_viewHierarchyProvider,
301+
[[SentryViewHierarchyProvider alloc] initWithDispatchQueueWrapper:self.dispatchQueueWrapper
302+
sentryUIApplication:self.application]);
301303
# else
302304
SENTRY_LOG_DEBUG(
303305
@"SentryDependencyContainer.viewHierarchyProvider only works with UIKit "

Sources/Sentry/SentryViewHierarchyProvider.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
# import "SentryCrashFileUtils.h"
66
# import "SentryCrashJSONCodec.h"
7-
# import "SentryDependencyContainer.h"
87
# import "SentryDispatchQueueWrapper.h"
98
# import "SentryLog.h"
109
# import "SentrySwift.h"
@@ -27,19 +26,29 @@
2726
return SentryCrashJSON_OK;
2827
}
2928

29+
@interface SentryViewHierarchyProvider ()
30+
31+
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueueWrapper;
32+
@property (nonatomic, strong) SentryUIApplication *sentryUIApplication;
33+
34+
@end
35+
3036
@implementation SentryViewHierarchyProvider
3137

32-
- (instancetype)init
38+
- (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
39+
sentryUIApplication:(SentryUIApplication *)sentryUIApplication
3340
{
3441
if (self = [super init]) {
3542
self.reportAccessibilityIdentifier = YES;
43+
self.dispatchQueueWrapper = dispatchQueueWrapper;
44+
self.sentryUIApplication = sentryUIApplication;
3645
}
3746
return self;
3847
}
3948

4049
- (BOOL)saveViewHierarchy:(NSString *)filePath
4150
{
42-
NSArray<UIWindow *> *windows = [SentryDependencyContainer.sharedInstance.application windows];
51+
NSArray<UIWindow *> *windows = [self.sentryUIApplication windows];
4352

4453
const char *path = [filePath UTF8String];
4554
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
@@ -62,8 +71,7 @@ - (NSData *)appViewHierarchyFromMainThread
6271

6372
SENTRY_LOG_INFO(@"Starting to fetch the view hierarchy from the main thread.");
6473

65-
[[SentryDependencyContainer sharedInstance].dispatchQueueWrapper
66-
dispatchSyncOnMainQueue:fetchViewHierarchy];
74+
[self.dispatchQueueWrapper dispatchSyncOnMainQueue:fetchViewHierarchy];
6775

6876
SENTRY_LOG_INFO(@"Finished fetching the view hierarchy from the main thread.");
6977

@@ -73,7 +81,7 @@ - (NSData *)appViewHierarchyFromMainThread
7381
- (NSData *)appViewHierarchy
7482
{
7583
NSMutableData *result = [[NSMutableData alloc] init];
76-
NSArray<UIWindow *> *windows = [SentryDependencyContainer.sharedInstance.application windows];
84+
NSArray<UIWindow *> *windows = [self.sentryUIApplication windows];
7785

7886
if (![self processViewHierarchy:windows
7987
addFunction:writeJSONDataToMemory

Sources/Sentry/include/SentryViewHierarchyProvider.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
NS_ASSUME_NONNULL_BEGIN
66

7+
@class SentryDispatchQueueWrapper;
8+
@class SentryUIApplication;
9+
710
@interface SentryViewHierarchyProvider : NSObject
811

12+
- (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
13+
sentryUIApplication:(SentryUIApplication *)sentryUIApplication;
14+
915
/**
1016
* Whether we should add `accessibilityIdentifier` to the view hierarchy.
1117
*/

Tests/SentryTests/SentryViewHierarchyProviderTests.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SentryViewHierarchyProviderTests: XCTestCase {
77
let uiApplication = TestSentryUIApplication()
88

99
var sut: SentryViewHierarchyProvider {
10-
return SentryViewHierarchyProvider()
10+
return SentryViewHierarchyProvider(dispatchQueueWrapper: SentryDispatchQueueWrapper(), sentryUIApplication: uiApplication)
1111
}
1212
}
1313

@@ -17,7 +17,6 @@ class SentryViewHierarchyProviderTests: XCTestCase {
1717
super.setUp()
1818

1919
fixture = Fixture()
20-
SentryDependencyContainer.sharedInstance().application = fixture.uiApplication
2120
}
2221

2322
override func setUpWithError() throws {
@@ -35,11 +34,6 @@ class SentryViewHierarchyProviderTests: XCTestCase {
3534
}
3635
}
3736

38-
override func tearDown() {
39-
super.tearDown()
40-
SentryDependencyContainer.reset()
41-
}
42-
4337
func test_Multiple_Window() {
4438
let firstWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
4539
let secondWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
@@ -175,7 +169,7 @@ class SentryViewHierarchyProviderTests: XCTestCase {
175169
}
176170

177171
func test_invalidSerialization() {
178-
let sut = TestSentryViewHierarchyProvider()
172+
let sut = TestSentryViewHierarchyProvider(dispatchQueueWrapper: SentryDispatchQueueWrapper(), sentryUIApplication: fixture.uiApplication)
179173
sut.viewHierarchyResult = -1
180174
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
181175
window.accessibilityIdentifier = "WindowId"
@@ -186,7 +180,7 @@ class SentryViewHierarchyProviderTests: XCTestCase {
186180
}
187181

188182
func test_appViewHierarchyFromBackgroundTest() {
189-
let sut = TestSentryViewHierarchyProvider()
183+
let sut = TestSentryViewHierarchyProvider(dispatchQueueWrapper: SentryDispatchQueueWrapper(), sentryUIApplication: fixture.uiApplication)
190184
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
191185
fixture.uiApplication.windows = [window]
192186

@@ -205,7 +199,7 @@ class SentryViewHierarchyProviderTests: XCTestCase {
205199
}
206200

207201
func test_appViewHierarchy_usesMainThread() {
208-
let sut = TestSentryViewHierarchyProvider()
202+
let sut = TestSentryViewHierarchyProvider(dispatchQueueWrapper: SentryDispatchQueueWrapper(), sentryUIApplication: fixture.uiApplication)
209203
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
210204
fixture.uiApplication.windows = [window]
211205

0 commit comments

Comments
 (0)