updated user preferences and tests

This commit is contained in:
Stanimir Karoserov
2014-05-12 19:00:45 +03:00
parent cf3b358947
commit d7fda0b91c
6 changed files with 160 additions and 18 deletions

View File

@ -198,6 +198,7 @@
<TypeScriptCompile Include="timer\timer.ios.ts"> <TypeScriptCompile Include="timer\timer.ios.ts">
<DependentUpon>timer.d.ts</DependentUpon> <DependentUpon>timer.d.ts</DependentUpon>
</TypeScriptCompile> </TypeScriptCompile>
<TypeScriptCompile Include="Tests\user_preferences_tests.ts" />
<Content Include="_references.ts" /> <Content Include="_references.ts" />
<Content Include="Image\Readme.md" /> <Content Include="Image\Readme.md" />
</ItemGroup> </ItemGroup>

View File

@ -2,9 +2,11 @@
var fsTests = require("Tests/file_system_tests"); var fsTests = require("Tests/file_system_tests");
var httpTests = require("Tests/http_tests"); var httpTests = require("Tests/http_tests");
var locationTests = require("Tests/location_tests"); var locationTests = require("Tests/location_tests");
var userPreferencesTests = require("Tests/user_preferences_tests");
export var runAll = function () { export var runAll = function () {
TKUnit.runTestModule(fsTests, "FILE SYSTEM"); TKUnit.runTestModule(fsTests, "FILE SYSTEM");
TKUnit.runTestModule(httpTests, "HTTP"); TKUnit.runTestModule(httpTests, "HTTP");
TKUnit.runTestModule(locationTests, "LOCATION"); TKUnit.runTestModule(locationTests, "LOCATION");
} TKUnit.runTestModule(userPreferencesTests, "USER PREFERENCES");
}

View File

@ -0,0 +1,137 @@

var TKUnit = require("TestModules/TKUnit");
var UserPreferences = require("UserPreferences");
var stringKey:string = "stringKey";
var boolKey: string = "boolKey";
var numberKey: string = "numberKey";
var noStringKey: string = "noStringKey";
var noBoolKey: string = "noBoolKey";
var noNumberKey: string = "noNumberKey";
export var testBoolean = function () {
UserPreferences.setBoolean(boolKey, false);
TKUnit.assert(false == UserPreferences.getBoolean(boolKey), "Cannot set boolean to false, currently it is: " + UserPreferences.getBoolean(boolKey));
UserPreferences.setBoolean(boolKey, true);
TKUnit.assert(true == UserPreferences.getBoolean(boolKey, false), "Cannot set boolean to true");
TKUnit.assert(true == UserPreferences.getBoolean(boolKey), "Cannot set boolean to true (no default)");
};
export var testString = function () {
UserPreferences.setString(stringKey, "String value");
TKUnit.assert("String value" === UserPreferences.getString(stringKey), "Cannot set string value");
};
export var testNumber = function () {
UserPreferences.setNumber(numberKey, 54.321);
var value = UserPreferences.getNumber(numberKey).toFixed(3);
TKUnit.assert(54.321 == value, "Cannot set number value 54.321 != " + value);
};
export var testDefaults = function () {
TKUnit.assert("No string value" === UserPreferences.getString(noStringKey, "No string value"), "Bad default string value");
TKUnit.assert(true === UserPreferences.getBoolean(noBoolKey, true), "Bad default boolean value");
TKUnit.assert(123.45 === UserPreferences.getNumber(noNumberKey, 123.45), "Bad default number value");
};
export var testHasKey = function () {
TKUnit.assert(!UserPreferences.hasKey(noBoolKey), "There is a key: " + noBoolKey);
TKUnit.assert(!UserPreferences.hasKey(noStringKey), "There is a key: " + noStringKey);
TKUnit.assert(!UserPreferences.hasKey(noNumberKey), "There is a key: " + noNumberKey);
TKUnit.assert(UserPreferences.hasKey(boolKey), "There is no key: " + boolKey);
TKUnit.assert(UserPreferences.hasKey(stringKey), "There is no key: " + stringKey);
TKUnit.assert(UserPreferences.hasKey(numberKey), "There is no key: " + numberKey);
};
export var testRemove = function () {
UserPreferences.remove(boolKey);
TKUnit.assert(!UserPreferences.hasKey(boolKey), "Failed to remove key: " + boolKey);
UserPreferences.remove(stringKey);
TKUnit.assert(!UserPreferences.hasKey(stringKey), "Failed to remove key: " + stringKey);
UserPreferences.remove(numberKey);
TKUnit.assert(!UserPreferences.hasKey(numberKey), "Failed to remove key: " + numberKey);
};
export var testInvalidKey = function () {
try {
UserPreferences.hasKey(undefined);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.hasKey(null);
TKUnit.assert(false, "There is a key null");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.hasKey(123);
TKUnit.assert(false, "There is a key number");
}
catch (e) {
// we should receive an exception here
}
UserPreferences.hasKey("string");
};
export var testInvalidValue = function () {
try {
UserPreferences.setBoolean(boolKey, "str");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.setBoolean(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.setString(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.setString(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.setNumber(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
UserPreferences.setNumber(boolKey, "123");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
};

View File

@ -58,6 +58,13 @@ export var setNumber = function(key: string, value: number): void {
editor.commit(); editor.commit();
} }
export var remove = function (key: string): void {
Common.checkKey(key);
var editor = sharedPreferences.edit();
editor.remove(key);
editor.commit();
}
/* /*
these are commented out to be used only if requested by users or otherwise needed these are commented out to be used only if requested by users or otherwise needed

View File

@ -1,20 +1,4 @@
export declare class UserPreferences { /**
containsKey(key: string): boolean;
getBoolean(key: string, defaultValue?: boolean): boolean;
getDouble(key: string, defaultValue?: number): number;
getInt(key: string, defaultValue?: number): number;
getLong(key: string, defaultValue?: number): number;
getString(key: string, defaultValue?: string): string;
getStrings(key: string, defaultValue?: string[]): string[];
setBoolean(key: string, value: boolean);
setDouble(key: string, value: number);
setInt(key: string, value: number);
setLong(key: string, value: number);
setString(key: string, value: string);
setStrings(key: string, value: string[]);
}
/**
* report does such key exist * report does such key exist
*/ */
export declare var hasKey: (key: string) => boolean; export declare var hasKey: (key: string) => boolean;
@ -82,3 +66,8 @@ export declare var setInt: (key: string, value: number) => void;
* sets value for a key as long integer * sets value for a key as long integer
*/ */
export declare var setLong: (key: string, value: number) => void; export declare var setLong: (key: string, value: number) => void;
/**
* removes a value for key
*/
export declare var remove: (key: string) => void;

View File

@ -54,6 +54,12 @@ export var setNumber = function (key: string, value: number): void {
userDefaults.synchronize(); userDefaults.synchronize();
} }
export var remove = function (key: string): void {
Common.checkKey(key);
userDefaults.removeObjectForKey(key);
userDefaults.synchronize();
}
/* /*
these are commented out to be used only if requested by users or otherwise needed these are commented out to be used only if requested by users or otherwise needed