1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/installation_notifier.h"
#import <UIKit/UIKit.h>
#include "base/ios/block_types.h"
#include "base/mac/scoped_nsobject.h"
#include "base/message_loop/message_loop.h"
#include "base/test/histogram_tester.h"
#include "ios/web/public/test/test_web_thread.h"
#include "net/base/backoff_entry.h"
#include "testing/platform_test.h"
@interface MockDispatcher : NSObject<DispatcherProtocol>
- (int64_t)lastDelayInNSec;
@end
@implementation MockDispatcher {
int _dispatchCount;
int64_t _lastDelayInNSec;
base::scoped_nsobject<NSMutableDictionary> _blocks;
}
- (instancetype)init {
if ((self = [super init]))
_blocks.reset([[NSMutableDictionary alloc] init]);
return self;
}
#pragma mark -
#pragma mark Testing methods
- (void)executeAfter:(int)dispatchCount block:(ProceduralBlock)block {
[_blocks setObject:[[block copy] autorelease]
forKey:[NSNumber numberWithInt:dispatchCount]];
}
- (int64_t)lastDelayInNSec {
return _lastDelayInNSec;
}
#pragma mark -
#pragma mark DispatcherProtocol
- (void)dispatchAfter:(int64_t)delayInNSec withBlock:(dispatch_block_t)block {
_lastDelayInNSec = delayInNSec;
void (^blockToCallForThisIteration)(void) =
[_blocks objectForKey:[NSNumber numberWithInt:_dispatchCount]];
if (blockToCallForThisIteration)
blockToCallForThisIteration();
_dispatchCount++;
block();
}
@end
@interface MockNotificationReceiver : NSObject
@end
@implementation MockNotificationReceiver {
int notificationCount_;
}
- (int)notificationCount {
return notificationCount_;
}
- (void)receivedNotification {
notificationCount_++;
}
@end
@interface MockUIApplication : NSObject
// Mocks UIApplication's canOpenURL.
@end
@implementation MockUIApplication {
BOOL canOpen_;
}
- (void)setCanOpenURL:(BOOL)canOpen {
canOpen_ = canOpen;
}
- (BOOL)canOpenURL:(NSURL*)url {
return canOpen_;
}
@end
@interface InstallationNotifier (Testing)
- (void)setDispatcher:(id<DispatcherProtocol>)dispatcher;
- (void)setSharedApplication:(id)sharedApplication;
- (void)dispatchInstallationNotifierBlock;
- (void)registerForInstallationNotifications:(id)observer
withSelector:(SEL)notificationSelector
forScheme:(NSString*)scheme
startPolling:(BOOL)poll;
- (net::BackoffEntry::Policy const*)backOffPolicy;
@end
namespace {
class InstallationNotifierTest : public PlatformTest {
public:
InstallationNotifierTest() : ui_thread_(web::WebThread::UI, &message_loop_) {}
protected:
void SetUp() override {
installationNotifier_ = [InstallationNotifier sharedInstance];
dispatcher_ = [[MockDispatcher alloc] init];
notificationReceiver1_.reset(([[MockNotificationReceiver alloc] init]));
notificationReceiver2_.reset(([[MockNotificationReceiver alloc] init]));
sharedApplication_.reset([[MockUIApplication alloc] init]);
[installationNotifier_ setSharedApplication:sharedApplication_];
[installationNotifier_ setDispatcher:dispatcher_];
histogramTester_.reset(new base::HistogramTester());
}
void VerifyHistogramValidity(int expectedYes, int expectedNo) {
histogramTester_->ExpectTotalCount("NativeAppLauncher.InstallationDetected",
expectedYes + expectedNo);
histogramTester_->ExpectBucketCount(
"NativeAppLauncher.InstallationDetected", YES, expectedYes);
histogramTester_->ExpectBucketCount(
"NativeAppLauncher.InstallationDetected", NO, expectedNo);
}
void VerifyDelay(int pollingIteration) {
double delayInMSec = [dispatcher_ lastDelayInNSec] / NSEC_PER_MSEC;
double initialDelayInMSec =
[installationNotifier_ backOffPolicy]->initial_delay_ms;
double multiplyFactor =
[installationNotifier_ backOffPolicy]->multiply_factor;
double expectedDelayInMSec =
initialDelayInMSec * pow(multiplyFactor, pollingIteration);
double jitter = [installationNotifier_ backOffPolicy]->jitter_factor;
EXPECT_NEAR(delayInMSec, expectedDelayInMSec,
50 + jitter * expectedDelayInMSec);
}
base::MessageLoopForUI message_loop_;
web::TestWebThread ui_thread_;
InstallationNotifier* installationNotifier_; // Weak pointer to singleton.
MockDispatcher* dispatcher_; // Weak. installationNotifier_ owns it.
base::scoped_nsobject<MockNotificationReceiver> notificationReceiver1_;
base::scoped_nsobject<MockNotificationReceiver> notificationReceiver2_;
base::scoped_nsobject<MockUIApplication> sharedApplication_;
scoped_ptr<base::HistogramTester> histogramTester_;
};
TEST_F(InstallationNotifierTest, RegisterWithAppAlreadyInstalled) {
[sharedApplication_ setCanOpenURL:YES];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"];
EXPECT_EQ(1, [notificationReceiver1_ notificationCount]);
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"];
EXPECT_EQ(2, [notificationReceiver1_ notificationCount]);
VerifyHistogramValidity(2, 0);
}
TEST_F(InstallationNotifierTest, RegisterWithAppInstalledAfterSomeTime) {
[sharedApplication_ setCanOpenURL:NO];
[dispatcher_ executeAfter:10
block:^{
[sharedApplication_ setCanOpenURL:YES];
}];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"];
EXPECT_EQ(1, [notificationReceiver1_ notificationCount]);
VerifyHistogramValidity(1, 0);
}
TEST_F(InstallationNotifierTest, RegisterForTwoInstallations) {
[sharedApplication_ setCanOpenURL:NO];
[dispatcher_ executeAfter:10
block:^{
[sharedApplication_ setCanOpenURL:YES];
}];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"
startPolling:NO];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver2_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"
startPolling:NO];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver2_
withSelector:@selector(receivedNotification)
forScheme:@"bar-scheme"
startPolling:NO];
[installationNotifier_ dispatchInstallationNotifierBlock];
EXPECT_EQ(1, [notificationReceiver1_ notificationCount]);
EXPECT_EQ(2, [notificationReceiver2_ notificationCount]);
VerifyHistogramValidity(2, 0);
}
TEST_F(InstallationNotifierTest, RegisterAndThenUnregister) {
[sharedApplication_ setCanOpenURL:NO];
[dispatcher_ executeAfter:10
block:^{
[installationNotifier_
unregisterForNotifications:notificationReceiver1_];
}];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"];
EXPECT_EQ(0, [notificationReceiver1_ notificationCount]);
VerifyHistogramValidity(0, 1);
}
TEST_F(InstallationNotifierTest, TestExponentialBackoff) {
[sharedApplication_ setCanOpenURL:NO];
// Making sure that delay is multiplied by |multiplyFactor| every time.
[dispatcher_ executeAfter:0
block:^{
VerifyDelay(0);
}];
[dispatcher_ executeAfter:1
block:^{
VerifyDelay(1);
}];
[dispatcher_ executeAfter:2
block:^{
VerifyDelay(2);
}];
// Registering for the installation of another application and making sure
// that the delay is reset to the initial delay.
[dispatcher_ executeAfter:
3 block:^{
VerifyDelay(3);
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"bar-scheme"
startPolling:NO];
}];
[dispatcher_ executeAfter:4
block:^{
VerifyDelay(0);
}];
[dispatcher_ executeAfter:5
block:^{
VerifyDelay(1);
[installationNotifier_
unregisterForNotifications:notificationReceiver1_];
}];
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:@"foo-scheme"];
VerifyHistogramValidity(0, 2);
}
TEST_F(InstallationNotifierTest, TestThatEmptySchemeDoesntCrashChrome) {
[installationNotifier_
registerForInstallationNotifications:notificationReceiver1_
withSelector:@selector(receivedNotification)
forScheme:nil];
[installationNotifier_ unregisterForNotifications:notificationReceiver1_];
}
} // namespace
|