GDTCORLifecycleTest.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2018 Google
  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. */
  16. #import <XCTest/XCTest.h>
  17. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREventDataObject.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTransport.h"
  20. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  21. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
  22. #import "GoogleDataTransport/GDTCORTests/Lifecycle/Helpers/GDTCORLifecycleTestUploader.h"
  23. #import "GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORFlatFileStorage+Testing.h"
  24. #import "GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h"
  25. #import "GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORUploadCoordinator+Testing.h"
  26. /** Waits for the result of waitBlock to be YES, or times out and fails.
  27. *
  28. * @param waitBlock The block to periodically execute.
  29. * @param timeInterval The timeout.
  30. */
  31. #define GDTCORWaitForBlock(waitBlock, timeInterval) \
  32. { \
  33. NSPredicate *pred = \
  34. [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, \
  35. NSDictionary<NSString *, id> *_Nullable bindings) { \
  36. return waitBlock(); \
  37. }]; \
  38. XCTestExpectation *expectation = [self expectationForPredicate:pred \
  39. evaluatedWithObject:nil \
  40. handler:nil]; \
  41. [self waitForExpectations:@[ expectation ] timeout:timeInterval]; \
  42. }
  43. /** A test-only event data object used in this integration test. */
  44. @interface GDTCORLifecycleTestEvent : NSObject <GDTCOREventDataObject>
  45. @end
  46. @implementation GDTCORLifecycleTestEvent
  47. - (NSData *)transportBytes {
  48. // In real usage, protobuf's -data method or a custom implementation using nanopb are used.
  49. return [[NSString stringWithFormat:@"%@", [NSDate date]] dataUsingEncoding:NSUTF8StringEncoding];
  50. }
  51. @end
  52. @interface GDTCORLifecycleTest : XCTestCase
  53. /** The test uploader. */
  54. @property(nonatomic) GDTCORLifecycleTestUploader *uploader;
  55. @end
  56. @implementation GDTCORLifecycleTest
  57. - (void)setUp {
  58. [super setUp];
  59. [[GDTCORFlatFileStorage sharedInstance] reset];
  60. self.uploader = [[GDTCORLifecycleTestUploader alloc] init];
  61. [[GDTCORRegistrar sharedInstance] registerUploader:self.uploader target:kGDTCORTargetTest];
  62. }
  63. - (void)tearDown {
  64. [super tearDown];
  65. self.uploader = nil;
  66. [[GDTCORRegistrar sharedInstance] reset];
  67. [[GDTCORFlatFileStorage sharedInstance] reset];
  68. [[GDTCORUploadCoordinator sharedInstance] reset];
  69. }
  70. // Backgrounding and foregrounding are only applicable for iOS and tvOS.
  71. #if TARGET_OS_IOS || TARGET_OS_TV
  72. /** Tests that the library serializes itself to disk when the app backgrounds. */
  73. - (void)testBackgrounding {
  74. GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"test"
  75. transformers:nil
  76. target:kGDTCORTargetTest];
  77. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  78. [notifCenter postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
  79. XCTAssertTrue([GDTCORApplication sharedApplication].isRunningInBackground);
  80. GDTCOREvent *event = [transport eventForTransport];
  81. event.dataObject = [[GDTCORLifecycleTestEvent alloc] init];
  82. XCTestExpectation *expectation = [self expectationWithDescription:@"hasEvent completion called"];
  83. [[GDTCORFlatFileStorage sharedInstance] hasEventsForTarget:kGDTCORTargetTest
  84. onComplete:^(BOOL hasEvents) {
  85. XCTAssertFalse(hasEvents);
  86. [expectation fulfill];
  87. }];
  88. [self waitForExpectations:@[ expectation ] timeout:10];
  89. [transport sendDataEvent:event];
  90. dispatch_sync([GDTCORTransformer sharedInstance].eventWritingQueue, ^{
  91. });
  92. expectation = [self expectationWithDescription:@"hasEvent completion called"];
  93. [[GDTCORFlatFileStorage sharedInstance] hasEventsForTarget:kGDTCORTargetTest
  94. onComplete:^(BOOL hasEvents) {
  95. XCTAssertTrue(hasEvents);
  96. [expectation fulfill];
  97. }];
  98. [self waitForExpectations:@[ expectation ] timeout:10];
  99. }
  100. #endif // #if TARGET_OS_IOS || TARGET_OS_TV
  101. /** Tests that the library gracefully stops doing stuff when terminating. */
  102. - (void)testTermination {
  103. GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"test"
  104. transformers:nil
  105. target:kGDTCORTargetTest];
  106. NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
  107. [notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
  108. GDTCOREvent *event = [transport eventForTransport];
  109. event.dataObject = [[GDTCORLifecycleTestEvent alloc] init];
  110. XCTestExpectation *expectation = [self expectationWithDescription:@"hasEvent completion called"];
  111. [[GDTCORFlatFileStorage sharedInstance] hasEventsForTarget:kGDTCORTargetTest
  112. onComplete:^(BOOL hasEvents) {
  113. XCTAssertFalse(hasEvents);
  114. [expectation fulfill];
  115. }];
  116. [self waitForExpectations:@[ expectation ] timeout:10];
  117. [transport sendDataEvent:event];
  118. dispatch_sync([GDTCORTransformer sharedInstance].eventWritingQueue, ^{
  119. });
  120. expectation = [self expectationWithDescription:@"hasEvent completion called"];
  121. [[GDTCORFlatFileStorage sharedInstance] hasEventsForTarget:kGDTCORTargetTest
  122. onComplete:^(BOOL hasEvents) {
  123. XCTAssertTrue(hasEvents);
  124. [expectation fulfill];
  125. }];
  126. [self waitForExpectations:@[ expectation ] timeout:10];
  127. }
  128. @end