FIRCLSContextManagerTests.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import <XCTest/XCTest.h>
  16. #import "Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.h"
  17. #import "Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter.h"
  18. #import "Crashlytics/Crashlytics/Models/Record/FIRCLSReportAdapter_Private.h"
  19. #import "Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.h"
  20. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  21. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h"
  22. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockSettings.h"
  23. #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
  24. NSString *const TestContextReportID = @"TestContextReportID";
  25. NSString *const TestContextSessionID = @"TestContextSessionID";
  26. NSString *const TestContextSessionID2 = @"TestContextSessionID2";
  27. @interface FIRCLSContextManagerTests : XCTestCase
  28. @property(nonatomic, strong) FIRCLSMockFileManager *fileManager;
  29. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  30. @property(nonatomic, strong) FIRCLSContextManager *contextManager;
  31. @property(nonatomic, strong) FIRCLSInternalReport *report;
  32. @property(nonatomic, strong) FIRCLSInstallIdentifierModel *installIDModel;
  33. @end
  34. @implementation FIRCLSContextManagerTests
  35. - (void)setUp {
  36. self.fileManager = [[FIRCLSMockFileManager alloc] init];
  37. [self.fileManager createReportDirectories];
  38. [self.fileManager setupNewPathForExecutionIdentifier:TestContextReportID];
  39. FIRCLSApplicationIdentifierModel *appIDModel = [[FIRCLSApplicationIdentifierModel alloc] init];
  40. _mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  41. appIDModel:appIDModel];
  42. // NSString *name = @"exception_model_report";
  43. NSString *reportPath =
  44. [self.fileManager.activePath stringByAppendingPathComponent:TestContextReportID];
  45. self.report = [[FIRCLSInternalReport alloc] initWithPath:reportPath
  46. executionIdentifier:TestContextReportID];
  47. self.contextManager = [[FIRCLSContextManager alloc] init];
  48. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_token"];
  49. self.installIDModel = [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  50. }
  51. - (void)tearDown {
  52. [[NSFileManager defaultManager] removeItemAtPath:self.fileManager.rootPath error:nil];
  53. [super tearDown];
  54. }
  55. - (void)test_notSettingSessionID_protoHasNilSessionID {
  56. FBLPromiseAwait([self.contextManager setupContextWithReport:self.report
  57. settings:self.mockSettings
  58. fileManager:self.fileManager],
  59. nil);
  60. FIRCLSReportAdapter *adapter = [[FIRCLSReportAdapter alloc] initWithPath:self.report.path
  61. googleAppId:@"TestGoogleAppID"
  62. installIDModel:self.installIDModel
  63. fiid:@"TestFIID"
  64. authToken:@"TestAuthToken"];
  65. XCTAssertEqualObjects(adapter.identity.app_quality_session_id, @"");
  66. }
  67. - (void)test_settingSessionIDMultipleTimes_protoHasLastSessionID {
  68. [self.contextManager setAppQualitySessionId:TestContextSessionID];
  69. FBLPromiseAwait([self.contextManager setupContextWithReport:self.report
  70. settings:self.mockSettings
  71. fileManager:self.fileManager],
  72. nil);
  73. [self.contextManager setAppQualitySessionId:TestContextSessionID2];
  74. FIRCLSReportAdapter *adapter = [[FIRCLSReportAdapter alloc] initWithPath:self.report.path
  75. googleAppId:@"TestGoogleAppID"
  76. installIDModel:self.installIDModel
  77. fiid:@"TestFIID"
  78. authToken:@"TestAuthToken"];
  79. NSLog(@"reportPath: %@", self.report.path);
  80. XCTAssertEqualObjects(adapter.identity.app_quality_session_id, TestContextSessionID2);
  81. }
  82. - (void)test_settingSessionIDOutOfOrder_protoHasLastSessionID {
  83. FBLPromiseAwait([self.contextManager setupContextWithReport:self.report
  84. settings:self.mockSettings
  85. fileManager:self.fileManager],
  86. nil);
  87. [self.contextManager setAppQualitySessionId:TestContextSessionID];
  88. [self.contextManager setAppQualitySessionId:TestContextSessionID2];
  89. FIRCLSReportAdapter *adapter = [[FIRCLSReportAdapter alloc] initWithPath:self.report.path
  90. googleAppId:@"TestGoogleAppID"
  91. installIDModel:self.installIDModel
  92. fiid:@"TestFIID"
  93. authToken:@"TestAuthToken"];
  94. NSLog(@"reportPath: %@", self.report.path);
  95. XCTAssertEqualObjects(adapter.identity.app_quality_session_id, TestContextSessionID2);
  96. }
  97. // This test is for chain on init promise for development platform related setters
  98. - (void)test_promisesChainOnInitPromiseInOrder {
  99. NSMutableArray<NSString *> *result = @[].mutableCopy;
  100. NSMutableArray<NSString *> *expectation = @[].mutableCopy;
  101. for (int j = 0; j < 100; j++) {
  102. [expectation addObject:[NSString stringWithFormat:@"%d", j]];
  103. }
  104. FBLPromise *promise = [self.contextManager setupContextWithReport:self.report
  105. settings:self.mockSettings
  106. fileManager:self.fileManager];
  107. for (int i = 0; i < 100; i++) {
  108. [promise then:^id _Nullable(id _Nullable value) {
  109. [result addObject:[NSString stringWithFormat:@"%d", i]];
  110. if (i == 99) {
  111. XCTAssertTrue([result isEqualToArray:expectation]);
  112. }
  113. return nil;
  114. }];
  115. }
  116. }
  117. @end