Handle case where there is no window and test that case

This commit is contained in:
Indragie Karunaratne 2013-03-25 14:21:51 -06:00
Родитель 74b02de53b
Коммит 2062ad4bd2
2 изменённых файлов: 18 добавлений и 5 удалений

Просмотреть файл

@ -11,10 +11,17 @@
@implementation NSView (RBLAlignmentAdditions)
- (NSRect)rbl_viewBackingAlignedRect:(NSRect)rect options:(NSAlignmentOptions)options {
NSAssert(self.window != nil, @"View must have a window in order to obtain a rectangle aligned to the backing.");
if (self.window) {
NSRect windowRect = [self convertRect:rect toView:nil];
NSRect windowBackingRect = [self backingAlignedRect:windowRect options:options];
return [self convertRect:windowBackingRect fromView:nil];
} else {
CGFloat scaleFactor = [[NSScreen mainScreen] backingScaleFactor];
CGAffineTransform transformToBacking = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGRect backingRect = CGRectApplyAffineTransform(rect, transformToBacking);
backingRect = NSIntegralRectWithOptions(backingRect, options);
return CGRectApplyAffineTransform(backingRect, CGAffineTransformInvert(transformToBacking));
}
}
@end

Просмотреть файл

@ -9,10 +9,16 @@
SpecBegin(NSViewRBLAlignmentAdditions)
describe(@"view alignment", ^{
__block NSView *view;
before(^{
view = [[NSView alloc] initWithFrame:NSMakeRect(20.f, 20.f, 20.f, 20.f)];
});
it(@"should return a rect aligned to the view backing", ^{
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0.f, 0.f, 100.f, 100.f) styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(20.f, 20.f, 20.f, 20.f)];
[window.contentView addSubview:view];
});
it(@"should return a rect aligned to the view backing without a window.", ^{});
after(^{
NSRect nonAlignedRect = NSMakeRect(10.2f, 11.8f, 12.f, 13.f);
NSRect alignedRect = [view rbl_viewBackingAlignedRect:nonAlignedRect options:NSAlignAllEdgesNearest];
expect(NSEqualRects(alignedRect, NSMakeRect(10.f, 12.f, 12.f, 13.f))).to.beTruthy();