/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMUILocalizerAndLayoutTweakerTest.m
Objective C | 325 lines | 264 code | 23 blank | 38 comment | 17 complexity | e6a141dbb2619bbc9cc5cee677dd1371 MD5 | raw file
1// 2// GTMUILocalizerAndLayoutTweakerTest.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 under 16// the License. 17// 18 19#import "GTMSenTestCase.h" 20#import "GTMUILocalizerAndLayoutTweakerTest.h" 21#import "GTMNSObject+UnitTesting.h" 22#import "GTMUILocalizerAndLayoutTweaker.h" 23 24static NSUInteger gTestPass = 0; 25 26@interface GTMUILocalizerAndLayoutTweakerTest : GTMTestCase 27@end 28 29@implementation GTMUILocalizerAndLayoutTweakerTest 30 31- (void)testWindowLocalization { 32 // Test with nib 1 33 for (gTestPass = 0; gTestPass < 3; ++gTestPass) { 34 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 35 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 36 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest1"]; 37 NSWindow *window = [controller window]; 38 STAssertNotNil(window, @"Pass %zu", gTestPass); 39 NSString *imageName = 40 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest1-%ld", 41 (long)gTestPass]; 42 GTMAssertObjectImageEqualToImageNamed(window, imageName, 43 @"Pass %zu", gTestPass); 44 [controller release]; 45 } 46 // Test with nib 2 47 for (gTestPass = 0; gTestPass < 3; ++gTestPass) { 48 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 49 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 50 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest2"]; 51 NSWindow *window = [controller window]; 52 STAssertNotNil(window, @"Pass %zu", gTestPass); 53 NSString *imageName = 54 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest2-%ld", 55 (long)gTestPass]; 56 GTMAssertObjectImageEqualToImageNamed(window, imageName, 57 @"Pass %zu", gTestPass); 58 [controller release]; 59 } 60} 61 62- (void)testSizeToFitFixedWidthTextField { 63 // In the xib, the one field is over sized, the other is undersized, this 64 // way we make sure the code handles both condions as there was a bahavior 65 // change between the 10.4 and 10.5 SDKs. 66 // The right field is also right aligned to make sure the width of the text 67 // field stays constant. 68 NSString *kTestStrings[] = { 69 @"The fox jumps the dog.", 70 @"The quick brown fox jumps over the lazy dog.", 71 @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps " 72 @"over the lazy dog. The quick brown fox jumps over the lazy dog. " 73 @"The quick brown fox jumps over the lazy dog. The quick brown fox " 74 @"jumps over the lazy dog.", 75 @"The quick brown fox jumps over the lazy dog.\nThe quick brown fox jumps " 76 @"over the lazy dog.\nThe quick brown fox jumps over the lazy dog.\n" 77 @"The quick brown fox jumps over the lazy dog.\nThe quick brown fox " 78 @"jumps over the lazy dog.", 79 @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps " 80 @"over the lazy dog.\nThe quick brown fox jumps over the lazy dog. " 81 @"The quick brown fox jumps over the lazy dog. The quick brown fox " 82 @"jumps over the lazy dog. The quick brown fox jumps over the lazy " 83 @"dog. The quick brown fox jumps over the lazy dog.\n\nThe End.", 84 }; 85 for (size_t lp = 0; 86 lp < (sizeof(kTestStrings) / sizeof(kTestStrings[0])); 87 ++lp) { 88 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 89 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 90 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest3"]; 91 NSWindow *window = [controller window]; 92 STAssertNotNil(window, @"Pass %zu", lp); 93 NSTextField *field; 94 GTM_FOREACH_OBJECT(field, [[window contentView] subviews]) { 95 STAssertTrue([field isMemberOfClass:[NSTextField class]], 96 @"Pass %zu", lp); 97 [field setStringValue:kTestStrings[lp]]; 98 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:field]; 99 } 100 NSString *imageName = 101 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest3-%ld", 102 (long)lp]; 103 GTMAssertObjectImageEqualToImageNamed(window, imageName, 104 @"Pass %zu", lp); 105 [controller release]; 106 } 107} 108 109- (void)testButtonStyleLocalizations { 110 // Since we special case standard push buttons, test all button types. 111 112 // This also tests the vertical vs. horizontal layout code on widthbox, if 113 // you look at the xib in IB, turn on the bounds rectagle display, and you'll 114 // see how IB's left alignment is a visual alignment, it doesn't actually 115 // align the bounds of the views. 116 117 for (gTestPass = 0; gTestPass < 3; ++gTestPass) { 118 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 119 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 120 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest4"]; 121 NSWindow *window = [controller window]; 122 STAssertNotNil(window, @"Pass %zu", gTestPass); 123 NSString *imageName = 124 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest4-%ld", 125 (long)gTestPass]; 126 GTMAssertObjectImageEqualToImageNamed(window, imageName, 127 @"Pass %zu", gTestPass); 128 [controller release]; 129 } 130} 131 132- (void)testWrappingForWidth { 133 NSString *kTestStrings[] = { 134 @"The fox jumps the dog.", 135 @"The quick brown fox jumps over the lazy dog.", 136 @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps " 137 @"over the lazy dog. The quick brown fox jumps over the lazy dog. " 138 @"The quick brown fox jumps over the lazy dog.", 139 }; 140 for (size_t lp = 0; 141 lp < (sizeof(kTestStrings) / sizeof(kTestStrings[0])); 142 ++lp) { 143 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 144 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 145 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest5"]; 146 NSWindow *window = [controller window]; 147 STAssertNotNil(window, @"Pass %zu", lp); 148 NSView *view; 149 GTM_FOREACH_OBJECT(view, [[window contentView] subviews]) { 150 if ([view isMemberOfClass:[NSButton class]]) { 151 NSButton *btn = (id)view; 152 [btn setTitle:kTestStrings[lp]]; 153 [GTMUILocalizerAndLayoutTweaker wrapButtonTitleForWidth:btn]; 154 } else { 155 STAssertTrue([view isMemberOfClass:[NSMatrix class]], 156 @"Pass %zu", lp); 157 NSMatrix *mtx = (id)view; 158 NSCell *cell; 159 int i = 0; 160 GTM_FOREACH_OBJECT(cell, [mtx cells]) { 161 [cell setTitle:[NSString stringWithFormat:@"%d %@", 162 ++i, kTestStrings[lp]]]; 163 } 164 [GTMUILocalizerAndLayoutTweaker wrapRadioGroupForWidth:mtx]; 165 } 166 [GTMUILocalizerAndLayoutTweaker sizeToFitView:view]; 167 } 168 NSString *imageName = 169 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest5-%ld", 170 (long)lp]; 171 GTMAssertObjectImageEqualToImageNamed(window, imageName, 172 @"Pass %zu", lp); 173 [controller release]; 174 } 175} 176 177- (void)testTabViewLocalization { 178 // Test with nib 6 179 for (gTestPass = 0; gTestPass < 3; ++gTestPass) { 180 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 181 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 182 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest6"]; 183 NSWindow *window = [controller window]; 184 STAssertNotNil(window, @"Pass %zu", gTestPass); 185 NSTabView *tabView = [controller tabView]; 186 for (NSInteger tabIndex = 0; tabIndex < [tabView numberOfTabViewItems]; 187 ++tabIndex) { 188 [tabView selectTabViewItemAtIndex:tabIndex]; 189 NSString *imageName = 190 [NSString stringWithFormat: 191 @"GTMUILocalizerAndLayoutTweakerTest6-tab%ld-%ld", 192 (long)tabIndex, (long)gTestPass]; 193 GTMAssertObjectImageEqualToImageNamed(window, imageName, 194 @"Pass %zu", gTestPass); 195 } 196 [controller release]; 197 } 198 // Test with nib 2 199 for (gTestPass = 0; gTestPass < 3; ++gTestPass) { 200 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 201 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 202 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest2"]; 203 NSWindow *window = [controller window]; 204 STAssertNotNil(window, @"Pass %zu", gTestPass); 205 NSString *imageName = 206 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest2-%ld", 207 (long)gTestPass]; 208 GTMAssertObjectImageEqualToImageNamed(window, imageName, 209 @"Pass %zu", gTestPass); 210 [controller release]; 211 } 212} 213 214#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 215 216- (void)testSizeToFitFixedHeightTextField { 217 struct { 218 const char *name; 219 NSUInteger minWidth; 220 } kTestModes[] = { 221 { "NoMin", 0 }, 222 { "Min", 450 }, 223 }; 224 NSString *kTestStrings[] = { 225 @"The fox jumps the dog.", 226 @"The quick brown fox jumps over the lazy dog.", 227 @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps " 228 @"over the lazy dog. The quick brown fox jumps over the lazy dog. " 229 @"The quick brown fox jumps over the lazy dog. The quick brown fox " 230 @"jumps over the lazy dog.", 231 @"The quick brown fox jumps over the lazy dog. The quick brown fox jumps " 232 @"over the lazy dog. The quick brown fox jumps over the lazy dog. " 233 @"The quick brown fox jumps over the lazy dog. The quick brown fox " 234 @"jumps over the lazy dog. The quick brown fox jumps over the lazy " 235 @"dog. The quick brown fox jumps over the lazy dog. The End.", 236 }; 237 for (size_t modeLoop = 0; 238 modeLoop < (sizeof(kTestModes) / sizeof(kTestModes[0])); 239 ++modeLoop) { 240 for (size_t lp = 0; 241 lp < (sizeof(kTestStrings) / sizeof(kTestStrings[0])); 242 ++lp) { 243 GTMUILocalizerAndLayoutTweakerTestWindowController *controller = 244 [[GTMUILocalizerAndLayoutTweakerTestWindowController alloc] 245 initWithWindowNibName:@"GTMUILocalizerAndLayoutTweakerTest7"]; 246 NSWindow *window = [controller window]; 247 STAssertNotNil(window, @"Pass %zu", lp); 248 NSTextField *field; 249 GTM_FOREACH_OBJECT(field, [[window contentView] subviews]) { 250 STAssertTrue([field isMemberOfClass:[NSTextField class]], 251 @"Pass %zu", lp); 252 [field setStringValue:kTestStrings[lp]]; 253 NSUInteger minWidth = kTestModes[modeLoop].minWidth; 254 if (minWidth) { 255 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedHeightTextField:field 256 minWidth:minWidth]; 257 } else { 258 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedHeightTextField:field]; 259 } 260 } 261 NSString *imageName = 262 [NSString stringWithFormat:@"GTMUILocalizerAndLayoutTweakerTest7-%s-%ld", 263 kTestModes[modeLoop].name, (long)lp]; 264 GTMAssertObjectImageEqualToImageNamed(window, imageName, 265 @"Pass %zu-%zu", modeLoop, lp); 266 [controller release]; 267 } 268 } 269} 270 271- (void)testSizeToFitFixedHeightTextFieldIntegral { 272 NSTextField* textField = 273 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 50)]; 274 [textField setBezeled:NO]; 275 [textField setStringValue:@"The quick brown fox jumps over the lazy dog."]; 276 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedHeightTextField:textField]; 277 STAssertTrue( 278 NSEqualRects([textField bounds], NSIntegralRect([textField bounds])), 279 nil); 280 [textField release]; 281} 282 283#endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 284 285@end 286 287@implementation GTMUILocalizerAndLayoutTweakerTestWindowController 288 289- (NSTabView *)tabView { 290 return tabView_; 291} 292 293@end 294 295@implementation GTMUILocalizerAndLayoutTweakerTestLocalizer 296 297- (NSString *)localizedStringForString:(NSString *)string { 298 // "[FRAGMENT]:[COUNT]:[COUNT]" 299 // String is split, fragment is repeated count times, and spaces are then 300 // trimmed. Gives us strings that don't change for the test, but easy to 301 // vary in size. Which count is used, is controled by |gTestPass| so we 302 // can use a nib more then once. 303 NSArray *parts = [string componentsSeparatedByString:@":"]; 304 if ([parts count] > (gTestPass + 1)) { 305 NSString *fragment = [parts objectAtIndex:0]; 306 NSInteger count = [[parts objectAtIndex:gTestPass + 1] intValue]; 307 if (count) { 308 NSMutableString *result = 309 [NSMutableString stringWithCapacity:count * [fragment length]]; 310 while (count--) { 311 [result appendString:fragment]; 312 } 313 NSCharacterSet *ws = [NSCharacterSet whitespaceCharacterSet]; 314 return [result stringByTrimmingCharactersInSet:ws]; 315 } 316 } 317 return nil; 318} 319 320- (void)awakeFromNib { 321 // Stop the base one from logging or doing anything since we don't need it 322 // for this test. 323} 324 325@end