| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/mac/scoped_nsobject.h" | |
| 9 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/user_metrics.h" | 9 #include "base/metrics/user_metrics.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 12 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 11 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 13 #import "ios/chrome/browser/installation_notifier.h" | 12 #import "ios/chrome/browser/installation_notifier.h" |
| 14 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h" | 13 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h" |
| 15 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller
.h" | 14 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller
.h" |
| 16 #import "ios/chrome/browser/web/chrome_web_test.h" | 15 #import "ios/chrome/browser/web/chrome_web_test.h" |
| 17 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h" | 16 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h" |
| 18 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_
metadata.h" | 17 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_
metadata.h" |
| 19 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_
whitelist_manager.h" | 18 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_
whitelist_manager.h" |
| 20 #include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h" | 19 #include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h" |
| 21 #import "testing/gtest_mac.h" | 20 #import "testing/gtest_mac.h" |
| 22 | 21 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." |
| 24 #endif |
| 25 |
| 23 @interface NativeAppNavigationController (Testing) | 26 @interface NativeAppNavigationController (Testing) |
| 24 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type | 27 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type |
| 25 onLinkNavigation:(BOOL)isLinkNavigation; | 28 onLinkNavigation:(BOOL)isLinkNavigation; |
| 26 - (NSMutableSet*)appsPossiblyBeingInstalled; | 29 - (NSMutableSet*)appsPossiblyBeingInstalled; |
| 27 - (void)removeAppFromNotification:(NSNotification*)notification; | 30 - (void)removeAppFromNotification:(NSNotification*)notification; |
| 28 @end | 31 @end |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider { | 35 class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider { |
| 33 public: | 36 public: |
| 34 FakeChromeBrowserProvider(FakeNativeAppWhitelistManager* fake_manager) { | 37 FakeChromeBrowserProvider(FakeNativeAppWhitelistManager* fake_manager) { |
| 35 manager_.reset([fake_manager retain]); | 38 manager_ = fake_manager; |
| 36 } | 39 } |
| 37 ~FakeChromeBrowserProvider() override {} | 40 ~FakeChromeBrowserProvider() override {} |
| 38 | 41 |
| 39 id<NativeAppWhitelistManager> GetNativeAppWhitelistManager() const override { | 42 id<NativeAppWhitelistManager> GetNativeAppWhitelistManager() const override { |
| 40 return manager_; | 43 return manager_; |
| 41 } | 44 } |
| 42 | 45 |
| 43 private: | 46 private: |
| 44 base::scoped_nsprotocol<id<NativeAppWhitelistManager>> manager_; | 47 id<NativeAppWhitelistManager> manager_; |
| 45 }; | 48 }; |
| 46 | 49 |
| 47 class NativeAppNavigationControllerTest : public ChromeWebTest { | 50 class NativeAppNavigationControllerTest : public ChromeWebTest { |
| 48 protected: | 51 protected: |
| 49 void SetUp() override { | 52 void SetUp() override { |
| 50 ChromeWebTest::SetUp(); | 53 ChromeWebTest::SetUp(); |
| 51 controller_.reset( | 54 controller_ = |
| 52 [[NativeAppNavigationController alloc] initWithWebState:web_state()]); | 55 [[NativeAppNavigationController alloc] initWithWebState:web_state()]; |
| 53 | 56 |
| 54 action_callback_ = | 57 action_callback_ = |
| 55 base::Bind(&NativeAppNavigationControllerTest::OnUserAction, | 58 base::Bind(&NativeAppNavigationControllerTest::OnUserAction, |
| 56 base::Unretained(this)); | 59 base::Unretained(this)); |
| 57 base::AddActionCallback(action_callback_); | 60 base::AddActionCallback(action_callback_); |
| 58 | 61 |
| 59 handler_called_counter_ = 0; | 62 handler_called_counter_ = 0; |
| 60 } | 63 } |
| 61 | 64 |
| 62 void TearDown() override { | 65 void TearDown() override { |
| 63 base::RemoveActionCallback(action_callback_); | 66 base::RemoveActionCallback(action_callback_); |
| 64 ChromeWebTest::TearDown(); | 67 ChromeWebTest::TearDown(); |
| 65 } | 68 } |
| 66 | 69 |
| 67 void SetExpectedActionName(const std::string& action_name) { | 70 void SetExpectedActionName(const std::string& action_name) { |
| 68 expected_action_name_.reset(new std::string(action_name)); | 71 expected_action_name_.reset(new std::string(action_name)); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void OnUserAction(const std::string& action_name) { | 74 void OnUserAction(const std::string& action_name) { |
| 72 EXPECT_EQ(*expected_action_name_, action_name); | 75 EXPECT_EQ(*expected_action_name_, action_name); |
| 73 handler_called_counter_++; | 76 handler_called_counter_++; |
| 74 } | 77 } |
| 75 | 78 |
| 76 void ExpectHandlerCalledAndReset(int number_of_calls) { | 79 void ExpectHandlerCalledAndReset(int number_of_calls) { |
| 77 EXPECT_EQ(number_of_calls, handler_called_counter_); | 80 EXPECT_EQ(number_of_calls, handler_called_counter_); |
| 78 handler_called_counter_ = 0; | 81 handler_called_counter_ = 0; |
| 79 } | 82 } |
| 80 | 83 |
| 81 base::scoped_nsobject<NativeAppNavigationController> controller_; | 84 NativeAppNavigationController* controller_; |
| 82 | 85 |
| 83 // The callback to invoke when an action is recorded. | 86 // The callback to invoke when an action is recorded. |
| 84 base::ActionCallback action_callback_; | 87 base::ActionCallback action_callback_; |
| 85 std::unique_ptr<std::string> expected_action_name_; | 88 std::unique_ptr<std::string> expected_action_name_; |
| 86 int handler_called_counter_; | 89 int handler_called_counter_; |
| 87 }; | 90 }; |
| 88 | 91 |
| 89 TEST_F(NativeAppNavigationControllerTest, TestConstructor) { | 92 TEST_F(NativeAppNavigationControllerTest, TestConstructor) { |
| 90 EXPECT_TRUE(controller_); | 93 EXPECT_TRUE(controller_); |
| 91 } | 94 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 118 ExpectHandlerCalledAndReset(1); | 121 ExpectHandlerCalledAndReset(1); |
| 119 } | 122 } |
| 120 | 123 |
| 121 // Tests presenting NativeAppInfoBar after page is loaded. | 124 // Tests presenting NativeAppInfoBar after page is loaded. |
| 122 TEST_F(NativeAppNavigationControllerTest, NativeAppInfoBar) { | 125 TEST_F(NativeAppNavigationControllerTest, NativeAppInfoBar) { |
| 123 SetExpectedActionName("MobileGALInstallInfoBarDirectNavigation"); | 126 SetExpectedActionName("MobileGALInstallInfoBarDirectNavigation"); |
| 124 InfoBarManagerImpl::CreateForWebState(web_state()); | 127 InfoBarManagerImpl::CreateForWebState(web_state()); |
| 125 | 128 |
| 126 // Set up fake metadata. | 129 // Set up fake metadata. |
| 127 FakeNativeAppWhitelistManager* fakeManager = | 130 FakeNativeAppWhitelistManager* fakeManager = |
| 128 [[[FakeNativeAppWhitelistManager alloc] init] autorelease]; | 131 [[FakeNativeAppWhitelistManager alloc] init]; |
| 129 IOSChromeScopedTestingChromeBrowserProvider provider( | 132 IOSChromeScopedTestingChromeBrowserProvider provider( |
| 130 base::MakeUnique<FakeChromeBrowserProvider>(fakeManager)); | 133 base::MakeUnique<FakeChromeBrowserProvider>(fakeManager)); |
| 131 FakeNativeAppMetadata* metadata = | 134 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; |
| 132 [[[FakeNativeAppMetadata alloc] init] autorelease]; | |
| 133 fakeManager.metadata = metadata; | 135 fakeManager.metadata = metadata; |
| 134 metadata.appName = @"App"; | 136 metadata.appName = @"App"; |
| 135 metadata.appId = @"App-ID"; | 137 metadata.appId = @"App-ID"; |
| 136 | 138 |
| 137 // Load the page to trigger infobar presentation. | 139 // Load the page to trigger infobar presentation. |
| 138 LoadHtml(@"<html><body></body></html>", GURL("http://test.com")); | 140 LoadHtml(@"<html><body></body></html>", GURL("http://test.com")); |
| 139 | 141 |
| 140 // Verify that infobar was presented | 142 // Verify that infobar was presented |
| 141 auto* infobar_manager = InfoBarManagerImpl::FromWebState(web_state()); | 143 auto* infobar_manager = InfoBarManagerImpl::FromWebState(web_state()); |
| 142 ASSERT_EQ(1U, infobar_manager->infobar_count()); | 144 ASSERT_EQ(1U, infobar_manager->infobar_count()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 155 EXPECT_NSEQ(@"App-ID", delegate->GetAppId()); | 157 EXPECT_NSEQ(@"App-ID", delegate->GetAppId()); |
| 156 } | 158 } |
| 157 | 159 |
| 158 TEST_F(NativeAppNavigationControllerTest, | 160 TEST_F(NativeAppNavigationControllerTest, |
| 159 TestRemovingAppFromListAfterInstallation) { | 161 TestRemovingAppFromListAfterInstallation) { |
| 160 NSString* const kMapsAppName = @"Maps"; | 162 NSString* const kMapsAppName = @"Maps"; |
| 161 NSString* const kMapsAppId = @"1"; | 163 NSString* const kMapsAppId = @"1"; |
| 162 NSString* const kYoutubeAppName = @"Youtube"; | 164 NSString* const kYoutubeAppName = @"Youtube"; |
| 163 NSString* const kYoutubeAppId = @"2"; | 165 NSString* const kYoutubeAppId = @"2"; |
| 164 | 166 |
| 165 base::scoped_nsobject<InstallationNotifier> installationNotifier( | 167 InstallationNotifier* installationNotifier = |
| 166 [[InstallationNotifier alloc] init]); | 168 [[InstallationNotifier alloc] init]; |
| 167 | 169 |
| 168 FakeNativeAppWhitelistManager* fakeManager = | 170 FakeNativeAppWhitelistManager* fakeManager = |
| 169 [[[FakeNativeAppWhitelistManager alloc] init] autorelease]; | 171 [[FakeNativeAppWhitelistManager alloc] init]; |
| 170 IOSChromeScopedTestingChromeBrowserProvider provider( | 172 IOSChromeScopedTestingChromeBrowserProvider provider( |
| 171 base::MakeUnique<FakeChromeBrowserProvider>(fakeManager)); | 173 base::MakeUnique<FakeChromeBrowserProvider>(fakeManager)); |
| 172 | 174 |
| 173 base::scoped_nsobject<FakeNativeAppMetadata> metadataMaps( | 175 FakeNativeAppMetadata* metadataMaps = [[FakeNativeAppMetadata alloc] init]; |
| 174 [[FakeNativeAppMetadata alloc] init]); | |
| 175 [metadataMaps setAppName:kMapsAppName]; | 176 [metadataMaps setAppName:kMapsAppName]; |
| 176 [metadataMaps setAppId:kMapsAppId]; | 177 [metadataMaps setAppId:kMapsAppId]; |
| 177 ASSERT_TRUE(metadataMaps.get()); | 178 ASSERT_TRUE(metadataMaps); |
| 178 NSString* appIdMaps = [metadataMaps appId]; | 179 NSString* appIdMaps = [metadataMaps appId]; |
| 179 NSNotification* notificationMaps = | 180 NSNotification* notificationMaps = |
| 180 [NSNotification notificationWithName:kMapsAppName | 181 [NSNotification notificationWithName:kMapsAppName |
| 181 object:installationNotifier]; | 182 object:installationNotifier]; |
| 182 | 183 |
| 183 base::scoped_nsobject<FakeNativeAppMetadata> metadataYouTube( | 184 FakeNativeAppMetadata* metadataYouTube = [[FakeNativeAppMetadata alloc] init]; |
| 184 [[FakeNativeAppMetadata alloc] init]); | |
| 185 [metadataYouTube setAppName:kYoutubeAppName]; | 185 [metadataYouTube setAppName:kYoutubeAppName]; |
| 186 [metadataYouTube setAppId:kYoutubeAppId]; | 186 [metadataYouTube setAppId:kYoutubeAppId]; |
| 187 | 187 |
| 188 ASSERT_TRUE(metadataYouTube.get()); | 188 ASSERT_TRUE(metadataYouTube); |
| 189 NSString* appIdYouTube = [metadataYouTube appId]; | 189 NSString* appIdYouTube = [metadataYouTube appId]; |
| 190 NSNotification* notificationYouTube = | 190 NSNotification* notificationYouTube = |
| 191 [NSNotification notificationWithName:kYoutubeAppName | 191 [NSNotification notificationWithName:kYoutubeAppName |
| 192 object:installationNotifier]; | 192 object:installationNotifier]; |
| 193 | 193 |
| 194 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 0); | 194 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 0); |
| 195 [[controller_ appsPossiblyBeingInstalled] addObject:appIdMaps]; | 195 [[controller_ appsPossiblyBeingInstalled] addObject:appIdMaps]; |
| 196 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 1); | 196 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 1); |
| 197 [[controller_ appsPossiblyBeingInstalled] addObject:appIdYouTube]; | 197 [[controller_ appsPossiblyBeingInstalled] addObject:appIdYouTube]; |
| 198 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 2); | 198 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 2); |
| 199 [fakeManager setAppScheme:kMapsAppName]; | 199 [fakeManager setAppScheme:kMapsAppName]; |
| 200 [controller_ removeAppFromNotification:notificationMaps]; | 200 [controller_ removeAppFromNotification:notificationMaps]; |
| 201 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 1); | 201 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 1); |
| 202 [fakeManager setAppScheme:kYoutubeAppName]; | 202 [fakeManager setAppScheme:kYoutubeAppName]; |
| 203 [controller_ removeAppFromNotification:notificationYouTube]; | 203 [controller_ removeAppFromNotification:notificationYouTube]; |
| 204 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 0); | 204 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 0); |
| 205 } | 205 } |
| 206 | 206 |
| 207 } // namespace | 207 } // namespace |
| OLD | NEW |