-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[interactive_media_ads] Migrate XCTest to Swift Testing #10786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,109 +4,103 @@ | |
|
|
||
| import Flutter | ||
| import GoogleInteractiveMediaAds | ||
| import XCTest | ||
| import Testing | ||
|
|
||
| @testable import interactive_media_ads | ||
|
|
||
| final class AdDisplayContainerTests: XCTestCase { | ||
| func testPigeonDefaultConstructor() { | ||
| @MainActor | ||
| struct AdDisplayContainerTests { | ||
| @Test func pigeonDefaultConstructor() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = try? api.pigeonDelegate.pigeonDefaultConstructor( | ||
| let instance = try api.pigeonDelegate.pigeonDefaultConstructor( | ||
| pigeonApi: api, adContainer: UIView(), companionSlots: [], | ||
| adContainerViewController: UIViewController()) | ||
| XCTAssertNotNil(instance) | ||
| } | ||
|
|
||
| func testAdContainer() { | ||
| @Test func adContainer() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = TestAdDisplayContainer() | ||
| let value = try? api.pigeonDelegate.adContainer(pigeonApi: api, pigeonInstance: instance) | ||
| let instance = TestAdDisplayContainer( | ||
| adContainer: UIView(), viewController: UIViewController()) | ||
| let value = try api.pigeonDelegate.adContainer(pigeonApi: api, pigeonInstance: instance) | ||
|
|
||
| XCTAssertEqual(value, instance.adContainer) | ||
| #expect(value == instance.adContainer) | ||
| } | ||
|
|
||
| func testCompanionSlots() { | ||
| @Test func companionSlots() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = TestAdDisplayContainer() | ||
| let value = try? api.pigeonDelegate.companionSlots(pigeonApi: api, pigeonInstance: instance) | ||
| let instance = TestAdDisplayContainer( | ||
| adContainer: UIView(), | ||
| viewController: UIViewController(), | ||
| companionSlots: [IMACompanionAdSlot(view: UIView())]) | ||
| let value = try api.pigeonDelegate.companionSlots(pigeonApi: api, pigeonInstance: instance) | ||
|
|
||
| XCTAssertEqual(value, instance.companionSlots) | ||
| #expect(value == instance.companionSlots) | ||
| } | ||
|
|
||
| func testSetAdContainerViewController() { | ||
| @Test func setAdContainerViewController() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = TestAdDisplayContainer() | ||
| let instance = TestAdDisplayContainer( | ||
| adContainer: UIView(), viewController: UIViewController()) | ||
| let controller = UIViewController() | ||
| try? api.pigeonDelegate.setAdContainerViewController( | ||
| try api.pigeonDelegate.setAdContainerViewController( | ||
| pigeonApi: api, pigeonInstance: instance, controller: controller) | ||
|
|
||
| XCTAssertEqual(instance.adContainerViewController, controller) | ||
| #expect(instance.adContainerViewController == controller) | ||
| } | ||
|
|
||
| func testGetAdContainerViewController() { | ||
| @Test func getAdContainerViewController() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = TestAdDisplayContainer() | ||
| let instance = TestAdDisplayContainer( | ||
| adContainer: UIView(), viewController: UIViewController()) | ||
| let adContainerViewController = UIViewController() | ||
| instance.adContainerViewController = adContainerViewController | ||
| let value = try? api.pigeonDelegate.getAdContainerViewController( | ||
| let value = try api.pigeonDelegate.getAdContainerViewController( | ||
| pigeonApi: api, pigeonInstance: instance) | ||
|
|
||
| XCTAssertEqual(value, adContainerViewController) | ||
| #expect(value == adContainerViewController) | ||
| } | ||
|
|
||
| func testRegisterFriendlyObstruction() { | ||
| @Test func registerFriendlyObstruction() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = TestAdDisplayContainer() | ||
| let instance = TestAdDisplayContainer( | ||
| adContainer: UIView(), viewController: UIViewController()) | ||
| let friendlyObstruction = IMAFriendlyObstruction( | ||
| view: UIView(), purpose: IMAFriendlyObstructionPurpose.closeAd, detailedReason: "reason") | ||
| try? api.pigeonDelegate.registerFriendlyObstruction( | ||
| try api.pigeonDelegate.registerFriendlyObstruction( | ||
| pigeonApi: api, pigeonInstance: instance, friendlyObstruction: friendlyObstruction) | ||
|
|
||
| XCTAssertEqual(instance.registerFriendlyObstructionArgs, [friendlyObstruction]) | ||
| #expect(instance.registerFriendlyObstructionArgs as! [AnyHashable] == [friendlyObstruction]) | ||
| } | ||
|
|
||
| func testUnregisterAllFriendlyObstructions() { | ||
| @Test func unregisterAllFriendlyObstructions() throws { | ||
| let registrar = TestProxyApiRegistrar() | ||
| let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar) | ||
|
|
||
| let instance = TestAdDisplayContainer() | ||
| try? api.pigeonDelegate.unregisterAllFriendlyObstructions( | ||
| let instance = TestAdDisplayContainer( | ||
| adContainer: UIView(), viewController: UIViewController()) | ||
| try api.pigeonDelegate.unregisterAllFriendlyObstructions( | ||
| pigeonApi: api, pigeonInstance: instance) | ||
|
|
||
| XCTAssertTrue(instance.unregisterAllFriendlyObstructionsCalled) | ||
| #expect(instance.unregisterAllFriendlyObstructionsCalled) | ||
| } | ||
| } | ||
|
|
||
| class TestAdDisplayContainer: IMAAdDisplayContainer { | ||
| private var adContainerTestValue = UIView() | ||
| private var companionSlotsTestValue = [IMACompanionAdSlot(view: UIView())] | ||
| class TestAdDisplayContainer: IMAAdDisplayContainer, @unchecked Sendable { | ||
| var registerFriendlyObstructionArgs: [AnyHashable?]? = nil | ||
| var unregisterAllFriendlyObstructionsCalled = false | ||
|
|
||
| convenience init() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I deleted this convenience method because There's some duplication in the tests now, but the one |
||
| self.init(adContainer: UIView(), viewController: UIViewController()) | ||
| } | ||
|
|
||
| override var adContainer: UIView { | ||
| return adContainerTestValue | ||
| } | ||
|
|
||
| override var companionSlots: [IMACompanionAdSlot] { | ||
| return companionSlotsTestValue | ||
| } | ||
|
|
||
| override func register(_ friendlyObstruction: IMAFriendlyObstruction) { | ||
| registerFriendlyObstructionArgs = [friendlyObstruction] | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we fix the type definition instead to avoid the unsafe cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better for it to crash in a test, or explicitly fail, we'll get a nice stack that says it's expected to be
AnyHashablebut instead it's X. That preferred pattern is why hard-assert patterns liketry #requireare being adopted.I could swap it to a more explicit type check assert:
though in practice I you'll get the same information either way it crashes.