FIRIAMElapsedTimeTrackerTests.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2017 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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseInAppMessaging/Sources/Util/FIRIAMElapsedTimeTracker.h"
  19. @interface FIRIAMElapsedTimeTrackerTests : XCTestCase
  20. @property id<FIRIAMTimeFetcher> mockTimeFetcher;
  21. @property FIRIAMElapsedTimeTracker *tracker;
  22. @end
  23. @implementation FIRIAMElapsedTimeTrackerTests
  24. - (void)setUp {
  25. [super setUp];
  26. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  27. }
  28. - (void)tearDown {
  29. // Put teardown code here. This method is called after the invocation of each test method in the
  30. // class.
  31. [super tearDown];
  32. }
  33. - (void)testTrackingTimeWithPauses {
  34. // This is an example of a functional test case.
  35. // Use XCTAssert and related functions to verify your tests produce the correct results.
  36. // set up the time moments to be returned
  37. // 0 start
  38. // 15 pause
  39. // 20 resume
  40. // 30 measure the total tracked time
  41. // given the above sequence,
  42. // at time = 30 seconds, we expect the tracked time to be 15 + (30 - 20) = 25 seconds
  43. NSArray<NSNumber *> *currentTimes = @[
  44. [NSNumber numberWithDouble:0], [NSNumber numberWithDouble:15], [NSNumber numberWithDouble:20],
  45. [NSNumber numberWithDouble:30]
  46. ];
  47. __block int nextTimeToReturn = 0;
  48. // start with timestamp as 0
  49. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andDo(^(NSInvocation *invocation) {
  50. NSTimeInterval time = [currentTimes[nextTimeToReturn++] doubleValue];
  51. [invocation setReturnValue:&time];
  52. });
  53. self.tracker = [[FIRIAMElapsedTimeTracker alloc] initWithTimeFetcher:_mockTimeFetcher];
  54. [self.tracker pause];
  55. [self.tracker resume];
  56. NSTimeInterval trackedTime = [self.tracker trackedTimeSoFar];
  57. XCTAssertEqualWithAccuracy(25, trackedTime, 0.01);
  58. }
  59. @end