/core/externals/google-toolbox-for-mac/iPhone/GTMUIView+SubtreeDescriptionTest.m
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 19#import "GTMNSObject+UnitTesting.h" 20#import "GTMSenTestCase.h" 21#import "GTMUIView+SubtreeDescription.h" 22 23#if !NDEBUG 24 25@interface GTMUIView_SubtreeDescriptionTest : GTMTestCase 26@end 27 28@implementation GTMUIView_SubtreeDescriptionTest 29 30- (void)testSubtreeDescription { 31 // Test a single, simple view. 32 CGRect frame1 = CGRectMake(0, 0, 100, 200); 33 UIView *view1 = [[[UIView alloc] initWithFrame:frame1] autorelease]; 34 NSString *actual = [view1 subtreeDescription]; 35 NSString *expected = [NSString stringWithFormat: 36 @"UIView %p {x:0 y:0 w:100 h:200}\n", view1]; 37 STAssertEqualObjects(actual, expected, @"a single, simple view failed"); 38 39 // Test a view with one child. 40 CGRect frame2 = CGRectMake(2, 2, 102, 202); 41 UIView *view2 = [[[UIView alloc] initWithFrame:frame2] autorelease]; 42 [view1 addSubview:view2]; 43 NSString *actual2 = [view1 subtreeDescription]; 44 NSString *expected2 = [NSString stringWithFormat: 45 @"UIView %p {x:0 y:0 w:100 h:200}\n" 46 @" UIView %p {x:2 y:2 w:102 h:202}\n", view1, view2]; 47 STAssertEqualObjects(actual2, expected2, @"a view with one child"); 48 49 // Test a view with two children. 50 CGRect frame3 = CGRectMake(3, 3, 103, 203); 51 UIView *view3 = [[[UIView alloc] initWithFrame:frame3] autorelease]; 52 [view1 addSubview:view3]; 53 NSString *actual3 = [view1 subtreeDescription]; 54 NSString *expected3 = [NSString stringWithFormat: 55 @"UIView %p {x:0 y:0 w:100 h:200}\n" 56 @" UIView %p {x:2 y:2 w:102 h:202}\n" 57 @" UIView %p {x:3 y:3 w:103 h:203}\n", 58 view1, view2, view3]; 59 STAssertEqualObjects(actual3, expected3, @"a view with two children"); 60 61 // Test a view with two children, one hidden. 62 [view3 setHidden:YES]; 63 NSString *actual4 = [view1 subtreeDescription]; 64 NSString *expected4 = [NSString stringWithFormat: 65 @"UIView %p {x:0 y:0 w:100 h:200}\n" 66 @" UIView %p {x:2 y:2 w:102 h:202}\n" 67 @" UIView %p {x:3 y:3 w:103 h:203} hid\n", 68 view1, view2, view3]; 69 STAssertEqualObjects(actual4, expected4, @"with two children, one hidden"); 70} 71 72- (void)testSublayersDescription { 73 // Test a single, simple layer. 74 CGRect frame1 = CGRectMake(0, 0, 100, 200); 75 UIView *view1 = [[[UIView alloc] initWithFrame:frame1] autorelease]; 76 NSString *actual = [view1 sublayersDescription]; 77 NSString *expected = [NSString stringWithFormat: 78 @"CALayer %p {x:0 y:0 w:100 h:200}\n", [view1 layer]]; 79 STAssertEqualObjects(actual, expected, @"a single, simple layer failed"); 80 81 // Test a layer with one child. 82 CGRect frame2 = CGRectMake(2, 2, 102, 202); 83 UIView *view2 = [[[UIView alloc] initWithFrame:frame2] autorelease]; 84 [view1 addSubview:view2]; 85 NSString *actual2 = [view1 sublayersDescription]; 86 NSString *expected2 = [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 [view1 layer], [view2 layer]]; 90 STAssertEqualObjects(actual2, expected2, @"a layer with one child"); 91 92 // Test a layer with two children. 93 CGRect frame3 = CGRectMake(3, 3, 103, 203); 94 UIView *view3 = [[[UIView alloc] initWithFrame:frame3] autorelease]; 95 [view1 addSubview:view3]; 96 NSString *actual3 = [view1 sublayersDescription]; 97 NSString *expected3 = [NSString stringWithFormat: 98 @"CALayer %p {x:0 y:0 w:100 h:200}\n" 99 @" CALayer %p {x:2 y:2 w:102 h:202}\n" 100 @" CALayer %p {x:3 y:3 w:103 h:203}\n", 101 [view1 layer], [view2 layer], [view3 layer]]; 102 STAssertEqualObjects(actual3, expected3, @"a layer with two children"); 103 104 // Test a layer with two children, one hidden. 105 [view3 setHidden:YES]; 106 NSString *actual4 = [view1 sublayersDescription]; 107 NSString *expected4 = [NSString stringWithFormat: 108 @"CALayer %p {x:0 y:0 w:100 h:200}\n" 109 @" CALayer %p {x:2 y:2 w:102 h:202}\n" 110 @" CALayer %p {x:3 y:3 w:103 h:203} hid\n", 111 [view1 layer], [view2 layer], [view3 layer]]; 112 STAssertEqualObjects(actual4, expected4, @"with two children, one hidden"); 113} 114 115@end 116 117@interface UIMyTestView : UIView 118- (NSString *)myViewDescriptionLine; 119@end 120 121@implementation UIMyTestView 122- (NSString *)myViewDescriptionLine { 123 NSString *result = [NSString stringWithFormat:@"alpha: %3.1f", [self alpha]]; 124 return result; 125} 126@end 127 128@interface GTMUIView_SubtreeSubClassDescriptionTest : GTMTestCase 129@end 130 131@implementation GTMUIView_SubtreeSubClassDescriptionTest 132- (void)testSubtreeDescription { 133 CGRect frame1 = CGRectMake(0, 0, 100, 200); 134 UIView *view1 = [[[UIView alloc] initWithFrame:frame1] autorelease]; 135 136 // Test a view with one child. 137 CGRect frame2 = CGRectMake(2, 2, 102, 202); 138 UIView *view2 = [[[UIMyTestView alloc] initWithFrame:frame2] autorelease]; 139 [view1 addSubview:view2]; 140 NSString *actual2 = [view1 subtreeDescription]; 141 NSString *expected2 = [NSString stringWithFormat: 142 @"UIView %p {x:0 y:0 w:100 h:200}\n" 143 @" UIMyTestView %p {x:2 y:2 w:102 h:202} alpha: 1.0\n", 144 view1, view2]; 145 STAssertEqualObjects(actual2, expected2, @"a view with one subclassed child"); 146} 147@end 148 149 150#endif