| Index: ios/chrome/browser/web/mailto_url_rewriter_unittest.mm
|
| diff --git a/ios/chrome/browser/web/mailto_url_rewriter_unittest.mm b/ios/chrome/browser/web/mailto_url_rewriter_unittest.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9f62edc058b97f098e9142103d9744ce69f0b0f6
|
| --- /dev/null
|
| +++ b/ios/chrome/browser/web/mailto_url_rewriter_unittest.mm
|
| @@ -0,0 +1,191 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#import "ios/chrome/browser/web/mailto_url_rewriter.h"
|
| +
|
| +#import "ios/chrome/browser/web/mailto_handler.h"
|
| +#import "ios/chrome/browser/web/mailto_handler_gmail.h"
|
| +#import "ios/chrome/browser/web/mailto_handler_system_mail.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "testing/gtest_mac.h"
|
| +
|
| +#if !defined(__has_feature) || !__has_feature(objc_arc)
|
| +#error "This file requires ARC support."
|
| +#endif
|
| +
|
| +namespace {
|
| +
|
| +// Defines the 3 valid states for ShouldAutoOpenLinks_422689480.
|
| +enum {
|
| + kAutoOpenLinksNotSet = 0,
|
| + kAutoOpenLinksNo = 1,
|
| + kAutoOpenLinksYes = 2,
|
| +};
|
| +
|
| +NSString* const kMailtoDefaultHandlerKey = @"MailtoHandlerDefault";
|
| +NSString* const kLegacyShouldAutoOpenKey = @"ShouldAutoOpenLinks_422689480";
|
| +NSString* const kGmailAppStoreID = @"422689480";
|
| +}
|
| +
|
| +#pragma mark - Gmail not installed
|
| +
|
| +@interface FakeMailtoHandlerGmailNotInstalled : MailtoHandlerGmail
|
| +@end
|
| +
|
| +@implementation FakeMailtoHandlerGmailNotInstalled
|
| +- (BOOL)isAvailable {
|
| + return NO;
|
| +}
|
| +@end
|
| +
|
| +#pragma mark - Gmail is installed
|
| +
|
| +@interface FakeMailtoHandlerGmailInstalled : MailtoHandlerGmail
|
| +@end
|
| +
|
| +@implementation FakeMailtoHandlerGmailInstalled
|
| +- (BOOL)isAvailable {
|
| + return YES;
|
| +}
|
| +@end
|
| +
|
| +#pragma mark - MailtoURLRewriter private interfaces for testing.
|
| +
|
| +@interface MailtoURLRewriter ()
|
| +- (void)addMailtoApp:(MailtoHandler*)handlerApp;
|
| +- (void)convertLegacyOptions;
|
| +@end
|
| +
|
| +#pragma mark - Unit Test Cases
|
| +
|
| +// Tests that singleton instance has the expected values.
|
| +TEST(MailtoURLRewriterTest, TestSharedInstance) {
|
| + MailtoURLRewriter* launcher = [MailtoURLRewriter sharedInstance];
|
| + EXPECT_TRUE(launcher);
|
| + // ID for system Mail client app must not be an empty string.
|
| + EXPECT_GT([[MailtoURLRewriter systemMailApp] length], 0U);
|
| +
|
| + NSArray<MailtoHandler*>* handlers = [launcher defaultHandlers];
|
| + EXPECT_GE([handlers count], 1U);
|
| + for (MailtoHandler* handler : handlers) {
|
| + ASSERT_TRUE(handler);
|
| + NSString* appStoreID = [handler appStoreID];
|
| + [launcher setDefaultHandlerID:appStoreID];
|
| + EXPECT_NSEQ(appStoreID, [launcher defaultHandlerID]);
|
| + }
|
| +}
|
| +
|
| +// Tests that a new user without Gmail app installed launches system Mail app.
|
| +TEST(MailtoURLRewriterTest, TestNewUserNoGmail) {
|
| + // Sets pre-condition for a user who did not have Chrome installed.
|
| + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
| + [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
|
| + [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
|
| + // A faked MailtoHandler for Gmail.
|
| + MailtoHandler* fakeGmailHandler =
|
| + [[FakeMailtoHandlerGmailNotInstalled alloc] init];
|
| +
|
| + // Sets up a MailtoURLRewriter for testing.
|
| + MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
|
| + MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
|
| + [launcher addMailtoApp:systemMailHandler];
|
| + [launcher addMailtoApp:fakeGmailHandler];
|
| + [launcher convertLegacyOptions];
|
| +
|
| + // Verify that MailtoURLRewriter will use the system Mail app.
|
| + EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [launcher defaultHandlerID]);
|
| +}
|
| +
|
| +// Tests that a new user with Gmail app installed launches Gmail app.
|
| +TEST(MailtoURLRewriterTest, TestNewUserWithGmail) {
|
| + // Sets pre-condition for a user who did not have Chrome installed.
|
| + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
| + [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
|
| + [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
|
| + // A faked MailtoHandler for Gmail.
|
| + MailtoHandler* fakeGmailHandler =
|
| + [[FakeMailtoHandlerGmailInstalled alloc] init];
|
| +
|
| + // Sets up a MailtoURLRewriter for testing.
|
| + MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
|
| + MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
|
| + [launcher addMailtoApp:systemMailHandler];
|
| + [launcher addMailtoApp:fakeGmailHandler];
|
| + [launcher convertLegacyOptions];
|
| +
|
| + // Verify that MailtoURLRewriter will use Gmail app.
|
| + EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]);
|
| +}
|
| +
|
| +// Tests that a user who has Gmail installed but has chosen not to use Gmail
|
| +// as the app to handle mailto: links retains the same behavior when upgrading
|
| +// Chrome.
|
| +TEST(MailtoURLRewriterTest, TestUpgradeUserWithGmailDisabled) {
|
| + // Sets pre-condition for a user who had Chrome installed.
|
| + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
| + [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
|
| + [defaults setObject:@(kAutoOpenLinksNo) forKey:kLegacyShouldAutoOpenKey];
|
| + // A faked MailtoHandler for Gmail.
|
| + MailtoHandler* fakeGmailHandler =
|
| + [[FakeMailtoHandlerGmailInstalled alloc] init];
|
| +
|
| + // Sets up a MailtoURLRewriter for testing.
|
| + MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
|
| + MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
|
| + [launcher addMailtoApp:systemMailHandler];
|
| + [launcher addMailtoApp:fakeGmailHandler];
|
| + [launcher convertLegacyOptions];
|
| +
|
| + // Verify that MailtoURLRewriter will use the system Mail app. As part of the
|
| + // "upgrade", the legacy key should be removed as well.
|
| + EXPECT_NSEQ([MailtoURLRewriter systemMailApp], [launcher defaultHandlerID]);
|
| + EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]);
|
| +}
|
| +
|
| +// Tests that a user who has Gmail installed and has chosen to use Gmail as the
|
| +// app to handle mailto: links retains the same behavior.
|
| +TEST(MailtoURLRewriterTest, TestUpgradeUserWithGmailEnabled) {
|
| + // Sets pre-condition for a user who did not have Chrome installed.
|
| + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
| + [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
|
| + [defaults setObject:@(kAutoOpenLinksYes) forKey:kLegacyShouldAutoOpenKey];
|
| + // A faked MailtoHandler for Gmail.
|
| + MailtoHandler* fakeGmailHandler =
|
| + [[FakeMailtoHandlerGmailInstalled alloc] init];
|
| +
|
| + // Sets up a MailtoURLRewriter for testing.
|
| + MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
|
| + MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
|
| + [launcher addMailtoApp:systemMailHandler];
|
| + [launcher addMailtoApp:fakeGmailHandler];
|
| + [launcher convertLegacyOptions];
|
| +
|
| + // Verify that MailtoURLRewriter will use Gmail app. As part of the upgrade,
|
| + // the legacy key should be removed as well.
|
| + EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]);
|
| + EXPECT_FALSE([defaults objectForKey:kLegacyShouldAutoOpenKey]);
|
| +}
|
| +
|
| +// Tests that a user who installed Gmail after started using Chrome gets Gmail
|
| +// as the handler of mailto: links.
|
| +TEST(MailtoURLRewriterTest, TestInstalledGmailAfterChrome) {
|
| + // Pre-condition for a user who did not have Chrome installed.
|
| + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
| + [defaults setObject:[MailtoURLRewriter systemMailApp]
|
| + forKey:kMailtoDefaultHandlerKey];
|
| + [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
|
| + // A faked MailtoHandler for Gmail.
|
| + MailtoHandler* fakeGmailHandler =
|
| + [[FakeMailtoHandlerGmailInstalled alloc] init];
|
| +
|
| + // Sets up a MailtoURLRewriter for testing.
|
| + MailtoURLRewriter* launcher = [[MailtoURLRewriter alloc] init];
|
| + MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init];
|
| + [launcher addMailtoApp:systemMailHandler];
|
| + [launcher addMailtoApp:fakeGmailHandler];
|
| + [launcher convertLegacyOptions];
|
| +
|
| + // Verify that MailtoURLRewriter will use Gmail app.
|
| + EXPECT_NSEQ(kGmailAppStoreID, [launcher defaultHandlerID]);
|
| +}
|
|
|