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

Side by Side Diff: ios/chrome/browser/installation_notifier_unittest.mm

Issue 2684263003: [ObjC ARC] Converts ios/chrome/browser:unit_tests to ARC. (Closed)
Patch Set: comments Created 3 years, 10 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 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 #import "ios/chrome/browser/installation_notifier.h" 5 #import "ios/chrome/browser/installation_notifier.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #import <UIKit/UIKit.h> 8 #import <UIKit/UIKit.h>
9 9
10 #include "base/ios/block_types.h" 10 #include "base/ios/block_types.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
13 #include "base/test/histogram_tester.h" 12 #include "base/test/histogram_tester.h"
14 #include "ios/web/public/test/test_web_thread.h" 13 #include "ios/web/public/test/test_web_thread.h"
15 #include "net/base/backoff_entry.h" 14 #include "net/base/backoff_entry.h"
16 #include "testing/platform_test.h" 15 #include "testing/platform_test.h"
17 16
18 @interface MockDispatcher : NSObject<DispatcherProtocol> 17 #if !defined(__has_feature) || !__has_feature(objc_arc)
18 #error "This file requires ARC support."
19 #endif
20
21 @interface FakeDispatcher : NSObject<DispatcherProtocol>
19 - (int64_t)lastDelayInNSec; 22 - (int64_t)lastDelayInNSec;
20 @end 23 @end
21 24
22 @implementation MockDispatcher { 25 @implementation FakeDispatcher {
23 int _dispatchCount; 26 int _dispatchCount;
24 int64_t _lastDelayInNSec; 27 int64_t _lastDelayInNSec;
25 base::scoped_nsobject<NSMutableDictionary> _blocks; 28 NSMutableDictionary* _blocks;
26 } 29 }
27 30
28 - (instancetype)init { 31 - (instancetype)init {
29 if ((self = [super init])) 32 if ((self = [super init]))
30 _blocks.reset([[NSMutableDictionary alloc] init]); 33 _blocks = [[NSMutableDictionary alloc] init];
31 return self; 34 return self;
32 } 35 }
33 36
34 #pragma mark - 37 #pragma mark -
35 #pragma mark Testing methods 38 #pragma mark Testing methods
36 39
37 - (void)executeAfter:(int)dispatchCount block:(ProceduralBlock)block { 40 - (void)executeAfter:(int)dispatchCount block:(ProceduralBlock)block {
38 [_blocks setObject:[[block copy] autorelease] 41 [_blocks setObject:[block copy]
39 forKey:[NSNumber numberWithInt:dispatchCount]]; 42 forKey:[NSNumber numberWithInt:dispatchCount]];
40 } 43 }
41 44
42 - (int64_t)lastDelayInNSec { 45 - (int64_t)lastDelayInNSec {
43 return _lastDelayInNSec; 46 return _lastDelayInNSec;
44 } 47 }
45 48
46 #pragma mark - 49 #pragma mark -
47 #pragma mark DispatcherProtocol 50 #pragma mark DispatcherProtocol
48 51
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 109
107 namespace { 110 namespace {
108 111
109 class InstallationNotifierTest : public PlatformTest { 112 class InstallationNotifierTest : public PlatformTest {
110 public: 113 public:
111 InstallationNotifierTest() : ui_thread_(web::WebThread::UI, &message_loop_) {} 114 InstallationNotifierTest() : ui_thread_(web::WebThread::UI, &message_loop_) {}
112 115
113 protected: 116 protected:
114 void SetUp() override { 117 void SetUp() override {
115 installationNotifier_ = [InstallationNotifier sharedInstance]; 118 installationNotifier_ = [InstallationNotifier sharedInstance];
116 dispatcher_ = [[MockDispatcher alloc] init]; 119 FakeDispatcher* dispatcher = [[FakeDispatcher alloc] init];
117 notificationReceiver1_.reset(([[MockNotificationReceiver alloc] init])); 120 dispatcher_ = dispatcher;
118 notificationReceiver2_.reset(([[MockNotificationReceiver alloc] init])); 121 notificationReceiver1_ = ([[MockNotificationReceiver alloc] init]);
119 sharedApplication_.reset([[MockUIApplication alloc] init]); 122 notificationReceiver2_ = ([[MockNotificationReceiver alloc] init]);
123 sharedApplication_ = [[MockUIApplication alloc] init];
120 [installationNotifier_ setSharedApplication:sharedApplication_]; 124 [installationNotifier_ setSharedApplication:sharedApplication_];
121 [installationNotifier_ setDispatcher:dispatcher_]; 125 [installationNotifier_ setDispatcher:dispatcher_];
122 histogramTester_.reset(new base::HistogramTester()); 126 histogramTester_.reset(new base::HistogramTester());
123 } 127 }
124 128
125 void VerifyHistogramValidity(int expectedYes, int expectedNo) { 129 void VerifyHistogramValidity(int expectedYes, int expectedNo) {
126 histogramTester_->ExpectTotalCount("NativeAppLauncher.InstallationDetected", 130 histogramTester_->ExpectTotalCount("NativeAppLauncher.InstallationDetected",
127 expectedYes + expectedNo); 131 expectedYes + expectedNo);
128 histogramTester_->ExpectBucketCount( 132 histogramTester_->ExpectBucketCount(
129 "NativeAppLauncher.InstallationDetected", YES, expectedYes); 133 "NativeAppLauncher.InstallationDetected", YES, expectedYes);
130 histogramTester_->ExpectBucketCount( 134 histogramTester_->ExpectBucketCount(
131 "NativeAppLauncher.InstallationDetected", NO, expectedNo); 135 "NativeAppLauncher.InstallationDetected", NO, expectedNo);
132 } 136 }
133 137
134 void VerifyDelay(int pollingIteration) { 138 void VerifyDelay(int pollingIteration) {
135 double delayInMSec = [dispatcher_ lastDelayInNSec] / NSEC_PER_MSEC; 139 double delayInMSec = [dispatcher_ lastDelayInNSec] / NSEC_PER_MSEC;
136 double initialDelayInMSec = 140 double initialDelayInMSec =
137 [installationNotifier_ backOffPolicy]->initial_delay_ms; 141 [installationNotifier_ backOffPolicy]->initial_delay_ms;
138 double multiplyFactor = 142 double multiplyFactor =
139 [installationNotifier_ backOffPolicy]->multiply_factor; 143 [installationNotifier_ backOffPolicy]->multiply_factor;
140 double expectedDelayInMSec = 144 double expectedDelayInMSec =
141 initialDelayInMSec * pow(multiplyFactor, pollingIteration); 145 initialDelayInMSec * pow(multiplyFactor, pollingIteration);
142 double jitter = [installationNotifier_ backOffPolicy]->jitter_factor; 146 double jitter = [installationNotifier_ backOffPolicy]->jitter_factor;
143 EXPECT_NEAR(delayInMSec, expectedDelayInMSec, 147 EXPECT_NEAR(delayInMSec, expectedDelayInMSec,
144 50 + jitter * expectedDelayInMSec); 148 50 + jitter * expectedDelayInMSec);
145 } 149 }
146 150
147 base::MessageLoopForUI message_loop_; 151 base::MessageLoopForUI message_loop_;
148 web::TestWebThread ui_thread_; 152 web::TestWebThread ui_thread_;
149 __unsafe_unretained InstallationNotifier* 153 __weak InstallationNotifier* installationNotifier_;
150 installationNotifier_; // Weak pointer to singleton. 154 __weak FakeDispatcher* dispatcher_;
151 __unsafe_unretained MockDispatcher* 155 MockNotificationReceiver* notificationReceiver1_;
152 dispatcher_; // Weak. installationNotifier_ owns it. 156 MockNotificationReceiver* notificationReceiver2_;
153 base::scoped_nsobject<MockNotificationReceiver> notificationReceiver1_; 157 MockUIApplication* sharedApplication_;
154 base::scoped_nsobject<MockNotificationReceiver> notificationReceiver2_;
155 base::scoped_nsobject<MockUIApplication> sharedApplication_;
156 std::unique_ptr<base::HistogramTester> histogramTester_; 158 std::unique_ptr<base::HistogramTester> histogramTester_;
157 }; 159 };
158 160
159 TEST_F(InstallationNotifierTest, RegisterWithAppAlreadyInstalled) { 161 TEST_F(InstallationNotifierTest, RegisterWithAppAlreadyInstalled) {
160 [sharedApplication_ setCanOpenURL:YES]; 162 [sharedApplication_ setCanOpenURL:YES];
161 [installationNotifier_ 163 [installationNotifier_
162 registerForInstallationNotifications:notificationReceiver1_ 164 registerForInstallationNotifications:notificationReceiver1_
163 withSelector:@selector(receivedNotification) 165 withSelector:@selector(receivedNotification)
164 forScheme:@"foo-scheme"]; 166 forScheme:@"foo-scheme"];
165 EXPECT_EQ(1, [notificationReceiver1_ notificationCount]); 167 EXPECT_EQ(1, [notificationReceiver1_ notificationCount]);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 275
274 TEST_F(InstallationNotifierTest, TestThatEmptySchemeDoesntCrashChrome) { 276 TEST_F(InstallationNotifierTest, TestThatEmptySchemeDoesntCrashChrome) {
275 [installationNotifier_ 277 [installationNotifier_
276 registerForInstallationNotifications:notificationReceiver1_ 278 registerForInstallationNotifications:notificationReceiver1_
277 withSelector:@selector(receivedNotification) 279 withSelector:@selector(receivedNotification)
278 forScheme:nil]; 280 forScheme:nil];
279 [installationNotifier_ unregisterForNotifications:notificationReceiver1_]; 281 [installationNotifier_ unregisterForNotifications:notificationReceiver1_];
280 } 282 }
281 283
282 } // namespace 284 } // namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/install_time_util_unittest.mm ('k') | ios/chrome/browser/ios_chrome_io_thread_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698