FPRTraceBackgroundActivityTrackerTest.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/XCTest.h>
  15. #import "FirebasePerformance/Sources/AppActivity/FPRTraceBackgroundActivityTracker.h"
  16. @interface FPRTraceBackgroundActivityTrackerTest : XCTestCase
  17. @end
  18. @implementation FPRTraceBackgroundActivityTrackerTest
  19. /** Validate instance creation. */
  20. - (void)testInstanceCreation {
  21. XCTAssertNotNil([[FPRTraceBackgroundActivityTracker alloc] init]);
  22. }
  23. /** Validates if the foreground state is captured correctly. */
  24. - (void)testForegroundTracking {
  25. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  26. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  27. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  28. object:[UIApplication sharedApplication]];
  29. XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);
  30. }
  31. /** Validates if the foreground & background state is captured correctly. */
  32. - (void)testBackgroundTracking {
  33. XCTestExpectation *expectation = [self expectationWithDescription:@"Application state change"];
  34. FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
  35. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  36. dispatch_async(dispatch_get_main_queue(), ^{
  37. [defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
  38. object:[UIApplication sharedApplication]];
  39. [defaultCenter postNotificationName:UIApplicationDidEnterBackgroundNotification
  40. object:[UIApplication sharedApplication]];
  41. [expectation fulfill];
  42. });
  43. [self waitForExpectationsWithTimeout:5.0
  44. handler:^(NSError *_Nullable error) {
  45. if (error) {
  46. XCTFail(@"Expectation failed with error: %@", error);
  47. } else {
  48. XCTAssertEqual(tracker.traceBackgroundState,
  49. FPRTraceStateBackgroundAndForeground);
  50. }
  51. }];
  52. }
  53. @end