/core/externals/google-toolbox-for-mac/AppKit/GTMGoogleSearchTest.m

http://macfuse.googlecode.com/ · Objective C · 208 lines · 139 code · 22 blank · 47 comment · 2 complexity · ab59a0fe85865e3d9bbfd3ac0e95f5e6 MD5 · raw file

  1. //
  2. // GTMGoogleSearchTest.m
  3. //
  4. // Copyright 2006-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 "GTMGoogleSearch.h"
  19. #import "GTMSenTestCase.h"
  20. #import "GTMUnitTestDevLog.h"
  21. #import <unistd.h>
  22. @interface GTMGoogleSearchTest : GTMTestCase
  23. @end
  24. @implementation GTMGoogleSearchTest
  25. - (void)testSearches {
  26. typedef struct {
  27. NSString *type;
  28. NSString *expectedPrefix;
  29. } TestSearchDesc;
  30. static TestSearchDesc testSearches[] = {
  31. { GTMGoogleSearchFroogle, @"http://www.google.xxx/products?" },
  32. { GTMGoogleSearchGroups, @"http://www.google.xxx/groups?" },
  33. { GTMGoogleSearchImages, @"http://www.google.xxx/images?"},
  34. { GTMGoogleSearchLocal, @"http://www.google.xxx/local?"},
  35. { GTMGoogleSearchNews, @"http://www.google.xxx/news?"},
  36. { GTMGoogleSearchFinance, @"http://www.google.xxx/finance?"},
  37. { GTMGoogleSearchBooks, @"http://www.google.xxx/books?"},
  38. { GTMGoogleSearchWeb, @"http://www.google.xxx/search?"},
  39. };
  40. GTMGoogleSearch *googleSearch = [GTMGoogleSearch sharedInstance];
  41. STAssertNotNil(googleSearch, nil);
  42. // force the current app values so we aren't at the mercy of the
  43. // global setting the users locale.
  44. [googleSearch updatePreferredDomain:@"xxx"
  45. language:@"yyy"
  46. currentApplicationOnly:TRUE];
  47. size_t count = sizeof(testSearches) / sizeof(testSearches[0]);
  48. NSDictionary *globalArgs
  49. = [NSDictionary dictionaryWithObject:@"f" forKey:@"foo"];
  50. [googleSearch setGlobalSearchArguments:globalArgs];
  51. NSDictionary *args = [NSDictionary dictionaryWithObject:@"Baba"
  52. forKey:@"BaR"];
  53. NSString *expectedStrings[] = {
  54. @"oe=UTF-8", @"hl=yyy", @"q=Foobar",
  55. @"foo=f", @"ie=UTF-8", @"BaR=Baba"
  56. };
  57. for (size_t i = 0; i < count; i++) {
  58. // test building the url
  59. NSString *urlString = [googleSearch searchURLFor:@"Foobar"
  60. ofType:testSearches[i].type
  61. arguments:args];
  62. STAssertTrue([urlString hasPrefix:testSearches[i].expectedPrefix],
  63. @"Bad URL? URL:%@ Expected Prefix:%@",
  64. urlString, testSearches[i].expectedPrefix);
  65. for (size_t j = 0;
  66. j < sizeof(expectedStrings) / sizeof(expectedStrings[0]);
  67. ++j) {
  68. STAssertGreaterThan([urlString rangeOfString:expectedStrings[j]].length,
  69. (NSUInteger)0, @"URL: %@ expectedString: %@",
  70. urlString, expectedStrings[j]);
  71. }
  72. }
  73. // clear what we just set for this test
  74. [googleSearch setGlobalSearchArguments:nil];
  75. [googleSearch clearPreferredDomainAndLanguageForCurrentApplication];
  76. }
  77. - (void)testBadInputs {
  78. GTMGoogleSearch *googleSearch = [GTMGoogleSearch sharedInstance];
  79. STAssertNotNil(googleSearch, nil);
  80. NSDictionary *args = [NSDictionary dictionaryWithObject:@"Ba!ba"
  81. forKey:@"Ba=R"];
  82. [GTMUnitTestDevLogDebug expectString:
  83. @"Unescaped string Foo bar in argument pair {q,Foo bar } "
  84. @"in -[GTMGoogleSearch searchURLFor:ofType:arguments:]"];
  85. [GTMUnitTestDevLogDebug expectString:
  86. @"Unescaped string Ba=R in argument pair {Ba=R, Ba!ba} "
  87. @"in -[GTMGoogleSearch searchURLFor:ofType:arguments:]"];
  88. [GTMUnitTestDevLogDebug expectString:
  89. @"Unescaped string Ba!ba in argument pair {Ba=R,Ba!ba } "
  90. @"in -[GTMGoogleSearch searchURLFor:ofType:arguments:]"];
  91. NSString *urlString = [googleSearch searchURLFor:@"Foo bar"
  92. ofType:GTMGoogleSearchFroogle
  93. arguments:args];
  94. STAssertNotNil(urlString, nil);
  95. }
  96. - (void)testPreferredDefaults {
  97. GTMGoogleSearch *googleSearch = [GTMGoogleSearch sharedInstance];
  98. STAssertNotNil(googleSearch, nil);
  99. // hey, we're a unit test, so start by blowing away what we have at the
  100. // app level.
  101. [googleSearch clearPreferredDomainAndLanguageForCurrentApplication];
  102. // in theory, we could fetch now and save off what we get to reset at the
  103. // end of this, but we can't tell if that was an "all apps" setting, or if
  104. // it was the default, so...hey, we're a unit test, we'll just stomp what's
  105. // there and clear it out when done...
  106. [googleSearch clearPreferredDomainAndLanguageForAllApps];
  107. // make sure the individual accessors work...
  108. // since they system level default can be set by any app, we just have to
  109. // check for non nil here (also the users locale could control what
  110. // we get if nothing is set).
  111. NSString *domain;
  112. NSString *lang;
  113. // now do a detailed check...
  114. BOOL areCurrentAppOnly = YES;
  115. [googleSearch preferredDomain:&domain
  116. language:&lang
  117. areCurrentAppOnly:&areCurrentAppOnly];
  118. // should get something for defaults...
  119. STAssertNotNil(domain, nil);
  120. STAssertNotNil(lang, nil);
  121. STAssertFalse(areCurrentAppOnly, nil);
  122. // test it for "all apps"...
  123. [googleSearch updatePreferredDomain:@"domain"
  124. language:@"lang"
  125. currentApplicationOnly:NO];
  126. [googleSearch preferredDomain:&domain
  127. language:&lang
  128. areCurrentAppOnly:&areCurrentAppOnly];
  129. STAssertEqualObjects(domain, @"domain", nil);
  130. STAssertEqualObjects(lang, @"lang", nil);
  131. STAssertFalse(areCurrentAppOnly, nil);
  132. // test it for this app...
  133. [googleSearch updatePreferredDomain:@"domainThisApp"
  134. language:@"langThisApp"
  135. currentApplicationOnly:YES];
  136. [googleSearch preferredDomain:&domain
  137. language:&lang
  138. areCurrentAppOnly:&areCurrentAppOnly];
  139. STAssertEqualObjects(domain, @"domainThisApp", nil);
  140. STAssertEqualObjects(lang, @"langThisApp", nil);
  141. STAssertTrue(areCurrentAppOnly, nil);
  142. // clear what we just set for this app
  143. [googleSearch clearPreferredDomainAndLanguageForCurrentApplication];
  144. // should get back what we set for all apps
  145. [googleSearch preferredDomain:&domain
  146. language:&lang
  147. areCurrentAppOnly:&areCurrentAppOnly];
  148. STAssertEqualObjects(domain, @"domain", nil);
  149. STAssertEqualObjects(lang, @"lang", nil);
  150. STAssertFalse(areCurrentAppOnly, nil);
  151. #if GTM_GOOGLE_SEARCH_SUPPORTS_DISTRIBUTED_NOTIFICATIONS
  152. // We don't test launching other tasks on the phone since this isn't a valid
  153. // case until we can support real multiple tasks on the phone.
  154. // try changing the value directly in the plist file (as if another app had
  155. // done it) and sending our notification.
  156. [[NSTask launchedTaskWithLaunchPath:@"/usr/bin/defaults"
  157. arguments:[NSArray arrayWithObjects:@"write",
  158. @"com.google.GoogleSearchAllApps",
  159. @"{ \"com.google.PreferredDomain\" = xxx;"
  160. @"\"com.google.PreferredLanguage\" = yyy; }",
  161. nil]] waitUntilExit];
  162. // Sleep for a moment to let things flush
  163. // (seen rarely as a problem on aharper's machine).
  164. sleep(1);
  165. NSDistributedNotificationCenter *distCenter =
  166. [NSDistributedNotificationCenter defaultCenter];
  167. [distCenter postNotificationName:@"com.google.GoogleSearchAllApps.prefsWritten"
  168. object:nil
  169. userInfo:nil
  170. options:NSNotificationDeliverImmediately];
  171. // Spin the runloop so the notifications fire.
  172. NSRunLoop *currentLoop = [NSRunLoop currentRunLoop];
  173. [currentLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
  174. // did we get what we expected?
  175. [googleSearch preferredDomain:&domain
  176. language:&lang
  177. areCurrentAppOnly:&areCurrentAppOnly];
  178. STAssertEqualObjects(domain, @"xxx", nil);
  179. STAssertEqualObjects(lang, @"yyy", nil);
  180. STAssertFalse(areCurrentAppOnly, nil);
  181. #endif // GTM_GOOGLE_SEARCH_SUPPORTS_DISTRIBUTED_NOTIFICATIONS
  182. // lastly, clean up what we set for all apps to leave the system at the
  183. // default.
  184. [googleSearch clearPreferredDomainAndLanguageForAllApps];
  185. }
  186. @end