CallsignalingProtocolTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import XCTest
  21. @testable import ThreemaFramework
  22. class CallsignalingProtocolTests: XCTestCase {
  23. /**
  24. * Helper class.
  25. */
  26. struct Pair {
  27. let a: CallsignalingProtocol.ThreemaVideoCallQualityProfiles
  28. let b: CallsignalingProtocol.ThreemaVideoCallQualityProfiles
  29. }
  30. func testSerializeProfile() throws {
  31. // Create a high quality profile
  32. let highProfile = CallsignalingProtocol.ThreemaVideoCallQualityProfiles.high
  33. // Serialize to protobuf
  34. guard let bytes: Data = CallsignalingProtocol.encodeVideoQuality(highProfile) else {
  35. XCTFail("quality profile bytes are empty")
  36. return
  37. }
  38. // Deserialize
  39. guard let envelop: Callsignaling_Envelope = try? Callsignaling_Envelope(serializedData: bytes) else {
  40. XCTFail("envelope is nil")
  41. return
  42. }
  43. var hasQualityProfile = false
  44. switch envelop.content {
  45. case .videoQualityProfile(envelop.videoQualityProfile):
  46. hasQualityProfile = true
  47. break
  48. default: break
  49. }
  50. XCTAssertTrue(hasQualityProfile)
  51. let profile = envelop.videoQualityProfile
  52. XCTAssertTrue(profile.hasMaxResolution)
  53. XCTAssertEqual(Callsignaling_VideoQualityProfile.QualityProfile.high, profile.profile)
  54. XCTAssertEqual(2000, profile.maxBitrateKbps)
  55. XCTAssertEqual(25, profile.maxFps)
  56. XCTAssertEqual(1280, profile.maxResolution.width)
  57. XCTAssertEqual(720, profile.maxResolution.height)
  58. }
  59. func testFindCommonProfileLow() throws {
  60. let pairs = [Pair.init(a: .low, b: .low),
  61. Pair.init(a: .low, b: .high),
  62. Pair.init(a: .low, b: .max),
  63. Pair.init(a: .high, b: .low),
  64. Pair.init(a: .max, b: .low)]
  65. for pair in pairs {
  66. let common = CallsignalingProtocol.findCommonProfile(remoteProfile: pair.a.qualityProfile(), networkIsRelayed: false, pair.b.qualityProfile())
  67. XCTAssertEqual(.low, common.profile)
  68. }
  69. }
  70. func testFindCommonProfileHigh() throws {
  71. let pairs = [Pair.init(a: .high, b: .high),
  72. Pair.init(a: .high, b: .max),
  73. Pair.init(a: .max, b: .high)]
  74. for pair in pairs {
  75. let common = CallsignalingProtocol.findCommonProfile(remoteProfile: pair.a.qualityProfile(), networkIsRelayed: false, pair.b.qualityProfile())
  76. XCTAssertEqual(.high, common.profile)
  77. }
  78. }
  79. func testFindCommonProfileMax() throws {
  80. let a: CallsignalingProtocol.ThreemaVideoCallQualityProfiles = .max
  81. let b: CallsignalingProtocol.ThreemaVideoCallQualityProfiles = .max
  82. let commonNonRelayed = CallsignalingProtocol.findCommonProfile(remoteProfile: a.qualityProfile(), networkIsRelayed: false, b.qualityProfile())
  83. let commonRelayed = CallsignalingProtocol.findCommonProfile(remoteProfile: a.qualityProfile(), networkIsRelayed: true, b.qualityProfile())
  84. XCTAssertEqual(.max, commonNonRelayed.profile)
  85. XCTAssertEqual(.high, commonRelayed.profile)
  86. }
  87. func testFindCommonProfileNil() throws {
  88. let params: [CallsignalingProtocol.ThreemaVideoCallQualityProfiles] = [.low, .high, .max]
  89. for param in params {
  90. let commonNonRelayed = CallsignalingProtocol.findCommonProfile(remoteProfile: nil, networkIsRelayed: false, param.qualityProfile())
  91. let commonRelayed = CallsignalingProtocol.findCommonProfile(remoteProfile: nil, networkIsRelayed: true, param.qualityProfile())
  92. XCTAssertEqual(param, commonNonRelayed.profile)
  93. XCTAssertEqual(param, commonRelayed.profile)
  94. }
  95. }
  96. func testFindCommonProfileRawValues() throws {
  97. let a = CallsignalingProtocol.ThreemaVideoCallQualityProfile.init(bitrate: 600, maxResolution: CGSize(width: 2000, height: 700), maxFps: 23, profile: CallsignalingProtocol.ThreemaVideoCallQualityProfiles.init(rawValue: 1234) ?? nil)
  98. let b: CallsignalingProtocol.ThreemaVideoCallQualityProfiles = .high
  99. XCTAssertNotNil(a)
  100. let common = CallsignalingProtocol.findCommonProfile(remoteProfile: a, networkIsRelayed: false, b.qualityProfile())
  101. XCTAssertNil(common.profile)
  102. XCTAssertEqual(600, common.bitrate)
  103. XCTAssertEqual(23, common.maxFps)
  104. XCTAssertEqual(1280, common.maxResolution.width)
  105. XCTAssertEqual(700, common.maxResolution.height)
  106. }
  107. func testFindCommonProfileRawValuesWithClamping() throws {
  108. let a = CallsignalingProtocol.ThreemaVideoCallQualityProfile.init(bitrate: 1, maxResolution: CGSize(width: 1, height: 1), maxFps: 1, profile: CallsignalingProtocol.ThreemaVideoCallQualityProfiles.init(rawValue: 1234) ?? nil)
  109. let b: CallsignalingProtocol.ThreemaVideoCallQualityProfiles = .high
  110. XCTAssertNotNil(a)
  111. let common = CallsignalingProtocol.findCommonProfile(remoteProfile: a, networkIsRelayed: false, b.qualityProfile())
  112. XCTAssertNil(common.profile)
  113. XCTAssertEqual(CallsignalingProtocol.minBitrate, common.bitrate)
  114. XCTAssertEqual(CallsignalingProtocol.minFps, common.maxFps)
  115. XCTAssertEqual(CallsignalingProtocol.minResolutionWidth, common.maxResolution.width)
  116. XCTAssertEqual(CallsignalingProtocol.minResolutionHeight, common.maxResolution.height)
  117. }
  118. }