Skip to content

Commit

Permalink
[interactive_media_ads] Adds initial iOS implementation (flutter#7063)
Browse files Browse the repository at this point in the history
iOS implementation for flutter#134228
  • Loading branch information
bparrishMines authored Jul 31, 2024
1 parent b20d2c5 commit 46a712f
Show file tree
Hide file tree
Showing 69 changed files with 10,301 additions and 164 deletions.
5 changes: 5 additions & 0 deletions packages/interactive_media_ads/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.1.1

* Adds iOS implementation.
* Adds support for setting the layout direction of the `AdDisplayContainer`.

## 0.1.0+2

* Bumps androidx.annotation:annotation from 1.7.1 to 1.8.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class AdsRequestProxyApi(override val pigeonRegistrar: ProxyApiRegistrar) :
*
* This must match the version in pubspec.yaml.
*/
const val pluginVersion = "0.1.0+2"
const val pluginVersion = "0.1.1"
}

override fun setAdTagUrl(pigeon_instance: AdsRequest, adTagUrl: String) {
// Ensure adTag can append a custom parameter.
require(adTagUrl.contains("?"))
require(!adTagUrl.contains("#"))

pigeon_instance.adTagUrl = "$adTagUrl&request_agent=Flutter-IMA-$pluginVersion"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class AdsRequestProxyApiTest {
val api = TestProxyApiRegistrar().getPigeonApiAdsRequest()

val instance = mock<AdsRequest>()
api.setAdTagUrl(instance, "adTag")
api.setAdTagUrl(instance, "adTag?")

verify(instance).adTagUrl =
"adTag&request_agent=Flutter-IMA-${AdsRequestProxyApi.pluginVersion}"
"adTag?&request_agent=Flutter-IMA-${AdsRequestProxyApi.pluginVersion}"
}

@Test
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Flutter
import UIKit

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter
import XCTest

@testable import interactive_media_ads

final class AdDisplayContainerTests: XCTestCase {
func testPigeonDefaultConstructor() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdDisplayContainer(registrar)

let instance = try? api.pigeonDelegate.pigeonDefaultConstructor(
pigeonApi: api, adContainer: UIView(), adContainerViewController: UIViewController())

XCTAssertNotNil(instance)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter
import GoogleInteractiveMediaAds
import XCTest

@testable import interactive_media_ads

final class AdErrorTests: XCTestCase {
func testType() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdError(registrar)

let instance = TestAdError.customInit()

let value = try? api.pigeonDelegate.type(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, .loadingFailed)
}

func testCode() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdError(registrar)

let instance = TestAdError.customInit()

let value = try? api.pigeonDelegate.code(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, .apiError)
}

func testMessage() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdError(registrar)

let instance = TestAdError.customInit()

let value = try? api.pigeonDelegate.message(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, "message")
}
}

class TestAdError: IMAAdError {
// Workaround to subclass an Objective-C class that has an `init` constructor with NS_UNAVAILABLE
static func customInit() -> IMAAdError {
let instance =
TestAdError.perform(NSSelectorFromString("new")).takeRetainedValue() as! TestAdError
return instance
}

override var type: IMAErrorType {
return .adLoadingFailed
}

override var code: IMAErrorCode {
return .API_ERROR
}

override var message: String? {
return "message"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter
import GoogleInteractiveMediaAds
import XCTest

@testable import interactive_media_ads

final class AdEventTests: XCTestCase {
func testType() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdEvent(registrar)

let instance = TestAdEvent.customInit()

let value = try? api.pigeonDelegate.type(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, .adBreakEnded)
}

func testMessage() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdEvent(registrar)

let instance = TestAdEvent.customInit()

let value = try? api.pigeonDelegate.typeString(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, "message")
}
}

class TestAdEvent: IMAAdEvent {
// Workaround to subclass an Objective-C class that has an `init` constructor with NS_UNAVAILABLE
static func customInit() -> TestAdEvent {
let instance =
TestAdEvent.perform(NSSelectorFromString("new")).takeRetainedValue() as! TestAdEvent
return instance
}

override var type: IMAAdEventType {
return .AD_BREAK_ENDED
}

override var typeString: String {
return "message"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter
import GoogleInteractiveMediaAds
import XCTest

@testable import interactive_media_ads

final class AdLoadingErrorTests: XCTestCase {
func testAdError() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdLoadingErrorData(registrar)

let instance = TestAdLoadingErrorData.customInit()

let value = try? api.pigeonDelegate.adError(pigeonApi: api, pigeonInstance: instance)

XCTAssertTrue(value is TestAdError)
}
}

class TestAdLoadingErrorData: IMAAdLoadingErrorData {
// Workaround to subclass an Objective-C class that has an `init` constructor with NS_UNAVAILABLE
static func customInit() -> TestAdLoadingErrorData {
let instance =
TestAdLoadingErrorData.perform(NSSelectorFromString("new")).takeRetainedValue()
as! TestAdLoadingErrorData
return instance
}

override var adError: IMAAdError {
return TestAdError.customInit()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter
import GoogleInteractiveMediaAds
import XCTest

@testable import interactive_media_ads

final class AdsLoadedDataTests: XCTestCase {
func testAdsManager() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdsLoadedData(registrar)

let instance = TestAdsLoadedData.customInit()

let value = try? api.pigeonDelegate.adsManager(pigeonApi: api, pigeonInstance: instance)

XCTAssertTrue(value is TestAdsManager)
}
}

class TestAdsLoadedData: IMAAdsLoadedData {
// Workaround to subclass an Objective-C class that has an `init` constructor with NS_UNAVAILABLE
static func customInit() -> TestAdsLoadedData {
let instance =
TestAdsLoadedData.perform(NSSelectorFromString("new")).takeRetainedValue()
as! TestAdsLoadedData
return instance
}

override var adsManager: IMAAdsManager? {
return TestAdsManager.customInit()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Flutter
import GoogleInteractiveMediaAds
import XCTest

@testable import interactive_media_ads

final class AdsLoaderDelegateTests: XCTestCase {
func testPigeonDefaultConstructor() {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiIMAAdsLoaderDelegate(registrar)

let instance = try? api.pigeonDelegate.pigeonDefaultConstructor(pigeonApi: api)

XCTAssertTrue(instance is AdsLoaderDelegateImpl)
}

func testAdLoaderLoadedWith() {
let api = TestAdsLoaderDelegateApi()
let instance = AdsLoaderDelegateImpl(api: api)

let adsLoader = IMAAdsLoader(settings: nil)
let data = TestAdsLoadedData()
instance.adsLoader(adsLoader, adsLoadedWith: data)

XCTAssertEqual(api.adLoaderLoadedWithArgs, [adsLoader, data])
}

func testAdsLoaderFailedWithErrorData() {
let api = TestAdsLoaderDelegateApi()
let instance = AdsLoaderDelegateImpl(api: api)

let adsLoader = IMAAdsLoader(settings: nil)
let error = TestAdLoadingErrorData.customInit()
instance.adsLoader(adsLoader, failedWith: error)

XCTAssertEqual(api.adsLoaderFailedWithErrorDataArgs, [adsLoader, error])
}
}

class TestAdsLoaderDelegateApi: PigeonApiProtocolIMAAdsLoaderDelegate {
var adLoaderLoadedWithArgs: [AnyHashable?]? = nil
var adsLoaderFailedWithErrorDataArgs: [AnyHashable?]? = nil

func adLoaderLoadedWith(
pigeonInstance pigeonInstanceArg: IMAAdsLoaderDelegate, loader loaderArg: IMAAdsLoader,
adsLoadedData adsLoadedDataArg: IMAAdsLoadedData,
completion: @escaping (Result<Void, PigeonError>) -> Void
) {
adLoaderLoadedWithArgs = [loaderArg, adsLoadedDataArg]
}

func adsLoaderFailedWithErrorData(
pigeonInstance pigeonInstanceArg: IMAAdsLoaderDelegate, loader loaderArg: IMAAdsLoader,
adErrorData adErrorDataArg: IMAAdLoadingErrorData,
completion: @escaping (Result<Void, PigeonError>) -> Void
) {
adsLoaderFailedWithErrorDataArgs = [loaderArg, adErrorDataArg]
}
}
Loading

0 comments on commit 46a712f

Please sign in to comment.