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

http://macfuse.googlecode.com/ · C++ Header · 128 lines · 29 code · 16 blank · 83 comment · 1 complexity · 911087152c24036e45c07c4af058c7b5 MD5 · raw file

  1. //
  2. // GTMUIKit+UnitTesting.h
  3. //
  4. // Code for making unit testing of graphics/UI easier. Generally you
  5. // will only want to look at the macros:
  6. // GTMAssertDrawingEqualToFile
  7. // GTMAssertViewRepEqualToFile
  8. // and the protocol GTMUnitTestViewDrawer. When using these routines
  9. // make sure you are using device colors and not calibrated/generic colors
  10. // or else your test graphics WILL NOT match across devices/graphics cards.
  11. //
  12. // Copyright 2006-2008 Google Inc.
  13. //
  14. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  15. // use this file except in compliance with the License. You may obtain a copy
  16. // of the License at
  17. //
  18. // http://www.apache.org/licenses/LICENSE-2.0
  19. //
  20. // Unless required by applicable law or agreed to in writing, software
  21. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  23. // License for the specific language governing permissions and limitations under
  24. // the License.
  25. //
  26. #import <UIKit/UIKit.h>
  27. #import "GTMNSObject+UnitTesting.h"
  28. @protocol GTMUnitTestViewDrawer;
  29. // Fails when the |a1|'s drawing in an area |a2| does not equal the image file named |a3|.
  30. // See the description of the GTMAssertViewRepEqualToFile macro
  31. // to understand how |a3| is found and written out.
  32. // See the description of the GTMUnitTestView for a better idea
  33. // how the view works.
  34. // Implemented as a macro to match the rest of the SenTest macros.
  35. //
  36. // Args:
  37. // a1: The object that implements the GTMUnitTestViewDrawer protocol
  38. // that is doing the drawing.
  39. // a2: The size of the drawing
  40. // a3: The name of the image file to check against.
  41. // Do not include the extension
  42. // a4: contextInfo to pass to drawer
  43. // description: A format string as in the printf() function.
  44. // Can be nil or an empty string but must be present.
  45. // ...: A variable number of arguments to the format string. Can be absent.
  46. //
  47. #define GTMAssertDrawingEqualToFile(a1, a2, a3, a4, description, ...) \
  48. do { \
  49. id<GTMUnitTestViewDrawer> a1Drawer = (a1); \
  50. CGSize a2Size = (a2); \
  51. NSString* a3String = (a3); \
  52. void *a4ContextInfo = (a4); \
  53. CGRect frame = CGRectMake(0, 0, a2Size.width, a2Size.height); \
  54. GTMUnitTestView *view = [[[GTMUnitTestView alloc] initWithFrame:frame drawer:a1Drawer contextInfo:a4ContextInfo] autorelease]; \
  55. GTMAssertObjectImageEqualToImageNamed(view, a3String, @"%@", STComposeString(description, ##__VA_ARGS__)); \
  56. } while(0)
  57. // Category for making unit testing of graphics/UI easier.
  58. // Allows you to take a state of a view. Supports both image and state.
  59. // See GTMNSObject+UnitTesting.h for details.
  60. @interface UIView (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
  61. // Encodes the state of an object in a manner suitable for comparing against a master state file
  62. // This enables us to determine whether the object is in a suitable state.
  63. //
  64. // Arguments:
  65. // inCoder - the coder to encode our state into
  66. - (void)gtm_unitTestEncodeState:(NSCoder*)inCoder;
  67. // Returns whether gtm_unitTestEncodeState should recurse into subviews
  68. //
  69. // Returns:
  70. // should gtm_unitTestEncodeState pick up subview state.
  71. - (BOOL)gtm_shouldEncodeStateForSubviews;
  72. @end
  73. // Category to help UIImage testing. UIImage can be tested using
  74. // GTMAssertObjectImageEqualToImageNamed macro, which automatically creates
  75. // result images and diff images in case test fails.
  76. @interface UIImage (GTMUnitTestingAdditions) <GTMUnitTestingImaging>
  77. @end
  78. // A view that allows you to delegate out drawing using the formal
  79. // GTMUnitTestViewDelegate protocol
  80. // This is useful when writing up unit tests for visual elements.
  81. // Your test will often end up looking like this:
  82. // - (void)testFoo {
  83. // GTMAssertDrawingEqualToFile(self, CGSizeMake(200, 200), @"Foo", nil, nil);
  84. // }
  85. // and your testSuite will also implement the unitTestViewDrawRect method to do
  86. // it's actual drawing. The above creates a view of size 200x200 that draws
  87. // it's content using |self|'s unitTestViewDrawRect method and compares it to
  88. // the contents of the file Foo.tif to make sure it's valid
  89. @interface GTMUnitTestView : UIView {
  90. @private
  91. id<GTMUnitTestViewDrawer> drawer_; // delegate for doing drawing (STRONG)
  92. void* contextInfo_; // info passed in by user for them to use when drawing
  93. }
  94. // Create a GTMUnitTestView.
  95. //
  96. // Args:
  97. // rect: the area to draw.
  98. // drawer: the object that will do the drawing via the GTMUnitTestViewDrawer
  99. // protocol
  100. // contextInfo:
  101. - (id)initWithFrame:(CGRect)frame drawer:(id<GTMUnitTestViewDrawer>)drawer contextInfo:(void*)contextInfo;
  102. @end
  103. /// \cond Protocols
  104. // Formal protocol for doing unit testing of views. See description of
  105. // GTMUnitTestView for details.
  106. @protocol GTMUnitTestViewDrawer <NSObject>
  107. // Draw the view. Equivalent to drawRect on a standard UIView.
  108. //
  109. // Args:
  110. // rect: the area to draw.
  111. - (void)gtm_unitTestViewDrawRect:(CGRect)rect contextInfo:(void*)contextInfo;
  112. @end