FIRDLScionLoggingTest.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "DynamicLinks/FIRDLScionLogging.h"
  18. #import "OCMock.h"
  19. static const NSTimeInterval kAsyncTestTimout = 0.5;
  20. typedef void (^FakeAnalyticsLogEventWithOriginNameParametersHandler)(NSString *origin,
  21. NSString *name,
  22. NSDictionary *parameters);
  23. @interface FakeAnalytics : NSObject <FIRAnalyticsInterop>
  24. - (instancetype)initWithHandler:(FakeAnalyticsLogEventWithOriginNameParametersHandler)handler;
  25. @end
  26. @implementation FakeAnalytics
  27. static FakeAnalyticsLogEventWithOriginNameParametersHandler _handler;
  28. - (instancetype)initWithHandler:(FakeAnalyticsLogEventWithOriginNameParametersHandler)handler {
  29. self = [super init];
  30. if (self) {
  31. _handler = handler;
  32. }
  33. return self;
  34. }
  35. - (void)logEventWithOrigin:(nonnull NSString *)origin
  36. name:(nonnull NSString *)name
  37. parameters:(nullable NSDictionary<NSString *, id> *)parameters {
  38. if (_handler) {
  39. _handler(origin, name, parameters);
  40. }
  41. }
  42. // Stubs
  43. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  44. clearEventName:(nonnull NSString *)clearEventName
  45. clearEventParameters:(nonnull NSDictionary *)clearEventParameters {
  46. }
  47. - (nonnull NSArray<FIRAConditionalUserProperty *> *)
  48. conditionalUserProperties:(nonnull NSString *)origin
  49. propertyNamePrefix:(nonnull NSString *)propertyNamePrefix {
  50. return @[];
  51. }
  52. - (NSInteger)maxUserProperties:(nonnull NSString *)origin {
  53. return -1;
  54. }
  55. - (void)setConditionalUserProperty:(nonnull FIRAConditionalUserProperty *)conditionalUserProperty {
  56. }
  57. - (void)setUserPropertyWithOrigin:(nonnull NSString *)origin
  58. name:(nonnull NSString *)name
  59. value:(nonnull id)value {
  60. }
  61. @end
  62. @interface FIRDLScionLoggingTest : XCTestCase
  63. @end
  64. @implementation FIRDLScionLoggingTest
  65. - (void)testGINLogEventToScionCallsLogMethodWithFirstOpen {
  66. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  67. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  68. initWithHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  69. [expectation fulfill];
  70. }];
  71. FIRDLLogEventToScion(FIRDLLogEventFirstOpen, nil, nil, nil, analytics);
  72. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  73. }
  74. - (void)testGINLogEventToScionContainsCorrectNameWithFirstOpen {
  75. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  76. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  77. initWithHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  78. XCTAssertEqualObjects(name, @"dynamic_link_first_open", @"scion name param was incorrect");
  79. [expectation fulfill];
  80. }];
  81. FIRDLLogEventToScion(FIRDLLogEventFirstOpen, nil, nil, nil, analytics);
  82. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  83. }
  84. - (void)testGINLogEventToScionCallsLogMethodWithAppOpen {
  85. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  86. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  87. initWithHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  88. [expectation fulfill];
  89. }];
  90. FIRDLLogEventToScion(FIRDLLogEventAppOpen, nil, nil, nil, analytics);
  91. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  92. }
  93. - (void)testGINLogEventToScionContainsCorrectNameWithAppOpen {
  94. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  95. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  96. initWithHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  97. XCTAssertEqualObjects(name, @"dynamic_link_app_open", @"scion name param was incorrect");
  98. [expectation fulfill];
  99. }];
  100. FIRDLLogEventToScion(FIRDLLogEventAppOpen, nil, nil, nil, analytics);
  101. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  102. }
  103. - (void)testGINLogEventToScionLogsParametersCorrectly {
  104. NSString *source = @"9-2nkg";
  105. NSString *medium = @"fjg0";
  106. NSString *campaign = @"gjoo3u5";
  107. NSString *sourceKey = @"source";
  108. NSString *mediumKey = @"medium";
  109. NSString *campaignKey = @"campaign";
  110. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  111. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  112. initWithHandler:^(NSString *origin, NSString *name, NSDictionary *params) {
  113. XCTAssertEqualObjects(params[sourceKey], source, @"scion logger has incorrect source.");
  114. XCTAssertEqualObjects(params[mediumKey], medium, @"scion logger has incorrect medium.");
  115. XCTAssertEqualObjects(params[campaignKey], campaign,
  116. @"scion logger has incorrect campaign.");
  117. [expectation fulfill];
  118. }];
  119. FIRDLLogEventToScion(FIRDLLogEventAppOpen, source, medium, campaign, analytics);
  120. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  121. }
  122. @end