ModelDownloaderUnitTests.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2020 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. func testExample() {
  42. // This is an example of a functional test case.
  43. // Use XCTAssert and related functions to verify your tests produce the correct
  44. // results.
  45. guard let testApp = FirebaseApp.app() else {
  46. XCTFail("Default app was not configured.")
  47. return
  48. }
  49. let modelDownloader = ModelDownloader.modelDownloader()
  50. let modelDownloaderWithApp = ModelDownloader.modelDownloader(app: testApp)
  51. /// These should point to the same instance.
  52. XCTAssert(modelDownloader === modelDownloaderWithApp)
  53. let conditions = ModelDownloadConditions()
  54. // Download model w/ progress handler
  55. modelDownloader.getModel(
  56. name: "your_model_name",
  57. downloadType: .latestModel,
  58. conditions: conditions,
  59. progressHandler: { progress in
  60. // Handle progress
  61. }
  62. ) { result in
  63. switch result {
  64. case .success:
  65. // Use model with your inference API
  66. // let interpreter = Interpreter(modelPath: customModel.modelPath)
  67. break
  68. case .failure:
  69. // Handle download error
  70. break
  71. }
  72. }
  73. // Access array of downloaded models
  74. modelDownloaderWithApp.listDownloadedModels { result in
  75. switch result {
  76. case .success:
  77. // Pick model(s) for further use
  78. break
  79. case .failure:
  80. // Handle failure
  81. break
  82. }
  83. }
  84. // Delete downloaded model
  85. modelDownloader.deleteDownloadedModel(name: "your_model_name") { result in
  86. switch result {
  87. case .success():
  88. // Apply any other clean up
  89. break
  90. case .failure:
  91. // Handle failure
  92. break
  93. }
  94. }
  95. }
  96. }