Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: ios/chrome/browser/web/mailto_url_rewriter_unittest.mm

Issue 2852003002: Adds mailto: URL support to app launching. (Closed)
Patch Set: separate migration from auto-default even more Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/chrome/browser/web/mailto_url_rewriter.mm ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "testing/platform_test.h"
13
14 #if !defined(__has_feature) || !__has_feature(objc_arc)
15 #error "This file requires ARC support."
16 #endif
17
18 namespace {
19
20 // Defines the 3 valid states for ShouldAutoOpenLinks_422689480.
21 enum {
22 kAutoOpenLinksNotSet = 0,
23 kAutoOpenLinksNo = 1,
24 kAutoOpenLinksYes = 2,
25 };
26 NSString* const kLegacyShouldAutoOpenKey = @"ShouldAutoOpenLinks_422689480";
27 NSString* const kGmailAppStoreID = @"422689480";
28
29 } // namespace
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)resetDefaultHandlerIDForTesting;
57 - (void)addMailtoApps:(NSArray<MailtoHandler*>*)handlerApps;
58 @end
59
60 #pragma mark - Unit Test Cases
61
62 class MailtoURLRewriterTest : public PlatformTest {
63 protected:
64 void SetUp() override { [MailtoURLRewriter resetDefaultHandlerIDForTesting]; }
65 };
66
67 // Tests that a standard instance has the expected values.
68 TEST_F(MailtoURLRewriterTest, TestStandardInstance) {
69 MailtoURLRewriter* rewriter =
70 [[MailtoURLRewriter alloc] initWithStandardHandlers];
71 EXPECT_TRUE(rewriter);
72 // ID for system Mail client app must not be an empty string.
73 EXPECT_GT([[MailtoURLRewriter systemMailApp] length], 0U);
74
75 NSArray<MailtoHandler*>* handlers = [rewriter defaultHandlers];
76 EXPECT_GE([handlers count], 1U);
77 for (MailtoHandler* handler in handlers) {
78 ASSERT_TRUE(handler);
79 NSString* appStoreID = [handler appStoreID];
80 [rewriter setDefaultHandlerID:appStoreID];
81 EXPECT_NSEQ(appStoreID, [rewriter defaultHandlerID]);
82 }
83 }
84
85 TEST_F(MailtoURLRewriterTest, TestUserPreferencePersistence) {
86 // Sets up a first MailtoURLRewriter with at least 2 MailtoHandler objects.
87 // A faked Gmail handler that is installed must be used or -addMailtoApp:
88 // will just skip it.
89 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init];
90 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
91 MailtoHandler* fakeGmailHandler =
92 [[FakeMailtoHandlerGmailInstalled alloc] init];
93 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
94
95 // Verifies that there must be 2 registered handlers. Then find a
96 // MailtoHandler that is not the current default and set that as the new
97 // default.
98 NSArray<MailtoHandler*>* handlers = [rewriter defaultHandlers];
99 ASSERT_GE([handlers count], 2U);
100 NSString* initialHandlerID = [rewriter defaultHandlerID];
101 NSString* otherHandlerID = nil;
102 for (MailtoHandler* handler in handlers) {
103 if (![initialHandlerID isEqualToString:[handler appStoreID]]) {
104 otherHandlerID = [handler appStoreID];
105 break;
106 }
107 }
108 ASSERT_TRUE([otherHandlerID length]);
109 [rewriter setDefaultHandlerID:otherHandlerID];
110
111 // Create a new MailtoURLRewriter object and verify that the current
112 // default is the |otherHandlerID| set in the previous step.
113 MailtoURLRewriter* rewriter2 = [[MailtoURLRewriter alloc] init];
114 [rewriter2 addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
115 EXPECT_NSEQ(otherHandlerID, [rewriter2 defaultHandlerID]);
116 }
117
118 // Tests that a new user without Gmail app installed launches system Mail app.
119 TEST_F(MailtoURLRewriterTest, TestNewUserNoGmail) {
120 // Sets pre-condition for a user who did not have Chrome installed.
121 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
122 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
123 // A faked MailtoHandler for Gmail.
124 MailtoHandler* fakeGmailHandler =
125 [[FakeMailtoHandlerGmailNotInstalled alloc] init];
126
127 // Sets up a MailtoURLRewriter for testing.
128 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init];
129 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
130 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
131
132 // Verify that MailtoURLRewriter will use the system Mail app.
133 EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [rewriter defaultHandlerID]);
134 }
135
136 // Tests that a new user with Gmail app installed launches Gmail app.
137 TEST_F(MailtoURLRewriterTest, TestNewUserWithGmail) {
138 // Sets pre-condition for a user who did not have Chrome installed.
139 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
140 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
141 // A faked MailtoHandler for Gmail.
142 MailtoHandler* fakeGmailHandler =
143 [[FakeMailtoHandlerGmailInstalled alloc] init];
144
145 // Sets up a MailtoURLRewriter for testing.
146 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init];
147 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
148 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
149
150 // Verify that MailtoURLRewriter will use Gmail app.
151 EXPECT_NSEQ(kGmailAppStoreID, [rewriter defaultHandlerID]);
152 }
153
154 // Tests that a user who has Gmail installed but has chosen not to use Gmail
155 // as the app to handle mailto: links retains the same behavior when upgrading
156 // Chrome.
157 TEST_F(MailtoURLRewriterTest, TestUpgradeUserWithGmailDisabled) {
158 // Sets pre-condition for a user who had Chrome installed.
159 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
160 [defaults setObject:@(kAutoOpenLinksNo) forKey:kLegacyShouldAutoOpenKey];
161 // A faked MailtoHandler for Gmail.
162 MailtoHandler* fakeGmailHandler =
163 [[FakeMailtoHandlerGmailInstalled alloc] init];
164
165 // Sets up a MailtoURLRewriter for testing.
166 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init];
167 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
168 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
169
170 // Verify that MailtoURLRewriter will use the system Mail app. As part of the
171 // "upgrade", the legacy key should be removed as well.
172 EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [rewriter defaultHandlerID]);
173 EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]);
174 }
175
176 // Tests that a user who has Gmail installed and has chosen to use Gmail as the
177 // app to handle mailto: links retains the same behavior.
178 TEST_F(MailtoURLRewriterTest, TestUpgradeUserWithGmailEnabled) {
179 // Sets pre-condition for a user who did not have Chrome installed.
180 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
181 [defaults setObject:@(kAutoOpenLinksYes) forKey:kLegacyShouldAutoOpenKey];
182 // A faked MailtoHandler for Gmail.
183 MailtoHandler* fakeGmailHandler =
184 [[FakeMailtoHandlerGmailInstalled alloc] init];
185
186 // Sets up a MailtoURLRewriter for testing.
187 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init];
188 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
189 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
190
191 // Verify that MailtoURLRewriter will use Gmail app. As part of the upgrade,
192 // the legacy key should be removed as well.
193 EXPECT_NSEQ(kGmailAppStoreID, [rewriter defaultHandlerID]);
194 EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]);
195 }
196
197 // Tests that a user who installed Gmail after started using Chrome gets Gmail
198 // as the handler of mailto: links.
199 TEST_F(MailtoURLRewriterTest, TestInstalledGmailAfterChrome) {
200 // Pre-condition for a user who did not have Chrome installed.
201 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
202 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
203 // A faked MailtoHandler for Gmail.
204 MailtoHandler* fakeGmailHandler =
205 [[FakeMailtoHandlerGmailInstalled alloc] init];
206
207 // Sets up a MailtoURLRewriter for testing.
208 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init];
209 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
210 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]];
211
212 // Verify that MailtoURLRewriter will use Gmail app.
213 EXPECT_NSEQ(kGmailAppStoreID, [rewriter defaultHandlerID]);
214 }
OLDNEW
« no previous file with comments | « ios/chrome/browser/web/mailto_url_rewriter.mm ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698