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

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: rebase 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
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
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 a standard instance has the expected values.
63 TEST(MailtoURLRewriterTest, TestStandardInstance) {
64 MailtoURLRewriter* launcher =
65 [[MailtoURLRewriter alloc] initWithStandardHandlers];
66 EXPECT_TRUE(launcher);
67 // ID for system Mail client app must not be an empty string.
68 EXPECT_GT([[MailtoURLRewriter systemMailApp] length], 0U);
69
70 NSArray<MailtoHandler*>* handlers = [launcher defaultHandlers];
71 EXPECT_GE([handlers count], 1U);
72 for (MailtoHandler* handler : handlers) {
73 ASSERT_TRUE(handler);
74 NSString* appStoreID = [handler appStoreID];
75 [launcher setDefaultHandlerID:appStoreID];
76 EXPECT_NSEQ(appStoreID, [launcher defaultHandlerID]);
77 }
78 }
79
80 // Tests that a new user without Gmail app installed launches system Mail app.
81 TEST(MailtoURLRewriterTest, TestNewUserNoGmail) {
82 // Sets pre-condition for a user who did not have Chrome installed.
83 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
84 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
85 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
86 // A faked MailtoHandler for Gmail.
87 MailtoHandler* fakeGmailHandler =
88 [[FakeMailtoHandlerGmailNotInstalled alloc] init];
89
90 // Sets up a MailtoURLRewriter for testing.
91 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
92 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
93 [launcher addMailtoApp:systemMailHandler];
94 [launcher addMailtoApp:fakeGmailHandler];
95 [launcher convertLegacyOptions];
96
97 // Verify that MailtoURLRewriter will use the system Mail app.
98 EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [launcher defaultHandlerID]);
99 }
100
101 // Tests that a new user with Gmail app installed launches Gmail app.
102 TEST(MailtoURLRewriterTest, TestNewUserWithGmail) {
103 // Sets pre-condition for a user who did not have Chrome installed.
104 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
105 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
106 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
107 // A faked MailtoHandler for Gmail.
108 MailtoHandler* fakeGmailHandler =
109 [[FakeMailtoHandlerGmailInstalled alloc] init];
110
111 // Sets up a MailtoURLRewriter for testing.
112 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
113 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
114 [launcher addMailtoApp:systemMailHandler];
115 [launcher addMailtoApp:fakeGmailHandler];
116 [launcher convertLegacyOptions];
117
118 // Verify that MailtoURLRewriter will use Gmail app.
119 EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]);
120 }
121
122 // Tests that a user who has Gmail installed but has chosen not to use Gmail
123 // as the app to handle mailto: links retains the same behavior when upgrading
124 // Chrome.
125 TEST(MailtoURLRewriterTest, TestUpgradeUserWithGmailDisabled) {
126 // Sets pre-condition for a user who had Chrome installed.
127 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
128 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
129 [defaults setObject:@(kAutoOpenLinksNo) forKey:kLegacyShouldAutoOpenKey];
130 // A faked MailtoHandler for Gmail.
131 MailtoHandler* fakeGmailHandler =
132 [[FakeMailtoHandlerGmailInstalled alloc] init];
133
134 // Sets up a MailtoURLRewriter for testing.
135 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
136 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
137 [launcher addMailtoApp:systemMailHandler];
138 [launcher addMailtoApp:fakeGmailHandler];
139 [launcher convertLegacyOptions];
140
141 // Verify that MailtoURLRewriter will use the system Mail app. As part of the
142 // "upgrade", the legacy key should be removed as well.
143 EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [launcher defaultHandlerID]);
144 EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]);
145 }
146
147 // Tests that a user who has Gmail installed and has chosen to use Gmail as the
148 // app to handle mailto: links retains the same behavior.
149 TEST(MailtoURLRewriterTest, TestUpgradeUserWithGmailEnabled) {
150 // Sets pre-condition for a user who did not have Chrome installed.
151 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
152 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
153 [defaults setObject:@(kAutoOpenLinksYes) forKey:kLegacyShouldAutoOpenKey];
154 // A faked MailtoHandler for Gmail.
155 MailtoHandler* fakeGmailHandler =
156 [[FakeMailtoHandlerGmailInstalled alloc] init];
157
158 // Sets up a MailtoURLRewriter for testing.
159 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
160 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
161 [launcher addMailtoApp:systemMailHandler];
162 [launcher addMailtoApp:fakeGmailHandler];
163 [launcher convertLegacyOptions];
164
165 // Verify that MailtoURLRewriter will use Gmail app. As part of the upgrade,
166 // the legacy key should be removed as well.
167 EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]);
168 EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]);
169 }
170
171 // Tests that a user who installed Gmail after started using Chrome gets Gmail
172 // as the handler of mailto: links.
173 TEST(MailtoURLRewriterTest, TestInstalledGmailAfterChrome) {
174 // Pre-condition for a user who did not have Chrome installed.
175 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
176 [defaults setObject:[MailtoURLRewriter systemMailApp]
177 forKey:kMailtoDefaultHandlerKey];
178 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
179 // A faked MailtoHandler for Gmail.
180 MailtoHandler* fakeGmailHandler =
181 [[FakeMailtoHandlerGmailInstalled alloc] init];
182
183 // Sets up a MailtoURLRewriter for testing.
184 MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
185 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
186 [launcher addMailtoApp:systemMailHandler];
187 [launcher addMailtoApp:fakeGmailHandler];
188 [launcher convertLegacyOptions];
189
190 // Verify that MailtoURLRewriter will use Gmail app.
191 EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]);
192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698