PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/test/UnitTest/Classes/UnitTestViewController.m

https://github.com/vassan/facebook-ios-sdk
Objective C | 321 lines | 203 code | 72 blank | 46 comment | 27 complexity | 339b597d100bbb8729144e10db5b1131 MD5 | raw file
  1. /*
  2. * Copyright 2010 Facebook
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #import "UnitTestViewController.h"
  16. #import "FBConnect.h"
  17. // Your Facebook APP Id must be set before running this example
  18. // See http://www.facebook.com/developers/createapp.php
  19. static NSString* kAppId = @"230820755197";
  20. static NSString* kTestUser =@"499095509";
  21. @implementation UnitTestViewController
  22. //////////////////////////////////////////////////////////////////////////////////////////////////
  23. // Private helper function
  24. - (NSString *) getStringFromUrl: (NSString*) url needle:(NSString *) needle {
  25. NSString * str = nil;
  26. NSRange start = [url rangeOfString:needle];
  27. if (start.location != NSNotFound) {
  28. NSRange end = [[url substringFromIndex:start.location+start.length] rangeOfString:@"&"];
  29. NSUInteger offset = start.location+start.length;
  30. str = end.location == NSNotFound
  31. ? [url substringFromIndex:offset]
  32. : [url substringWithRange:NSMakeRange(offset, end.location)];
  33. str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  34. }
  35. return str;
  36. }
  37. - (void) verifySessionInvalid {
  38. SBJSON *jsonWriter = [[SBJSON new] autorelease];
  39. NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
  40. @"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil];
  41. NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
  42. NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
  43. @"a long run", @"name",
  44. @"The Facebook Running app", @"caption",
  45. @"it is fun", @"description",
  46. @"http://itsti.me/", @"href", nil];
  47. NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
  48. NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  49. kAppId, @"api_key",
  50. @"Share on Facebook", @"user_message_prompt",
  51. actionLinksStr, @"action_links",
  52. attachmentStr, @"attachment",
  53. _token,@"access_token",
  54. nil];
  55. [_facebook dialog: @"stream.publish"
  56. andParams: params
  57. andDelegate:self];
  58. }
  59. //////////////////////////////////////////////////////////////////////////////////////////////////
  60. // UIViewController
  61. /**
  62. * initialization
  63. */
  64. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  65. if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
  66. _permissions = [[NSArray arrayWithObjects:
  67. @"publish_stream",@"read_stream", @"offline_access",nil] retain];
  68. }
  69. return self;
  70. }
  71. /**
  72. * Set initial view
  73. */
  74. - (void) viewDidLoad {
  75. _facebook = [[[[Facebook alloc] init] autorelease] retain];
  76. }
  77. /**
  78. * Test start as view appear
  79. */
  80. - (void) viewDidAppear:(BOOL)animated {
  81. [self startTest];
  82. }
  83. /**
  84. * Start test with authorize as the first step
  85. */
  86. - (void) startTest {
  87. [_facebook authorize:kAppId permissions:_permissions delegate:self];
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////////////////////////
  90. // NSObject
  91. - (void) dealloc {
  92. [_facebook release];
  93. [_permissions release];
  94. [super dealloc];
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////////////////////////
  97. // Test Cases
  98. - (void) testLogin {
  99. NSLog(@"Test authorization");
  100. if (_facebook.accessToken) {
  101. NSLog(@"Test pass, accessToken obtained:%@",_facebook.accessToken);
  102. } else {
  103. NSLog(@"Test fail, unable to obtain accessToken");
  104. }
  105. _token = [_facebook.accessToken retain];
  106. }
  107. - (void) testLogout {
  108. NSLog(@"Test Logout");
  109. [_facebook logout:self];
  110. }
  111. - (void) testStreamPublish {
  112. SBJSON *jsonWriter = [[SBJSON new] autorelease];
  113. NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
  114. @"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil];
  115. NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
  116. NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
  117. @"a long run", @"name",
  118. @"The Facebook Running app", @"caption",
  119. @"it is fun", @"description",
  120. @"http://itsti.me/", @"href", nil];
  121. NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
  122. NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  123. kAppId, @"api_key",
  124. @"Share on Facebook", @"user_message_prompt",
  125. actionLinksStr, @"action_links",
  126. attachmentStr, @"attachment",
  127. nil];
  128. [_facebook dialog: @"stream.publish"
  129. andParams: params
  130. andDelegate:self];
  131. }
  132. - (void) testPublicApi {
  133. NSLog(@"Test Public Api");
  134. NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  135. [NSString stringWithFormat:@"SELECT uid,name FROM user WHERE uid=%@", kTestUser],
  136. @"query",
  137. nil];
  138. [_facebook requestWithMethodName: @"fql.query"
  139. andParams: params
  140. andHttpMethod: @"POST"
  141. andDelegate: self];
  142. }
  143. - (void) testAuthenticatedApi {
  144. NSLog(@"Test Authenticated Api");
  145. [_facebook requestWithGraphPath:@"me" andDelegate:self];
  146. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  147. NSString *dateString;
  148. [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
  149. dateString = [formatter stringFromDate:[NSDate date]];
  150. [formatter release];
  151. NSString *msg = @"Hello World";
  152. msg = [msg stringByAppendingString:dateString];
  153. NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  154. msg, @"message",
  155. nil];
  156. [_facebook requestWithGraphPath:@"me/feed"
  157. andParams:params
  158. andHttpMethod:@"POST"
  159. andDelegate:self];
  160. }
  161. - (void) testApiError {
  162. NSLog(@"Test Authenticated Api Error");
  163. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  164. NSString *dateString;
  165. [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
  166. dateString = [formatter stringFromDate:[NSDate date]];
  167. [formatter release];
  168. NSString *msg = @"Hi World";
  169. msg = [msg stringByAppendingString:dateString];
  170. NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  171. msg, @"message",
  172. nil];
  173. [_facebook requestWithGraphPath:[NSString stringWithFormat:@"%@/feed", kTestUser]
  174. andParams:params
  175. andHttpMethod:@"POST"
  176. andDelegate:self];
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////////////////////////
  179. // FBSessionDelegate
  180. /**
  181. * Start All test when the user logged in
  182. */
  183. - (void) fbDidLogin {
  184. [self testLogin];
  185. [self testPublicApi];
  186. [self testAuthenticatedApi];
  187. [self testApiError];
  188. [self testStreamPublish];
  189. }
  190. -(void) fbDidLogout {
  191. if (_facebook.accessToken != nil) {
  192. NSLog(@"Test fail for test Logout");
  193. } else {
  194. [self verifySessionInvalid];
  195. }
  196. }
  197. ///////////////////////////////////////////////////////////////////////////////////////////////////
  198. // FBRequestDelegate
  199. - (void)request:(FBRequest*)request didLoad:(id)result{
  200. if ([request.url hasPrefix:@"https://api.facebook.com/method/fql.query"]) {
  201. if (![[[result objectAtIndex:0] objectForKey:@"name"]
  202. isEqualToString:@"Maria Diijieeji Letuchywitz"]) {
  203. NSLog(@"Test fail at test Public Api ");
  204. } else {
  205. NSLog(@"Test Success at test Public Api");
  206. }
  207. } else if ([request.url hasPrefix:@"https://graph.facebook.com/me/feed"]) {
  208. NSString *post_id = [result objectForKey:@"id"];
  209. if (post_id.length > 0) {
  210. NSLog(@"Test Success for testAuthenticatedApi POST");
  211. } else {
  212. NSLog(@"Test Fail for testAuthenticatedApi POST");
  213. }
  214. } else if ([request.url hasPrefix:@"https://graph.facebook.com/me"]) {
  215. if ([[result objectForKey:@"name"] length] > 0 ) {
  216. NSLog(@"Test Success at testAuthenticatedApi");
  217. } else {
  218. NSLog(@"Test Fail at testAuthenticatedApi");
  219. }
  220. }
  221. }
  222. - (void)request:(FBRequest*)request didFailWithError:(NSError*)error{
  223. if ([request.url hasPrefix:[NSString stringWithFormat:@"%https://graph.facebook.com/%@/feed",
  224. kTestUser]]) {
  225. if ([[[[error userInfo] objectForKey:@"error"] objectForKey:@"message"]
  226. isEqualToString:@"(#210) User not visible"])
  227. NSLog(@"Test Success at test Authenticated Api Error");
  228. }
  229. }
  230. ///////////////////////////////////////////////////////////////////////////////////////////////////
  231. // FBDialogDelegate
  232. - (void)dialogCompleteWithUrl:(NSURL *)url {
  233. NSString *post_id = [self getStringFromUrl:[url absoluteString] needle:@"post_id="];
  234. if (post_id.length > 0) {
  235. NSLog(@"Test Success for testStreamPublish");
  236. } else {
  237. NSLog(@"Test Fail for testStreamPublish");
  238. }
  239. [self testLogout];
  240. }
  241. - (void)dialog:(FBDialog*)dialog didFailWithError:(NSError*)error {
  242. if (error.code == 190) {
  243. NSLog(@"Test Logout Succeed");
  244. } else {
  245. NSLog(@"Test Logout Fail");
  246. }
  247. }
  248. @end