How to click contextual action bar item using Espresso framework - java

I am writing a test case that long clicks an item in my ListView (backed by a CursorAdapter) which will pull up a Contextual Action Bar and has a menu item that allows the user to delete that item.
I use the following code:
public void testDeleteFirstAccount(){
// Long click
onData(is(instanceOf(Cursor.class))).atPosition(0).perform(longClick());
// Click delete menu item
onView(withId(R.id.action_delete_account)).perform(click());
// Find alert button with text
onView(withText("Yes")).perform(click());
}
However, I am unable to click the button because I believe the test is running too quickly. I get the following error:
android.support.test.espresso.PerformException: Error performing
'single click' on view 'with id:
com.example.android.cashcaretaker:id/action_delete_account'.
Caused by: java.lang.RuntimeException: Action will not be performed
because the target view does not match one or more of the following
constraints: at least 90 percent of the view's area is displayed to
the user.
The reason I believe the test is too quick is because if I add Thread.sleep() after the long click, everything will work fine. I only did this as a test to verify my suspicions, I'm not sure stopping the UI like that is the proper way to move forward.
I have also tried adding getInstrumentation().waitForIdleSync() but had no success.
Am I doing something else wrong? What is the proper way to use Espresso to click a CAB item?

Perhaps the view is still being animated at the moment that Espresso tries to click the ContextualActionBar item. And if you set a timeout, the animation has time to finish and the view is completely displayed by the time the click is performed.
Have you tried disabling animations in the test device as stated in the Espresso Setup guide?

Related

Android Studio, change the behavior of the back button to affect items in a RecyclerView

I have a problem regarding the back button feature together with RecyclerView, my goal is to emulate the behavior in the Contacts application, where you press and hold (long press) an item in the RecyclerView and a check box appears behind the item(s) (all of them). Then, when you press the back button, all the check boxes disappear.
Regarding the information I provided we can break the problem in two parts:
First, we need to solve the long click problem, which I suspect we can use
NotifyDataSetChanged() together with hiding the check boxes in the XML so that we can switch between checkbox.setVisibility(VISIBLE) and checkbox.setVisibility(GONE).
Lastly, and this is the hardest part for me, I would like that, when back button is pressed, all the check boxes disappear, instead of leaving the app.
Thank you in advance and sorry if the problem is not understandable, as this is the first time I am posting a problem and English is not my native language.
What you are describing is known as Contextual Action Mode.
You'll notice, that when you long press the item the toolbar (action bar) on top changes and shows the number of selected items and a set of action you can apply to the selected items.
Pressing the back button then cancels the action mode.
It is beyond Stackoverflow's scope to explain the whole action mode system, but you can simply search for it in the internet.
Here is a tutorial for beginners, for example.
I can give you a better answer if you post your code.
You'd probably want your adapter to contain a list of selected items. If the list has any elements, you show the checkboxes and check the ones that correspond to the list. If it is empty, you don't do that. You're correct about notifying data set changed. Long press on an item would add that item to this list. Then when your adapter re-laid out your items, it would show the checkboxes since that list would not be empty.
For the second issue, you'd want to override onBackPressed() in your activity. Then you can have some code like:
public void onBackPressed() {
if (adapter.hasItemsSelected()) {
adapter.clearSelection()
} else {
super.onBackPressed()
}
}
You'll need to write these adapter methods. hasItemsSelected should check to see if there are elements in the selected items list and clearSelection should clear the list and notify data set changed.

Input.TextInputListener brings up Navigation bar but doesn't get rid of it

My game is set to immersive mode using the Application Config (config.useImmersiveMode = true).
Also, in my game, I implement Input.TextInputListener in order to get some user input. However, when someone activates the TextInputListener, the navigation bar comes up, and stays even after they exit out of the TextInputListener. How would I set it to re-hide the navigation bar whenever the user navigates out of the TextInputListener?
Here is my game so you can see the issue yourself:
https://play.google.com/store/apps/details?id=com.samuel.doubledodge
The TextInputListener should come up the first time you start the game, and you can manually make it come up by pressing the "Change name" button on the Game Over screen.
Thanks

Unfortunately MyApp has stop working when some listview item click

I am new in android. I have been developing a simple App. I have listview that contain 50 items. It's working when i click some top items. But when i scroll down the item and click some bottom item, it gives error "unfortunately myapp has stop working".
I used switch case on listview item click.
can you give me how to solve this problem.?
Although you havent given us any code or stack trace, hence it is pretty difficult to gauge what you are running into. But you can check whether you have handled for onclick of items after a certain range (When it fails). As you have stated that you are using switch case for the item click.

How to handle multiple pop-ups

I am writing a selenium test for the following flow:
Select all items.
Click on the Run button - opens a pop-up with two buttons "Proceed" and "Cancel".
Click on "Proceed", opens the new pop-up which will display the progress bar.
Here are two problems I am facing:
I am able to click the "Proceed" button via selenium code, but the moment it gets clicked the other pop-up which displays the progress bar doesn't show up, because of thisthe selenium is not able to identify the element. How can I handle this?
The Progress bar has one button "Cancel", while the progress bar is being filled the Value of the button remains "Cancel", but after it reaches 100% the Value changes to "Close". How should I handle this?
I tried the following, but it didn't work:
Tried to wait until the visibiltyOfElement, presenceOfElement of the element (progress bar) by XPath and id.
Tried Thread.currentThread.sleep after clicking "Proceed" and Implicit wait too.
Tried to wait until AlertIsPresent.
The code:
// Select all items
wd.findElement(By.id("ListSelectAll")).click();
// click on Add the selected
wd.findElement(By.xpath("//*#id='ListRunLink']/img")).click();
// Proceed to confirm Adding
wd.findElement(By.xpath(".//*#id='Proceed']")).click();
// Wait until the Progress Bar Gets Displayed using the buttons
// present in the Progress bar to address Wait!!
wt.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='Close']‌​")));
// The above code does NOT work - "not able to locate element with id CLOSE"
Can someone help me with this?

Toggling the state of a menu item

I have an Eclipse RCP app I'm working on. It has some view-specific menus and one of the menu items is an item which I would like to display a tick next to when the corresponding functionality is enabled. Similarly, the next time the item is selected, the item should become unticked to reflect that the corresponding functionality is disabled.
My question is this: how do I set the toggle state of these menu items? I have an IHandler to deal with the event when the menu item is selected but I'm unsure how to update the GUI element itself.
Does the StackOverflow community have any thoughts on how I might solve this?
The solution involves having the command handler implement the IElementUpdater interface. The UI element can then be updated as so:
public void updateElement(UIElement element, Map parameters)
{
element.setChecked(isSelected);
}
updateElement is called as part of a UI refresh which can be invoked from the handler's execute command as so:
ICommandService service = (ICommandService) HandlerUtil
.getActiveWorkbenchWindowChecked(event).getService(
ICommandService.class);
service.refreshElements(event.getCommand().getId(), null);
Lots more info here (see Radio Button Command and Update checked state entries)
Maybe things have changed, maybe there's a difference between RCP and an Eclipse plug-in, but I just set the style of my action to toggle, and it handled the toggling automatically. You can see the source code on github.com.
<action
class="live_py.EnableAction"
id="live-py.enable.action"
label="Li&ve Coding"
menubarPath="pyGeneralMenu/pyNavigateGroup"
state="false"
style="toggle">
To see whether the feature is currently enabled, I just look up the action by its id, and then call isChecked().

Categories

Resources