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

http://macfuse.googlecode.com/ · Objective C · 153 lines · 114 code · 19 blank · 20 comment · 1 complexity · a7c26264a9b09add9190e0930e627502 MD5 · raw file

  1. //
  2. // GTMUILocalizerTest.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 "GTMUILocalizerTest.h"
  20. #import "GTMNSObject+UnitTesting.h"
  21. #import "GTMUILocalizer.h"
  22. @interface GTMUILocalizerTest : GTMTestCase
  23. @end
  24. @implementation GTMUILocalizerTest
  25. // Utility method to verify that all the options for |binding| on |object| have
  26. // been localized.
  27. - (void)verifyBinding:(NSString *)binding forObject:(id)object {
  28. NSDictionary *bindingInfo
  29. = [object infoForBinding:binding];
  30. STAssertNotNil(bindingInfo,
  31. @"Can't get binding info for %@ from %@.\nExposed bindings: %@",
  32. binding, object, [object exposedBindings]);
  33. NSDictionary *bindingOptions = [bindingInfo objectForKey:NSOptionsKey];
  34. STAssertNotNil(bindingOptions, nil);
  35. NSString *key = nil;
  36. GTM_FOREACH_KEY(key, bindingOptions) {
  37. id value = [bindingOptions objectForKey:key];
  38. if ([value isKindOfClass:[NSString class]]) {
  39. STAssertFalse([value hasPrefix:@"^"],
  40. @"Binding option %@ not localized. Has value %@.",
  41. key, value);
  42. }
  43. }
  44. }
  45. - (void)testWindowLocalization {
  46. GTMUILocalizerTestWindowController *controller
  47. = [[GTMUILocalizerTestWindowController alloc] init];
  48. NSWindow *window = [controller window];
  49. STAssertNotNil(window, nil);
  50. GTMAssertObjectStateEqualToStateNamed(window,
  51. @"GTMUILocalizerWindow1State", nil);
  52. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  53. GTMUILocalizer *localizer = [[GTMUILocalizer alloc] initWithBundle:bundle];
  54. window = [controller otherWindow];
  55. STAssertNotNil(window, nil);
  56. [localizer localizeObject:window recursively:YES];
  57. GTMAssertObjectStateEqualToStateNamed(window,
  58. @"GTMUILocalizerWindow2State", nil);
  59. window = [controller anotherWindow];
  60. STAssertNotNil(window, nil);
  61. [localizer localizeObject:window recursively:YES];
  62. GTMAssertObjectStateEqualToStateNamed(window,
  63. @"GTMUILocalizerWindow3State", nil);
  64. NSMenu *menu = [controller otherMenu];
  65. STAssertNotNil(menu, nil);
  66. [localizer localizeObject:menu recursively:YES];
  67. GTMAssertObjectStateEqualToStateNamed(menu,
  68. @"GTMUILocalizerMenuState", nil);
  69. // Test binding localization.
  70. NSTextField *textField = [controller bindingsTextField];
  71. STAssertNotNil(textField, nil);
  72. NSString *displayPatternValue1Binding
  73. = [NSString stringWithFormat:@"%@1", NSDisplayPatternValueBinding];
  74. [self verifyBinding:displayPatternValue1Binding forObject:textField];
  75. NSSearchField *searchField = [controller bindingsSearchField];
  76. STAssertNotNil(searchField, nil);
  77. [self verifyBinding:NSPredicateBinding forObject:searchField];
  78. [localizer release];
  79. [controller release];
  80. }
  81. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  82. - (void)testViewLocalization {
  83. GTMUILocalizerTestViewController *controller
  84. = [[GTMUILocalizerTestViewController alloc] init];
  85. NSView *view = [controller view];
  86. STAssertNotNil(view, nil);
  87. GTMAssertObjectStateEqualToStateNamed(view,
  88. @"GTMUILocalizerView1State", nil);
  89. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  90. GTMUILocalizer *localizer = [[GTMUILocalizer alloc] initWithBundle:bundle];
  91. view = [controller otherView];
  92. STAssertNotNil(view, nil);
  93. [localizer localizeObject:view recursively:YES];
  94. GTMAssertObjectStateEqualToStateNamed(view, @"GTMUILocalizerView2State", nil);
  95. [localizer release];
  96. [controller release];
  97. }
  98. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  99. @end
  100. @implementation GTMUILocalizerTestWindowController
  101. - (id)init {
  102. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  103. return [self initWithWindowNibName:@"GTMUILocalizerTestWindow"];
  104. #else
  105. return [self initWithWindowNibName:@"GTMUILocalizerTestWindow_10_4"];
  106. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  107. }
  108. - (NSWindow *)otherWindow {
  109. return otherWindow_;
  110. }
  111. - (NSWindow *)anotherWindow {
  112. return anotherWindow_;
  113. }
  114. - (NSMenu *)otherMenu {
  115. return otherMenu_;
  116. }
  117. - (NSTextField *)bindingsTextField {
  118. return bindingsTextField_;
  119. }
  120. - (NSSearchField *)bindingsSearchField {
  121. return bindingsSearchField_;
  122. }
  123. @end
  124. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  125. @implementation GTMUILocalizerTestViewController
  126. - (id)init {
  127. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  128. return [self initWithNibName:@"GTMUILocalizerTestView" bundle:bundle];
  129. }
  130. - (NSView *)otherView {
  131. return otherView_;
  132. }
  133. @end
  134. #endif