/core/externals/update-engine/externals/google-toolbox-for-mac/iPhone/GTMUIView+SubtreeDescriptionTest.m

http://macfuse.googlecode.com/ · Objective C · 150 lines · 104 code · 20 blank · 26 comment · 0 complexity · 0ac8a86dbc632ed4c22bbeedb26ff931 MD5 · raw file

  1. //
  2. // GTMUIView+SubtreeDescriptionTest.m
  3. //
  4. // Copyright 2009 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations
  16. // under the License.
  17. //
  18. #import "GTMNSObject+UnitTesting.h"
  19. #import "GTMSenTestCase.h"
  20. #import "GTMUIView+SubtreeDescription.h"
  21. #if !NDEBUG
  22. @interface GTMUIView_SubtreeDescriptionTest : GTMTestCase
  23. @end
  24. @implementation GTMUIView_SubtreeDescriptionTest
  25. - (void)testSubtreeDescription {
  26. // Test a single, simple view.
  27. CGRect frame1 = CGRectMake(0, 0, 100, 200);
  28. UIView *view1 = [[[UIView alloc] initWithFrame:frame1] autorelease];
  29. NSString *actual = [view1 subtreeDescription];
  30. NSString *expected = [NSString stringWithFormat:
  31. @"UIView %p {x:0 y:0 w:100 h:200}\n", view1];
  32. STAssertEqualObjects(actual, expected, @"a single, simple view failed");
  33. // Test a view with one child.
  34. CGRect frame2 = CGRectMake(2, 2, 102, 202);
  35. UIView *view2 = [[[UIView alloc] initWithFrame:frame2] autorelease];
  36. [view1 addSubview:view2];
  37. NSString *actual2 = [view1 subtreeDescription];
  38. NSString *expected2 = [NSString stringWithFormat:
  39. @"UIView %p {x:0 y:0 w:100 h:200}\n"
  40. @" UIView %p {x:2 y:2 w:102 h:202}\n", view1, view2];
  41. STAssertEqualObjects(actual2, expected2, @"a view with one child");
  42. // Test a view with two children.
  43. CGRect frame3 = CGRectMake(3, 3, 103, 203);
  44. UIView *view3 = [[[UIView alloc] initWithFrame:frame3] autorelease];
  45. [view1 addSubview:view3];
  46. NSString *actual3 = [view1 subtreeDescription];
  47. NSString *expected3 = [NSString stringWithFormat:
  48. @"UIView %p {x:0 y:0 w:100 h:200}\n"
  49. @" UIView %p {x:2 y:2 w:102 h:202}\n"
  50. @" UIView %p {x:3 y:3 w:103 h:203}\n",
  51. view1, view2, view3];
  52. STAssertEqualObjects(actual3, expected3, @"a view with two children");
  53. // Test a view with two children, one hidden.
  54. [view3 setHidden:YES];
  55. NSString *actual4 = [view1 subtreeDescription];
  56. NSString *expected4 = [NSString stringWithFormat:
  57. @"UIView %p {x:0 y:0 w:100 h:200}\n"
  58. @" UIView %p {x:2 y:2 w:102 h:202}\n"
  59. @" UIView %p {x:3 y:3 w:103 h:203} hid\n",
  60. view1, view2, view3];
  61. STAssertEqualObjects(actual4, expected4, @"with two children, one hidden");
  62. }
  63. - (void)testSublayersDescription {
  64. // Test a single, simple layer.
  65. CGRect frame1 = CGRectMake(0, 0, 100, 200);
  66. UIView *view1 = [[[UIView alloc] initWithFrame:frame1] autorelease];
  67. NSString *actual = [view1 sublayersDescription];
  68. NSString *expected = [NSString stringWithFormat:
  69. @"CALayer %p {x:0 y:0 w:100 h:200}\n", [view1 layer]];
  70. STAssertEqualObjects(actual, expected, @"a single, simple layer failed");
  71. // Test a layer with one child.
  72. CGRect frame2 = CGRectMake(2, 2, 102, 202);
  73. UIView *view2 = [[[UIView alloc] initWithFrame:frame2] autorelease];
  74. [view1 addSubview:view2];
  75. NSString *actual2 = [view1 sublayersDescription];
  76. NSString *expected2 = [NSString stringWithFormat:
  77. @"CALayer %p {x:0 y:0 w:100 h:200}\n"
  78. @" CALayer %p {x:2 y:2 w:102 h:202}\n",
  79. [view1 layer], [view2 layer]];
  80. STAssertEqualObjects(actual2, expected2, @"a layer with one child");
  81. // Test a layer with two children.
  82. CGRect frame3 = CGRectMake(3, 3, 103, 203);
  83. UIView *view3 = [[[UIView alloc] initWithFrame:frame3] autorelease];
  84. [view1 addSubview:view3];
  85. NSString *actual3 = [view1 sublayersDescription];
  86. NSString *expected3 = [NSString stringWithFormat:
  87. @"CALayer %p {x:0 y:0 w:100 h:200}\n"
  88. @" CALayer %p {x:2 y:2 w:102 h:202}\n"
  89. @" CALayer %p {x:3 y:3 w:103 h:203}\n",
  90. [view1 layer], [view2 layer], [view3 layer]];
  91. STAssertEqualObjects(actual3, expected3, @"a layer with two children");
  92. // Test a layer with two children, one hidden.
  93. [view3 setHidden:YES];
  94. NSString *actual4 = [view1 sublayersDescription];
  95. NSString *expected4 = [NSString stringWithFormat:
  96. @"CALayer %p {x:0 y:0 w:100 h:200}\n"
  97. @" CALayer %p {x:2 y:2 w:102 h:202}\n"
  98. @" CALayer %p {x:3 y:3 w:103 h:203} hid\n",
  99. [view1 layer], [view2 layer], [view3 layer]];
  100. STAssertEqualObjects(actual4, expected4, @"with two children, one hidden");
  101. }
  102. @end
  103. @interface UIMyTestView : UIView
  104. - (NSString *)myViewDescriptionLine;
  105. @end
  106. @implementation UIMyTestView
  107. - (NSString *)myViewDescriptionLine {
  108. NSString *result = [NSString stringWithFormat:@"alpha: %3.1f", [self alpha]];
  109. return result;
  110. }
  111. @end
  112. @interface GTMUIView_SubtreeSubClassDescriptionTest : GTMTestCase
  113. @end
  114. @implementation GTMUIView_SubtreeSubClassDescriptionTest
  115. - (void)testSubtreeDescription {
  116. CGRect frame1 = CGRectMake(0, 0, 100, 200);
  117. UIView *view1 = [[[UIView alloc] initWithFrame:frame1] autorelease];
  118. // Test a view with one child.
  119. CGRect frame2 = CGRectMake(2, 2, 102, 202);
  120. UIView *view2 = [[[UIMyTestView alloc] initWithFrame:frame2] autorelease];
  121. [view1 addSubview:view2];
  122. NSString *actual2 = [view1 subtreeDescription];
  123. NSString *expected2 = [NSString stringWithFormat:
  124. @"UIView %p {x:0 y:0 w:100 h:200}\n"
  125. @" UIMyTestView %p {x:2 y:2 w:102 h:202} alpha: 1.0\n",
  126. view1, view2];
  127. STAssertEqualObjects(actual2, expected2, @"a view with one subclassed child");
  128. }
  129. @end
  130. #endif