mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Added Tests module to the BCL. Added two new build configurations - Android_Tests & iOS_Tests.
This commit is contained in:
58
BCL.csproj
58
BCL.csproj
@ -46,13 +46,23 @@
|
|||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'iOS_Tests'">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<OutputPath>bin\</OutputPath>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'Android_Tests'">
|
||||||
|
<OutputPath>bin\</OutputPath>
|
||||||
|
</PropertyGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
|
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<VisualStudio>
|
<VisualStudio>
|
||||||
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
|
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
|
||||||
<WebProjectProperties>
|
<WebProjectProperties>
|
||||||
<UseIIS>True</UseIIS>
|
<UseIIS>False</UseIIS>
|
||||||
<AutoAssignPort>True</AutoAssignPort>
|
<AutoAssignPort>True</AutoAssignPort>
|
||||||
<DevelopmentServerPort>63708</DevelopmentServerPort>
|
<DevelopmentServerPort>63708</DevelopmentServerPort>
|
||||||
<DevelopmentServerVPath>/</DevelopmentServerVPath>
|
<DevelopmentServerVPath>/</DevelopmentServerVPath>
|
||||||
@ -129,6 +139,12 @@
|
|||||||
<TypeScriptCompile Include="Location\location_types.ts">
|
<TypeScriptCompile Include="Location\location_types.ts">
|
||||||
<DependentUpon>location.d.ts</DependentUpon>
|
<DependentUpon>location.d.ts</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
|
<TypeScriptCompile Include="Tests\file_system_tests.ts" />
|
||||||
|
<TypeScriptCompile Include="Tests\http_tests.ts" />
|
||||||
|
<TypeScriptCompile Include="Tests\index.ts" />
|
||||||
|
<TypeScriptCompile Include="Tests\location_tests.ts" />
|
||||||
|
<TypeScriptCompile Include="Tests\testRunner.ts" />
|
||||||
|
<TypeScriptCompile Include="Tests\TKUnit.ts" />
|
||||||
<TypeScriptCompile Include="text\index.ts" />
|
<TypeScriptCompile Include="text\index.ts" />
|
||||||
<TypeScriptCompile Include="text\text.android.ts">
|
<TypeScriptCompile Include="text\text.android.ts">
|
||||||
<DependentUpon>text.d.ts</DependentUpon>
|
<DependentUpon>text.d.ts</DependentUpon>
|
||||||
@ -177,7 +193,6 @@
|
|||||||
<TypeScriptCompile Include="UserPreferences\user_preferences_common.ts">
|
<TypeScriptCompile Include="UserPreferences\user_preferences_common.ts">
|
||||||
<DependentUpon>user_preferences.d.ts</DependentUpon>
|
<DependentUpon>user_preferences.d.ts</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<Content Include="_references.ts" />
|
|
||||||
<TypeScriptCompile Include="Console\console.d.ts" />
|
<TypeScriptCompile Include="Console\console.d.ts" />
|
||||||
<TypeScriptCompile Include="timer\timer.d.ts" />
|
<TypeScriptCompile Include="timer\timer.d.ts" />
|
||||||
<TypeScriptCompile Include="timer\timer.ios.ts">
|
<TypeScriptCompile Include="timer\timer.ios.ts">
|
||||||
@ -227,9 +242,6 @@
|
|||||||
<TypeScriptSourceMap>True</TypeScriptSourceMap>
|
<TypeScriptSourceMap>True</TypeScriptSourceMap>
|
||||||
<TypeScriptTarget>ES5</TypeScriptTarget>
|
<TypeScriptTarget>ES5</TypeScriptTarget>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
|
|
||||||
<TypeScriptSourceMap>True</TypeScriptSourceMap>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)' == 'iOS_Deploy'">
|
<PropertyGroup Condition="'$(Configuration)' == 'iOS_Deploy'">
|
||||||
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
|
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
|
||||||
<TypeScriptRemoveComments>True</TypeScriptRemoveComments>
|
<TypeScriptRemoveComments>True</TypeScriptRemoveComments>
|
||||||
@ -242,21 +254,51 @@
|
|||||||
<TypeScriptSourceMap>True</TypeScriptSourceMap>
|
<TypeScriptSourceMap>True</TypeScriptSourceMap>
|
||||||
<TypeScriptTarget>ES5</TypeScriptTarget>
|
<TypeScriptTarget>ES5</TypeScriptTarget>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'Android_Tests'">
|
||||||
|
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
|
||||||
|
<TypeScriptTarget>ES5</TypeScriptTarget>
|
||||||
|
<TypeScriptRemoveComments>True</TypeScriptRemoveComments>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'iOS_Tests'">
|
||||||
|
<TypeScriptModuleKind>commonjs</TypeScriptModuleKind>
|
||||||
|
<TypeScriptRemoveComments>True</TypeScriptRemoveComments>
|
||||||
|
<TypeScriptTarget>ES5</TypeScriptTarget>
|
||||||
|
</PropertyGroup>
|
||||||
<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets')" />
|
<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets')" />
|
||||||
<!--
|
<!--
|
||||||
Custom build tasks
|
Custom build tasks
|
||||||
-->
|
-->
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)' == 'iOS'">
|
||||||
|
<TargetOS>iOS</TargetOS>
|
||||||
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'iOS_Deploy'">
|
<PropertyGroup Condition=" '$(Configuration)' == 'iOS_Deploy'">
|
||||||
|
<TargetOS>iOS</TargetOS>
|
||||||
<JSConfig>Deploy\xCode\Configuration.xml</JSConfig>
|
<JSConfig>Deploy\xCode\Configuration.xml</JSConfig>
|
||||||
<JSMainFile>app.js</JSMainFile>
|
<JSMainFile>bootstrap.js</JSMainFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)' == 'iOS_Tests'">
|
||||||
|
<TargetOS>iOS</TargetOS>
|
||||||
|
<JSConfig>Deploy\xCode\Configuration.xml</JSConfig>
|
||||||
|
<JSMainFile>bootstrap.js</JSMainFile>
|
||||||
|
<CopyTests>true</CopyTests>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)' == 'Android'">
|
||||||
|
<TargetOS>Android</TargetOS>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Android_Deploy'">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Android_Deploy'">
|
||||||
|
<TargetOS>Android</TargetOS>
|
||||||
<JSConfig>Deploy\Eclipse\Configuration.xml</JSConfig>
|
<JSConfig>Deploy\Eclipse\Configuration.xml</JSConfig>
|
||||||
<JSMainFile>app.js</JSMainFile>
|
<JSMainFile>bootstrap.js</JSMainFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)' == 'Android_Tests'">
|
||||||
|
<TargetOS>Android</TargetOS>
|
||||||
|
<JSConfig>Deploy\Eclipse\Configuration.xml</JSConfig>
|
||||||
|
<JSMainFile>bootstrap.js</JSMainFile>
|
||||||
|
<CopyTests>true</CopyTests>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<UsingTask TaskName="BuildTasks.CopyForPlatformBuildTask" AssemblyFile="../../Build/lib/BuildTasks.dll" />
|
<UsingTask TaskName="BuildTasks.CopyForPlatformBuildTask" AssemblyFile="../../Build/lib/BuildTasks.dll" />
|
||||||
<Target Name="AfterBuild">
|
<Target Name="AfterBuild">
|
||||||
<CopyForPlatformBuildTask TargetPlatform="$(Configuration)" Platforms="iOS;Android" InputFiles="@(GeneratedJavascript)" DestinationFolder="$(OutputPath)\$(Configuration)\" JSConfigFile="$(JSConfig)" AppMainJSFile="$(JSMainFile)" />
|
<CopyForPlatformBuildTask TargetPlatform="$(TargetOS)" IncludeTests="$(CopyTests)" Platforms="iOS;Android" InputFiles="@(GeneratedJavascript)" DestinationFolder="$(OutputPath)\$(Configuration)\" JSConfigFile="$(JSConfig)" AppMainJSFile="$(JSMainFile)" />
|
||||||
</Target>
|
</Target>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
|
8
BCL.sln
8
BCL.sln
@ -1,24 +1,30 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 2013
|
# Visual Studio 2013
|
||||||
VisualStudioVersion = 12.0.30110.0
|
VisualStudioVersion = 12.0.30324.0
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BCL", "BCL.csproj", "{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BCL", "BCL.csproj", "{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Android_Deploy|Any CPU = Android_Deploy|Any CPU
|
Android_Deploy|Any CPU = Android_Deploy|Any CPU
|
||||||
|
Android_Tests|Any CPU = Android_Tests|Any CPU
|
||||||
Android|Any CPU = Android|Any CPU
|
Android|Any CPU = Android|Any CPU
|
||||||
iOS_Deploy|Any CPU = iOS_Deploy|Any CPU
|
iOS_Deploy|Any CPU = iOS_Deploy|Any CPU
|
||||||
|
iOS_Tests|Any CPU = iOS_Tests|Any CPU
|
||||||
iOS|Any CPU = iOS|Any CPU
|
iOS|Any CPU = iOS|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android_Deploy|Any CPU.ActiveCfg = Android_Deploy|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android_Deploy|Any CPU.ActiveCfg = Android_Deploy|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android_Deploy|Any CPU.Build.0 = Android_Deploy|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android_Deploy|Any CPU.Build.0 = Android_Deploy|Any CPU
|
||||||
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android_Tests|Any CPU.ActiveCfg = Android_Tests|Any CPU
|
||||||
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android_Tests|Any CPU.Build.0 = Android_Tests|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android|Any CPU.ActiveCfg = Android|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android|Any CPU.ActiveCfg = Android|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android|Any CPU.Build.0 = Android|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.Android|Any CPU.Build.0 = Android|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS_Deploy|Any CPU.ActiveCfg = iOS_Deploy|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS_Deploy|Any CPU.ActiveCfg = iOS_Deploy|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS_Deploy|Any CPU.Build.0 = iOS_Deploy|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS_Deploy|Any CPU.Build.0 = iOS_Deploy|Any CPU
|
||||||
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS_Tests|Any CPU.ActiveCfg = iOS_Tests|Any CPU
|
||||||
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS_Tests|Any CPU.Build.0 = iOS_Tests|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS|Any CPU.ActiveCfg = iOS|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS|Any CPU.ActiveCfg = iOS|Any CPU
|
||||||
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS|Any CPU.Build.0 = iOS|Any CPU
|
{2313F1BF-1F2D-4F11-806A-87927FA6A7C0}.iOS|Any CPU.Build.0 = iOS|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
96
Tests/TKUnit.ts
Normal file
96
Tests/TKUnit.ts
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
|
||||||
|
/* Notes:
|
||||||
|
|
||||||
|
1. all test function names should begin with 'test'
|
||||||
|
2. (if exists) at the beginning of module test setUpModule() module function is called
|
||||||
|
3. (if exists) at the beginning of each test setUp() module function is called
|
||||||
|
4. tests should use TKUnit.assert(condition, message) to mark error. If no assert fails test is successful
|
||||||
|
5. (if exists) at the end of each test tearDown() module function is called
|
||||||
|
6. (if exists) at the end of module test tearDownModule() module function is called
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Application = require("Application");
|
||||||
|
|
||||||
|
var runTest = function(test, testName) {
|
||||||
|
test();
|
||||||
|
console.info("--- [" + testName + "] OK");
|
||||||
|
}
|
||||||
|
|
||||||
|
export var runTestModule = function(module, moduleName){
|
||||||
|
console.info("--- " + moduleName + " TESTS BEGIN ---");
|
||||||
|
console.time(moduleName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (module.setUpModule) {
|
||||||
|
module.setUpModule();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error("--- [" + moduleName + ".setUpModule()] FAILED: " + e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalTests = 0;
|
||||||
|
var totalSuccess = 0;
|
||||||
|
for (var testName in module) {
|
||||||
|
var testFunction = module[testName];
|
||||||
|
if ((typeof(testFunction) === "function") && (testName.substring(0, 4) == "test")) {
|
||||||
|
try {
|
||||||
|
if (module.setUp) {
|
||||||
|
module.setUp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error("--- [" + moduleName + ".setUp()] FAILED: " + e.message);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
totalTests++;
|
||||||
|
runTest(testFunction, testName);
|
||||||
|
totalSuccess++;
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error("--- [" + testName + "] FAILED: " + e.message);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (module.tearDown) {
|
||||||
|
module.tearDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error("--- [" + moduleName + ".tearDown()] FAILED: " + e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (module.tearDownModule) {
|
||||||
|
module.tearDownModule();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error("--- [" + moduleName + ".tearDownModule()] FAILED: " + e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.timeEnd(moduleName);
|
||||||
|
console.info("--- " + moduleName + " TESTS COMPLETE --- (" + totalSuccess + " of " + totalTests + ") OK, " + (totalTests - totalSuccess) + " failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
export var assert = function(test: boolean, message?: string)
|
||||||
|
{
|
||||||
|
//console.assert(test, message);
|
||||||
|
if (!test) {
|
||||||
|
throw new Error(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export var wait = function(ms)
|
||||||
|
{
|
||||||
|
if (Application.ios) {
|
||||||
|
Foundation.NSRunLoop.currentRunLoop().runUntilDate(Foundation.NSDate.dateWithTimeIntervalSinceNow(ms / 1000));
|
||||||
|
}
|
||||||
|
else if (Application.android) {
|
||||||
|
java.lang.Thread.sleep(long(ms));
|
||||||
|
java.lang.Thread.yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
428
Tests/file_system_tests.ts
Normal file
428
Tests/file_system_tests.ts
Normal file
@ -0,0 +1,428 @@
|
|||||||
|
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// # File System
|
||||||
|
// Using the file system requires the FileSystem module.
|
||||||
|
// ``` JavaScript
|
||||||
|
var fs = require("FileSystem");
|
||||||
|
// ```
|
||||||
|
// The pre-required `fs` module is used throughout the following code snippets.
|
||||||
|
// </snippet>
|
||||||
|
|
||||||
|
var TKUnit = require("TestModules/TKUnit");
|
||||||
|
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ## Create
|
||||||
|
// </snippet>
|
||||||
|
|
||||||
|
// TODO: Split the "Read and Write" in "Read" and "Write"
|
||||||
|
export var testFileReadWrite = function () {
|
||||||
|
// TODO: Add description if writeText replaces the file? Does it create the file? Does it append to existing file?
|
||||||
|
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Writing and Reading a String to a File
|
||||||
|
// The following example writes some text to a file and then reads it back.
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var myFile = documents.getFile("Test_Write.txt");
|
||||||
|
|
||||||
|
var written: boolean;
|
||||||
|
//// Writing text to the file.
|
||||||
|
myFile.writeText("Something")
|
||||||
|
.then(function () {
|
||||||
|
//// Succeeded writing to the file.
|
||||||
|
|
||||||
|
//// Getting back the contents of the file.
|
||||||
|
myFile.readText()
|
||||||
|
.then(function (content) {
|
||||||
|
//// Successfuly read the file's content.
|
||||||
|
// <hide>
|
||||||
|
written = content === "Something";
|
||||||
|
TKUnit.assert(written, "File read/write not working.");
|
||||||
|
myFile.remove();
|
||||||
|
// </hide>
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to read from the file.
|
||||||
|
// <hide>
|
||||||
|
console.error("Failed to read/write text");
|
||||||
|
console.dump(error);
|
||||||
|
// </hide>
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to write to the file.
|
||||||
|
// <hide>
|
||||||
|
console.error("Failed to read/write text");
|
||||||
|
console.dump(error);
|
||||||
|
// </hide>
|
||||||
|
});
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ## Read
|
||||||
|
// </snippet>
|
||||||
|
|
||||||
|
export var testGetKnownFolders = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Getting the Known Folders
|
||||||
|
// Each app has several well known folders. This is how to access them:
|
||||||
|
// ``` JavaScript
|
||||||
|
//// Getting the application's 'documents' folder.
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(documents, "Could not retrieve the Documents known folder.");
|
||||||
|
TKUnit.assert(documents.isKnown, "The Documents folder should have its isKnown property set to true.");
|
||||||
|
// </hide>
|
||||||
|
//// Getting the application's 'temp' folder.
|
||||||
|
var temp = fs.knownFolders.temp();
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(temp, "Could not retrieve the Temporary known folder.");
|
||||||
|
TKUnit.assert(temp.isKnown, "The Temporary folder should have its isKnown property set to true.");
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testGetEntities = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Getting Folder Contents
|
||||||
|
// Getting all files and folders within a folder:
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
// <hide>
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
var file1 = documents.getFile("Test1.txt");
|
||||||
|
|
||||||
|
var fileFound,
|
||||||
|
file1Found;
|
||||||
|
|
||||||
|
// IMPORTANT: console.log is mocked to make the snippet pretty.
|
||||||
|
var globalConsole = console;
|
||||||
|
var console = {
|
||||||
|
log: function (file) {
|
||||||
|
if (file === "Test.txt") {
|
||||||
|
fileFound = true;
|
||||||
|
} else if (file === "Test1.txt") {
|
||||||
|
file1Found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// </hide>
|
||||||
|
documents.getEntities()
|
||||||
|
.then(function (entities) {
|
||||||
|
//// entities is array with the document's files and folders.
|
||||||
|
entities.forEach(function (entity) {
|
||||||
|
console.log(entity.name);
|
||||||
|
});
|
||||||
|
// <hide>
|
||||||
|
|
||||||
|
TKUnit.assert(fileFound, "Failed to enumerate Test.txt");
|
||||||
|
TKUnit.assert(file1Found, "Failed to enumerate Test1.txt");
|
||||||
|
|
||||||
|
file.remove();
|
||||||
|
file1.remove();
|
||||||
|
// </hide>
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to obtain folder's contents.
|
||||||
|
// globalConsole.error(error.message);
|
||||||
|
});
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testEnumEntities = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Enumerating Folder Contents
|
||||||
|
// Getting all folder entities in array may be slow with large number of files.
|
||||||
|
// Enumerating the folder entities would itterate the files one by one without blocking the UI.
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
// <hide>
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
var file1 = documents.getFile("Test1.txt");
|
||||||
|
var fileFound,
|
||||||
|
file1Found;
|
||||||
|
var console = {
|
||||||
|
log: function (file) {
|
||||||
|
if (file === "Test.txt") {
|
||||||
|
fileFound = true;
|
||||||
|
} else if (file === "Test1.txt") {
|
||||||
|
file1Found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// </hide>
|
||||||
|
documents.eachEntity(function (entity) {
|
||||||
|
console.log(entity.name);
|
||||||
|
});
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(fileFound, "Failed to enumerate Test.txt");
|
||||||
|
TKUnit.assert(file1Found, "Failed to enumerate Test1.txt");
|
||||||
|
|
||||||
|
file.remove();
|
||||||
|
file1.remove();
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: testGetFile
|
||||||
|
|
||||||
|
export var testGetFolder = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Getting Folder by Name
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var myFolder = documents.getFolder("Tests__");
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(myFolder, "Folder.getFolder API not working.");
|
||||||
|
TKUnit.assert(fs.Folder.exists(myFolder.path), "Folder.getFolder API not working.");
|
||||||
|
myFolder.remove();
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testGetParent = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Getting Parent Folder
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(file, "Failed to create file in the Documents folder.");
|
||||||
|
// </hide>
|
||||||
|
//// The parent folder of the file would be the documents folder.
|
||||||
|
var parent = file.getParent();
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(documents == parent, "The parent folder should be the Documents folder.");
|
||||||
|
file.remove();
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testFileNameExtension = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Getting File Name and Extension
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
//// Getting the file name "Test.txt".
|
||||||
|
var fileName = file.name;
|
||||||
|
//// Getting the file extension ".txt".
|
||||||
|
var fileExtension = file.extension;
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(fileName, "Test.txt");
|
||||||
|
TKUnit.assert(fileExtension, ".txt");
|
||||||
|
file.remove();
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testFileExists = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Checking if a File Exists
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
var exists = fs.File.exists(file.path);
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(exists, "File.exists API not working.");
|
||||||
|
exists = fs.File.exists(file.path + "_");
|
||||||
|
TKUnit.assert(!exists, "File.exists API not working.");
|
||||||
|
file.remove();
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testFolderExists = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Checking if a Folder Exists
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var exists = fs.Folder.exists(documents.path);
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(exists, "Folder.exists API not working.");
|
||||||
|
exists = fs.Folder.exists(documents.path + "_");
|
||||||
|
TKUnit.assert(!exists, "Folder.exists API not working.");
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testContainsFile = function () {
|
||||||
|
var folder = fs.knownFolders.documents();
|
||||||
|
var file = folder.getFile("Test.txt");
|
||||||
|
|
||||||
|
var contains = folder.contains("Test.txt");
|
||||||
|
TKUnit.assert(contains, "Folder.contains API not working.");
|
||||||
|
contains = folder.contains("Test_xxx.txt");
|
||||||
|
TKUnit.assert(!contains, "Folder.contains API not working.");
|
||||||
|
|
||||||
|
file.remove();
|
||||||
|
};
|
||||||
|
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ## Update
|
||||||
|
// </snippet>
|
||||||
|
|
||||||
|
export var testFileRename = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Renaming a File
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
|
||||||
|
file.rename("Test_renamed.txt")
|
||||||
|
.then(function (result) {
|
||||||
|
//// Successfully Renamed.
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(file.name === "Test_renamed.txt", "File.rename API not working.");
|
||||||
|
file.remove();
|
||||||
|
documents.getFile("Test.txt").remove();
|
||||||
|
// </hide>
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to rename the file.
|
||||||
|
// <hide>
|
||||||
|
console.error("Failed to rename file");
|
||||||
|
// </hide>
|
||||||
|
});
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testFolderRename = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Renaming a Folder
|
||||||
|
// ``` JavaScript
|
||||||
|
var folder = fs.knownFolders.documents();
|
||||||
|
var myFolder = folder.getFolder("Test__");
|
||||||
|
|
||||||
|
myFolder.rename("Something")
|
||||||
|
.then(function (result) {
|
||||||
|
//// Successfully Renamed.
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(myFolder.name === "Something", "Folder.rename API not working.");
|
||||||
|
myFolder.remove();
|
||||||
|
folder.getFolder("Test__").remove();
|
||||||
|
// </hide>
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to rename the folder.
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(false, "Folder.rename API not working.");
|
||||||
|
// </hide>
|
||||||
|
});
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ## Delete
|
||||||
|
// </snippet>
|
||||||
|
|
||||||
|
export var testFileRemove = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Removing a File
|
||||||
|
// To 'delete', 'remove' or 'unlink' a file use the file's remove method:
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var file = documents.getFile("Test.txt");
|
||||||
|
file.remove()
|
||||||
|
.then(function (result) {
|
||||||
|
//// Success removing the file.
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(!fs.File.exists(file.path));
|
||||||
|
// </hide>
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to remove the file.
|
||||||
|
// <hide>
|
||||||
|
TKUnit.assert(false, "File.remove API not working.");
|
||||||
|
// </hide>
|
||||||
|
});
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testFolderClear = function () {
|
||||||
|
// <snippet name="file-system">
|
||||||
|
// ### Clearing the Contents of a Folder
|
||||||
|
// The clear method removes all files within a folder.
|
||||||
|
// ``` JavaScript
|
||||||
|
var documents = fs.knownFolders.documents();
|
||||||
|
var folder = documents.getFolder("testFolderEmpty");
|
||||||
|
// <hide>
|
||||||
|
var file1 = folder.getFile("Test1.txt");
|
||||||
|
var file2 = folder.getFile("Test2.txt");
|
||||||
|
var emptied;
|
||||||
|
// </hide>
|
||||||
|
folder.clear()
|
||||||
|
.then(function () {
|
||||||
|
//// Successfully cleared the folder.
|
||||||
|
// <hide>
|
||||||
|
emptied = true;
|
||||||
|
// </hide>
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
//// Failed to clear the folder.
|
||||||
|
// <hide>
|
||||||
|
console.error(error.message);
|
||||||
|
// </hide>
|
||||||
|
});
|
||||||
|
// <hide>
|
||||||
|
folder.getEntities()
|
||||||
|
.then(function (entities) {
|
||||||
|
TKUnit.assert(entities.length === 0, "Failed to clear a Folder");
|
||||||
|
folder.remove();
|
||||||
|
});
|
||||||
|
// </hide>
|
||||||
|
// ```
|
||||||
|
// </snippet>
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Removing a folder.
|
||||||
|
|
||||||
|
// misc
|
||||||
|
|
||||||
|
export var testKnownFolderRename = function () {
|
||||||
|
var folder = fs.knownFolders.documents();
|
||||||
|
|
||||||
|
folder.rename("Something")
|
||||||
|
.then(function (result) {
|
||||||
|
console.error("Known folders should not be renamed.");
|
||||||
|
})
|
||||||
|
.fail(function (error) {
|
||||||
|
TKUnit.assert(true);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testPathNormalize = function () {
|
||||||
|
var folder = fs.knownFolders.documents();
|
||||||
|
var testPath = "///test.txt";
|
||||||
|
var normalizedPath = fs.path.normalize(folder.path + testPath);
|
||||||
|
var expected = folder.path + "/test.txt";
|
||||||
|
TKUnit.assert(normalizedPath === expected);
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testPathJoin = function () {
|
||||||
|
var folder = fs.knownFolders.documents();
|
||||||
|
var path = fs.path.join(folder.path, "myFiles", "test.txt");
|
||||||
|
var expected = folder.path + "/myFiles/test.txt";
|
||||||
|
TKUnit.assert(path === expected);
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testPathSeparator = function () {
|
||||||
|
var separator = fs.path.separator;
|
||||||
|
var expected = "/";
|
||||||
|
TKUnit.assert(separator === expected);
|
||||||
|
};
|
||||||
|
|
1
Tests/http_tests.ts
Normal file
1
Tests/http_tests.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
2
Tests/index.ts
Normal file
2
Tests/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
declare var module, require;
|
||||||
|
module.exports = require("Tests/testRunner");
|
54
Tests/location_tests.ts
Normal file
54
Tests/location_tests.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import TKUnit = require("Tests/TKUnit");
|
||||||
|
import locationModule = require("Location/location");
|
||||||
|
|
||||||
|
var LocationManager = locationModule.LocationManager;
|
||||||
|
var Location = locationModule.Location;
|
||||||
|
|
||||||
|
export var testIsEnabled = function () {
|
||||||
|
TKUnit.assert(LocationManager.isEnabled());
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testLocation = function () {
|
||||||
|
var locationReceived;
|
||||||
|
|
||||||
|
var locationManager = new LocationManager();
|
||||||
|
|
||||||
|
locationManager.startLocationMonitoring(function(location) {
|
||||||
|
locationReceived = true;
|
||||||
|
}, function(error) {
|
||||||
|
console.log('Location error received: ' + error);
|
||||||
|
locationReceived = error;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var timeoutAfter = 50; // 5 seconds timeout
|
||||||
|
while (typeof locationReceived == 'undefined') {
|
||||||
|
if (0 >= --timeoutAfter) {
|
||||||
|
locationReceived = 'Location search timed out!';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
TKUnit.wait(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
locationManager.stopLocationMonitoring();
|
||||||
|
|
||||||
|
TKUnit.assert(true === locationReceived, locationReceived);
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testDistance = function () {
|
||||||
|
var locSofia = new Location();
|
||||||
|
locSofia.longitude = 42.696552;
|
||||||
|
locSofia.latitude = 23.32601;
|
||||||
|
var locNewYork = new Location();
|
||||||
|
locNewYork.longitude = 40.71448;
|
||||||
|
locNewYork.latitude = -74.00598;
|
||||||
|
var distance = LocationManager.distance(locSofia, locNewYork);
|
||||||
|
TKUnit.assert((distance > 10780000) && (distance < 10860000), "invalid distance " + distance);
|
||||||
|
};
|
||||||
|
|
||||||
|
export var testLastKnownLocation = function () {
|
||||||
|
var locationManager = new LocationManager();
|
||||||
|
var lastKnownLocation = locationManager.lastKnownLocation;
|
||||||
|
TKUnit.assert((lastKnownLocation != null), "There is no last known location");
|
||||||
|
};
|
||||||
|
|
10
Tests/testRunner.ts
Normal file
10
Tests/testRunner.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
var TKUnit = require("Tests/TKUnit");
|
||||||
|
var fsTests = require("Tests/file_system_tests");
|
||||||
|
var httpTests = require("Tests/http_tests");
|
||||||
|
var locationTests = require("Tests/location_tests");
|
||||||
|
|
||||||
|
export var runAll = function () {
|
||||||
|
TKUnit.runTestModule(fsTests, "FILE SYSTEM");
|
||||||
|
TKUnit.runTestModule(httpTests, "HTTP");
|
||||||
|
TKUnit.runTestModule(locationTests, "LOCATION");
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
///<reference no-default-lib="true"/>
|
///<reference no-default-lib="true"/>
|
||||||
///<reference path='libjs.d.ts' />
|
///<reference path='libjs.d.ts' />
|
||||||
///<reference path='declarations.d.ts' />
|
///<reference path='declarations.d.ts' />
|
||||||
|
///<reference path='android17.d.ts' />
|
||||||
|
///<reference path='ios7.d.ts' />
|
Reference in New Issue
Block a user