GIDProfileDataTest.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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>
  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. @end
  64. @interface GIDProfileDataTest : XCTestCase
  65. @end
  66. @implementation GIDProfileDataTest
  67. #pragma mark - Tests
  68. - (void)testInitialization {
  69. GIDProfileData *profileData = [self profileData];
  70. XCTAssertEqualObjects(profileData.email, kEmail);
  71. XCTAssertEqualObjects(profileData.name, kName);
  72. XCTAssertEqualObjects(profileData.givenName, kGivenName);
  73. XCTAssertEqualObjects(profileData.familyName, kFamilyName);
  74. XCTAssertTrue(profileData.hasImage);
  75. }
  76. - (void)testCoding {
  77. GIDProfileData *profileData = [self profileData];
  78. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profileData];
  79. GIDProfileData *newProfileData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  80. XCTAssertEqualObjects(profileData, newProfileData);
  81. XCTAssertTrue(GIDProfileData.supportsSecureCoding);
  82. }
  83. - (void)testOldArchiveFormat {
  84. GIDProfileDataOld *oldProfile = [[GIDProfileDataOld alloc] initWithEmail:kEmail
  85. name:kName
  86. imageURL:kFIFEImageURL];
  87. [NSKeyedArchiver setClassName:@"GIDProfileData" forClass:[GIDProfileDataOld class]];
  88. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:oldProfile];
  89. GIDProfileData *profileData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  90. XCTAssertEqualObjects(profileData.email, kEmail);
  91. XCTAssertEqualObjects(profileData.name, kName);
  92. XCTAssertNil(profileData.givenName);
  93. XCTAssertNil(profileData.familyName);
  94. XCTAssertTrue(profileData.hasImage);
  95. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  96. kFIFEImageURLWithDimension);
  97. }
  98. - (void)testImageURLWithDimension {
  99. GIDProfileData *profileData;
  100. // Test FIFE Image URLs
  101. profileData = [self profileDataWithImageURL:kFIFEImageURL];
  102. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  103. kFIFEImageURLWithDimension);
  104. profileData = [self profileDataWithImageURL:kFIFEImageURL2];
  105. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  106. kFIFEImageURL2WithDimension);
  107. // with preexisting options
  108. profileData = [self profileDataWithImageURL:kFIFEImageURLWithDimension];
  109. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  110. kFIFEImageURLWithDimension);
  111. profileData = [self profileDataWithImageURL:kFIFEImageURL2WithDimension];
  112. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  113. kFIFEImageURL2WithDimension);
  114. // Test FIFE Avatar URLs
  115. profileData = [self profileDataWithImageURL:kFIFEAvatarURL];
  116. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  117. kFIFEAvatarURLWithDimension);
  118. profileData = [self profileDataWithImageURL:kFIFEAvatarURL2];
  119. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  120. kFIFEAvatarURL2WithDimension);
  121. // with preexisting options
  122. profileData = [self profileDataWithImageURL:kFIFEAvatarURLWithDimension];
  123. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  124. kFIFEAvatarURLWithDimension);
  125. profileData = [self profileDataWithImageURL:kFIFEAvatarURL2WithDimension];
  126. XCTAssertEqualObjects([profileData imageURLWithDimension:kDimension].absoluteString,
  127. kFIFEAvatarURL2WithDimension);
  128. }
  129. #pragma mark - Helpers
  130. - (GIDProfileData *)profileData {
  131. return [self profileDataWithImageURL:kFIFEImageURL];
  132. }
  133. - (GIDProfileData *)profileDataWithImageURL:(NSString *)imageURL {
  134. return [GIDProfileData testInstanceWithImageURL:imageURL];
  135. }
  136. @end