Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/web/mailto_url_rewriter.h" | 5 #import "ios/chrome/browser/web/mailto_url_rewriter.h" |
| 6 | 6 |
| 7 #import "ios/chrome/browser/web/mailto_handler.h" | 7 #import "ios/chrome/browser/web/mailto_handler.h" |
| 8 #import "ios/chrome/browser/web/mailto_handler_gmail.h" | 8 #import "ios/chrome/browser/web/mailto_handler_gmail.h" |
| 9 #import "ios/chrome/browser/web/mailto_handler_system_mail.h" | 9 #import "ios/chrome/browser/web/mailto_handler_system_mail.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 | 43 |
| 44 @interface FakeMailtoHandlerGmailInstalled : MailtoHandlerGmail | 44 @interface FakeMailtoHandlerGmailInstalled : MailtoHandlerGmail |
| 45 @end | 45 @end |
| 46 | 46 |
| 47 @implementation FakeMailtoHandlerGmailInstalled | 47 @implementation FakeMailtoHandlerGmailInstalled |
| 48 - (BOOL)isAvailable { | 48 - (BOOL)isAvailable { |
| 49 return YES; | 49 return YES; |
| 50 } | 50 } |
| 51 @end | 51 @end |
| 52 | 52 |
| 53 #pragma mark - Test Observer object | |
| 54 | |
| 55 @interface RewriterObserver : NSObject<MailtoURLRewriterObserver> | |
| 56 @property(nonatomic, readonly) int changeCount; | |
| 57 @end | |
| 58 | |
| 59 @implementation RewriterObserver | |
| 60 @synthesize changeCount = _changeCount; | |
| 61 - (void)rewriterDidChange:(MailtoURLRewriter*)rewriter { | |
| 62 ++_changeCount; | |
| 63 } | |
| 64 @end | |
| 65 | |
| 53 #pragma mark - MailtoURLRewriter private interfaces for testing. | 66 #pragma mark - MailtoURLRewriter private interfaces for testing. |
| 54 | 67 |
| 55 @interface MailtoURLRewriter () | 68 @interface MailtoURLRewriter () |
| 56 + (void)resetDefaultHandlerIDForTesting; | 69 + (void)resetDefaultHandlerIDForTesting; |
| 57 - (void)addMailtoApps:(NSArray<MailtoHandler*>*)handlerApps; | 70 - (void)addMailtoApps:(NSArray<MailtoHandler*>*)handlerApps; |
| 58 @end | 71 @end |
| 59 | 72 |
| 60 #pragma mark - Unit Test Cases | 73 #pragma mark - Unit Test Cases |
| 61 | 74 |
| 62 class MailtoURLRewriterTest : public PlatformTest { | 75 class MailtoURLRewriterTest : public PlatformTest { |
| 63 protected: | 76 protected: |
| 64 void SetUp() override { [MailtoURLRewriter resetDefaultHandlerIDForTesting]; } | 77 void SetUp() override { [MailtoURLRewriter resetDefaultHandlerIDForTesting]; } |
| 65 }; | 78 }; |
| 66 | 79 |
| 67 // Tests that a standard instance has the expected values. | 80 // Tests that a standard instance has the expected values. |
| 68 TEST_F(MailtoURLRewriterTest, TestStandardInstance) { | 81 TEST_F(MailtoURLRewriterTest, TestStandardInstance) { |
| 69 MailtoURLRewriter* rewriter = | 82 MailtoURLRewriter* rewriter = |
| 70 [[MailtoURLRewriter alloc] initWithStandardHandlers]; | 83 [[MailtoURLRewriter alloc] initWithStandardHandlers]; |
| 71 EXPECT_TRUE(rewriter); | 84 EXPECT_TRUE(rewriter); |
| 85 EXPECT_GT([[rewriter defaultHandlerName] length], 0U); | |
| 72 // ID for system Mail client app must not be an empty string. | 86 // ID for system Mail client app must not be an empty string. |
| 73 EXPECT_GT([[MailtoURLRewriter systemMailApp] length], 0U); | 87 EXPECT_GT([[MailtoURLRewriter systemMailApp] length], 0U); |
| 74 | 88 |
| 75 NSArray<MailtoHandler*>* handlers = [rewriter defaultHandlers]; | 89 NSArray<MailtoHandler*>* handlers = [rewriter defaultHandlers]; |
| 76 EXPECT_GE([handlers count], 1U); | 90 EXPECT_GE([handlers count], 1U); |
| 77 for (MailtoHandler* handler in handlers) { | 91 for (MailtoHandler* handler in handlers) { |
| 78 ASSERT_TRUE(handler); | 92 ASSERT_TRUE(handler); |
| 79 NSString* appStoreID = [handler appStoreID]; | 93 NSString* appStoreID = [handler appStoreID]; |
| 80 [rewriter setDefaultHandlerID:appStoreID]; | 94 [rewriter setDefaultHandlerID:appStoreID]; |
| 81 EXPECT_NSEQ(appStoreID, [rewriter defaultHandlerID]); | 95 EXPECT_NSEQ(appStoreID, [rewriter defaultHandlerID]); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 108 ASSERT_TRUE([otherHandlerID length]); | 122 ASSERT_TRUE([otherHandlerID length]); |
| 109 [rewriter setDefaultHandlerID:otherHandlerID]; | 123 [rewriter setDefaultHandlerID:otherHandlerID]; |
| 110 | 124 |
| 111 // Create a new MailtoURLRewriter object and verify that the current | 125 // Create a new MailtoURLRewriter object and verify that the current |
| 112 // default is the |otherHandlerID| set in the previous step. | 126 // default is the |otherHandlerID| set in the previous step. |
| 113 MailtoURLRewriter* rewriter2 = [[MailtoURLRewriter alloc] init]; | 127 MailtoURLRewriter* rewriter2 = [[MailtoURLRewriter alloc] init]; |
| 114 [rewriter2 addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]]; | 128 [rewriter2 addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]]; |
| 115 EXPECT_NSEQ(otherHandlerID, [rewriter2 defaultHandlerID]); | 129 EXPECT_NSEQ(otherHandlerID, [rewriter2 defaultHandlerID]); |
| 116 } | 130 } |
| 117 | 131 |
| 132 TEST_F(MailtoURLRewriterTest, TestChangeObserver) { | |
| 133 RewriterObserver* observer = [[RewriterObserver alloc] init]; | |
| 134 ASSERT_EQ(0, [observer changeCount]); | |
| 135 | |
| 136 // Sets up a MailtoURLRewriter object. The default handler is Gmail app | |
| 137 // because |fakeGmailHandler| reports that it is "installed". | |
| 138 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init]; | |
| 139 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; | |
| 140 MailtoHandler* fakeGmailHandler = | |
| 141 [[FakeMailtoHandlerGmailInstalled alloc] init]; | |
| 142 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]]; | |
| 143 EXPECT_NSEQ([fakeGmailHandler appStoreID], [rewriter defaultHandlerID]); | |
| 144 [rewriter setObserver:observer]; | |
| 145 | |
| 146 // Setting system Mail app as handler while current handler is Gmail should | |
| 147 // trigger the observer callback, incrementing count from 0 to 1. | |
| 148 [rewriter setDefaultHandlerID:[systemMailHandler appStoreID]]; | |
| 149 EXPECT_EQ(1, [observer changeCount]); | |
| 150 | |
| 151 // Setting system Mail app as handler while current handler is already | |
| 152 // system Mail app should not trigger the observer callback. Count remains | |
| 153 // at 1. | |
| 154 [rewriter setDefaultHandlerID:[systemMailHandler appStoreID]]; | |
| 155 EXPECT_EQ(1, [observer changeCount]); | |
| 156 | |
| 157 // Setting Gmail app as the default handler when current handler is system | |
| 158 // Mail app should trigger the observer callback. Count increases from 1 to 2. | |
| 159 [rewriter setDefaultHandlerID:[fakeGmailHandler appStoreID]]; | |
| 160 EXPECT_EQ(2, [observer changeCount]); | |
|
jif
2017/05/10 09:32:07
Add final test where |observer| is deallocated and
pkl (ping after 24h if needed)
2017/05/10 16:28:06
Done.
| |
| 161 } | |
| 162 | |
| 118 // Tests that a new user without Gmail app installed launches system Mail app. | 163 // Tests that a new user without Gmail app installed launches system Mail app. |
| 119 TEST_F(MailtoURLRewriterTest, TestNewUserNoGmail) { | 164 TEST_F(MailtoURLRewriterTest, TestNewUserNoGmail) { |
| 120 // Sets pre-condition for a user who did not have Chrome installed. | 165 // Sets pre-condition for a user who did not have Chrome installed. |
| 121 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 166 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 122 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; | 167 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; |
| 123 // A faked MailtoHandler for Gmail. | 168 // A faked MailtoHandler for Gmail. |
| 124 MailtoHandler* fakeGmailHandler = | 169 MailtoHandler* fakeGmailHandler = |
| 125 [[FakeMailtoHandlerGmailNotInstalled alloc] init]; | 170 [[FakeMailtoHandlerGmailNotInstalled alloc] init]; |
| 126 | 171 |
| 127 // Sets up a MailtoURLRewriter for testing. | 172 // Sets up a MailtoURLRewriter for testing. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 [[FakeMailtoHandlerGmailInstalled alloc] init]; | 250 [[FakeMailtoHandlerGmailInstalled alloc] init]; |
| 206 | 251 |
| 207 // Sets up a MailtoURLRewriter for testing. | 252 // Sets up a MailtoURLRewriter for testing. |
| 208 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init]; | 253 MailtoURLRewriter* rewriter = [[MailtoURLRewriter alloc] init]; |
| 209 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; | 254 MailtoHandler* systemMailHandler = [[MailtoHandlerSystemMail alloc] init]; |
| 210 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]]; | 255 [rewriter addMailtoApps:@[ systemMailHandler, fakeGmailHandler ]]; |
| 211 | 256 |
| 212 // Verify that MailtoURLRewriter will use Gmail app. | 257 // Verify that MailtoURLRewriter will use Gmail app. |
| 213 EXPECT_NSEQ(kGmailAppStoreID, [rewriter defaultHandlerID]); | 258 EXPECT_NSEQ(kGmailAppStoreID, [rewriter defaultHandlerID]); |
| 214 } | 259 } |
| OLD | NEW |