I am writing Instrumentation tests with usage of Espresso 2.2.
Flow I want to test:
radioButton clicked by test
onClick launches request to API
after every time different time I receive response
positive response triggers interface method that is called in activity
onRequestSuccess I am making additional panel on screen named vSetupAmount visible
I want to register IdleResource after click on radioButton so it waits until vSetupAmount becomes visible. But I can't make it work. Please tell me what am I doing wrong.
I have written such IdleResource:
public class AmountViewIdlingResource implements IdlingResource {
private CountriesAndRatesActivity activity;
private ResourceCallback callback;
private SetupAmountView amountView;
public AmountViewIdlingResource(CountriesAndRatesActivity activity) {
this.activity = activity;
amountView = (SetupAmountView) this.activity.findViewById(R.id.vSetupAmount);
}
#Override public String getName() {
return "Amount View idling resource";
}
#Override public boolean isIdleNow() {
callback.onTransitionToIdle();
return amountView.getVisibility() == View.VISIBLE;
}
#Override public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.callback = resourceCallback;
}
}
So I am passing activity to IdleResource, link view with variable. I understand that IdleResource won't let test go through until isIdleNow() returns value true. So if view is View.GONE then it won't go further.
How it looks in test:
// click of radioButton picked from radioGroup
onView(withId(rgDeliveries.getChildAt(id).getId())).perform(scrollTo(), click());
// wait for view to become visible
Espresso.registerIdlingResources(new AmountViewIdlingResource(getActivity()));
// go to button on view
onView(withId(R.id.btnGetStarted)).perform(scrollTo());
// unregister idle resource
for ( IdlingResource resource : getIdlingResources()) {
Espresso.unregisterIdlingResources(resource);
}
So I get my click on radioButton. IdleResource is successfully registered but nothing happens. On my device API response comes. vSetupAmount is being displayed but
amountView.getVisibility() == View.VISIBLE;
which is being checked forever (but I see my view on screen) always returns false.
What am I doing wrong?
I know this might be too late, but my answer might help someone.
When you instantiate and register your AmountViewIdlingResource, keep a reference to it and only unregister that reference, don't unregister all IdlingResources.
Your implementation of the isIdleNow() method should only call the callback's onTransitionToIdle() method if your idling resource is really idle not every time.
I needed to do something similar, to wait for a button to appear on the screen, which in turn only got to be visible after an HTTP call finished. My implementation of a view visibility Idling Resource is as follows:
public class ViewVisibilityIdlingResource implements IdlingResource {
private final View mView;
private final int mExpectedVisibility;
private boolean mIdle;
private ResourceCallback mResourceCallback;
public ViewVisibilityIdlingResource(final View view, final int expectedVisibility) {
this.mView = view;
this.mExpectedVisibility = expectedVisibility;
this.mIdle = false;
this.mResourceCallback = null;
}
#Override
public final String getName() {
return ViewVisibilityIdlingResource.class.getSimpleName();
}
#Override
public final boolean isIdleNow() {
mIdle = mIdle || mView.getVisibility() == mExpectedVisibility;
if (mIdle) {
if (mResourceCallback != null) {
mResourceCallback.onTransitionToIdle();
}
}
return mIdle;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
mResourceCallback = resourceCallback;
}
}
Hope this helps
Related
I understand there are a lot of information about it out there, but I haven't found one that matches my case yet.
I have a recycleview on a fragment that is always open, so the fragment basically never re-creates itself.
This is my code to load the adapter.
reLoad(); //method shown below
mRecycler.setAdapter(new SolicitationAdapter(myRealm.where(SolicitationDatabase.class).findAllAsync()));
And this is the logic I came up with:
public void reLoad() {
if (!myRealm.where(SolicitationDatabase.class).findAll().isEmpty()) {
mNothingHere.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
mNothingHere.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
It works great the first time the user opens the app.
The trouble starts when the user creates a record, since the fragment doesn't re-create itself it never reloads.
The reason I haven't been able to reload after user adds something is because the method to add a new record is on a singleton being called from a different activity. Which means when I try to do it I get a nullpointerexception when declaring the the recycleview and the textview.
Edit - What I tried (reloading views from another place)
I have a class called PostHelper, this class is in charge of posting a new record.
This is the constructor:
public PostHelper(Context context, Activity activity) {
this.mContext = context;
this.mActivity = activity; //I call this in order to use "findViewById"
This is where the post happens:
public String addSolicitation(File _file, boolean fromQueue) {
//loading view
TextView nothingHere = (TextView) mActivity.findViewById(R.id.nothing_here);
RecyclerView recycler = (RecyclerView) mActivity.findViewById(R.id.recycler);
...so on until after the post:
SolicitationAdapter n = new SolicitationAdapter(myRealm.where(SolicitationDatabase.class).findAll());
n.notifyDataSetChanged();
nothingHere.setVisibility(View.GONE);
recycler.setVisibility(View.VISIBLE);
And this is the stacktrace:
06-01 21:43:37.511 9122-9122/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ga.realm3, PID: 9122
io.reactivex.exceptions.OnErrorNotImplementedException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
Edit 2 - I load PostHelper class using the following:
mPostHelper = new PostHelper(this, PostSolicitationActivity.this);
You're supposed to make sure that SolicitationAdapter is a RealmRecyclerViewAdapter, like so:
public class SolicitationAdapter extends RealmRecyclerViewAdapter<SolicitationDatabase, SolicitationViewHolder> {
public SolicitationAdapter(OrderedRealmCollection<SolicitationDatabase> results) {
super(results, true);
}
...
}
And then what you need to do is that you put the RealmResults as a field reference in your Activity:
public class PostSoliticiationActivity extends AppCompatActivity {
RealmResults<Solicitation> results;
Realm realm;
RealmChangeListener<RealmResults<Solicitiation> realmChangeListener = (results) -> {
if(results.isLoaded() && results.isValid()) {
if(results.isEmpty()) {
mNothingHere.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
mNothingHere.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
SolicitationAdapter adapter;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.soliticiation_activity);
// bind views
realm = Realm.getDefaultInstance();
results = realm.where(SolicitationDatabase.class).findAllSortedAsync("id");
// .sort("id").findAllAsync(); in 4.3.0+
results.addChangeListener(realmChangeListener);
adapter = new SoliticiationAdapter(results);
mRecycler.setAdapter(adapter);
// layout manager as well
}
#Override
public void onDestroy() {
results.removeChangeListener(realmChangeListener);
realm.close();
super.onDestroy();
}
}
So things you don't need:
1.) reLoad() method
2.) onPostAdded callback
3.) PostActionListener
As long as you just add the SoliticiationDatabase to the Realm in a transaction, it'll all work without manually syncing ui.
If I understand correctly, you want to be notified in another view when an action happens elsewhere.
The way to do that is usually interfaces.
public class PostHelper {
// Define these
public interface PostActionListener {
void onPostAdded();
}
private PostActionListener postListener;
public void setPostActionListener(PostActionListener listener) throws ClassCastException {
this.postListener = (PostActionListener) context;
}
public PostHelper(Context context) {
this.mContext = context;
// Cast the passed context as a listener
if (mContext instanceof PostActionListener) {
this.postListener = (PostActionListener) mContext;
}
}
public String addSolicitation(File _file, boolean fromQueue) {
// Do something
// Callback to the UI to update
if (this.postListener != null) {
this.postListener.onPostAdded();
}
}
Then, in your initial Activity
public class YourActivity extends AppCompatActivity
implements PostHandler.PostActionListener {
// ... fields
#Override
public void onPostAdded() {
reLoad();
}
public void reLoad() {
boolean emptyList = myRealm.where(SolicitationDatabase.class).findAll().isEmpty();
mNothingHere.setVisibility(emptyList ? View.VISIBLE : View.GONE);
mRecycler.setVisibility(emptyList ? View.GONE : View.VISIBLE);
}
#Override
public void onCreate(Bundle b) {
...
mPostHelper = new PostHelper(this);
}
However, since you are using Realm, and there really is no data that you need to "return" here, then you can simply let the Android Activity lifecycle refresh the data for you.
In Android, how do I take an action whenever a variable changes?
So I want to implement a listener for an object I created. What I want it to do is execute a block of code when its value changes from false to true.
As I am following this thread, I can't understand where the person wants us to implement the last block of code containing the logic for the listener.
Could someone, hopefully, guide me in the right direction?
(This question is being asked here as I don't have enough rep. points)
That last bit of example code triggers the listener, so it basically needs to be run whenever the "event" occurs. In this case the "event" is whenever (wherever in the code) the value of the variable changes.
If you have a setter and that is the only place the value changes, that is where you'd put it. If you are changing the value in multiple places throughout your code, I would make a new private method (call it signalChanged), put your code there, and then call it immediately after the variable assignment in the cases you want the listener to fire.
Here's an example (some code borrowed from linked answer, haven't checked that it compiles).
public class MyObj
{
public MyObj(int value)
{
setValue(value);
}
private int myValue;
public int getValue() { return myValue; }
public void setValue( int value )
{
if (value != myValue)
{
myValue = value;
signalChanged();
}
}
public interface VariableChangeListener
{
public void onVariableChanged(Object... variableThatHasChanged);
}
private VariableChangeListener variableChangeListener;
public void setVariableChangeListener(VariableChangeListener variableChangeListener)
{
this.variableChangeListener = variableChangeListener;
}
private void signalChanged()
{
if (variableChangeListener != null)
variableChangeListener.onVariableChanged(myValue);
}
}
you have to create a callback interface
here is a good about custom listener tutorial
here is a sample
public class MyObj {
VariableChanger onVariableChanged ;
public void setOnVariableChanged(VariableChanger onVariableChanged) {
this.onVariableChanged = onVariableChanged;
}
void log(){
boolean changed = false;
onVariableChanged.onVariableChanged();
//this will call it
}
interface VariableChanger{
void onVariableChanged();
}
}
class logic {
MyObj mo = new MyObj();
void main(){
mo.setOnVariableChanged(new MyObj.VariableChanger() {
#Override
public void onVariableChanged() {
//do your action
}
});
}
}
In Android, like any language, most developper uses logic comparisons to check values (if, else, switch, =, !=, >, <, etc) or Event (signal)
What kind of listener do you want to implement?
I'm trying to test that the AutoCompleteTextView will show the items after some word will be typed. But there is a delay between typing and showing the popup. First i was using Thread.sleep() and it was working just fine. But I know that this approach isn't clear so I'm trying to accomplish it with IdlingResource. But it doesn't work for me. I literally read first 5 pages of Google responses, but either I don't understand how it should work, or I have some error in my code.
Here is the code:
static class AutocompleteShowIdlingResource implements IdlingResource {
private Activity activity;
private #IdRes int resId;
private ResourceCallback resourceCallback;
public AutocompleteShowIdlingResource(Activity activity, #IdRes int resId) {
this.activity = activity;
this.resId = resId;
}
#Override
public String getName() {
return this.getClass().getName() + resId;
}
#Override
public boolean isIdleNow() {
boolean idle = ((AutoCompleteTextView) activity.findViewById(resId)).getAdapter() != null;
Log.d(TAG, "isIdleNow: " + idle);
if (idle) {
resourceCallback.onTransitionToIdle();
}
return idle;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.resourceCallback = callback;
}
}
The test itself:
Activity activity = calibrationActivityRule.getActivity();
onView(withId(R.id.autocomplete_occupation)).perform(typeText("dok"));
IdlingResource idlingResource = new AutocompleteShowIdlingResource(activity, R.id.autocomplete_occupation);
Espresso.registerIdlingResources(idlingResource);
assertEquals(((AutoCompleteTextView) activity.findViewById(R.id.autocomplete_occupation)).getAdapter().getCount(), 3);
Espresso.unregisterIdlingResources(idlingResource);
But the test fails on java.lang.NullPointerException when trying to call getCount() on null adapter. The log is printing
isIdleNow: false
just once, which is quite strange.
There isn't much clear examples how to use IdlingResource, so maybe someone can make it clear for me. Thanks.
Your IdlingResource will have an effect only if you use it together with onView(...).check(...) or onData(...).check(...). Actually, the "magic" will happen in the check call - it's the place where Espresso waits until there are no running AsyncTasks or no blocking IdlingResources.
Now let's correct your code so that it works:
Activity activity = calibrationActivityRule.getActivity();
onView(withId(R.id.autocomplete_occupation)).perform(typeText("dok"));
IdlingResource idlingResource = new AutocompleteShowIdlingResource(activity, R.id.autocomplete_occupation);
try {
Espresso.registerIdlingResources(idlingResource);
//that's where Espresso will wait until the idling resource is idle
onData(anything()).inAdapter(withId(R.id.autocomplete_occupation)).check(matches(isDisplayed());
finally {
Espresso.unregisterIdlingResources(idlingResource);
}
assertEquals(((AutoCompleteTextView) activity.findViewById(R.id.autocomplete_occupation)).getAdapter().getCount(), 3);
There is an IdlingResource, e.g. like this
public class IRWatchlistNamesLoaded implements IdlingResource {
private final static String CLASSNAME = "IRWatchlistNamesLoaded";
private ResourceCallback callback;
public IRWatchlistNamesLoaded () {}
#Override
public String getName() {
return getClass().getName();
}
#Override
public boolean isIdleNow() {
if(LMaxApplication.watchlists.getNames() != null && LMaxApplication.watchlists.getNames().size() > 0) {
callback.onTransitionToIdle();
CustomLog.print(CustomLog.UI_TEST, CLASSNAME, "isIdleNow = TRUE. size = " + LMaxApplication.watchlists.getNames().size());
return true;
}
CustomLog.print(CustomLog.UI_TEST, CLASSNAME, "isIdleNow = FALSE. size = " + LMaxApplication.watchlists.getNames().size());
return false;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.callback = resourceCallback;
}
}
And the usage is standard - when I need the resource I call
watchlistLoadedIR = new IRWatchlistNamesLoaded();
needToUnregisterWatchlistLoadedIR = true;
Espresso.registerIdlingResources(watchlistLoadedIR);
What I see in logs is that isIdle() returns false (1 or 2 times), my test keeps going and my resource is not loaded properly, so test fails. Also, need to notice that in some other tests this IdlingResource works and espresso really waits for the resource to be idle. The usage is absolutely the same.
Please, maybe somebody has any idea why this could happen?
I don't know if this will help but there is a great way of setting up IdlingResources described here which might help solve your problem. I have a sneaking suspicion that it has to do with the fact that you are instantiating your class within the test, which I don't think is what you want to be doing, since you need to be using the IdlingResources from with an activity, using onView to perform action on different components which would then end up running the class in question.
Is there a canonical solution using Espresso to wait for a specific Activity to finish or start?
I have a SplashActivity that appears for a few seconds, then a MainActivity. I want Espresso to interact with the MainActivity, not the SplashActivity, but I can't seem to find any information about waiting for such a condition.
The closest thing I can find is a mention of idle resources but its not clear to me how I would use that here to wait for the Activity.
I guess your splash activity is performing some initialization.
If this is the case, my suggestion is to define some sort of listener pattern in order to be able to get a callback when the initialization is done. Then, you can make Espresso wait for the initialization with an IdlingResource.
NB: The following is NOT complete code, but it is meant to give you a hint in how to do so:
public class SplashIdlingResource implements IdlingResource, YourApplicationInitListener {
// volatile because can be set by a different
// thread than the test runner: the one calling back
private volatile boolean mIsInitialized;
private ResourceCallback mCallback;
public SplashIdlingResource() {
YourApplication application = // retrieve your Application object
mIsInitialized = application.isInitialized();
if (!mIsInitialized) {
application.addInitListener(this);
}
}
#Override
public String getName() {
return SplashIdlingResource.class.getName();
}
#Override
public boolean isIdleNow() {
return mIsInitialized;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
mCallback = callback;
}
#Override
public void onApplicationInitCompleted() {
m_isInitialized = true;
if (m_callback != null) {
m_callback.onTransitionToIdle();
}
}
}
Where onApplicationInitCompleted() is the callback you defined and which must be called when the Splash Activity, and so the initialization, is done.
Finally, register this new IdlingResource with Espresso by calling Espresso.registerIdlingResource in test setup.