I'm really new with esspresso issues and I have a little question, I need to emulate that users click on a button, tree times, but with some delay. A Human, takes some time to click a button, maybe with one second delay.
Whats the better way to made that on an esspresso test? othere frameworks have sleep, and so on... but I think that espresso hasen't. Any idea?
--Edited:
I made this, but don't know if it's correct:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Another way, but more clear, if you don't use thread.interrupt() is this:
SystemClock.sleep(millis)
for (int = i; i < numOfClicks; i++) {
onView(<matcher for your view>).perform(click());
SystemClock.sleep(1000);
}
Related
I am trying to save data into a Firebase RealtimeDatabase.
The process of saving is fine and works just well, but I was curious about an idea I had: To force the .setValue operation into a synchronous structure. I know, that that way isn't the best one, but I wanted to try it regardless of that fact.
So I came up with this code:
Thread t1 = new Thread(){
public void run(){
try {
Tasks.await(databaseReference.child("someChild").setValue(someObject));
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished");
}
In theory, that code snippet is intended to first set the value in the Database, then wait for the completion of that (through the Tasks.await()), and then print out **after that* the line "finished".
I wrapped the Tasks.await()-command in a second thread because when I don't, I get an exception which says something like "Tasks.await() must not be called in the main application thread".
But when I run the whole thing, it just gets stuck at t1.join();.
When I delete the t1.join();-statement, everything works just fine, but the second Thread (t1) isn't finished before the "finished"-output is there. I know why this is like that, but I am nontheless interested in a solution to that problem.
Please check this link. Following code is from this link. I am not sure why it says we have to use thread sleep to limit number of touch events.
I have a surfaceview and gamethread which processes events pushed by touch event. Is this sleep required or should I handle the sleep in gamethread instead by controlling framerate.
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
}
whole code
#Override
public boolean onTouchEvent(MotionEvent event) {
// we only care about down actions in this game.
try {
// history first
int hist = event.getHistorySize();
if (hist > 0) {
// add from oldest to newest
for (int i = 0; i < hist; i++) {
InputObject input = inputObjectPool.take();
input.useEventHistory(event, i);
gameThread.feedInput(input);
}
}
// current last
InputObject input = inputObjectPool.take();
input.useEvent(event);
gameThread.feedInput(input);
} catch (InterruptedException e) {
}
// don't allow more than 60 motion events per second
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
}
Your link points to an article that has been posted in 2009, you should really try to find one that is more up to date.
The code itself looks wrong, the author explains that his code is designed to avoid blocking the UI thread but then calls Thread.sleep(16) in onTouchEvent() which is called from the UI thread and blocks it.
What about this example? I haven't read it in detail, but the code looks much better : https://www.codeproject.com/Articles/827608/Android-Basic-Game-Loop
I want to send five data to Bluetooth. First one is turn on and second one is turn off, third one is turn on, and so on... I want post delay between turn on and turn off (about 1 second). Which is best one to do it. Currently, I am using sleep in Thread class
for (int i=1;i<5;i++) {
try {
if(i%2==0){
send(1);
Thread.sleep(500);
}
else{
send(0);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I'm working on a memory game for Android and I'm having a problem. When the user taps the second image - if the images are not the same I want the second image to show for 1, 2 seconds.
What I've tried is to sleep for 1-2 sec. the UI thread after the second image is activated - but this doesn't seem to work - the second image doesn't seem to show! (only the first images is showed)
Here's my code:
public void whenTapedImage(View v, int position)
{
i++;
ImageView imgV=(ImageView)v;
if(i%2!=0)
{
firstClick=position;
imgV.setImageResource(im.images.get(firstClick));
}
else
{
secondClick=position;
imgV.setImageResource(im.images.get(secondClick));
try {
Thread.currentThread().sleep(1000);
if(!(im.images.get(firstClick).equals(im.images.get(secondClick))))
{
Toast.makeText(easyGame.this, "Try Again!", Toast.LENGTH_SHORT).show();
im.notifyDataSetChanged();
gridview.setAdapter(im);
gridview.invalidate();
aux=player1Turn;
player1Turn=player2Turn;
player2Turn=aux;
}
else{
done=done+2;
ImageAdapter.keepVisibleViews.add(firstClick);
ImageAdapter.keepVisibleViews.add(secondClick);
if(player1Turn==true)
{
player1Score++;
String score=Integer.toString(player1Score);
score1.setText(score);
}
if(player2Turn==true)
{
player2Score++;
String score=Integer.toString(player2Score);
score2.setText(score);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
What am I doing wrong?
You must not sleep the UI thread as this would prevent android from delivering any other events to your activity's UI.
Instead, do something such as use a timer and have the timer's method use the run on ui thread facility to make the desired postponed change.
For robustness you may need to implement a state machine (either formally, or in effect) to keep track of what is supposed to be happening - you'll need to decide if the current delay should be aborted or enforced if another button is pushed, and make the state machine treat that appropriately.
This is similar to Waiting in android app
Try following the solution posted there and use the Timer Class
So my application re-uses quite a few activities. Originally I was simply adding a "fromClass" line as an extra to Intents as I switched between activities. The problem now is that I need to know which class I started with several activities ago, in order to display information properly on the current activity. I was trying to use an arraylist of strings to store the order and remove them from the list as I went backwards, but I cannot get strings to translate into class names and work correctly.
At the start of the activity I'm adding the following way:
Global.appNavigation.add("SecondActivity.class")
When I call onBackPressed() I'm doing this:
/* Remove Last Object (this class) */
Globals.appNavigation
.remove(Globals.appNavigation.size() - 1);
try {
Class<?> c = Class.forName(Globals.applicationNavigation
.get(Globals.appNavigation.size() - 1));
Activity obj = (Activity) c.newInstance();
Intent i = new Intent(mActivity, c);
startActivity(i);
finish();
} catch (ClassNotFoundException e) {
Log.e("Back", "Could not get a class name");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm constantly getting the ClassNotFoundException. Is there a better way to do this? This application I'm working with is getting quite large, so not calling finish() to an activity may not work in this case.
I ended up calling the "finish()" method, but did a better job keeping track of data that needed to be persistent.
Thanks to Ben Von Handorf for the suggestion (see comments 1 and 3 on original question)