From 0f88207d7b29fe3dd0afac0f2591d00eb9c8efd6 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Fri, 29 Apr 2011 12:01:23 +0200 Subject: [PATCH] fix out of bounds error in itemWithTitle: --- AppKit/CPPopUpButton.j | 18 ++++++++---------- Tests/AppKit/CPPopUpButtonTest.j | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/AppKit/CPPopUpButton.j b/AppKit/CPPopUpButton.j index bea80de88..ca1a548cb 100644 --- a/AppKit/CPPopUpButton.j +++ b/AppKit/CPPopUpButton.j @@ -86,10 +86,7 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); [self setPullsDown:shouldPullDown]; - var options = CPKeyValueObservingOptionNew | - CPKeyValueObservingOptionOld;/* | - CPKeyValueObservingOptionInitial; -*/ + var options = CPKeyValueObservingOptionNew |CPKeyValueObservingOptionOld;/* |CPKeyValueObservingOptionInitial;*/ [self addObserver:self forKeyPath:@"menu.items" options:options context:nil]; [self addObserver:self forKeyPath:@"_firstItem.changeCount" options:options context:nil]; [self addObserver:self forKeyPath:@"selectedItem.changeCount" options:options context:nil]; @@ -402,9 +399,13 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); */ - (CPMenuItem)itemWithTitle:(CPString)aTitle { - var menu = [self menu]; + var menu = [self menu], + itemIndex = [menu indexOfItemWithTitle:aTitle]; - return [menu itemAtIndex:[menu indexOfItemWithTitle:aTitle]]; + if (itemIndex === CPNotFound) + return nil; + + return [menu itemAtIndex:itemIndex]; } /*! @@ -810,10 +811,7 @@ var DEPRECATED_CPPopUpButtonMenuKey = @"CPPopUpButtonMenuKey", [self setObjectValue:[aCoder decodeObjectForKey:DEPRECATED_CPPopUpButtonSelectedIndexKey]]; } - var options = CPKeyValueObservingOptionNew | - CPKeyValueObservingOptionOld;/* | - CPKeyValueObservingOptionInitial; -*/ + var options = CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld;/* | CPKeyValueObservingOptionInitial */ [self addObserver:self forKeyPath:@"menu.items" options:options context:nil]; [self addObserver:self forKeyPath:@"_firstItem.changeCount" options:options context:nil]; diff --git a/Tests/AppKit/CPPopUpButtonTest.j b/Tests/AppKit/CPPopUpButtonTest.j index 4b1cf7af5..1a970ac78 100644 --- a/Tests/AppKit/CPPopUpButtonTest.j +++ b/Tests/AppKit/CPPopUpButtonTest.j @@ -4,7 +4,7 @@ @implementation CPPopUpButtonTest : OJTestCase { - CPPopUpButton button; + CPPopUpButton button @accessors; } - (void)setUp @@ -189,4 +189,17 @@ // [self assert:4990000 equals:[dict objectForKey:@"weight"]]; } +- (void)testItemWithTitle +{ + [self assert:nil equals:[[self button] itemWithTitle:@"I dont exist"]]; + + [[self button] addItemWithTitle:@"one"]; + [[self button] addItemWithTitle:@"two"]; + [[self button] addItemWithTitle:@"three"]; + + [self assert:0 equals:[[self button] indexOfItemWithTitle:@"one"]]; + [self assert:1 equals:[[self button] indexOfItemWithTitle:@"two"]]; + [self assert:2 equals:[[self button] indexOfItemWithTitle:@"three"]]; +} + @end