/***
 * Excerpted from "iOS SDK Development",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/adios for more book information.
***/
//
//  RecipeUnitTests.m
//  RecipeUnitTests
//
//  Created by Chris Adamson on 9/20/12.
//  Copyright (c) 2012 Pragmatic Programmers LLC. All rights reserved.
//

#import "RecipeUnitTests.h"
#import "PRPRecipe.h"
#import "PRPRecipesDocument.h"

@implementation RecipeUnitTests

- (void)setUp
{
    [super setUp];
    
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

//
-(void) testPersistence {
  NSURL *docDir = [[[NSFileManager defaultManager]
                    URLsForDirectory:NSDocumentDirectory
                    inDomains:NSUserDomainMask] lastObject];
  NSURL *docURL = [docDir URLByAppendingPathComponent:@"Recipes.recipes"];
  PRPRecipesDocument *recipesDocForSaving =
  [[PRPRecipesDocument alloc] initWithFileURL:docURL];
  PRPRecipe *recipeToSave = [recipesDocForSaving createNewRecipe];
  //
  //
  STAssertNotNil(recipeToSave, @"Failed to create recipe from document");
  //
  //
  recipeToSave.title = NSLocalizedString(@"Nachos", nil);
  recipeToSave.directions = NSLocalizedString(
    @"Open bag\nOpen jar of salsa\nEnjoy", nil);
  recipeToSave.preparationTime = [NSNumber numberWithInt:1];
  [recipesDocForSaving recipesChanged];
  [recipesDocForSaving closeWithCompletionHandler:^(BOOL success) {
    STAssertTrue(success, @"failed to save recipes doc");
  }];
  [[NSRunLoop currentRunLoop] runUntilDate:
   [NSDate dateWithTimeIntervalSinceNow:30.0]];
  //
  
  //
  PRPRecipesDocument *recipesDocForLoading = [[PRPRecipesDocument alloc]
                                              initWithFileURL:docURL];
  [recipesDocForLoading openWithCompletionHandler:^(BOOL success) {
    STAssertTrue(success, @"failed to open recipesDocForLoading");
    NSInteger recipeCount = [recipesDocForLoading recipeCount] ;
    STAssertEquals(recipeCount, 1, @"Wrong number of recipes: %d", recipeCount);
    if (recipeCount > 0) {
      PRPRecipe *recipeToLoad = [recipesDocForLoading recipeAtIndex:0];
      STAssertNotNil(recipeToLoad, @"Couldn't load first recipe");
      //
      if (! [recipeToLoad.title isEqualToString:
             NSLocalizedString(@"Nachos", nil)]) {
        STFail (@"First recipe has wrong title: %@", recipeToLoad.title);
      }
    }
    //
  }];
  [[NSRunLoop currentRunLoop] runUntilDate:
   [NSDate dateWithTimeIntervalSinceNow:2.0]];
}
//


/* This worked in iOS 5, but the closeWithCompletionHandler block is never
 called in iOS 6.0, so we have to abandon this approach for now.

 Filed with Apple as #12214822
 Also at http://openradar.appspot.com/radar?id=2015401
 
 // this version uses blockDone flag and shorter NSRunLoop waits
 -(void) testPersistence2 {
   NSURL *docDir = [[[NSFileManager defaultManager]
                     URLsForDirectory:NSDocumentDirectory
                     inDomains:NSUserDomainMask] lastObject];
   NSURL *docURL = [docDir URLByAppendingPathComponent:@"Recipes.recipes"];
   PRPRecipesDocument *recipesDocForSaving = [[PRPRecipesDocument alloc] initWithFileURL:docURL];
   PRPRecipe *recipeToSave = [recipesDocForSaving createNewRecipe];
   STAssertNotNil(recipeToSave, @"Failed to create recipe from document");
   recipeToSave.title = NSLocalizedString(@"Nachos", nil);
   recipeToSave.directions = NSLocalizedString(@"Open bag\nOpen jar of salsa\nEnjoy", nil);
   recipeToSave.preparationTime = [NSNumber numberWithInt:1];
   [recipesDocForSaving recipesChanged];
   //
   __block BOOL blockDone = NO;
   [recipesDocForSaving closeWithCompletionHandler:^(BOOL success) {
     NSLog (@"completion handler!");
     STAssertTrue(success, @"failed to save recipes doc");
     blockDone = YES;
   }];
   while (!blockDone) {
     [[NSRunLoop currentRunLoop] runUntilDate:
      [NSDate dateWithTimeIntervalSinceNow:0.1]];
   }
   //
   
   PRPRecipesDocument *recipesDocForLoading = [[PRPRecipesDocument alloc] initWithFileURL:docURL];
   blockDone = NO;
   [recipesDocForLoading openWithCompletionHandler:^(BOOL success) {
     STAssertTrue(success, @"failed to open recipesDocForLoading");
     NSInteger recipeCount = [recipesDocForLoading recipeCount] ;
     STAssertEquals(recipeCount, 1, @"Wrong number of recipes: %d", recipeCount);
     PRPRecipe *recipeToLoad = [recipesDocForLoading recipeAtIndex:0];
     STAssertNotNil(recipeToLoad, @"Couldn't load first recipe");
     if (! [recipeToLoad.title isEqualToString:NSLocalizedString(@"Nachos", nil)]) {
       STFail (@"First recipe has wrong title: %@", recipeToLoad.title);
     }
     blockDone = YES;
   }];
   
   while (!blockDone) {
     [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
   }
 
 }
 */

@end
