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

Side by Side Diff: ios/chrome/browser/ui/settings/native_apps_collection_view_controller_unittest.mm

Issue 2806323003: [ObjC ARC] Converts ios/chrome/browser/ui/settings:unit_tests to ARC. (Closed)
Patch Set: rebase Created 3 years, 8 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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/ui/settings/native_apps_collection_view_controller.h " 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" 6 #import "ios/chrome/browser/ui/settings/native_apps_collection_view_controller_p rivate.h"
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #import "base/ios/block_types.h" 11 #import "base/ios/block_types.h"
12 #include "base/test/histogram_tester.h" 12 #include "base/test/histogram_tester.h"
13 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
14 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.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" 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" 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" 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" 18 #include "ios/web/public/test/test_web_thread_bundle.h"
19 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 #import "testing/gtest_mac.h" 21 #import "testing/gtest_mac.h"
22 #import "third_party/ocmock/OCMock/OCMock.h" 22 #import "third_party/ocmock/OCMock/OCMock.h"
23 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
24 24
25 #if !defined(__has_feature) || !__has_feature(objc_arc)
26 #error "This file requires ARC support."
27 #endif
28
25 @interface NativeAppsCollectionViewController (Testing) 29 @interface NativeAppsCollectionViewController (Testing)
26 @property(nonatomic, retain) NSArray* appsInSettings; 30 @property(nonatomic, retain) NSArray* appsInSettings;
27 @property(nonatomic, assign) id<StoreKitLauncher> storeKitLauncher; 31 @property(nonatomic, assign) id<StoreKitLauncher> storeKitLauncher;
28 - (void)configureWithNativeAppWhiteListManager: 32 - (void)configureWithNativeAppWhiteListManager:
29 (id<NativeAppWhitelistManager>)nativeAppWhitelistManager; 33 (id<NativeAppWhitelistManager>)nativeAppWhitelistManager;
30 - (void)autoOpenInAppChanged:(UISwitch*)switchControl; 34 - (void)autoOpenInAppChanged:(UISwitch*)switchControl;
31 - (void)installApp:(UIButton*)button; 35 - (void)installApp:(UIButton*)button;
32 - (void)recordUserAction:(settings::NativeAppsAction)action; 36 - (void)recordUserAction:(settings::NativeAppsAction)action;
33 - (void)appDidInstall:(NSNotification*)note; 37 - (void)appDidInstall:(NSNotification*)note;
34 - (id<NativeAppMetadata>)nativeAppAtIndex:(NSUInteger)index; 38 - (id<NativeAppMetadata>)nativeAppAtIndex:(NSUInteger)index;
35 @end 39 @end
36 40
37 @interface MockNativeAppWhitelistManager : FakeNativeAppWhitelistManager 41 @interface MockNativeAppWhitelistManager : FakeNativeAppWhitelistManager
38 @end 42 @end
39 43
40 @implementation MockNativeAppWhitelistManager 44 @implementation MockNativeAppWhitelistManager
41 45
42 - (id)init { 46 - (id)init {
43 self = [super init]; 47 self = [super init];
44 if (self) { 48 if (self) {
45 base::scoped_nsobject<FakeNativeAppMetadata> app1( 49 FakeNativeAppMetadata* app1 = [[FakeNativeAppMetadata alloc] init];
46 [[FakeNativeAppMetadata alloc] init]);
47 [app1 setAppName:@"App1"]; 50 [app1 setAppName:@"App1"];
48 [app1 setAppId:@"1"]; 51 [app1 setAppId:@"1"];
49 [app1 setGoogleOwnedApp:YES]; 52 [app1 setGoogleOwnedApp:YES];
50 53
51 base::scoped_nsobject<FakeNativeAppMetadata> app2( 54 FakeNativeAppMetadata* app2 = [[FakeNativeAppMetadata alloc] init];
52 [[FakeNativeAppMetadata alloc] init]);
53 [app2 setAppName:@"App2"]; 55 [app2 setAppName:@"App2"];
54 [app2 setAppId:@"2"]; 56 [app2 setAppId:@"2"];
55 [app2 setGoogleOwnedApp:YES]; 57 [app2 setGoogleOwnedApp:YES];
56 58
57 base::scoped_nsobject<FakeNativeAppMetadata> app3( 59 FakeNativeAppMetadata* app3 = [[FakeNativeAppMetadata alloc] init];
58 [[FakeNativeAppMetadata alloc] init]);
59 [app3 setAppName:@"App3"]; 60 [app3 setAppName:@"App3"];
60 [app3 setAppId:@"3"]; 61 [app3 setAppId:@"3"];
61 [app3 setGoogleOwnedApp:YES]; 62 [app3 setGoogleOwnedApp:YES];
62 63
63 base::scoped_nsobject<FakeNativeAppMetadata> notOwnedApp( 64 FakeNativeAppMetadata* notOwnedApp = [[FakeNativeAppMetadata alloc] init];
64 [[FakeNativeAppMetadata alloc] init]);
65 [notOwnedApp setAppName:@"NotOwnedApp"]; 65 [notOwnedApp setAppName:@"NotOwnedApp"];
66 [notOwnedApp setAppId:@"999"]; 66 [notOwnedApp setAppId:@"999"];
67 [notOwnedApp setGoogleOwnedApp:NO]; 67 [notOwnedApp setGoogleOwnedApp:NO];
68 68
69 [self setAppList:@[ app1, app2, notOwnedApp, app3 ] 69 [self setAppList:@[ app1, app2, notOwnedApp, app3 ]
70 tldList:nil 70 tldList:nil
71 acceptStoreIDs:nil]; 71 acceptStoreIDs:nil];
72 } 72 }
73 return self; 73 return self;
74 } 74 }
(...skipping 13 matching lines...) Expand all
88 class NativeAppsCollectionViewControllerTest 88 class NativeAppsCollectionViewControllerTest
89 : public CollectionViewControllerTest { 89 : public CollectionViewControllerTest {
90 protected: 90 protected:
91 void SetUp() override { 91 void SetUp() override {
92 CollectionViewControllerTest::SetUp(); 92 CollectionViewControllerTest::SetUp();
93 request_context_getter_ = new net::TestURLRequestContextGetter( 93 request_context_getter_ = new net::TestURLRequestContextGetter(
94 base::ThreadTaskRunnerHandle::Get()); 94 base::ThreadTaskRunnerHandle::Get());
95 NativeAppsCollectionViewController* native_apps_controller = 95 NativeAppsCollectionViewController* native_apps_controller =
96 static_cast<NativeAppsCollectionViewController*>(controller()); 96 static_cast<NativeAppsCollectionViewController*>(controller());
97 97
98 mock_whitelist_manager_.reset([[MockNativeAppWhitelistManager alloc] init]); 98 mock_whitelist_manager_ = [[MockNativeAppWhitelistManager alloc] init];
99 [native_apps_controller 99 [native_apps_controller
100 configureWithNativeAppWhiteListManager:mock_whitelist_manager_]; 100 configureWithNativeAppWhiteListManager:mock_whitelist_manager_];
101 } 101 }
102 102
103 CollectionViewController* NewController() override NS_RETURNS_RETAINED { 103 CollectionViewController* InstantiateController() override {
104 DCHECK(request_context_getter_.get()); 104 DCHECK(request_context_getter_.get());
105 return [[NativeAppsCollectionViewController alloc] 105 return [[NativeAppsCollectionViewController alloc]
106 initWithURLRequestContextGetter:request_context_getter_.get()]; 106 initWithURLRequestContextGetter:request_context_getter_.get()];
107 } 107 }
108 108
109 // Runs the block and checks that the |action| (and only the action) has been 109 // Runs the block and checks that the |action| (and only the action) has been
110 // recorded. 110 // recorded.
111 void ExpectUserActionAfterBlock(settings::NativeAppsAction action, 111 void ExpectUserActionAfterBlock(settings::NativeAppsAction action,
112 ProceduralBlock block) { 112 ProceduralBlock block) {
113 std::unique_ptr<base::HistogramTester> histogram_tester( 113 std::unique_ptr<base::HistogramTester> histogram_tester(
114 new base::HistogramTester()); 114 new base::HistogramTester());
115 block(); 115 block();
116 histogram_tester->ExpectUniqueSample("NativeAppLauncher.Settings", action, 116 histogram_tester->ExpectUniqueSample("NativeAppLauncher.Settings", action,
117 1); 117 1);
118 } 118 }
119 119
120 // Adds a mocked app of class MockNativeAppMetadata at the end of the apps' 120 // Adds a mocked app of class MockNativeAppMetadata at the end of the apps'
121 // list. 121 // list.
122 void AddMockedApp() { 122 void AddMockedApp() {
123 NativeAppsCollectionViewController* native_apps_controller = 123 NativeAppsCollectionViewController* native_apps_controller =
124 static_cast<NativeAppsCollectionViewController*>(controller()); 124 static_cast<NativeAppsCollectionViewController*>(controller());
125 // Add a mock app at the end of the app list. 125 // Add a mock app at the end of the app list.
126 NSMutableArray* apps = 126 NSMutableArray* apps =
127 [NSMutableArray arrayWithArray:[native_apps_controller appsInSettings]]; 127 [NSMutableArray arrayWithArray:[native_apps_controller appsInSettings]];
128 ASSERT_GT([apps count], 0U); 128 ASSERT_GT([apps count], 0U);
129 base::scoped_nsobject<FakeNativeAppMetadata> installed_app( 129 FakeNativeAppMetadata* installed_app = [[FakeNativeAppMetadata alloc] init];
130 [[FakeNativeAppMetadata alloc] init]);
131 [installed_app setAppName:@"App4"]; 130 [installed_app setAppName:@"App4"];
132 [installed_app setAppId:@"4"]; 131 [installed_app setAppId:@"4"];
133 [installed_app setGoogleOwnedApp:YES]; 132 [installed_app setGoogleOwnedApp:YES];
134 [installed_app resetInfobarHistory]; 133 [installed_app resetInfobarHistory];
135 [installed_app unsetShouldAutoOpenLinks]; 134 [installed_app unsetShouldAutoOpenLinks];
136 [installed_app setInstalled:YES]; 135 [installed_app setInstalled:YES];
137 [apps addObject:installed_app]; 136 [apps addObject:installed_app];
138 [native_apps_controller setAppsInSettings:apps]; 137 [native_apps_controller setAppsInSettings:apps];
139 } 138 }
140 139
(...skipping 12 matching lines...) Expand all
153 EXPECT_EQ(NativeAppItemSwitchOn, item.state); 152 EXPECT_EQ(NativeAppItemSwitchOn, item.state);
154 else 153 else
155 EXPECT_EQ(NativeAppItemSwitchOff, item.state); 154 EXPECT_EQ(NativeAppItemSwitchOff, item.state);
156 } else { 155 } else {
157 EXPECT_EQ(NativeAppItemInstall, item.state); 156 EXPECT_EQ(NativeAppItemInstall, item.state);
158 } 157 }
159 } 158 }
160 159
161 web::TestWebThreadBundle thread_bundle_; 160 web::TestWebThreadBundle thread_bundle_;
162 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 161 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
163 base::scoped_nsobject<MockNativeAppWhitelistManager> mock_whitelist_manager_; 162 MockNativeAppWhitelistManager* mock_whitelist_manager_;
164 }; 163 };
165 164
166 // Tests the integrity of the loaded model: section titles, sections and rows, 165 // 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 166 // along with checking that objects are correctly set up and appear in the
168 // correct order. 167 // correct order.
169 TEST_F(NativeAppsCollectionViewControllerTest, TestModel) { 168 TEST_F(NativeAppsCollectionViewControllerTest, TestModel) {
170 NativeAppsCollectionViewController* native_apps_controller = 169 NativeAppsCollectionViewController* native_apps_controller =
171 static_cast<NativeAppsCollectionViewController*>(controller()); 170 static_cast<NativeAppsCollectionViewController*>(controller());
172 CheckController(); 171 CheckController();
173 EXPECT_EQ(2, NumberOfSections()); 172 EXPECT_EQ(2, NumberOfSections());
(...skipping 12 matching lines...) Expand all
186 NativeAppsCollectionViewController* native_apps_controller = 185 NativeAppsCollectionViewController* native_apps_controller =
187 static_cast<NativeAppsCollectionViewController*>(controller()); 186 static_cast<NativeAppsCollectionViewController*>(controller());
188 AddMockedApp(); 187 AddMockedApp();
189 // Make sure the last app is installed. 188 // Make sure the last app is installed.
190 NSInteger last_index = [[native_apps_controller appsInSettings] count] - 1; 189 NSInteger last_index = [[native_apps_controller appsInSettings] count] - 1;
191 id<NativeAppMetadata> last_app = 190 id<NativeAppMetadata> last_app =
192 [native_apps_controller nativeAppAtIndex:last_index]; 191 [native_apps_controller nativeAppAtIndex:last_index];
193 ASSERT_TRUE([last_app isInstalled]); 192 ASSERT_TRUE([last_app isInstalled]);
194 193
195 EXPECT_FALSE([last_app shouldAutoOpenLinks]); 194 EXPECT_FALSE([last_app shouldAutoOpenLinks]);
196 UISwitch* switch_from_cell = [[[UISwitch alloc] init] autorelease]; 195 UISwitch* switch_from_cell = [[UISwitch alloc] init];
197 switch_from_cell.on = YES; 196 switch_from_cell.on = YES;
198 switch_from_cell.tag = kTagShift + last_index; 197 switch_from_cell.tag = kTagShift + last_index;
199 198
200 ExpectUserActionAfterBlock(settings::kNativeAppsActionTurnedAutoOpenOn, ^{ 199 ExpectUserActionAfterBlock(settings::kNativeAppsActionTurnedAutoOpenOn, ^{
201 [native_apps_controller autoOpenInAppChanged:switch_from_cell]; 200 [native_apps_controller autoOpenInAppChanged:switch_from_cell];
202 EXPECT_TRUE([last_app shouldAutoOpenLinks]); 201 EXPECT_TRUE([last_app shouldAutoOpenLinks]);
203 }); 202 });
204 203
205 switch_from_cell.on = NO; 204 switch_from_cell.on = NO;
206 205
207 ExpectUserActionAfterBlock(settings::kNativeAppsActionTurnedAutoOpenOff, ^{ 206 ExpectUserActionAfterBlock(settings::kNativeAppsActionTurnedAutoOpenOff, ^{
208 [native_apps_controller autoOpenInAppChanged:switch_from_cell]; 207 [native_apps_controller autoOpenInAppChanged:switch_from_cell];
209 EXPECT_FALSE([last_app shouldAutoOpenLinks]); 208 EXPECT_FALSE([last_app shouldAutoOpenLinks]);
210 }); 209 });
211 } 210 }
212 211
213 // Tests that the App Store is launched when the user clicks on an Install 212 // 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. 213 // button. It checks that the appropriate UMA is sent for this user action.
215 TEST_F(NativeAppsCollectionViewControllerTest, InstallApp) { 214 TEST_F(NativeAppsCollectionViewControllerTest, InstallApp) {
216 NativeAppsCollectionViewController* native_apps_controller = 215 NativeAppsCollectionViewController* native_apps_controller =
217 static_cast<NativeAppsCollectionViewController*>(controller()); 216 static_cast<NativeAppsCollectionViewController*>(controller());
218 id<StoreKitLauncher> real_opener = [native_apps_controller storeKitLauncher]; 217 id<StoreKitLauncher> real_opener = [native_apps_controller storeKitLauncher];
219 [native_apps_controller 218 id<StoreKitLauncher> mockLauncher = [[MockStoreKitLauncher alloc] init];
220 setStoreKitLauncher:[[[MockStoreKitLauncher alloc] init] autorelease]]; 219 [native_apps_controller setStoreKitLauncher:mockLauncher];
221 UIButton* button_from_cell = [UIButton buttonWithType:UIButtonTypeCustom]; 220 UIButton* button_from_cell = [UIButton buttonWithType:UIButtonTypeCustom];
222 button_from_cell.tag = kTagShift; 221 button_from_cell.tag = kTagShift;
223 id mock_button = [OCMockObject partialMockForObject:button_from_cell]; 222 id mock_button = [OCMockObject partialMockForObject:button_from_cell];
224 ExpectUserActionAfterBlock(settings::kNativeAppsActionClickedInstall, ^{ 223 ExpectUserActionAfterBlock(settings::kNativeAppsActionClickedInstall, ^{
225 [native_apps_controller installApp:mock_button]; 224 [native_apps_controller installApp:mock_button];
226 }); 225 });
227 226
228 [mock_button verify]; 227 [mock_button verify];
229 228
230 [native_apps_controller setStoreKitLauncher:real_opener]; 229 [native_apps_controller setStoreKitLauncher:real_opener];
(...skipping 12 matching lines...) Expand all
243 242
244 EXPECT_FALSE([last_app shouldAutoOpenLinks]); 243 EXPECT_FALSE([last_app shouldAutoOpenLinks]);
245 [native_apps_controller 244 [native_apps_controller
246 appDidInstall:[NSNotification notificationWithName:@"App4" object:nil]]; 245 appDidInstall:[NSNotification notificationWithName:@"App4" object:nil]];
247 EXPECT_TRUE([last_app shouldAutoOpenLinks]); 246 EXPECT_TRUE([last_app shouldAutoOpenLinks]);
248 247
249 [last_app unsetShouldAutoOpenLinks]; 248 [last_app unsetShouldAutoOpenLinks];
250 } 249 }
251 250
252 } // namespace 251 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698