/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMUILocalizerAndLayoutTweakerTest.m

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