Commit 6c71a72e authored by dmaclach@chromium.org's avatar dmaclach@chromium.org

Added some enhancements to OCMock.

BUG=NONE
TEST=BUILD

Review URL: http://codereview.chromium.org/7004036

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86474 0039d316-1c4b-4281-b951-d872f2087c98
parent aad63572
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "third_party/ocmock/gtest_support.h" #include "third_party/ocmock/gtest_support.h"
#import "third_party/ocmock/OCMock/OCMock.h" #import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/ocmock_extensions.h"
namespace { namespace {
...@@ -60,12 +61,9 @@ id CreateBrowserWindowControllerMock() { ...@@ -60,12 +61,9 @@ id CreateBrowserWindowControllerMock() {
id delegate = [OCMockObject mockForClass:[BrowserWindowController class]]; id delegate = [OCMockObject mockForClass:[BrowserWindowController class]];
// Make conformsToProtocol return YES for @protocol(BrowserCommandExecutor) // Make conformsToProtocol return YES for @protocol(BrowserCommandExecutor)
// to satisfy the DCHECK() in handleExtraKeyboardShortcut. // to satisfy the DCHECK() in handleExtraKeyboardShortcut.
// [[[delegate stub] andReturnBool:YES]
// TODO(akalin): Figure out how to replace OCMOCK_ANY below with conformsToProtocol:[OCMArg conformsToProtocol:
// @protocol(BrowserCommandExecutor) and have it work. @protocol(BrowserCommandExecutor)]];
BOOL yes = YES;
[[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)]
conformsToProtocol:OCMOCK_ANY];
return delegate; return delegate;
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "testing/platform_test.h" #include "testing/platform_test.h"
#include "third_party/ocmock/gtest_support.h" #include "third_party/ocmock/gtest_support.h"
#import "third_party/ocmock/OCMock/OCMock.h" #import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/ocmock_extensions.h"
using ::testing::Return; using ::testing::Return;
using ::testing::ReturnArg; using ::testing::ReturnArg;
...@@ -110,9 +111,6 @@ TEST_F(AutocompleteTextFieldEditorTest, PageActionMenus) { ...@@ -110,9 +111,6 @@ TEST_F(AutocompleteTextFieldEditorTest, PageActionMenus) {
action:@selector(goFish:) action:@selector(goFish:)
keyEquivalent:@""]; keyEquivalent:@""];
// For OCMOCK_VALUE().
BOOL yes = YES;
// So that we don't have to mock the observer. // So that we don't have to mock the observer.
[editor_ setEditable:NO]; [editor_ setEditable:NO];
...@@ -120,7 +118,7 @@ TEST_F(AutocompleteTextFieldEditorTest, PageActionMenus) { ...@@ -120,7 +118,7 @@ TEST_F(AutocompleteTextFieldEditorTest, PageActionMenus) {
// propagated back. // propagated back.
{ {
id delegate = [OCMockObject mockForClass:[AutocompleteTextField class]]; id delegate = [OCMockObject mockForClass:[AutocompleteTextField class]];
[[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)] [[[delegate stub] andReturnBool:YES]
isKindOfClass:[AutocompleteTextField class]]; isKindOfClass:[AutocompleteTextField class]];
[[[delegate expect] andReturn:menu.get()] decorationMenuForEvent:event]; [[[delegate expect] andReturn:menu.get()] decorationMenuForEvent:event];
[editor_ setDelegate:delegate]; [editor_ setDelegate:delegate];
...@@ -135,7 +133,7 @@ TEST_F(AutocompleteTextFieldEditorTest, PageActionMenus) { ...@@ -135,7 +133,7 @@ TEST_F(AutocompleteTextFieldEditorTest, PageActionMenus) {
// returned. // returned.
{ {
id delegate = [OCMockObject mockForClass:[AutocompleteTextField class]]; id delegate = [OCMockObject mockForClass:[AutocompleteTextField class]];
[[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)] [[[delegate stub] andReturnBool:YES]
isKindOfClass:[AutocompleteTextField class]]; isKindOfClass:[AutocompleteTextField class]];
[[[delegate expect] andReturn:nil] decorationMenuForEvent:event]; [[[delegate expect] andReturn:nil] decorationMenuForEvent:event];
[editor_ setDelegate:delegate]; [editor_ setDelegate:delegate];
......
...@@ -12,5 +12,5 @@ with the concept of mock objects please visit mockobjects.com which has more ...@@ -12,5 +12,5 @@ with the concept of mock objects please visit mockobjects.com which has more
details and discussions about this approach to testing software. details and discussions about this approach to testing software.
Local Modifications: Local Modifications:
No Modifications gtest_support.h/.mm and ocmock_extensions.h/.mm are not part of the
ocmock package, and are chromium specific additions.
...@@ -23,6 +23,10 @@ ...@@ -23,6 +23,10 @@
'gtest_support.h', 'gtest_support.h',
'gtest_support.mm', 'gtest_support.mm',
# Some extra features to make using OCMock easier.
'ocmock_extensions.h',
'ocmock_extensions.mm',
# OCMock sources. # OCMock sources.
'OCMock/NSInvocation+OCMAdditions.h', 'OCMock/NSInvocation+OCMAdditions.h',
'OCMock/OCMObserverRecorder.m', 'OCMock/OCMObserverRecorder.m',
......
// Copyright (c) 2011 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.
#ifndef THIRD_PARTY_OCMOCK_OCMOCK_EXTENSIONS_H_
#define THIRD_PARTY_OCMOCK_OCMOCK_EXTENSIONS_H_
#import <Foundation/Foundation.h>
#import "third_party/ocmock/OCMock/OCMock.h"
// Some enhancements to OCMock to make it easier to write mocks.
// Pointers to objects still have to be handled with
// - (id)andReturnValue:OCMOCK_VALUE(blah)
// to keep the types working correctly.
@interface OCMockRecorder(CrExtensions)
- (id)andReturnChar:(char)value;
- (id)andReturnUnsignedChar:(unsigned char)value;
- (id)andReturnShort:(short)value;
- (id)andReturnUnsignedShort:(unsigned short)value;
- (id)andReturnInt:(int)value;
- (id)andReturnUnsignedInt:(unsigned int)value;
- (id)andReturnLong:(long)value;
- (id)andReturnUnsignedLong:(unsigned long)value;
- (id)andReturnLongLong:(long long)value;
- (id)andReturnUnsignedLongLong:(unsigned long long)value;
- (id)andReturnFloat:(float)value;
- (id)andReturnDouble:(double)value;
- (id)andReturnBool:(BOOL)value;
- (id)andReturnInteger:(NSInteger)value;
- (id)andReturnUnsignedInteger:(NSUInteger)value;
- (id)andReturnNSRect:(NSRect)rect;
- (id)andReturnCGRect:(CGRect)rect;
- (id)andReturnNSPoint:(NSPoint)point;
- (id)andReturnCGPoint:(CGPoint)point;
@end
// A constraint for verifying that something conforms to a protocol.
@interface cr_OCMConformToProtocolConstraint : OCMConstraint {
@private
Protocol* protocol_;
}
- (id)initWithProtocol:(Protocol*)protocol;
@end
@interface OCMArg(CrExtensions)
+ (id)conformsToProtocol:(Protocol*)protocol;
@end
#endif // THIRD_PARTY_OCMOCK_OCMOCK_EXTENSIONS_H_
// Copyright (c) 2011 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.
#include <objc/runtime.h>
#include "third_party/ocmock/ocmock_extensions.h"
#define CR_OCMOCK_RETURN_IMPL(type_name, type) \
- (id)andReturn##type_name:(type)value { \
return [self andReturnValue:OCMOCK_VALUE(value)]; \
}
@implementation OCMockRecorder(CrExtensions)
CR_OCMOCK_RETURN_IMPL(Char, char);
CR_OCMOCK_RETURN_IMPL(UnsignedChar, unsigned char);
CR_OCMOCK_RETURN_IMPL(Short, short);
CR_OCMOCK_RETURN_IMPL(UnsignedShort, unsigned short);
CR_OCMOCK_RETURN_IMPL(Int, int);
CR_OCMOCK_RETURN_IMPL(UnsignedInt, unsigned int);
CR_OCMOCK_RETURN_IMPL(Long, long);
CR_OCMOCK_RETURN_IMPL(UnsignedLong, unsigned long);
CR_OCMOCK_RETURN_IMPL(LongLong, long long);
CR_OCMOCK_RETURN_IMPL(UnsignedLongLong, unsigned long long);
CR_OCMOCK_RETURN_IMPL(Float, float);
CR_OCMOCK_RETURN_IMPL(Double, double);
CR_OCMOCK_RETURN_IMPL(Bool, BOOL);
CR_OCMOCK_RETURN_IMPL(Integer, NSInteger);
CR_OCMOCK_RETURN_IMPL(UnsignedInteger, NSUInteger);
- (id)andReturnNSRect:(NSRect)rect {
return [self andReturnValue:[NSValue valueWithRect:rect]];
}
- (id)andReturnCGRect:(CGRect)rect {
return [self andReturnNSRect:NSRectFromCGRect(rect)];
}
- (id)andReturnNSPoint:(NSPoint)point {
return [self andReturnValue:[NSValue valueWithPoint:point]];
}
- (id)andReturnCGPoint:(CGPoint)point {
return [self andReturnNSPoint:NSPointFromCGPoint(point)];
}
@end
@implementation cr_OCMConformToProtocolConstraint
- (id)initWithProtocol:(Protocol*)protocol {
if (self == [super init]) {
protocol_ = protocol;
}
return self;
}
- (BOOL)evaluate:(id)value {
return protocol_conformsToProtocol(protocol_, value);
}
@end
@implementation OCMArg(CrExtensions)
+ (id)conformsToProtocol:(Protocol*)protocol {
return [[[cr_OCMConformToProtocolConstraint alloc] initWithProtocol:protocol]
autorelease];
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment