Chromium Code Reviews| 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 #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 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 18 #error "This file requires ARC support." | |
| 19 #endif | |
| 20 | |
| 18 @interface MockDispatcher : NSObject<DispatcherProtocol> | 21 @interface MockDispatcher : NSObject<DispatcherProtocol> |
| 19 - (int64_t)lastDelayInNSec; | 22 - (int64_t)lastDelayInNSec; |
| 20 @end | 23 @end |
| 21 | 24 |
| 22 @implementation MockDispatcher { | 25 @implementation MockDispatcher { |
|
sdefresne
2017/02/13 14:21:23
This is not a mock, this is a fake :-(
lody
2017/02/13 15:40:10
Done.
| |
| 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 Loading... | |
| 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 MockDispatcher* dispatcher = [[MockDispatcher 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* |
| 150 installationNotifier_; // Weak pointer to singleton. | 154 installationNotifier_; // Weak pointer to singleton. |
|
sdefresne
2017/02/13 14:21:23
You can probably remove those comments now (since
lody
2017/02/13 15:40:10
Done.
| |
| 151 __unsafe_unretained MockDispatcher* | 155 __weak MockDispatcher* dispatcher_; // Weak. installationNotifier_ owns it. |
| 152 dispatcher_; // Weak. installationNotifier_ owns it. | 156 MockNotificationReceiver* notificationReceiver1_; |
| 153 base::scoped_nsobject<MockNotificationReceiver> notificationReceiver1_; | 157 MockNotificationReceiver* notificationReceiver2_; |
| 154 base::scoped_nsobject<MockNotificationReceiver> notificationReceiver2_; | 158 MockUIApplication* sharedApplication_; |
| 155 base::scoped_nsobject<MockUIApplication> sharedApplication_; | |
| 156 std::unique_ptr<base::HistogramTester> histogramTester_; | 159 std::unique_ptr<base::HistogramTester> histogramTester_; |
| 157 }; | 160 }; |
| 158 | 161 |
| 159 TEST_F(InstallationNotifierTest, RegisterWithAppAlreadyInstalled) { | 162 TEST_F(InstallationNotifierTest, RegisterWithAppAlreadyInstalled) { |
| 160 [sharedApplication_ setCanOpenURL:YES]; | 163 [sharedApplication_ setCanOpenURL:YES]; |
| 161 [installationNotifier_ | 164 [installationNotifier_ |
| 162 registerForInstallationNotifications:notificationReceiver1_ | 165 registerForInstallationNotifications:notificationReceiver1_ |
| 163 withSelector:@selector(receivedNotification) | 166 withSelector:@selector(receivedNotification) |
| 164 forScheme:@"foo-scheme"]; | 167 forScheme:@"foo-scheme"]; |
| 165 EXPECT_EQ(1, [notificationReceiver1_ notificationCount]); | 168 EXPECT_EQ(1, [notificationReceiver1_ notificationCount]); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 | 276 |
| 274 TEST_F(InstallationNotifierTest, TestThatEmptySchemeDoesntCrashChrome) { | 277 TEST_F(InstallationNotifierTest, TestThatEmptySchemeDoesntCrashChrome) { |
| 275 [installationNotifier_ | 278 [installationNotifier_ |
| 276 registerForInstallationNotifications:notificationReceiver1_ | 279 registerForInstallationNotifications:notificationReceiver1_ |
| 277 withSelector:@selector(receivedNotification) | 280 withSelector:@selector(receivedNotification) |
| 278 forScheme:nil]; | 281 forScheme:nil]; |
| 279 [installationNotifier_ unregisterForNotifications:notificationReceiver1_]; | 282 [installationNotifier_ unregisterForNotifications:notificationReceiver1_]; |
| 280 } | 283 } |
| 281 | 284 |
| 282 } // namespace | 285 } // namespace |
| OLD | NEW |