/core/externals/google-toolbox-for-mac/UnitTesting/GTMAppKit+UnitTesting.h

http://macfuse.googlecode.com/ · C Header · 171 lines · 60 code · 31 blank · 80 comment · 1 complexity · b53bf172494b83efde938059ddfe4a11 MD5 · raw file

  1. //
  2. // GTMAppKit+UnitTesting.m
  3. //
  4. // Categories for making unit testing of graphics/UI easier.
  5. //
  6. // Copyright 2006-2008 Google Inc.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. // use this file except in compliance with the License. You may obtain a copy
  10. // of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. // License for the specific language governing permissions and limitations under
  18. // the License.
  19. //
  20. #import <AppKit/AppKit.h>
  21. #import "GTMNSObject+UnitTesting.h"
  22. // Categories for making unit testing of graphics/UI easier.
  23. // Allows you to take a state/images of instances of AppKit classes.
  24. // See GTMNSObject+UnitTesting.h for details.
  25. @interface NSApplication (GTMUnitTestingAdditions)
  26. @end
  27. @interface NSWindow (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
  28. @end
  29. @interface NSControl (GTMUnitTestingAdditions)
  30. @end
  31. @interface NSButton (GTMUnitTestingAdditions)
  32. @end
  33. @interface NSTextField (GTMUnitTestingAdditions)
  34. @end
  35. @interface NSCell (GTMUnitTestingAdditions)
  36. @end
  37. @interface NSImage (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
  38. @end
  39. @interface NSMenu (GTMUnitTestingAdditions)
  40. @end
  41. @interface NSMenuItem (GTMUnitTestingAdditions)
  42. @end
  43. @interface NSTabView (GTMUnitTestingAdditions)
  44. @end
  45. @interface NSTabViewItem (GTMUnitTestingAdditions)
  46. @end
  47. @interface NSToolbar (GTMUnitTestingAdditions)
  48. @end
  49. @interface NSToolbarItem (GTMUnitTestingAdditions)
  50. @end
  51. @interface NSMatrix (GTMUnitTestingAdditions)
  52. @end
  53. @interface NSBox (GTMUnitTestingAdditions)
  54. @end
  55. @interface NSSegmentedControl (GTMUnitTestingAdditions)
  56. @end
  57. @interface NSComboBox (GTMUnitTestingAdditions)
  58. @end
  59. @protocol GTMUnitTestViewDrawer;
  60. // Fails when the |a1|'s drawing in an area |a2| does not equal the image file named |a3|.
  61. // See the description of the -gtm_pathForImageNamed method
  62. // to understand how |a3| is found and written out.
  63. // See the description of the GTMUnitTestView for a better idea
  64. // how the view works.
  65. // Implemented as a macro to match the rest of the SenTest macros.
  66. //
  67. // Args:
  68. // a1: The object that implements the GTMUnitTestViewDrawer protocol
  69. // that is doing the drawing.
  70. // a2: The size of the drawing
  71. // a3: The name of the image file to check against.
  72. // Do not include the extension
  73. // a4: contextInfo to pass to drawer
  74. // description: A format string as in the printf() function.
  75. // Can be nil or an empty string but must be present.
  76. // ...: A variable number of arguments to the format string. Can be absent.
  77. //
  78. #define GTMAssertDrawingEqualToImageNamed(a1, a2, a3, a4, description, ...) \
  79. do { \
  80. id<GTMUnitTestViewDrawer> a1Drawer = (a1); \
  81. NSSize a2Size = (a2); \
  82. NSString* a3String = (a3); \
  83. void *a4ContextInfo = (a4); \
  84. NSRect frame = NSMakeRect(0, 0, a2Size.width, a2Size.height); \
  85. GTMUnitTestView *view = [[[GTMUnitTestView alloc] initWithFrame:frame drawer:a1Drawer contextInfo:a4ContextInfo] autorelease]; \
  86. GTMAssertObjectImageEqualToImageNamed(view, a3String, STComposeString(description, ##__VA_ARGS__)); \
  87. } while(0)
  88. // Category for making unit testing of graphics/UI easier.
  89. // Allows you to take a state of a view. Supports both image and state.
  90. // See NSObject+UnitTesting.h for details.
  91. @interface NSView (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
  92. // Returns whether unitTestEncodeState should recurse into subviews
  93. //
  94. // If you have "Full keyboard access" in the
  95. // Keyboard & Mouse > Keyboard Shortcuts preferences pane set to "Text boxes
  96. // and Lists only" that Apple adds a set of subviews to NSTextFields. So in the
  97. // case of NSTextFields we don't want to recurse into their subviews. There may
  98. // be other cases like this, so instead of specializing unitTestEncodeState: to
  99. // look for NSTextFields, NSTextFields will just not allow us to recurse into
  100. // their subviews.
  101. //
  102. // Returns:
  103. // should unitTestEncodeState pick up subview state.
  104. - (BOOL)gtm_shouldEncodeStateForSubviews;
  105. @end
  106. // A view that allows you to delegate out drawing using the formal
  107. // GTMUnitTestViewDelegate protocol
  108. // This is useful when writing up unit tests for visual elements.
  109. // Your test will often end up looking like this:
  110. // - (void)testFoo {
  111. // GTMAssertDrawingEqualToFile(self, NSMakeSize(200, 200), @"Foo", nil, nil);
  112. // }
  113. // and your testSuite will also implement the unitTestViewDrawRect method to do
  114. // it's actual drawing. The above creates a view of size 200x200 that draws
  115. // it's content using |self|'s unitTestViewDrawRect method and compares it to
  116. // the contents of the file Foo.tif to make sure it's valid
  117. @interface GTMUnitTestView : NSView {
  118. @private
  119. id<GTMUnitTestViewDrawer> drawer_; // delegate for doing drawing (STRONG)
  120. void* contextInfo_; // info passed in by user for them to use when drawing
  121. }
  122. // Create a GTMUnitTestView.
  123. //
  124. // Args:
  125. // rect: the area to draw.
  126. // drawer: the object that will do the drawing via the GTMUnitTestViewDrawer
  127. // protocol
  128. // contextInfo:
  129. - (id)initWithFrame:(NSRect)frame drawer:(id<GTMUnitTestViewDrawer>)drawer contextInfo:(void*)contextInfo;
  130. @end
  131. /// \cond Protocols
  132. // Formal protocol for doing unit testing of views. See description of
  133. // GTMUnitTestView for details.
  134. @protocol GTMUnitTestViewDrawer <NSObject>
  135. // Draw the view. Equivalent to drawRect on a standard NSView.
  136. //
  137. // Args:
  138. // rect: the area to draw.
  139. - (void)gtm_unitTestViewDrawRect:(NSRect)rect contextInfo:(void*)contextInfo;
  140. @end