Skip to content

Commit

Permalink
RUMM-1615 Add RUMViewModifier unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Nov 3, 2021
1 parent 566a95b commit ea81936
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@
D24985A727292FCC00B4F72D /* SwiftUIRUMViewsHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D24985A627292FCC00B4F72D /* SwiftUIRUMViewsHandlerTests.swift */; };
D24C27EA270C8BEE005DE596 /* DataCompression.swift in Sources */ = {isa = PBXBuildFile; fileRef = D24C27E9270C8BEE005DE596 /* DataCompression.swift */; };
D2791EF927170A760046E07A /* RUMSwiftUIScenarioTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2791EF827170A760046E07A /* RUMSwiftUIScenarioTests.swift */; };
D2EFF3D52732D48800D09F33 /* RUMViewModifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2EFF3D42732D48800D09F33 /* RUMViewModifierTests.swift */; };
D2F1B81126D795F3009F3293 /* DDNoopRUMMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F1B81026D795F3009F3293 /* DDNoopRUMMonitor.swift */; };
D2F1B81326D8DA68009F3293 /* DDNoopRUMMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F1B81226D8DA68009F3293 /* DDNoopRUMMonitorTests.swift */; };
D2F1B81526D8E5FF009F3293 /* DDNoopTracerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F1B81426D8E5FF009F3293 /* DDNoopTracerTests.swift */; };
Expand Down Expand Up @@ -1172,6 +1173,7 @@
D24985A627292FCC00B4F72D /* SwiftUIRUMViewsHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIRUMViewsHandlerTests.swift; sourceTree = "<group>"; };
D24C27E9270C8BEE005DE596 /* DataCompression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataCompression.swift; sourceTree = "<group>"; };
D2791EF827170A760046E07A /* RUMSwiftUIScenarioTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMSwiftUIScenarioTests.swift; sourceTree = "<group>"; };
D2EFF3D42732D48800D09F33 /* RUMViewModifierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMViewModifierTests.swift; sourceTree = "<group>"; };
D2F1B81026D795F3009F3293 /* DDNoopRUMMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNoopRUMMonitor.swift; sourceTree = "<group>"; };
D2F1B81226D8DA68009F3293 /* DDNoopRUMMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNoopRUMMonitorTests.swift; sourceTree = "<group>"; };
D2F1B81426D8E5FF009F3293 /* DDNoopTracerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNoopTracerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3295,6 +3297,7 @@
isa = PBXGroup;
children = (
D24985A627292FCC00B4F72D /* SwiftUIRUMViewsHandlerTests.swift */,
D2EFF3D42732D48800D09F33 /* RUMViewModifierTests.swift */,
);
path = SwiftUI;
sourceTree = "<group>";
Expand Down Expand Up @@ -4061,6 +4064,7 @@
61D980BC24E293F600E03345 /* RUMIntegrationsTests.swift in Sources */,
61363D9F24D99BAA0084CD6F /* DDErrorTests.swift in Sources */,
61411B1024EC15AC0012EAB2 /* Casting+RUM.swift in Sources */,
D2EFF3D52732D48800D09F33 /* RUMViewModifierTests.swift in Sources */,
61133C622423990D00786299 /* InternalLoggersTests.swift in Sources */,
61FF283024BC5E2D000B3D9B /* RUMEventFileOutputTests.swift in Sources */,
9E986C302677B91400D62490 /* VitalRefreshRateReaderTests.swift in Sources */,
Expand Down
18 changes: 16 additions & 2 deletions Sources/Datadog/RUM/Instrumentation/RUMInstrumentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal final class RUMInstrumentation: RUMCommandPublisher {

// MARK: - Initialization

init(
convenience init(
configuration: FeaturesConfiguration.RUM.Instrumentation,
dateProvider: DateProvider
) {
Expand All @@ -83,10 +83,24 @@ internal final class RUMInstrumentation: RUMCommandPublisher {
longTasks = LongTaskObserver(threshold: threshold, dateProvider: dateProvider)
}

self.init(
viewsAutoInstrumentation: viewsAutoInstrumentation,
swiftUIViewInstrumentation: SwiftUIRUMViewsHandler(dateProvider: dateProvider),
userActionsAutoInstrumentation: userActionsAutoInstrumentation,
longTasks: longTasks
)
}

init(
viewsAutoInstrumentation: ViewsAutoInstrumentation?,
swiftUIViewInstrumentation: SwiftUIViewHandler,
userActionsAutoInstrumentation: UserActionsAutoInstrumentation?,
longTasks: LongTaskObserver?
) {
self.viewsAutoInstrumentation = viewsAutoInstrumentation
self.userActionsAutoInstrumentation = userActionsAutoInstrumentation
self.longTasks = longTasks
self.swiftUIViewInstrumentation = SwiftUIRUMViewsHandler(dateProvider: dateProvider)
self.swiftUIViewInstrumentation = swiftUIViewInstrumentation
}

func enable() {
Expand Down
18 changes: 18 additions & 0 deletions Tests/DatadogTests/Datadog/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,24 @@ class UIKitRUMUserActionsHandlerMock: UIEventHandler {
}
}

class SwiftUIViewHandlerMock: SwiftUIViewHandler {
var onSubscribe: ((RUMCommandSubscriber) -> Void)?
var notifyOnAppear: ((String, String, String, [AttributeKey: AttributeValue]) -> Void)?
var notifyOnDisappear: ((String) -> Void)?

func publish(to subscriber: RUMCommandSubscriber) {
onSubscribe?(subscriber)
}

func onAppear(identity: String, name: String, path: String, attributes: [AttributeKey: AttributeValue]) {
notifyOnAppear?(identity, name, path, attributes)
}

func onDisappear(identity: String) {
notifyOnDisappear?(identity)
}
}

class SamplingBasedVitalReaderMock: SamplingBasedVitalReader {
var vitalData: Double?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/

import XCTest
import SwiftUI

@testable import Datadog

class RUMViewModifierTests: XCTestCase {
let swiftUIHandler = SwiftUIViewHandlerMock()
let subscriber = RUMCommandSubscriberMock()

@available(iOS 13, *)
struct TestView: View {
let name: String
let attributes: [AttributeKey: AttributeValue]
var body: some View {
EmptyView()
.trackRUMView(
name: name,
attributes: attributes
)
}
}

override func setUp() {
super.setUp()

RUMInstrumentation.instance = RUMInstrumentation(
viewsAutoInstrumentation: nil,
swiftUIViewInstrumentation: swiftUIHandler,
userActionsAutoInstrumentation: nil,
longTasks: nil
)

RUMInstrumentation.instance?.publish(to: subscriber)
}

override func tearDown() {
RUMInstrumentation.instance?.deinitialize()
}

func testGivenASwiftUIView_WhenItAppearsAndDisappears_ItNotifiesTheRUMInstrumentation() throws {
guard #available(iOS 13, *) else {
return
}

let expectOnAppear = expectation(description: "SwiftUI.View.onAppear")
let expectOnDisappear = expectation(description: "SwiftUI.View.onDisappear")

// Given
let viewName: String = .mockRandom()
let viewAttributes = mockRandomAttributes()
let animated = Bool.random()

var host: UIHostingController? = UIHostingController(
rootView: TestView(
name: viewName,
attributes: viewAttributes
)
)

var viewIdentity: String?

swiftUIHandler.notifyOnAppear = { identity, name, path, attributes in
viewIdentity = identity
XCTAssertTrue(identity.matches(regex: .uuidRegex))
XCTAssertEqual(name, viewName)
XCTAssertTrue(path.matches(regex: "\(viewName)\\/[0-9]*"))
self.AssertDictionariesEqual(attributes, viewAttributes)
expectOnAppear.fulfill()
}

swiftUIHandler.notifyOnDisappear = { identity in
XCTAssertEqual(viewIdentity, identity)
expectOnDisappear.fulfill()
}

// When
// Trigger the `.onAppear`
host?.viewWillAppear(animated)
host?.viewDidAppear(animated)

// Then
wait(for: [expectOnAppear], timeout: 5)

// When
host = nil // Trigger the `.onDisappear`
wait(for: [expectOnDisappear], timeout: 5)
}
}

0 comments on commit ea81936

Please sign in to comment.