You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2012/07/10 00:40:20 UTC
[1/7] ios commit: Added unit test for
CDVLocalStorage::__verifyAndFixDatabaseLocations
Updated Branches:
refs/heads/master 95cfc5121 -> 4de303857
Added unit test for CDVLocalStorage::__verifyAndFixDatabaseLocations
Additional changes:
* Refactored __verifyAndFixDatabaseLocations into two methods so that it
can be tested without touching the file system.
* Added a FakeFileManager for stubbing out the call to fileExistsAtPath
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/4de30385
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/4de30385
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/4de30385
Branch: refs/heads/master
Commit: 4de3038575653c688fe5384f7e15a6bb14860ea4
Parents: f2d9939
Author: Andrew Grieve <ag...@chromium.org>
Authored: Fri Jul 6 11:15:32 2012 -0400
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Mon Jul 9 14:31:35 2012 -0700
----------------------------------------------------------------------
CordovaLib/Classes/CDVLocalStorage.h | 5 +-
CordovaLib/Classes/CDVLocalStorage.m | 38 +++++++-----
CordovaLib/CordovaLib.xcodeproj/project.pbxproj | 6 ++
CordovaLib/CordovaLibTests/CDVFakeFileManager.h | 35 +++++++++++
CordovaLib/CordovaLibTests/CDVFakeFileManager.m | 45 ++++++++++++++
CordovaLib/CordovaLibTests/CDVLocalStorageTests.m | 51 +++++++++++++++-
6 files changed, 160 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/4de30385/CordovaLib/Classes/CDVLocalStorage.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVLocalStorage.h b/CordovaLib/Classes/CDVLocalStorage.h
index 88becaf..d9a0235 100644
--- a/CordovaLib/Classes/CDVLocalStorage.h
+++ b/CordovaLib/Classes/CDVLocalStorage.h
@@ -33,7 +33,10 @@
- (void) verifyAndFixDatabaseLocations:(NSArray*)arguments withDict:(NSMutableDictionary*)options;
+ (void) __verifyAndFixDatabaseLocations;
-
+// Visible for testing.
++ (BOOL) __verifyAndFixDatabaseLocationsWithAppPlistDict:(NSMutableDictionary*)appPlistDict
+ bundlePath:(NSString*)bundlePath
+ fileManager:(NSFileManager*)fileManager;
@end
@interface CDVBackupInfo : NSObject
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/4de30385/CordovaLib/Classes/CDVLocalStorage.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVLocalStorage.m b/CordovaLib/Classes/CDVLocalStorage.m
index 3b8a8a6..5dc36da 100644
--- a/CordovaLib/Classes/CDVLocalStorage.m
+++ b/CordovaLib/Classes/CDVLocalStorage.m
@@ -260,18 +260,29 @@
+ (void) __verifyAndFixDatabaseLocations
{
- NSString* libraryCaches = @"Library/Caches";
- NSString* libraryWebKit = @"Library/WebKit";
- NSString* libraryPreferences = @"Library/Preferences";
-
- NSUserDefaults* appPreferences = [NSUserDefaults standardUserDefaults];
NSBundle* mainBundle = [NSBundle mainBundle];
-
NSString* bundlePath = [[mainBundle bundlePath] stringByDeletingLastPathComponent];
NSString* bundleIdentifier = [[mainBundle infoDictionary] objectForKey:@"CFBundleIdentifier"];
-
- NSString* appPlistPath = [[bundlePath stringByAppendingPathComponent:libraryPreferences] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", bundleIdentifier]];
+ NSString* appPlistPath = [bundlePath stringByAppendingPathComponent:[NSString stringWithFormat:@"Library/Preferences/%@.plist", bundleIdentifier]];
+
NSMutableDictionary* appPlistDict = [NSMutableDictionary dictionaryWithContentsOfFile:appPlistPath];
+ BOOL modified = [[self class] __verifyAndFixDatabaseLocationsWithAppPlistDict:appPlistDict
+ bundlePath:bundlePath
+ fileManager:[NSFileManager defaultManager]];
+
+ if (modified) {
+ BOOL ok = [appPlistDict writeToFile:appPlistPath atomically:YES];
+ [[NSUserDefaults standardUserDefaults] synchronize];
+ NSLog(@"Fix applied for database locations?: %@", ok? @"YES":@"NO");
+ }
+}
+
++ (BOOL) __verifyAndFixDatabaseLocationsWithAppPlistDict:(NSMutableDictionary*)appPlistDict
+ bundlePath:(NSString*)bundlePath
+ fileManager:(NSFileManager*)fileManager
+{
+ NSString* libraryCaches = @"Library/Caches";
+ NSString* libraryWebKit = @"Library/WebKit";
NSArray* keysToCheck = [NSArray arrayWithObjects:
@"WebKitLocalStorageDatabasePathPreferenceKey",
@@ -289,21 +300,14 @@
// the pathSuffix to use may be wrong - OTA upgrades from < 5.1 to 5.1 do keep the old path Library/WebKit,
// while Xcode synced ones do change the storage location to Library/Caches
NSString* newBundlePath = [bundlePath stringByAppendingPathComponent:libraryCaches];
- if (![[NSFileManager defaultManager] fileExistsAtPath:newBundlePath]) {
+ if (![fileManager fileExistsAtPath:newBundlePath]) {
newBundlePath = [bundlePath stringByAppendingPathComponent:libraryWebKit];
}
[appPlistDict setValue:newBundlePath forKey:key];
dirty = YES;
}
}
-
- if (dirty)
- {
- BOOL ok = [appPlistDict writeToFile:appPlistPath atomically:YES];
- NSLog(@"Fix applied for database locations?: %@", ok? @"YES":@"NO");
-
- [appPreferences synchronize];
- }
+ return dirty;
}
#pragma mark -
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/4de30385/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
----------------------------------------------------------------------
diff --git a/CordovaLib/CordovaLib.xcodeproj/project.pbxproj b/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
index 8f149e0..bd63d59 100644
--- a/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
+++ b/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
@@ -187,6 +187,7 @@
C937A4561337599E002C4C79 /* CDVFileTransfer.h in Headers */ = {isa = PBXBuildFile; fileRef = C937A4541337599E002C4C79 /* CDVFileTransfer.h */; };
C937A4571337599E002C4C79 /* CDVFileTransfer.m in Sources */ = {isa = PBXBuildFile; fileRef = C937A4551337599E002C4C79 /* CDVFileTransfer.m */; };
EBA3550D15A5E8D100F4DE24 /* VERSION in Resources */ = {isa = PBXBuildFile; fileRef = 30325A0B136B343700982B63 /* VERSION */; };
+ EBA3554615A731F100F4DE24 /* CDVFakeFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = EBA3554515A731F100F4DE24 /* CDVFakeFileManager.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -308,6 +309,8 @@
C937A4541337599E002C4C79 /* CDVFileTransfer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDVFileTransfer.h; path = Classes/CDVFileTransfer.h; sourceTree = "<group>"; };
C937A4551337599E002C4C79 /* CDVFileTransfer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CDVFileTransfer.m; path = Classes/CDVFileTransfer.m; sourceTree = "<group>"; };
EBA3550F15A5F18900F4DE24 /* CDVWebViewTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CDVWebViewTest.h; sourceTree = "<group>"; };
+ EBA3554415A731F100F4DE24 /* CDVFakeFileManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CDVFakeFileManager.h; sourceTree = "<group>"; };
+ EBA3554515A731F100F4DE24 /* CDVFakeFileManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CDVFakeFileManager.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -455,6 +458,8 @@
686357B0141002F100DF4CF2 /* CordovaLibTests */ = {
isa = PBXGroup;
children = (
+ EBA3554415A731F100F4DE24 /* CDVFakeFileManager.h */,
+ EBA3554515A731F100F4DE24 /* CDVFakeFileManager.m */,
EBA3550F15A5F18900F4DE24 /* CDVWebViewTest.h */,
30B342F415224B360070E6A5 /* CDVWebViewTest.m */,
30D1B08B15A2B36D0060C291 /* CDVBase64Tests.m */,
@@ -928,6 +933,7 @@
3062D1AE151D4D9D000D9128 /* CDVLocalStorageTests.m in Sources */,
30B342F515224B360070E6A5 /* CDVWebViewTest.m in Sources */,
30D1B08C15A2B36D0060C291 /* CDVBase64Tests.m in Sources */,
+ EBA3554615A731F100F4DE24 /* CDVFakeFileManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/4de30385/CordovaLib/CordovaLibTests/CDVFakeFileManager.h
----------------------------------------------------------------------
diff --git a/CordovaLib/CordovaLibTests/CDVFakeFileManager.h b/CordovaLib/CordovaLibTests/CDVFakeFileManager.h
new file mode 100644
index 0000000..eaf38ed
--- /dev/null
+++ b/CordovaLib/CordovaLibTests/CDVFakeFileManager.h
@@ -0,0 +1,35 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+typedef BOOL (^CDVFileExistsBlock)(NSString*);
+
+// Used in place of an NSFileManager for unit tests. It implements only those functions
+// which are required by the tests that use it.
+@interface CDVFakeFileManager : NSObject {
+ @private
+ CDVFileExistsBlock _fileExistsBlock;
+}
+
+- (id)initWithFileExistsBlock:(CDVFileExistsBlock)fileExistsBlock;
++ (id)managerWithFileExistsBlock:(CDVFileExistsBlock)fileExistsBlock;
+
+- (BOOL)fileExistsAtPath:(NSString*)path;
+@end
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/4de30385/CordovaLib/CordovaLibTests/CDVFakeFileManager.m
----------------------------------------------------------------------
diff --git a/CordovaLib/CordovaLibTests/CDVFakeFileManager.m b/CordovaLib/CordovaLibTests/CDVFakeFileManager.m
new file mode 100644
index 0000000..edbaad6
--- /dev/null
+++ b/CordovaLib/CordovaLibTests/CDVFakeFileManager.m
@@ -0,0 +1,45 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import "CDVFakeFileManager.h"
+
+@implementation CDVFakeFileManager
+
+- (id)initWithFileExistsBlock:(CDVFileExistsBlock)fileExistsBlock {
+ self = [super init];
+ if (self != nil) {
+ _fileExistsBlock = [fileExistsBlock copy];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [_fileExistsBlock release];
+ [super dealloc];
+}
+
++ (id)managerWithFileExistsBlock:(CDVFileExistsBlock)fileExistsBlock {
+ return [[[CDVFakeFileManager alloc] initWithFileExistsBlock:fileExistsBlock] autorelease];
+}
+
+- (BOOL)fileExistsAtPath:(NSString*)path {
+ return _fileExistsBlock(path);
+}
+
+@end
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/4de30385/CordovaLib/CordovaLibTests/CDVLocalStorageTests.m
----------------------------------------------------------------------
diff --git a/CordovaLib/CordovaLibTests/CDVLocalStorageTests.m b/CordovaLib/CordovaLibTests/CDVLocalStorageTests.m
index fd4b67a..ad936dc 100644
--- a/CordovaLib/CordovaLibTests/CDVLocalStorageTests.m
+++ b/CordovaLib/CordovaLibTests/CDVLocalStorageTests.m
@@ -21,6 +21,7 @@
#import "CDVLocalStorage.h"
#import "CDVWebViewTest.h"
+#import "CDVFakeFileManager.h"
#import "ViewController.h"
@interface CDVLocalStorageTests : CDVWebViewTest
@@ -84,9 +85,55 @@
STAssertFalse([localStorage shouldRestore], @"Restore did not complete successfully");
}
-- (void)testVerifyAndFixDatabaseLocations
+- (void)testVerifyAndFixDatabaseLocations_noChangeRequired
{
- STAssertTrue(NO, @"TODO: testVerifyAndFixDatabaseLocations");
+ NSString* const kBundlePath = @"/bpath";
+ id fakeFileManager = [CDVFakeFileManager managerWithFileExistsBlock:^(NSString* path) {
+ STFail(@"fileExists called.");
+ return NO;
+ }];
+ NSMutableDictionary* appPlistDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ @"/bpath/foo", @"WebKitLocalStorageDatabasePathPreferenceKey",
+ @"/bpath/foo", @"WebDatabaseDirectory",
+ nil];
+ BOOL modified = [CDVLocalStorage __verifyAndFixDatabaseLocationsWithAppPlistDict:appPlistDict
+ bundlePath:kBundlePath
+ fileManager:fakeFileManager];
+ STAssertFalse(modified, @"Should not have applied fix.");
+}
+
+- (void)testVerifyAndFixDatabaseLocations_changeRequired1
+{
+ NSString* const kBundlePath = @"/bpath";
+ id fakeFileManager = [CDVFakeFileManager managerWithFileExistsBlock:^(NSString* path) {
+ return YES;
+ }];
+ NSMutableDictionary* appPlistDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ @"/foo", @"WebKitLocalStorageDatabasePathPreferenceKey",
+ nil];
+ BOOL modified = [CDVLocalStorage __verifyAndFixDatabaseLocationsWithAppPlistDict:appPlistDict
+ bundlePath:kBundlePath
+ fileManager:fakeFileManager];
+ STAssertTrue(modified, @"Should have applied fix.");
+ NSString* newPath = [appPlistDict objectForKey:@"WebKitLocalStorageDatabasePathPreferenceKey"];
+ STAssertTrue([@"/bpath/Library/Caches" isEqualToString:newPath], nil);
+}
+
+- (void)testVerifyAndFixDatabaseLocations_changeRequired2
+{
+ NSString* const kBundlePath = @"/bpath";
+ id fakeFileManager = [CDVFakeFileManager managerWithFileExistsBlock:^(NSString* path) {
+ return NO;
+ }];
+ NSMutableDictionary* appPlistDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ @"/foo", @"WebDatabaseDirectory",
+ nil];
+ BOOL modified = [CDVLocalStorage __verifyAndFixDatabaseLocationsWithAppPlistDict:appPlistDict
+ bundlePath:kBundlePath
+ fileManager:fakeFileManager];
+ STAssertTrue(modified, @"Should have applied fix.");
+ NSString* newPath = [appPlistDict objectForKey:@"WebDatabaseDirectory"];
+ STAssertTrue([@"/bpath/Library/WebKit" isEqualToString:newPath], nil);
}
@end