Accessing the current activity when running an NunitLite test - xamarin.android

I've got an ISettings interface which is implemented in a platform-specific manner on Mono For Android and iOS. For Mono For Android, I'm using Activity.GetSharedPreferences to implement my settings - and this works fine from my application.
However, I want to be able to test this with NunitLite (which I'm using successfully to test my other core code).
The problem is that for my test fixture, I need an Activity on which to call GetSharedPreferences. Is there a way of accessing the currently running Activity either generically or via the NunitLite implementation?
James

You can access Shared preferences via:
Application.Context.GetSharedPreferences("whatever", FileCreationMode.Private)
You don't need an activity.

Related

How does framework like KIF or EarlGrey access the running application?

Usually in iOS unit tests, we create new objects, call the method we wanna test, and then validate the results. This is a stand-alone procedure. The test cases always start running with the app instance but we do not access that instance directly.
However, with the framework like KIF or EarlGrey, we are able to write functional tests by accessing UI elements with accessibility labels in the running app instance. I am wondering how it is implemented. We don't have something like context or root view controller object when tests start, how does the framework find the presenting view controller from "nowhere"?
Because they are based on XCTest's Unit Test paradigm. In it, the tests and the app are both in the same bundle and therefore have access to the app internals.
Using [UIApplication sharedApplication], you can actually get the UIWindow for the app and find the entire View Hierarchy.

iOS UnitTests with preloaded sqlite3 DB

I am required to create iOS unit tests, where i need to push an sql .db into app location/sandbox/whatever (may be from testsuit's setUp() func) and run some CRUD operations from the testcases and later delete the db from tearDown() when tests done. How can i push a .db into app location for test, which will be used just like a db used inside app.
Another question, I need to run automated tests, so is there any command (like in Android we use adb, in Tize we use gdb) for iOS app to insert these database file in app location to run those testcases. If i am missing any point please help.
What is the standard way to test CRUD operations in a simulated db for iOS.
As far as my study, there is no adb/sdb like tool for iOS. Moreover there is an workaround for performing test on real database.
Steps
Keep the test-database into App/resource folder. You can skip this large test-database from your release build by following these technique.
Make a function in App side to replaceWithTestDatabase(), which basically replace your App database with App/resource/test-database. You can temporary copy current App db if you need.
If you want to access from AppTests (unit test), you can simply call Aapp/replaceWithTestDatabase as iOS Tests are hosted under main App.
If you want to access from AppUITests, you need to send extra launch argument from setUp() method to let App know that it should call replaceWithTestDatabase from app delegates. Here is a nice explanation how we can send arguments to main App from AppUITests.
Hope it helps.

During iOS application tests, how can I access a singleton instance in the host application?

I'm writing an iOS app with Swift, and we are using Parse for our backend. I'm working on some acceptance tests, and have mocked the Parse API using OHHTTPStubs.
I'm writing a test that taps through our login form and logs in a given user. In the view controller, we are calling the PFUser.logInWithUsernameInBackground function. Inside the completion block, I'm calling println(PFUser.currentUser()), and I can see that it is set correctly.
However, when I call println(PFUser.currentUser()) from my test case, it is still nil. I want to make sure that the user is being signed in, and that their username is correct.
The tests are application tests which are injected into the host application, and are written with KIF. So I think the tests may contain their own separate PFUser singleton instance, instead of accessing it on the host application. So I need to figure out how to gain access to the currentUser() inside the main app.
Is there any way I can tell my tests to use the PFUser class from the main app?
If you think this should normally work out of the box, what kind of mistakes should I look for in my code? (e.g. bridging headers, build settings, etc.)
I figured out the solution. I needed to access the class from the main bundle, and I did so by adding this line to a helper file in my test target:
let AppUser: AnyClass! = NSBundle.mainBundle().classNamed("PFUser")
This lets me perform checks such as AppUser.currentUser() == nil, and call functions like AppUser.signOut() on the main bundle, instead of in the test bundle.
The reason is that the main bundle and test bundle are separate, and classes are not shared between them. This OHHTTPStubs wiki page goes into more depth about the difference between hosted and non-hosted tests in Xcode.

Arrange Cytoscape network windows using the API (Version 3)

I'm developing a bundle app for Cytoscape 3. In this app I need a funcionality very similar to the build-in View > Arrange Network Windows > Grid, or Ctrl+G.
However, I cannot seem to find anything in Cytoscape's API that allows me to arrange network windows.
The source code behind the build-in funcionality can be found here: https://github.com/cytoscape/cytoscape-impl/blob/cbd6ae7202a2137d0224862aa371b82c1ec9a7a7/swing-application-impl/src/main/java/org/cytoscape/internal/view/CyDesktopManager.java#L81
As you can see I need a reference to the JDesktopPane, how do I get this through the API?
I don't think there is clean API-way of achieving what I want. You can however do it as follows:
In your activator you are able to retrieve a CySwingApplication reference: getService(bc, CySwingApplication.class), from which you can call the method .getJFrame(). You can recursively scan through all swing Container components with until you find a component of the type JDesktopPane. When you call .getAllFrames() of the JDesktopPane you can do whatever with you network windows (JInternalFrame).

IOS testing: Test interaction between two apps

I need to automate a test on an IOS app using UI automation.
I need to test the following scenario.
1) open the mail app and select a file to share. This will open my app
2) Now I need to perform UI actions on my app and do some tests
I can't figure out how do automate this scenario using Instruments or Appium. All these tools take bundle name of one app. I need a way to control and perform UI actions on two app from a single script.
Any suggestions?
Edit: For clarification
This is not possible within one session
The solution is to split up your tests to encompass one or more webdriver sessions.
Part One:
desired_caps['app'] = 'sampleApp1'
driver = webdriver.new('http://0.0.0.0/wd/hub:4732', desired_caps)
// Do what you need to do.
driver.quit()
Part Two:
desired_caps['app'] = 'sampleApp2'
driver = webdriver.new('http://0.0.0.0/wd/hub:4732', desired_caps)
// Do what you need to do.
driver.quit()

Resources