PageRenderTime 44ms CodeModel.GetById 1ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 1ms

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