OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/web/mailto_url_rewriter.h" |
| 6 |
| 7 #import "ios/chrome/browser/web/mailto_handler.h" |
| 8 #import "ios/chrome/browser/web/mailto_handler_gmail.h" |
| 9 #import "ios/chrome/browser/web/mailto_handler_system_mail.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gtest_mac.h" |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Defines the 3 valid states for ShouldAutoOpenLinks_422689480. |
| 20 enum { |
| 21 kAutoOpenLinksNotSet = 0, |
| 22 kAutoOpenLinksNo = 1, |
| 23 kAutoOpenLinksYes = 2, |
| 24 }; |
| 25 |
| 26 NSString* const kMailtoDefaultHandlerKey = @"MailtoHandlerDefault"; |
| 27 NSString* const kLegacyShouldAutoOpenKey = @"ShouldAutoOpenLinks_422689480"; |
| 28 NSString* const kGmailAppStoreID = @"422689480"; |
| 29 } |
| 30 |
| 31 #pragma mark - Gmail not installed |
| 32 |
| 33 @interface FakeMailtoHandlerGmailNotInstalled : MailtoHandlerGmail |
| 34 @end |
| 35 |
| 36 @implementation FakeMailtoHandlerGmailNotInstalled |
| 37 - (BOOL)isAvailable { |
| 38 return NO; |
| 39 } |
| 40 @end |
| 41 |
| 42 #pragma mark - Gmail is installed |
| 43 |
| 44 @interface FakeMailtoHandlerGmailInstalled : MailtoHandlerGmail |
| 45 @end |
| 46 |
| 47 @implementation FakeMailtoHandlerGmailInstalled |
| 48 - (BOOL)isAvailable { |
| 49 return YES; |
| 50 } |
| 51 @end |
| 52 |
| 53 #pragma mark - MailtoURLRewriter private interfaces for testing. |
| 54 |
| 55 @interface MailtoURLRewriter () |
| 56 - (void)addMailtoApp:(MailtoHandler*)handlerApp; |
| 57 - (void)convertLegacyOptions; |
| 58 @end |
| 59 |
| 60 #pragma mark - Unit Test Cases |
| 61 |
| 62 // Tests that singleton instance has the expected values. |
| 63 TEST(MailtoURLRewriterTest, TestSharedInstance) { |
| 64 MailtoURLRewriter* launcher = [MailtoURLRewriter sharedInstance]; |
| 65 EXPECT_TRUE(launcher); |
| 66 // ID for system Mail client app must not be an empty string. |
| 67 EXPECT_GT([[MailtoURLRewriter systemMailApp] length], 0U); |
| 68 |
| 69 NSArray<MailtoHandler*>* handlers = [launcher defaultHandlers]; |
| 70 EXPECT_GE([handlers count], 1U); |
| 71 for (MailtoHandler* handler : handlers) { |
| 72 ASSERT_TRUE(handler); |
| 73 NSString* appStoreID = [handler appStoreID]; |
| 74 [launcher setDefaultHandlerID:appStoreID]; |
| 75 EXPECT_NSEQ(appStoreID, [launcher defaultHandlerID]); |
| 76 } |
| 77 } |
| 78 |
| 79 // Tests that a new user without Gmail app installed launches system Mail app. |
| 80 TEST(MailtoURLRewriterTest, TestNewUserNoGmail) { |
| 81 // Sets pre-condition for a user who did not have Chrome installed. |
| 82 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 83 [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
| 84 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; |
| 85 // A faked MailtoHandler for Gmail. |
| 86 MailtoHandler* fakeGmailHandler = |
| 87 [[FakeMailtoHandlerGmailNotInstalled alloc] init]; |
| 88 |
| 89 // Sets up a MailtoURLRewriter for testing. |
| 90 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init]; |
| 91 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; |
| 92 [launcher addMailtoApp:systemMailHandler]; |
| 93 [launcher addMailtoApp:fakeGmailHandler]; |
| 94 [launcher convertLegacyOptions]; |
| 95 |
| 96 // Verify that MailtoURLRewriter will use the system Mail app. |
| 97 EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [launcher defaultHandlerID]); |
| 98 } |
| 99 |
| 100 // Tests that a new user with Gmail app installed launches Gmail app. |
| 101 TEST(MailtoURLRewriterTest, TestNewUserWithGmail) { |
| 102 // Sets pre-condition for a user who did not have Chrome installed. |
| 103 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 104 [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
| 105 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; |
| 106 // A faked MailtoHandler for Gmail. |
| 107 MailtoHandler* fakeGmailHandler = |
| 108 [[FakeMailtoHandlerGmailInstalled alloc] init]; |
| 109 |
| 110 // Sets up a MailtoURLRewriter for testing. |
| 111 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init]; |
| 112 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; |
| 113 [launcher addMailtoApp:systemMailHandler]; |
| 114 [launcher addMailtoApp:fakeGmailHandler]; |
| 115 [launcher convertLegacyOptions]; |
| 116 |
| 117 // Verify that MailtoURLRewriter will use Gmail app. |
| 118 EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]); |
| 119 } |
| 120 |
| 121 // Tests that a user who has Gmail installed but has chosen not to use Gmail |
| 122 // as the app to handle mailto: links retains the same behavior when upgrading |
| 123 // Chrome. |
| 124 TEST(MailtoURLRewriterTest, TestUpgradeUserWithGmailDisabled) { |
| 125 // Sets pre-condition for a user who had Chrome installed. |
| 126 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 127 [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
| 128 [defaults setObject:@(kAutoOpenLinksNo) forKey:kLegacyShouldAutoOpenKey]; |
| 129 // A faked MailtoHandler for Gmail. |
| 130 MailtoHandler* fakeGmailHandler = |
| 131 [[FakeMailtoHandlerGmailInstalled alloc] init]; |
| 132 |
| 133 // Sets up a MailtoURLRewriter for testing. |
| 134 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init]; |
| 135 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; |
| 136 [launcher addMailtoApp:systemMailHandler]; |
| 137 [launcher addMailtoApp:fakeGmailHandler]; |
| 138 [launcher convertLegacyOptions]; |
| 139 |
| 140 // Verify that MailtoURLRewriter will use the system Mail app. As part of the |
| 141 // "upgrade", the legacy key should be removed as well. |
| 142 EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [launcher defaultHandlerID]); |
| 143 EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]); |
| 144 } |
| 145 |
| 146 // Tests that a user who has Gmail installed and has chosen to use Gmail as the |
| 147 // app to handle mailto: links retains the same behavior. |
| 148 TEST(MailtoURLRewriterTest, TestUpgradeUserWithGmailEnabled) { |
| 149 // Sets pre-condition for a user who did not have Chrome installed. |
| 150 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 151 [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
| 152 [defaults setObject:@(kAutoOpenLinksYes) forKey:kLegacyShouldAutoOpenKey]; |
| 153 // A faked MailtoHandler for Gmail. |
| 154 MailtoHandler* fakeGmailHandler = |
| 155 [[FakeMailtoHandlerGmailInstalled alloc] init]; |
| 156 |
| 157 // Sets up a MailtoURLRewriter for testing. |
| 158 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init]; |
| 159 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; |
| 160 [launcher addMailtoApp:systemMailHandler]; |
| 161 [launcher addMailtoApp:fakeGmailHandler]; |
| 162 [launcher convertLegacyOptions]; |
| 163 |
| 164 // Verify that MailtoURLRewriter will use Gmail app. As part of the upgrade, |
| 165 // the legacy key should be removed as well. |
| 166 EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]); |
| 167 EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]); |
| 168 } |
| 169 |
| 170 // Tests that a user who installed Gmail after started using Chrome gets Gmail |
| 171 // as the handler of mailto: links. |
| 172 TEST(MailtoURLRewriterTest, TestInstalledGmailAfterChrome) { |
| 173 // Pre-condition for a user who did not have Chrome installed. |
| 174 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 175 [defaults setObject:[MailtoURLRewriter systemMailApp] |
| 176 forKey:kMailtoDefaultHandlerKey]; |
| 177 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; |
| 178 // A faked MailtoHandler for Gmail. |
| 179 MailtoHandler* fakeGmailHandler = |
| 180 [[FakeMailtoHandlerGmailInstalled alloc] init]; |
| 181 |
| 182 // Sets up a MailtoURLRewriter for testing. |
| 183 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init]; |
| 184 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; |
| 185 [launcher addMailtoApp:systemMailHandler]; |
| 186 [launcher addMailtoApp:fakeGmailHandler]; |
| 187 [launcher convertLegacyOptions]; |
| 188 |
| 189 // Verify that MailtoURLRewriter will use Gmail app. |
| 190 EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]); |
| 191 } |
OLD | NEW |