Skip to content

Commit

Permalink
Merge pull request #55 from winappkits/UnitTests
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
toolboc committed Oct 15, 2014
2 parents 0962f65 + 3c96e56 commit 7499bc3
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ class LocalItemsFileService:IDataService
public async Task<List<Item>> GetItems()
{
LocalItems = new List<Item>();
if (AppSettings.EnableLocalItemsFileService == true)
{
string localItemsXML = await ServiceLocator.ResourceFileService.ReadFileFromInstallPath("LocalItemsFile.xml");

string localItemsXML = await ServiceLocator.ResourceFileService.ReadFileFromInstallPath("LocalItemsFile.xml");

try
{
await Parse(localItemsXML);
}
catch
{
ServiceLocator.MessageService.ShowErrorAsync("Error when Parsing Items from LocalItemsFile.xml", "Application Error");
}

try
{
await Parse(localItemsXML);
}
catch
{
ServiceLocator.MessageService.ShowErrorAsync("Error when Parsing Items from LocalItemsFile.xml", "Application Error");
}


if (!String.IsNullOrEmpty(AppSettings.RemoteItemFileService))
{
Expand All @@ -48,11 +47,11 @@ public async Task<List<Item>> GetItems()
url = Misc.CacheBusterUrl(url);


string localItemsXML = await httpClient.GetStringAsync(url);
string remoteItemsXML = await httpClient.GetStringAsync(url);

try
{
await Parse(localItemsXML);
await Parse(remoteItemsXML);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task<List<Item>> GetItems()
await Parse(twitterSource);
}
}
catch
catch(Exception e)
{
ServiceLocator.MessageService.ShowErrorAsync("Error when retrieving items from TwitterService", "Application Error");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: InternalsVisibleTo ("XPlatformCloudKit.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public ItemsShowcaseViewModel()
LoadItems(Debugger.IsAttached);
}

public ItemsShowcaseViewModel(bool overrideCache)
{
// If overrideCache is true, tell LoadItems() to ignore the cache, else if false, use the
// cache. This allows for direct control of cache availability during testing.
LoadItems(overrideCache);
}

#endregion // Constructors

#region Internal Methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

namespace XPlatformCloudKit.Tests
{
[TestClass]
public class AzureMobileServiceTest
{
[TestMethod]
public async Task ValidateAzureMobileService()
{
AppSettings.MobileServiceAddress = "https://xplatformcloudkit.azure-mobile.net/";
AppSettings.MobileServiceApplicationKey = "UYZnUrrabofKBELSRdRsmCGboyDGMJ15";
var azureMobileService = new DataServices.AzureMobileService();

var items = await azureMobileService.GetItems();

Assert.AreEqual(6, items.Count);
Assert.AreEqual("Captain America", items[0].Title);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using XPlatformCloudKit.DataServices;
using XPlatformCloudKit.Services;
using XPlatformCloudKit.Tests.Services;

namespace XPlatformCloudKit.Tests
{
/// <summary>
/// Summary description for LocalItemsFileServiceTest
/// </summary>
[TestClass]
public class LocalItemsFileServiceTest
{
[ClassInitialize]
public static void ClassInitialize(TestContext t)
{
ServiceLocator.ResourceFileService = new TestResourceFileService();
}

[TestMethod]
public async Task ValidateLocalItemsFileService()
{
var localItemsFileService = new LocalItemsFileService();

var items = await localItemsFileService.GetItems();

Assert.AreEqual(3, items.Count);
Assert.AreEqual("Warning", items[0].Subtitle);
}
}
}
67 changes: 67 additions & 0 deletions XPlatformCloudKit/XPlatformCloudKit.Tests/RemoteAppSettingsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XPlatformCloudKit.Services;
using Cirrious.CrossCore;
using XPlatformCloudKit.ViewModels;
using XPlatformCloudKit.Tests.Services;
using XPlatformCloudKit.DataServices;
using Cirrious.CrossCore.IoC;
using Cirrious.MvvmCross.Plugins.File;
using Cirrious.MvvmCross.Plugins.File.Wpf;
using System.Diagnostics;
using System.Threading;

namespace XPlatformCloudKit.Tests
{
[TestClass]
public class RemoteAppSettingsTest
{
public static int loadingOfDataSourcesTimeOutInMilliseconds = 30000;
public static AutoResetEvent waitHandle = new AutoResetEvent(false);
public static ItemsShowcaseViewModel.LoadCompletedEventHandler eventHandler = delegate (object sender, EventArgs e)
{
waitHandle.Set(); // signal that the finished event was raised
};

[ClassInitialize]
public static void ClassInitialize(TestContext t)
{
ServiceLocator.AzureMobileService = new AzureMobileService();
ServiceLocator.ResourceFileService = new TestResourceFileService();
ServiceLocator.MessageService = new TestMessageService();

var iocProvider = MvxSimpleIoCContainer.Initialize();
Mvx.RegisterSingleton<IMvxFileStore>(new MvxWpfFileStore());
}

[TestMethod]
public void ValidateRemoteAppSettingsService()
{
Debug.WriteLine("Initial App Name = " + AppSettings.ApplicationName);
Assert.IsFalse(AppSettings.ApplicationName.Contains("Remote Application"), "Application currently uses an inconclusive name of " + AppSettings.ApplicationName);

AppSettings.EnableRemoteAppSettings = true;
AppSettings.RemoteAppSettingsService = "http://pjdecarlo.com/playground/XPCKSampleRemoteAppSettings/AppSettings.html";

var itemsShowcaseViewModel = new ItemsShowcaseViewModel(true); // Ignore cache to retrieve AppSettings file from remote source
MonitorLoadingItems(itemsShowcaseViewModel);
Debug.WriteLine("Initial DataSources Loaded " + DateTime.Now.ToString());

Debug.WriteLine("Final App Name = " + AppSettings.ApplicationName);
Assert.IsTrue(AppSettings.ApplicationName.Contains("Remote Application"),
"Expected Remote Application Identifier not found."); // Will fail if invalid or no AppSettings file was retrieved
}

public static void MonitorLoadingItems(ItemsShowcaseViewModel itemsShowcaseViewModel)
{
itemsShowcaseViewModel.LoadCompleted += eventHandler;

if (!waitHandle.WaitOne(loadingOfDataSourcesTimeOutInMilliseconds, false))
{
Assert.Fail("Loading of DataSources timed out after " + loadingOfDataSourcesTimeOutInMilliseconds + " ms");
}

itemsShowcaseViewModel.LoadCompleted -= eventHandler;
}
}
}
30 changes: 30 additions & 0 deletions XPlatformCloudKit/XPlatformCloudKit.Tests/RssServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XPlatformCloudKit.Models;
using XPlatformCloudKit.DataServices;
using System.Threading.Tasks;

namespace XPlatformCloudKit.Tests
{
[TestClass]
public class RssServiceTest
{
[TestMethod]
public async Task ValidateRssService()
{
//If value == -1, ensures all items are fetched
AppSettings.RssMaxItemsPerFeed = -1;
AppSettings.RssAddressCollection =
new UrlSource[] {
new UrlSource() { Url = "http://reddit.com/.rss", Group = "Reddit" },
new UrlSource() { Url = "http://reddit.com/r/technology/.rss", Group = "Reddit Technology" },
new UrlSource() { Url = "http://www.bing.com/search?q=tesla&format=rss", Group = "Bing example"}
};
var rssService = new RssService();

var items = await rssService.GetItems();

Assert.IsTrue(items.Count > 0, "Error: Zero items retrieved from RSS Service");
}
}
}
45 changes: 45 additions & 0 deletions XPlatformCloudKit/XPlatformCloudKit.Tests/TwitterServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using XPlatformCloudKit.Models;
using XPlatformCloudKit.DataServices;
using XPlatformCloudKit.Services;
using XPlatformCloudKit.Tests.Services;
using AsyncOAuth;
using System.Security.Cryptography;

namespace XPlatformCloudKit.Tests
{
[TestClass]
public class TwitterServiceTest
{

[ClassInitialize]
public static void ClassInitialize(TestContext t)
{
//Oauth Init
OAuthUtility.ComputeHash = (key, buffer) => { using (var hmac = new HMACSHA1(key)) { return hmac.ComputeHash(buffer); } };
}

[TestMethod]
public async Task ValidateTwitterService()
{
ServiceLocator.MessageService = new TestMessageService();
//Created @ https://apps.twitter.com, found under "manage api keys"
AppSettings.TwitterConsumerKey = "F4OL4vtT0PidHiWuBaWePDEj8";
AppSettings.TwitterConsumerSecret = "GC7hETzCvSsGkCPgqN1fEVCBUsszk9wgZ5wt8kn8Dg2TWw05bE";
//In "manage api keys" scroll down to create access tokens
AppSettings.TwitterAccessToken = "1395095078-OJQnXgyLvLAPOYwTP4r13yDMRK2lcWDZXouCZBd";
AppSettings.TwitterAccessSecret = "7Hb4bChKQNOXhYwKJtQq3X8Vt9NZeRbZAKr1R9UFwcpjx";
AppSettings.TwitterAddressCollection = new UrlSource[]
{
new UrlSource {Url = "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=pjdecarlo", Group = "PJDeCarlo"}
};
var twitterService = new TwitterService();

var items = await twitterService.GetItems();

Assert.IsTrue(items.Count > 0, "Error: Zero items retrieved from Twitter Service");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AsyncOAuth, Version=0.8.4.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\AsyncOAuth.0.8.4\lib\AsyncOAuth.dll</HintPath>
</Reference>
<Reference Include="Cirrious.CrossCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e16445fd9b451819, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MvvmCross.HotTuna.CrossCore.3.1.1\lib\net45\Cirrious.CrossCore.dll</HintPath>
Expand All @@ -59,6 +63,12 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MvvmCross.HotTuna.Plugin.Json.3.1.1\lib\net45\Cirrious.MvvmCross.Plugins.Json.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Mobile, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\WindowsAzure.MobileServices.1.2.3\lib\net45\Microsoft.WindowsAzure.Mobile.dll</HintPath>
Expand All @@ -71,6 +81,7 @@
<HintPath>..\packages\Newtonsoft.Json.6.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\packages\Microsoft.Net.Http.2.2.19\lib\net45\System.Net.Http.Extensions.dll</HintPath>
Expand All @@ -80,6 +91,7 @@
<HintPath>..\packages\Microsoft.Net.Http.2.2.19\lib\net45\System.Net.Http.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Security" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
Expand All @@ -100,9 +112,15 @@
<Compile Include="Bootstrap\FilePluginBootstrap.cs" />
<Compile Include="Bootstrap\JsonPluginBootstrap.cs" />
<Compile Include="ItemsShowcaseViewModelTests.cs" />
<Compile Include="LocalItemsFileServiceTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\TestMessageService.cs" />
<Compile Include="Services\TestResourceFileService.cs" />
<Compile Include="AzureMobileServiceTest.cs" />
<Compile Include="RssServiceTest.cs" />
<Compile Include="TwitterServiceTest.cs" />
<Compile Include="RemoteAppSettingsTest.cs" />
<Compile Include="YoutubeServiceTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\XPlatformCloudKit.PCL\XPlatformCloudKit.PCL.csproj">
Expand Down
31 changes: 31 additions & 0 deletions XPlatformCloudKit/XPlatformCloudKit.Tests/YoutubeServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using XPlatformCloudKit.Models;
using XPlatformCloudKit.DataServices;

namespace XPlatformCloudKit.Tests
{
[TestClass]
public class YoutubeServiceTest
{
[TestMethod]
public async Task ValidYoutubeService()
{
//Created @ https://code.google.com/apis/console, enable YouTube Data API v3 under APIs then click Credentials
//Under Public API Access - click "Create New Key"
AppSettings.YoutubePublicAPIKey = "AIzaSyCfyarRgBTEHzpC1IaTqdjhen01rRBR-ow";
AppSettings.YoutubeAddressCollection = new UrlSource []
{
new UrlSource {Url = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId=PL0OTHVsGLN2medBm9k-VFtB6cvUbP9n6B&part=snippet&maxResults=50&fields=items%2CnextPageToken&key=" + AppSettings.YoutubePublicAPIKey, Group = "Youtube Playlist"}
};
var youtubeService = new YoutubeService();

var items = await youtubeService.GetItems();

Assert.IsTrue(items.Count > 0, "Error: Zero items retrieved from Twitter Service");
Assert.AreEqual(200, items.Count);
Assert.AreEqual("Candy Crush Saga Level 1", items[0].Title);
}
}
}
2 changes: 2 additions & 0 deletions XPlatformCloudKit/XPlatformCloudKit.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AsyncOAuth" version="0.8.4" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.7" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.165" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.19" targetFramework="net45" />
<package id="MvvmCross.HotTuna.CrossCore" version="3.1.1" targetFramework="net45" />
Expand Down

0 comments on commit 7499bc3

Please sign in to comment.