| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
| 6 | 6 |
| 7 #import "ios/chrome/test/block_cleanup_test.h" | 7 #import "ios/chrome/test/block_cleanup_test.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 11 #import "base/mac/foundation_util.h" |
| 11 | 12 |
| 12 void BlockCleanupTest::SetUp() { | 13 void BlockCleanupTest::SetUp() { |
| 13 block_cleanup_pool_ = [[NSAutoreleasePool alloc] init]; | 14 block_cleanup_pool_ = [[NSAutoreleasePool alloc] init]; |
| 14 } | 15 } |
| 15 | 16 |
| 16 void BlockCleanupTest::TearDown() { | 17 void BlockCleanupTest::TearDown() { |
| 17 // Block-copied items are released asynchronously; spin the loop to give them | 18 // Block-copied items are released asynchronously; spin the loop to give them |
| 18 // a chance to be cleaned up. | 19 // a chance to be cleaned up. |
| 19 const NSTimeInterval kCleanupTime = 0.1; | 20 const NSTimeInterval kCleanupTime = 0.1; |
| 20 SpinRunLoop(kCleanupTime); | 21 SpinRunLoop(kCleanupTime); |
| 21 | 22 |
| 22 // Drain the autorelease pool to finish cleaning up after blocks. | 23 // Drain the autorelease pool to finish cleaning up after blocks. |
| 23 // TODO(rohitrao): Can this be an EXPECT, so as to not crash the whole suite? | 24 // TODO(rohitrao): Can this be an EXPECT, so as to not crash the whole suite? |
| 24 DCHECK(block_cleanup_pool_); | 25 DCHECK(block_cleanup_pool_); |
| 25 [block_cleanup_pool_ release]; | 26 [base::mac::ObjCCastStrict<NSAutoreleasePool>(block_cleanup_pool_) release]; |
| 26 block_cleanup_pool_ = nil; | 27 block_cleanup_pool_ = nil; |
| 27 | 28 |
| 28 PlatformTest::TearDown(); | 29 PlatformTest::TearDown(); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void BlockCleanupTest::SpinRunLoop(NSTimeInterval cleanup_time) { | 32 void BlockCleanupTest::SpinRunLoop(NSTimeInterval cleanup_time) { |
| 32 NSDate* cleanup_start = [NSDate date]; | 33 NSDate* cleanup_start = [NSDate date]; |
| 33 while (fabs([cleanup_start timeIntervalSinceNow]) < cleanup_time) { | 34 while (fabs([cleanup_start timeIntervalSinceNow]) < cleanup_time) { |
| 34 base::scoped_nsobject<NSDate> beforeDate( | 35 base::scoped_nsobject<NSDate> beforeDate( |
| 35 [[NSDate alloc] initWithTimeIntervalSinceNow:cleanup_time]); | 36 [[NSDate alloc] initWithTimeIntervalSinceNow:cleanup_time]); |
| 36 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | 37 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode |
| 37 beforeDate:beforeDate]; | 38 beforeDate:beforeDate]; |
| 38 } | 39 } |
| 39 } | 40 } |
| OLD | NEW |