How do I have an if/else path in my tests
for example
when a certain dialog is present I dismiss it and continue
vs
when it's not present I continue nonetheless
What you need is something like an isVisible(int id), the implementation of which would be something like this:
public boolean isVisible(int elementID) {
try {
onView(withId(elementID)).check(matches(isDisplayed()));
return true;
} catch (Throwable t) {
return false;
}
}
You would then check for the dialog in your test like this:
if(isVisible(R.id.dialogID)) {
onView(withText("OK")).perform(click()); // dismiss the dialog by clicking 'OK' button
// do whatever you want to do after this
}
This should take care of your problem.
Related
I have created a custom document provider for Android using this code as a base.
https://learn.microsoft.com/en-us/samples/xamarin/monodroid-samples/storageprovider/
This allows for a new drive to be mapped onto the documents folder when browsing/saving documents.
If there is an exception due to a password timeout for example, I would like to pop back up the existing app so the users can entered their credentials again to log in.
Is this possible? As an example of what I am looking for, if the QueryRoots failed with a particular exception, could I run something to pop back up the app interface here?
public override ICursor QueryRoots(string[] projection)
{
Log.Verbose(TAG, "queryRoots");
var result = new MatrixCursor(ResolveRootProjection(projection));
try
{
if (!IsUserLoggedIn())
{
return result;
}
MatrixCursor.RowBuilder row = result.NewRow();
... other init code here
}
catch (Exception ex)
{
if (ex.Message == "NoSessionException")
{
// LOGIC TO BRING BACK APP TO LOG IN AGAIN HERE...
}
}
return result;
}
I make a sample code about how to lauch the app again for your reference. You could put Launch method in catch statement.
In Xamarin.Forms, you could use Dependency service to start the app with package name.
Create a interface:
public interface IDpendencyService
{
Task<bool> Launch(string stringUri);
}
Implemention of Android:
public class DependencyImplementation : Activity, IDpendencyService
{
public Task<bool> Launch(string stringUri)
{
Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
if (intent != null)
{
intent.AddFlags(ActivityFlags.NewTask);
Forms.Context.StartActivity(intent);
return Task.FromResult(true);
}
else
{
return Task.FromResult(true);
}
}
}
Register in MainActivity:
DependencyService.Register<IDpendencyService, DependencyImplementation>();
I use a Button event to invoke. You could try to invoke in catch.
DependencyService.Get<IDpendencyService>().Launch("com.companyname.xamarindemo");
Screenshot: I have a button on Page21. When i click the button, it would reload the app and pop back up the existing app.
I want to add a function in my app that checks if a menu item is disabled or enabled, if disabled I want my app to do tasl a, if enabled I want my app to do task b.
what I have tried so far;
if (menuItem.setEnabled()==false){
//do stuff
} else {
//do stuff
}
if (menuItem.setEnabled(false)){
//do stuff
} else {
//do stuff
}
if (menuItem.setEnabled().equals(false)){
//do stuff
} else {
//do stuff
}
I am not sure how I can do this, as whatever I tried doesn't seem to work.
Try this:
MenuItem mymenu = menu.findItem(R.id.mine_menu);
And:
if(mymenu.isEnabled()){
//do something
}
Here is a function that gets called when an item gets selected from a ListView:
async void detail_clicked(object sender, SelectedItemChangedEventArgs e){
if (e.SelectedItem == null) {
return;
}
Detail selected = (Detail)e.SelectedItem;
order_vm.List_of_details.Add(selected);
await DisplayAlert ("Item Added",
String.Format ("{0} added to cart.", selected.detail_name), "Okay");
((ListView)sender).SelectedItem = null;
}
I added this function using the ItemSelected event handler
details_list.ItemSelected += detail_clicked;
The first time I click on the Item, the DisplayAlert pops up. After the first click, the DisplayAlert inside detail_clicked no longer pops up. But the other code inside the handler does get called.
Anyone know how to fix this issue? Is it something I am not understanding about event handlers? Is it something about await/async?
The DisplayAlert might be running on a different thread. Try wrapping Display Alert in Device.BeginInvokeOnMainThread. You can ready about that here.
Please check again without async on method and await on DisplayAlert().
Use this following code. It will helps you.
private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
listView.SelectedItem = null;
DisplayAlert("Alert", e.SelectedItem.ToString(), "Ok");
}
I'm trying to implement a check box in a MaterialDialog using this library, and the check box asks the user if they don't want to see that dialog again. The dialog appears if the user's phone has NFC, but it is deactivated.
If the user presses the positive button in the dialog and has the box ticked, then it accesses a Realm object with a Boolean attribute named "NfcStatus", and sets that to true. If they press the negative button with the box ticked, then that Realm object's NfcStatus is set to false.
Here's the code of the MaterialDialog:
new MaterialDialog.Builder(context)
.title("NFC")
.content("NFC is disabled. Would you like to activate it?")
.items(R.array.checkbox) //this just has one string in it which says "Please don't show me this again"
.itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
#Override
public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
/**
* If you use alwaysCallMultiChoiceCallback(), which is discussed below,
* returning false here won't allow the newly selected check box to actually be selected.
* See the limited multi choice dialog example in the sample project for details.
**/
checkboxIsChecked = true; //TODO: checkboxIsChecked isn't being passed into onPositive or onNegative
return true;
}
})
.positiveText(R.string.accept)
.positiveColorRes(R.color.main_theme_color)
.negativeText(R.string.decline)
.negativeColorRes(R.color.main_theme_color)
.callback(new MaterialDialog.ButtonCallback() {
#Override
public void onPositive(MaterialDialog dialog) {
//this was how I was checking if checkboxIsChecked was true or false
Log.d("checkboxIsChecked", checkboxIsChecked?"true":"false"); }
if (checkboxIsChecked) {begins
if (ks.contains(KEY_NAME)) {
realmKey = ks.get(KEY_NAME);
}
realm = Realm.getInstance(context, realmKey);
RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst(); realmPhone.setNfcStatus(true);
}
activity.finish();
startNfcSettingsActivity();
Toast.makeText(context, R.string.nfc_disabled_message, Toast.LENGTH_LONG).show();
}
#Override
public void onNegative(MaterialDialog dialog) {
if (checkboxIsChecked) {
if (ks.contains(KEY_NAME)) {
realmKey = ks.get(KEY_NAME);
}
realm = Realm.getInstance(context, realmKey);
RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst();
realmPhone.setNfcStatus(false);
}
}
})
.cancelable(false)
.show();
The problem was that even if the check box was ticked, the checkboxIsChecked variable was still false when using it in onPositive or onNegative, so it was never being written to the Realm object. Am I doing this the wrong way?
For changing and saving the RealmObject, you need to use transactions. Related documents can be found here.
In you case, it would be something like:
realm = Realm.getInstance(context, realmKey);
// I'm not quite sure how did you create the realmPhone at the first time,
// just assume you have one realmPhone in the Realm.
RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst();
realm.beginTransaction();
realmPhone.setNfcStatus(false);
realm.commitTransaction();
// Close the realm instance after using it is very important! To avoid leaks.
realm.close();
BTW, it seems code:
RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst();
realmPhone.setNfcStatus(false);
is not called. If it does, a IllegalStateException should be thrown since you didn't call it in a Realm transaction. Or maybe RealmPhone is not inherited from RealmObject?
i am attempting to implement a built in controller that is part of the scoreloop library. the documentation states:
Basic Usage:
To invoke the TOS dialog if it was not accepted previously, the following code may be used:
final TermsOfServiceController controller = new TermsOfServiceController(new TermsOfServiceControllerObserver() {
#Override
public void termsOfServiceControllerDidFinish(final TermsOfServiceController controller, final Boolean accepted) {
if(accepted != null) {
// we have conclusive result.
if(accepted) {
// user did accept
}
else {
// user did reject
}
}
}
});
controller.query(activity);
but when i paste this into my code i get the following syntax errors:
am i using this incorrectly? how and where would this be used any ideas?
EDIT: after moving the statement to the method where i want to show the dialog i now get the following error:
You seem to be calling controller.query(activity) in a class body where a declaration is expected. Move the statement controller.query(activity) to a method where you would like to show the dialog.