OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/ui/settings/native_apps_collection_view_controller.h
" |
| 6 #import "ios/chrome/browser/ui/settings/native_apps_collection_view_controller_p
rivate.h" |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/ios/block_types.h" |
| 12 #include "base/test/histogram_tester.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 15 #import "ios/chrome/browser/ui/settings/cells/native_app_item.h" |
| 16 #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_
whitelist_manager.h" |
| 18 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 19 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "testing/gtest_mac.h" |
| 22 #import "third_party/ocmock/OCMock/OCMock.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 |
| 25 @interface NativeAppsCollectionViewController (Testing) |
| 26 @property(nonatomic, retain) NSArray* appsInSettings; |
| 27 @property(nonatomic, assign) id<StoreKitLauncher> storeKitLauncher; |
| 28 - (void)configureWithNativeAppWhiteListManager: |
| 29 (id<NativeAppWhitelistManager>)nativeAppWhitelistManager; |
| 30 - (void)autoOpenInAppChanged:(UISwitch*)switchControl; |
| 31 - (void)installApp:(UIButton*)button; |
| 32 - (void)recordUserAction:(settings::NativeAppsAction)action; |
| 33 - (void)appDidInstall:(NSNotification*)note; |
| 34 - (id<NativeAppMetadata>)nativeAppAtIndex:(NSUInteger)index; |
| 35 @end |
| 36 |
| 37 @interface MockNativeAppWhitelistManager : FakeNativeAppWhitelistManager |
| 38 @end |
| 39 |
| 40 @implementation MockNativeAppWhitelistManager |
| 41 |
| 42 - (id)init { |
| 43 self = [super init]; |
| 44 if (self) { |
| 45 base::scoped_nsobject<FakeNativeAppMetadata> app1( |
| 46 [[FakeNativeAppMetadata alloc] init]); |
| 47 [app1 setAppName:@"App1"]; |
| 48 [app1 setAppId:@"1"]; |
| 49 [app1 setGoogleOwnedApp:YES]; |
| 50 |
| 51 base::scoped_nsobject<FakeNativeAppMetadata> app2( |
| 52 [[FakeNativeAppMetadata alloc] init]); |
| 53 [app2 setAppName:@"App2"]; |
| 54 [app2 setAppId:@"2"]; |
| 55 [app2 setGoogleOwnedApp:YES]; |
| 56 |
| 57 base::scoped_nsobject<FakeNativeAppMetadata> app3( |
| 58 [[FakeNativeAppMetadata alloc] init]); |
| 59 [app3 setAppName:@"App3"]; |
| 60 [app3 setAppId:@"3"]; |
| 61 [app3 setGoogleOwnedApp:YES]; |
| 62 |
| 63 base::scoped_nsobject<FakeNativeAppMetadata> notOwnedApp( |
| 64 [[FakeNativeAppMetadata alloc] init]); |
| 65 [notOwnedApp setAppName:@"NotOwnedApp"]; |
| 66 [notOwnedApp setAppId:@"999"]; |
| 67 [notOwnedApp setGoogleOwnedApp:NO]; |
| 68 |
| 69 [self setAppList:@[ app1, app2, notOwnedApp, app3 ] |
| 70 tldList:nil |
| 71 acceptStoreIDs:nil]; |
| 72 } |
| 73 return self; |
| 74 } |
| 75 |
| 76 @end |
| 77 |
| 78 @interface MockStoreKitLauncher : NSObject<StoreKitLauncher> |
| 79 @end |
| 80 |
| 81 @implementation MockStoreKitLauncher |
| 82 - (void)openAppStore:(id<NativeAppMetadata>)metadata { |
| 83 } |
| 84 @end |
| 85 |
| 86 namespace { |
| 87 |
| 88 class NativeAppsCollectionViewControllerTest |
| 89 : public CollectionViewControllerTest { |
| 90 protected: |
| 91 void SetUp() override { |
| 92 CollectionViewControllerTest::SetUp(); |
| 93 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 94 base::ThreadTaskRunnerHandle::Get()); |
| 95 NativeAppsCollectionViewController* native_apps_controller = |
| 96 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 97 |
| 98 mock_whitelist_manager_.reset([[MockNativeAppWhitelistManager alloc] init]); |
| 99 [native_apps_controller |
| 100 configureWithNativeAppWhiteListManager:mock_whitelist_manager_]; |
| 101 } |
| 102 |
| 103 CollectionViewController* NewController() override NS_RETURNS_RETAINED { |
| 104 DCHECK(request_context_getter_.get()); |
| 105 return [[NativeAppsCollectionViewController alloc] |
| 106 initWithURLRequestContextGetter:request_context_getter_.get()]; |
| 107 } |
| 108 |
| 109 // Runs the block and checks that the |action| (and only the action) has been |
| 110 // recorded. |
| 111 void ExpectUserActionAfterBlock(settings::NativeAppsAction action, |
| 112 ProceduralBlock block) { |
| 113 std::unique_ptr<base::HistogramTester> histogram_tester( |
| 114 new base::HistogramTester()); |
| 115 block(); |
| 116 histogram_tester->ExpectUniqueSample("NativeAppLauncher.Settings", action, |
| 117 1); |
| 118 } |
| 119 |
| 120 // Adds a mocked app of class MockNativeAppMetadata at the end of the apps' |
| 121 // list. |
| 122 void AddMockedApp() { |
| 123 NativeAppsCollectionViewController* native_apps_controller = |
| 124 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 125 // Add a mock app at the end of the app list. |
| 126 NSMutableArray* apps = |
| 127 [NSMutableArray arrayWithArray:[native_apps_controller appsInSettings]]; |
| 128 ASSERT_GT([apps count], 0U); |
| 129 base::scoped_nsobject<FakeNativeAppMetadata> installed_app( |
| 130 [[FakeNativeAppMetadata alloc] init]); |
| 131 [installed_app setAppName:@"App4"]; |
| 132 [installed_app setAppId:@"4"]; |
| 133 [installed_app setGoogleOwnedApp:YES]; |
| 134 [installed_app resetInfobarHistory]; |
| 135 [installed_app unsetShouldAutoOpenLinks]; |
| 136 [installed_app setInstalled:YES]; |
| 137 [apps addObject:installed_app]; |
| 138 [native_apps_controller setAppsInSettings:apps]; |
| 139 } |
| 140 |
| 141 // Checks that the object's state is consistent with the represented app |
| 142 // metadata at |index| in |section|. |
| 143 void CheckNativeAppItem(NSUInteger section, NSUInteger index) { |
| 144 NativeAppsCollectionViewController* native_apps_controller = |
| 145 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 146 id<NativeAppMetadata> metadata = |
| 147 [native_apps_controller nativeAppAtIndex:index]; |
| 148 NativeAppItem* item = GetCollectionViewItem(section, index); |
| 149 EXPECT_TRUE(item); |
| 150 EXPECT_NSEQ([metadata appName], item.name); |
| 151 if ([metadata isInstalled]) { |
| 152 if ([metadata shouldAutoOpenLinks]) |
| 153 EXPECT_EQ(NativeAppItemSwitchOn, item.state); |
| 154 else |
| 155 EXPECT_EQ(NativeAppItemSwitchOff, item.state); |
| 156 } else { |
| 157 EXPECT_EQ(NativeAppItemInstall, item.state); |
| 158 } |
| 159 } |
| 160 |
| 161 web::TestWebThreadBundle thread_bundle_; |
| 162 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 163 base::scoped_nsobject<MockNativeAppWhitelistManager> mock_whitelist_manager_; |
| 164 }; |
| 165 |
| 166 // Tests the integrity of the loaded model: section titles, sections and rows, |
| 167 // along with checking that objects are correctly set up and appear in the |
| 168 // correct order. |
| 169 TEST_F(NativeAppsCollectionViewControllerTest, TestModel) { |
| 170 NativeAppsCollectionViewController* native_apps_controller = |
| 171 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 172 CheckController(); |
| 173 EXPECT_EQ(2, NumberOfSections()); |
| 174 |
| 175 // As many rows as there are apps. |
| 176 NSInteger apps_count = [[native_apps_controller appsInSettings] count]; |
| 177 EXPECT_EQ(apps_count, NumberOfItemsInSection(1)); |
| 178 for (NSInteger i = 0; i < apps_count; i++) { |
| 179 CheckNativeAppItem(1, i); |
| 180 } |
| 181 } |
| 182 |
| 183 // Tests that native app metadata are correctly updated when the user toggles |
| 184 // switches. It checks that the appropriate UMA is sent for this user action. |
| 185 TEST_F(NativeAppsCollectionViewControllerTest, AutoOpenInAppChanged) { |
| 186 NativeAppsCollectionViewController* native_apps_controller = |
| 187 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 188 AddMockedApp(); |
| 189 // Make sure the last app is installed. |
| 190 NSInteger last_index = [[native_apps_controller appsInSettings] count] - 1; |
| 191 id<NativeAppMetadata> last_app = |
| 192 [native_apps_controller nativeAppAtIndex:last_index]; |
| 193 ASSERT_TRUE([last_app isInstalled]); |
| 194 |
| 195 EXPECT_FALSE([last_app shouldAutoOpenLinks]); |
| 196 UISwitch* switch_from_cell = [[[UISwitch alloc] init] autorelease]; |
| 197 switch_from_cell.on = YES; |
| 198 switch_from_cell.tag = kTagShift + last_index; |
| 199 |
| 200 ExpectUserActionAfterBlock(settings::kNativeAppsActionTurnedAutoOpenOn, ^{ |
| 201 [native_apps_controller autoOpenInAppChanged:switch_from_cell]; |
| 202 EXPECT_TRUE([last_app shouldAutoOpenLinks]); |
| 203 }); |
| 204 |
| 205 switch_from_cell.on = NO; |
| 206 |
| 207 ExpectUserActionAfterBlock(settings::kNativeAppsActionTurnedAutoOpenOff, ^{ |
| 208 [native_apps_controller autoOpenInAppChanged:switch_from_cell]; |
| 209 EXPECT_FALSE([last_app shouldAutoOpenLinks]); |
| 210 }); |
| 211 } |
| 212 |
| 213 // Tests that the App Store is launched when the user clicks on an Install |
| 214 // button. It checks that the appropriate UMA is sent for this user action. |
| 215 TEST_F(NativeAppsCollectionViewControllerTest, InstallApp) { |
| 216 NativeAppsCollectionViewController* native_apps_controller = |
| 217 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 218 id<StoreKitLauncher> real_opener = [native_apps_controller storeKitLauncher]; |
| 219 [native_apps_controller |
| 220 setStoreKitLauncher:[[[MockStoreKitLauncher alloc] init] autorelease]]; |
| 221 UIButton* button_from_cell = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 222 button_from_cell.tag = kTagShift; |
| 223 id mock_button = [OCMockObject partialMockForObject:button_from_cell]; |
| 224 ExpectUserActionAfterBlock(settings::kNativeAppsActionClickedInstall, ^{ |
| 225 [native_apps_controller installApp:mock_button]; |
| 226 }); |
| 227 |
| 228 [mock_button verify]; |
| 229 |
| 230 [native_apps_controller setStoreKitLauncher:real_opener]; |
| 231 } |
| 232 |
| 233 // Tests that native app metadata are correctly updated when the related app has |
| 234 // been installed. |
| 235 TEST_F(NativeAppsCollectionViewControllerTest, AppDidInstall) { |
| 236 NativeAppsCollectionViewController* native_apps_controller = |
| 237 static_cast<NativeAppsCollectionViewController*>(controller()); |
| 238 AddMockedApp(); |
| 239 // Make sure the last app can open any URL. |
| 240 id<NativeAppMetadata> last_app = |
| 241 [[native_apps_controller appsInSettings] lastObject]; |
| 242 ASSERT_TRUE([last_app canOpenURL:GURL()]); |
| 243 |
| 244 EXPECT_FALSE([last_app shouldAutoOpenLinks]); |
| 245 [native_apps_controller |
| 246 appDidInstall:[NSNotification notificationWithName:@"App4" object:nil]]; |
| 247 EXPECT_TRUE([last_app shouldAutoOpenLinks]); |
| 248 |
| 249 [last_app unsetShouldAutoOpenLinks]; |
| 250 } |
| 251 |
| 252 } // namespace |
OLD | NEW |