ModelDownloaderUnitTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import XCTest
  15. @testable import FirebaseCore
  16. @testable import FirebaseInstallations
  17. @testable import FirebaseMLModelDownloader
  18. /// Mock options to configure default Firebase app.
  19. private enum MockOptions {
  20. static let appID = "1:123:ios:123abc"
  21. static let gcmSenderID = "mock-sender-id"
  22. static let projectID = "mock-project-id"
  23. static let apiKey = "ABcdEf-APIKeyWithValidFormat_0123456789"
  24. }
  25. final class ModelDownloaderUnitTests: XCTestCase {
  26. override class func setUp() {
  27. let options = FirebaseOptions(
  28. googleAppID: MockOptions.appID,
  29. gcmSenderID: MockOptions.gcmSenderID
  30. )
  31. options.apiKey = MockOptions.apiKey
  32. options.projectID = MockOptions.projectID
  33. FirebaseApp.configure(options: options)
  34. }
  35. /// Test to download model info.
  36. // TODO: Add unit test with mocks.
  37. func testDownloadModelInfo() {}
  38. /// Test to download model file.
  39. // TODO: Add unit test with mocks.
  40. func testStartModelDownload() {}
  41. /// Test model file deletion.
  42. // TODO: Add unit test.
  43. func testDeleteModel() {}
  44. /// Test listing models in model directory.
  45. // TODO: Add unit test.
  46. func testListModels() {
  47. let modelDownloader = ModelDownloader.modelDownloader()
  48. modelDownloader.listDownloadedModels { result in
  49. switch result {
  50. case .success: break
  51. case .failure: break
  52. }
  53. }
  54. }
  55. func testGetModel() {
  56. // This is an example of a functional test case.
  57. // Use XCTAssert and related functions to verify your tests produce the correct
  58. // results.
  59. guard let testApp = FirebaseApp.app() else {
  60. XCTFail("Default app was not configured.")
  61. return
  62. }
  63. let modelDownloader = ModelDownloader.modelDownloader()
  64. let modelDownloaderWithApp = ModelDownloader.modelDownloader(app: testApp)
  65. /// These should point to the same instance.
  66. XCTAssert(modelDownloader === modelDownloaderWithApp)
  67. let conditions = ModelDownloadConditions()
  68. // Download model w/ progress handler
  69. modelDownloader.getModel(
  70. name: "your_model_name",
  71. downloadType: .latestModel,
  72. conditions: conditions,
  73. progressHandler: { progress in
  74. // Handle progress
  75. }
  76. ) { result in
  77. switch result {
  78. case .success:
  79. // Use model with your inference API
  80. // let interpreter = Interpreter(modelPath: customModel.modelPath)
  81. break
  82. case .failure:
  83. // Handle download error
  84. break
  85. }
  86. }
  87. // Access array of downloaded models
  88. modelDownloaderWithApp.listDownloadedModels { result in
  89. switch result {
  90. case .success:
  91. // Pick model(s) for further use
  92. break
  93. case .failure:
  94. // Handle failure
  95. break
  96. }
  97. }
  98. // Delete downloaded model
  99. modelDownloader.deleteDownloadedModel(name: "your_model_name") { result in
  100. switch result {
  101. case .success():
  102. // Apply any other clean up
  103. break
  104. case .failure:
  105. // Handle failure
  106. break
  107. }
  108. }
  109. }
  110. /// Compare proto serialization methods.
  111. func testTelemetryEncoding() {
  112. let fakeModel = CustomModel(
  113. name: "fakeModelName",
  114. size: 10,
  115. path: "fakeModelPath",
  116. hash: "fakeModelHash"
  117. )
  118. var modelOptions = ModelOptions()
  119. modelOptions.setModelOptions(model: fakeModel)
  120. guard let binaryData = try? modelOptions.serializedData(),
  121. let jsonData = try? modelOptions.jsonUTF8Data(),
  122. let binaryEvent = try? ModelOptions(serializedData: binaryData),
  123. let jsonEvent = try? ModelOptions(jsonUTF8Data: jsonData) else {
  124. XCTFail("Encoding error.")
  125. return
  126. }
  127. XCTAssertNotNil(binaryData)
  128. XCTAssertNotNil(jsonData)
  129. XCTAssertLessThan(binaryData.count, jsonData.count)
  130. XCTAssertEqual(binaryEvent, jsonEvent)
  131. }
  132. }