GIDProfileDataTest.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2021 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 "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
  16. #import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
  17. static const NSUInteger kDimension = 100;
  18. static NSString *const kFIFEImageURL =
  19. @"https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg";
  20. static NSString *const kFIFEImageURLWithDimension =
  21. @"https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=100";
  22. static NSString *const kFIFEImageURL2 =
  23. @"https://lh3.googleusercontent.com/-sGxmwN8NbRA/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rd8EJb89PcveglTOdub_E1PO8ehJg/photo.jpg";
  24. static NSString *const kFIFEImageURL2WithDimension =
  25. @"https://lh3.googleusercontent.com/-sGxmwN8NbRA/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rd8EJb89PcveglTOdub_E1PO8ehJg/photo.jpg?sz=100";
  26. static NSString *const kFIFEAvatarURL =
  27. @"https://lh3.googleusercontent.com/a-/AAuE7mAWaTjIS4B9ojpOMNu247d6qO7LcH5teU0Idr5pK_E";
  28. static NSString *const kFIFEAvatarURLWithDimension =
  29. @"https://lh3.googleusercontent.com/a-/AAuE7mAWaTjIS4B9ojpOMNu247d6qO7LcH5teU0Idr5pK_E=s100";
  30. static NSString *const kFIFEAvatarURL2 =
  31. @"https://lh3.googleusercontent.com/a/default-user";
  32. static NSString *const kFIFEAvatarURL2WithDimension =
  33. @"https://lh3.googleusercontent.com/a/default-user=s100";
  34. @interface GIDProfileDataOld : NSObject <NSCoding, NSSecureCoding>
  35. @end
  36. @implementation GIDProfileDataOld {
  37. NSString *_email;
  38. NSString *_name;
  39. NSString *_imageURL;
  40. }
  41. - (instancetype)initWithEmail:(NSString *)email
  42. name:(NSString *)name
  43. imageURL:(NSString *)imageURL {
  44. self = [super init];
  45. if (self) {
  46. _email = [email copy];
  47. _name = [name copy];
  48. _imageURL = [imageURL copy];
  49. }
  50. return self;
  51. }
  52. - (instancetype)initWithCoder:(NSCoder *)decoder {
  53. self = [super init];
  54. if (self) {
  55. }
  56. return self;
  57. }
  58. - (void)encodeWithCoder:(NSCoder *)encoder {
  59. [encoder encodeObject:_email forKey:@"email"];
  60. [encoder encodeObject:_name forKey:@"name"];
  61. [encoder encodeObject:_imageURL forKey:@"picture"];
  62. }
  63. + (BOOL)supportsSecureCoding {
  64. return YES;
  65. }
  66. @end
  67. @interface GIDProfileDataTest : XCTestCase
  68. @end
  69. @implementation GIDProfileDataTest
  70. #pragma mark - Tests
  71. - (void)testInitialization {
  72. GIDProfileData *profileData = [self profileData];
  73. XCTAssertEqualObjects(profileData.email, kEmail);
  74. XCTAssertEqualObjects(profileData.name, kName);
  75. XCTAssertEqualObjects(profileData.givenName, kGivenName);
  76. XCTAssertEqualObjects(profileData.familyName, kFamilyName);
  77. XCTAssertTrue(profileData.hasImage);
  78. }
  79. - (void)testCoding {
  80. if (@available(iOS 11, macOS 10.13, *)) {
  81. GIDProfileData *profileData = [self profileData];
  82. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profileData
  83. requiringSecureCoding:YES
  84. error:nil];
  85. GIDProfileData *newProfileData = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDProfileData class]
  86. fromData:data
  87. error:nil];
  88. XCTAssertEqualObjects(profileData, newProfileData);
  89. XCTAssertTrue(GIDProfileData.supportsSecureCoding);
  90. } else {
  91. XCTSkip(@"Required API is not available for this test.");
  92. }
  93. }
  94. - (void)testOldArchiveFormat {
  95. if (@available(iOS 11, macOS 10.13, *)) {
  96. GIDProfileDataOld *oldProfile = [[GIDProfileDataOld alloc] initWithEmail:kEmail
  97. name:kName
  98. imageURL:kFIFEImageURL];
  99. [NSKeyedArchiver setClassName:@"GIDProfileData" forClass:[GIDProfileDataOld class]];
  100. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:oldProfile
  101. requiringSecureCoding:YES
  102. error:nil];
  103. GIDProfileData *profileData = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDProfileData class]
  104. fromData:data
  105. error:nil];
  106. XCTAssertEqualObjects(profileData.email, kEmail);
  107. XCTAssertEqualObjects(profileData.name, kName);
  108. XCTAssertNil(profileData.givenName);
  109. XCTAssertNil(profileData.familyName);
  110. XCTAssertTrue(profileData.hasImage);
  111. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  112. kFIFEImageURLWithDimension);
  113. } else {
  114. XCTSkip(@"Required API is not available for this test.");
  115. }
  116. }
  117. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  118. // Deprecated in iOS 13 and macOS 10.14
  119. - (void)testLegacyCoding {
  120. GIDProfileData *profileData = [self profileData];
  121. NSError *archiveError;
  122. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profileData
  123. requiringSecureCoding:NO
  124. error:&archiveError];
  125. XCTAssertNil(archiveError);
  126. NSError *unarchiveError;
  127. GIDProfileData *newProfileData = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDProfileData class]
  128. fromData:data
  129. error:&unarchiveError];
  130. XCTAssertNil(unarchiveError);
  131. XCTAssertEqualObjects(profileData, newProfileData);
  132. XCTAssertTrue(GIDProfileData.supportsSecureCoding);
  133. }
  134. - (void)testOldArchiveFormatLegacy {
  135. GIDProfileDataOld *oldProfile = [[GIDProfileDataOld alloc] initWithEmail:kEmail
  136. name:kName
  137. imageURL:kFIFEImageURL];
  138. [NSKeyedArchiver setClassName:@"GIDProfileData" forClass:[GIDProfileDataOld class]];
  139. NSError *archiveError;
  140. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:oldProfile
  141. requiringSecureCoding:NO
  142. error:&archiveError];
  143. XCTAssertNil(archiveError);
  144. NSError *unarchiveError;
  145. GIDProfileData *profileData = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDProfileData class]
  146. fromData:data
  147. error:&unarchiveError];
  148. XCTAssertNil(unarchiveError);
  149. XCTAssertEqualObjects(profileData.email, kEmail);
  150. XCTAssertEqualObjects(profileData.name, kName);
  151. XCTAssertNil(profileData.givenName);
  152. XCTAssertNil(profileData.familyName);
  153. XCTAssertTrue(profileData.hasImage);
  154. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  155. kFIFEImageURLWithDimension);
  156. }
  157. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  158. - (void)testImageURLWithDimension {
  159. GIDProfileData *profileData;
  160. // Test FIFE Image URLs
  161. profileData = [self profileDataWithImageURL:kFIFEImageURL];
  162. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  163. kFIFEImageURLWithDimension);
  164. profileData = [self profileDataWithImageURL:kFIFEImageURL2];
  165. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  166. kFIFEImageURL2WithDimension);
  167. // with preexisting options
  168. profileData = [self profileDataWithImageURL:kFIFEImageURLWithDimension];
  169. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  170. kFIFEImageURLWithDimension);
  171. profileData = [self profileDataWithImageURL:kFIFEImageURL2WithDimension];
  172. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  173. kFIFEImageURL2WithDimension);
  174. // Test FIFE Avatar URLs
  175. profileData = [self profileDataWithImageURL:kFIFEAvatarURL];
  176. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  177. kFIFEAvatarURLWithDimension);
  178. profileData = [self profileDataWithImageURL:kFIFEAvatarURL2];
  179. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  180. kFIFEAvatarURL2WithDimension);
  181. // with preexisting options
  182. profileData = [self profileDataWithImageURL:kFIFEAvatarURLWithDimension];
  183. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  184. kFIFEAvatarURLWithDimension);
  185. profileData = [self profileDataWithImageURL:kFIFEAvatarURL2WithDimension];
  186. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  187. kFIFEAvatarURL2WithDimension);
  188. }
  189. #pragma mark - Helpers
  190. - (GIDProfileData *)profileData {
  191. return [self profileDataWithImageURL:kFIFEImageURL];
  192. }
  193. - (GIDProfileData *)profileDataWithImageURL:(NSString *)imageURL {
  194. return [GIDProfileData testInstanceWithImageURL:imageURL];
  195. }
  196. @end