XCTest fails with with exception 'Non-UI clients cannont be autopaused' - unit-testing

I am trying to test creation of CLLocationManager as a singletone with default parameters:
+ (GeolocationService *)defaultGeolocationService
{
static GeolocationService *_defaultGeolocationService = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_defaultGeolocationService = [[GeolocationService alloc] init];
[_defaultGeolocationService initLocationManager];
});
return _defaultGeolocationService;
}
- (void)initLocationManager
{
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
[self.locationManager requestAlwaysAuthorization];
}
Test looks like this:
- (void)testInitWithDefaultsSettings
{
GeolocationService *defaultGeolocationService = [GeolocationService defaultGeolocationService];
XCTAssertTrue(defaultGeolocationService.settings.autoPause, #"autoPause");
}
And I get an exception:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Non-UI clients cannont be auto paused'
What should I do to make this test work?

The issue is with the following line
self.locationManager.pausesLocationUpdatesAutomatically = YES;
This property cannot be set on an application that is not running a UI (not sure why, I could not find any documentation about it).
You may want to do some checks in your test to ensure that the UI has loaded before initialising your GeoLocationService and setting self.locationManager.pausesLocationUpdatesAutomatically.

I searched a bit on simple and fast solution to avoid it and I have found some workaround to this problem I think you have three solutions :
- Easy One :
Create a Preprocessor Macro for Testing Target. But it's depend on how many Macro you have and can complicate the code management if you abuse on it.
- Medium one :
You know that all unit test are running on a simulator so why not try a
#if !(TARGET_IPHONE_SIMULATOR)
But you will tell me that you want as well to run the app on your simulator and if you do only that case you won't be able to use the property on the app launching case in the simulator.
So why not add a complementary test about knowing what XPCServiceName is running like below :
NSString *serviceName = [NSProcessInfo processInfo].environment[#"XPC_SERVICE_NAME"];
BOOL amITesting = ([serviceName rangeOfString:#"xctest"].location != NSNotFound);
So with #if !(TARGET_IPHONE_SIMULATOR) && !amITesting you can use your
self.locationManager.pausesLocationUpdatesAutomatically = YES;
And Test will pass.
- Third one :
I don't like this solution but I write it as well, it's the monkey solution... If you don't want to add a Pre-Processor Macro, add a try catch and test will pass as well. But I don't advice you to use that one.
I hope it will helps a bit more.

This is a restriction of unit tests and other non-UI processes, reason only known to Apple. Here is a solution I am using:
let serviceName = ProcessInfo.processInfo.environment["XPC_SERVICE_NAME"]
let testing = serviceName?.hasSuffix("xctest") ?? false
if !testing {
locationManager.pausesLocationUpdatesAutomatically = true
}

Related

Sitecore: Glass Mapper Code First

It is possible to automatically generate Sitecore templates just coding models? I'm using Sitecore 8.0 and I saw Glass Mapper Code First approach but I cant find more information about that.
Not sure why there isn't much info about it, but you can definitely model/code first!. I do it alot using the attribute configuration approach like so:
[SitecoreType(true, "{generated guid}")]
public class ExampleModel
{
[SitecoreField("{generated guid}", SitecoreFieldType.SingleLineText)]
public virtual string Title { get; set; }
}
Now how this works. The SitecoreType 'true' value for the first parameter indicates it may be used for codefirst. There is a GlassCodeFirstDataprovider which has an Initialize method, executed in Sitecore's Initialize pipeline. This method will collect all configurations marked for codefirst and create it in the sql dataprovider. The sections and fields are stored in memory. It also takes inheritance into account (base templates).
I think you first need to uncomment some code in the GlassMapperScCustom class you get when you install the project via Nuget. The PostLoad method contains the few lines that execute the Initialize method of each CodeFirstDataprovider.
var dbs = global::Sitecore.Configuration.Factory.GetDatabases();
foreach (var db in dbs)
{
var provider = db.GetDataProviders().FirstOrDefault(x => x is GlassDataProvider) as GlassDataProvider;
if (provider != null)
{
using (new SecurityDisabler())
{
provider.Initialise(db);
}
}
}
Furthermore I would advise to use code first on development only. You can create packages or serialize the templates as usual and deploy them to other environment so you dont need the dataprovider (and potential risks) there.
You can. But it's not going to be Glass related.
Code first is exactly what Sitecore.PathFinder is looking to achieve. There's not a lot of info publicly available on this yet however.
Get started here: https://github.com/JakobChristensen/Sitecore.Pathfinder

Roslyn Workspace API : Emiting Wpf and Silverlight Projects

I try Emit each project in this solution.
I wonder why there is a problem with Emiting "Wpf" and "Silverlight" projects. I can understand that I can't Emit Console Project that I am currently executing.
How I can add missing references?
Here is my code.:
public static async Task EmitProject(Project proj)
{
var c = await proj.GetCompilationAsync();
var r = c.Emit("my" + proj.Name );
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(r.Success + " " + proj.Name);
if (!r.Success)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(r.Diagnostics.First(k => k.WarningLevel == 0));
}
}
Silverlight and WPF projects have a somewhat complicated build process, where some of the code is generated at build time by things like the XAML Markup Compiler. Calling Emit doesn't trigger that code to run - it just represents a single call to the CSC task in MSBuild.
Most of the time OpenSolutionAsync actually causes the build to progress far enough that the invocation of CSC will work, but apparently not for these project types.
For the ConsoleApplication, the issue is that it references a PCL, and the facade references are not being added correctly.
Can you file an issue at http://github.com/dotnet/roslyn for us to investigate?

Unit test directive inside a directive

I'm having troubles unit testing a directive that wraps the ng-grid component.
I've written this punkler that shows the issue : http://plnkr.co/edit/HlB8Bt9M2TzsyM6XaDso?p=preview
There is a lot of code I know, but I can't reduce it more than that.
Basically, there is a custom-grid directive that wrapps the ng-grid component from angular-ui. I've made this directive because I have lots of grids in my app and I wouldn't duplicate the configuration of the grid.
The grid displayed on top of the test results use this directive. So , you can see it works fine :)
However, there is probably something I miss about how to test this directive.
I've written a simple test that assert that the first row, first col displays 'Martoni' but it fails. The same test using the ng-grid directive pass.
Any idea what's wrong in my test code ?
http://plnkr.co/edit/WwTyuQXNklL7CnjOxsB2?p=preview
I've had issues before calling directives recursively (or at least nested-ly), particularly when they make use of the $compile service (and ng-repeat's, especially). I'm convinced there's a bug there but I haven't taken the time to find an isolated case. Anyway I think what you've found is some sort of bug, but there's an easy workaround.
If you look at the source for ngGrid you'll see that columns are only added if the width is big enough. When I stepped through in your second example w was negative, which led to addCol never being called.
var w = col.width + colwidths;
if (col.pinned) {
addCol(col);
var newLeft = i > 0 ? (scrollLeft + totalLeft) : scrollLeft;
domUtilityService.setColLeft(col, newLeft, self);
totalLeft += col.width;
} else {
if (w >= scrollLeft) {
if (colwidths <= scrollLeft + self.rootDim.outerWidth) {
addCol(col);
}
}
}
colwidths += col.width;
This led me to believe that your elements had 0 height/width, which could be because they weren't actually in the document while they were being unit-tested.
So to fix it I added the following before your compile(elm)($scope);
angular.element('body').append(elm);
And then to clean up:
afterEach(function () {
angular.element(elm).remove();
});
I don't know if it was intentional or not, but you called $new() on $rootScope in the first unit test but didn't use the result of that to compile with, whereas you did it in the second.

TFS: Query for builds containing a specific changeset

I have a number of build definitions that get executed based upon a single branch in TFS (eg Main).
I'd like to (somehow) query TFS to find all builds containing a specific changeset number that I supply, and return a list of string of the names of the builds that TFS contains. Any kind of app (VS extension, CLI app, winforms, whatever) will do.
Note: this isn't a 'plz give me the code' request; I'm willing to hoof it and do serious work on this. Any pointers to documentation on how to query the database or SDK, or an example of how to query builds; just some place to start looking would be extremely helpful. Thanks.
The following snippet will crawl all Build Definitions of all Team Project of a Collection, and will check each and every build for an Association to the input changeset number:
using System;
using System.Linq;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace FindChangesetInBuild
{
class Program
{
static void Main(string[] args)
{
TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs:8080/tfs/collectionName"));
var versionControl = teamProjectCollection.GetService<VersionControlServer>();
var buildService = (IBuildServer)teamProjectCollection.GetService(typeof(IBuildServer));
var teamProjects = versionControl.GetAllTeamProjects(true);
foreach (var teamProject in teamProjects)
{
var buildDefinitions = buildService.QueryBuildDefinitions(teamProject.Name);
foreach (var buildDefinition in buildDefinitions)
{
var builds = buildService.QueryBuilds(buildDefinition);
foreach (var buildDetail in builds)
{
var changesets = InformationNodeConverters.GetAssociatedChangesets(buildDetail);
if (changesets.Any(changesetSummary => changesetSummary.ChangesetId == Convert.ToInt32(args[0])))
{
Console.WriteLine("Changeset was build in "+buildDetail.BuildNumber);
}
}
}
}
}
}
}
Needless to say, this is a brute force attack.You can further refine the code if you narrow down the list of buildDefinition, make focus on specific teamProjects etc. In any case I can hardly imagine the above to be useful as-is!Apart from (obviously) MSDN, a great resource for TFS-SDK is Shai Raiten's blog.For Build-Speficic examples, check also here & here for some possibly interesting SO posts.
You can use this little DB Query in TFS 2010 and just substitute 90264 with your changeset id.
USE Tfs_Warehouse
go
SELECT BuildName
FROM DimBuild
INNER JOIN FactBuildChangeset
ON DimBuild.BuildSK = FactBuildChangeset.BuildSK
WHERE FactBuildChangeset.ChangesetSK = 90264

Firebreath how to know if my method is called without debugging

I am using Objective-C++ in my firebreath project. The problem is that I am using Xcode 4 and I can not find the way to debug my project. So I have thought about if my method is been called from the web page.
Here is my source code:
In my OpenOnDesktopPluginAPI.h class:
class OpenOnDesktopPluginAPI : public FB::JSAPIAuto
{
public:
OpenOnDesktopPluginAPI(const OpenOnDesktopPluginPtr& plugin, const FB::BrowserHostPtr& host);
virtual ~OpenOnDesktopPluginAPI();
OpenOnDesktopPluginPtr getPlugin();
...
//This is my method
void runNotification();
...
};
In my OpenOnDesktopPluginAPI.mm class:
OpenOnDesktopPluginAPI::OpenOnDesktopPluginAPI(const OpenOnDesktopPluginPtr& plugin, const FB::BrowserHostPtr& host) : m_plugin(plugin), m_host(host)
{
...
//Register my method
registerMethod("runNotification", make_method(this, &OpenOnDesktopPluginAPI::runNotification));
...
}
//DistributedNotification class is my objective-c class with the implementation for post a distributed notification.
void OpenOnDesktopPluginAPI::runNotification()
{
DistributedNotification * notificationClass = [[DistributedNotification alloc] init];
[notificationClass postNotification];
[notificationClass release];
}
In my FBControl.html:
...
function myFunction()
{
plugin().runNotification();
}
...
My new method
...
I put my DistributedNotification.mm class in the
Build Phases -> "Compile Sources"
for my plugin target.
But I donĀ“t know if my runNotification method is called, because when (In my web page) I click on My new method link, nothing happens.
I'll repeat what I said on the forum when you ask; perhaps you haven't seen that answer yet:
First of all, you can debug with Xcode4, at least on some browsers; the trick is figuring out which process to connect to.
Secondly, you can always use NSLog to log things to the console. Thirdly, you could use log4cplus (see http://www.firebreath.org/display/documentation/Logging).
Finally, you haven't specified what browser you're testing on, nor have you indicated what happens. It looks reasonable, but aparently doesn't work? What doesn't work? What does it do?
It's nearly impossible to give you any useful advice without detailed information about what you are encountering.