How to setup IdlingResource for item inside RecyclerView using Espresso? - android

I have an activity. It contains fragment, and this fragment has RecyclerView. Some data loading from server and showing inside first item of RecyclerView.
I need setup IdlingResource for this item. So when the data will be loaded from server and showing inside this item, tests must starting.
How to setup IdlingResource for item inside RecyclerView?

IdlingResource on RecyclerView
I'm not sure that's how we use it, but it works...
In YourFragment:
public interface RecyclerViewHaveDataListener {
void recyclerViewHaveData();
}
private RecyclerViewHaveDataListener callbackIdl;
#VisibleForTesting
public void registerOnCallBackIdl(RecyclerViewHaveDataListener callbackIdl){
this.callbackIdl = callbackIdl;
if (adapter.getItemCount() > 0){
this.callbackIdl.recyclerViewHaveData();
this.callbackIdl = null;
}
}
// Use this where you add your datas to your RecyclerView by your adapter
if (callbackIdl != null && adapter.getItemCount() > 0){
callbackIdl.recyclerViewHaveData();
callbackIdl = null;
}
In Your test package:
public class RecyclerViewIdlingRes implements IdlingResource, YourFragment.RecyclerViewHaveDataListener {
private String name;
private boolean isIdle;
private volatile ResourceCallback resourceCallback;
public RecyclerViewIdlingRes(String name) {
this.name = name;
}
#Override
public String getName() {
return name;
}
#Override
public boolean isIdleNow() {
return isIdle;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.resourceCallback = callback;
}
#Override
public void recyclerViewHaveData() {
if (resourceCallback == null){
return;
}
isIdle = true;
resourceCallback.onTransitionToIdle();
}
}
in your test:
#Test
public void itemIsComming(){
RecyclerViewIdlingRes mIdl = new RecyclerViewIdlingRes("idlingRecyclerView");
IdlingRegistry.getInstance().register(mIdl);
Fragment fragment = activity.getSupportFragmentManager().findFragmentById(R.id.activity_main_frame_layout);
if (fragment instanceof YourFragment) {
((YourFragment)fragment).registerOnCallBack(mIdl);
}
onIdle();
IdlingRegistry.getInstance().unregister(mIdl);
// DO YOUR STUFF...
}

The good approach will be to implement your own IdlingResource that will notify Espresso when data loading is finished. You can find some example here - How to use Espresso Idling Resource for network calls

Related

Unable to change the value of a variable of my fragment from an external class

I am working with fragments but I am not able to access its variables from an external class.
Currently, I have a fragment fragmentView which has a settings button. Whenever it is pressed, it shows an UI Element to define different settings. I copy the code that I have:
Fragment
public static Boolean show = false;
private void initSettingsPanel() {
m_settingsBtn = (ImageButton) m_activity.findViewById(R.id.settingButton);
/* click settings panel button to open or close setting panel. */
m_settingsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
m_settingsLayout = (LinearLayout) m_activity.findViewById(R.id.settingsPanelLayout);
if (m_settingsLayout.getVisibility() == View.GONE) {
m_settingsLayout.setVisibility(View.VISIBLE);
if (m_settingsPanel == null) {
m_settingsPanel = new SettingsPanel(m_activity, show); //HERE I CALL THE EXTERNAL CLASS
}
} else {
m_settingsLayout.setVisibility(View.GONE);
}
}
});
}
SettingsPanel
private Activity m_activity;
private static Boolean p_show;
private Switch p_switch;
public SettingsPanel(Activity activity, Boolean show
) {
p_show = show;
m_activity = activity;
initUIElements(); // Init switch
}
private void initUIElements() {
p_switch = (Switch) m_activity.findViewById(R.id.showSwitch);
setUIListeners();
}
private void setUIListeners() {
p_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
p_show = isChecked;
}
});
}
}
What is currently happening is that when I activate the switch I change the value of th evariable of Pannel but it does not affect to the fragment. Why? Is there any other way to change the value of its variables not sending to SettingPanel each of the variables? Is it at least a correct way to do it?
You can register an interface between your activity and your fragment(s) -
public interface CallbackInterface {
//you can also return values from this interface
void performSomething(); //Add params to this method as per yourneed
}
In your activity class -
public YourActity extends AppCompactActivity implements CallbackInterface {
//your interface instance
private CallbackInterface mCallbackInterface;
#Override
void performSomething() {
// Write your code here.
}
}
}
In your fragment, you can override the
onAttach()
to register to the interface --
#Override
public void onAttach(Context context) {
super.onAttach(context);
mCallbackInterface = (CallbackInterface) context;
}
Voila! Now you can use this interface to send any data back to your activity.
In the end, I have created an abstract class in order to get and set the variables:
ShowVar
public abstract class ShowVar {
static private Boolean show = false;
public Boolean getShow() {
return show;
}
public void setShow(Boolean value) {
this.show = value;
}
}
From my SettingPanel I have instance ShowVar to set the new values of the variable each time and change the switch
SettingsPanel
public class SettingsPanel {
public ShowVar showVar = new ShowVar() {
#Override
public void set_Show(Boolean show) {
super.setShow(show);
}
};
}
and from my fragment I have access to the values by using my variable m_settingsPanel
Fragment
m_settingsPanel.showVar.getShow()

Livedata Data Change Pattern

I have a doubt.If i have a method that make asynchronous call to an api and converts the results of it to livedata object and in another place i am updating my recyclerview when data changes, then every time call to this method will update recyclerview or ,for eg:if url stays same then it won't update the recyclerview;Pls help.
Here is the code for observing data in Mainactivity onCreate method.
JsonViewModel model = new ViewModelProvider(this).get(JsonViewModel.class);
model.getData("top_rated").observe(this, data -> {
mRecyclerView.setAdapter(new MovieRecyclerViewAdapter(this,data));
});
Here is the JsonViewModel class
public class JsonViewModel extends AndroidViewModel {
private JsonLivedata data;
public JsonViewModel(#NonNull Application application) {
super(application);
data=new JsonLivedata();
}
public LiveData<List<Movie>> getData(String path) {
data.loadData(path);
return data;
}
}
Here is the JsonLivedata class
public class JsonLivedata extends LiveData<List<Movie>> {
private static final String TAG = "JsonLivedata";
public JsonLivedata() {
}
public void loadData(String path){
Log.d(TAG, "loadData: Called");
new AsyncTask<String,Void,List<Movie>>(){
#Override
protected List<Movie> doInBackground(String... path) {
List<Movie> allTopMovies= JsonResponseFetcher.makeAsyncQueryForMovies(path[0]);
return allTopMovies;
}
#Override
protected void onPostExecute(List<Movie> movies) {
setValue(movies);
}
}.execute(path);
}
}
And here is the method that call livedata loaddata method
changeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
model.getData("popular");
}
});
Or I am doing things wrongly.Can anyone suggest
First create adapter instance & set to RecyclerView
JsonViewModel model = new ViewModelProvider(this).get(JsonViewModel.class);
MovieRecyclerViewAdapter movieRecyclerViewAdapter = new MovieRecyclerViewAdapter(this, dataList)
mRecyclerView.setAdapter(movieRecyclerViewAdapter);
Then do this on data changes
model.getData("top_rated").observe(this, data -> {
dataList.clear();
dataList.addAll(data);
movieRecyclerViewAdapter.notifyDataSetChanged();
});

Load Large data from firebase into Viewpager

I have a situation when I have large set of data in firebase database and I want to show those data in ViewPager with Fragments.
The database and callbacks works fine but it clogging up the main thread when I try to add it to ViewPager and call notifyDataSetChanged().
Im using EventBus for communication between the Activity and Singleton class to handle add firebase related taks.
Here are some code snippets
Singleton Class
databaseProjectsRef.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot snapshot, String s) {
// Get the project from the snapshot and add it to the UI
Project project = snapshot.getValue(Project.class);
project._id = snapshot.getKey();
add(project, ProjectType.NORMAL);
}
...
});
private void add(Project project, ProjectType projectType) {
if (!data.contains(project)) {
project.projectType = projectType;
data.add(project);
EventBus.getDefault().post(new RefreshDataEvent());
}
}
MainActivity
#Subscribe(threadMode = ThreadMode.MAIN)
public void onRefreshEvent(RefreshDataEvent event) {
if (mAdaptor != null) {
mAdaptor.setData();
}
}
Adaptor
public class ProjectAdaptor extends FragmentStatePagerAdapter {
private final EmptyInterface emptyInterface;
private List<Project> mData;
public ProjectAdaptor(FragmentManager fm, EmptyInterface emptyInterface) {
super(fm);
mData = new ArrayList<>();
this.emptyInterface = emptyInterface;
}
#Override
public Fragment getItem(int position) {
PagerFragment item = PagerFragment.newInstance(mData.get(position));
return item;
}
#Override
public int getCount() {
return mData.size();
}
public void setData() {
mData = ProjectContext.getInstance().getData();
emptyInterface.isEmpty(mData.isEmpty());
notifyDataSetChanged();
}
public interface EmptyInterface {
void isEmpty(boolean isEmpty);
}
Any help would be appreciated.
The most relevant question i could find was this but with no answers.

Using Espresso idling resource with multiple activities

I have a firstActivity that launches the secondActivity, where in the secondActivity I have a loading Dialog (not AsyncTask), and I need to make Espresso wait until the dialog disappears before it continues with the test.
Where do I have to implement the IdlingResource? How can I make it wait for the dismissDialog() function?
Here is what I've tried to do:
class DocumentLoadingIdlingResource implements IdlingResource {
private ResourceCallback callback;
#Override
public String getName() {
return "Documnet loading idling resource";
}
#Override
public boolean isIdleNow() {
Activity activity;
try {
activity = getCurrentActivity();
} catch (Throwable e) {
return false;
}
if(activity.getClass().getName().equals(EditorActivity.class.getName())
&& activity.loadingDialogShowing() == false) {
return false;
}
return true;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.callback = callback;
}
}
Activity getCurrentActivity() throws Throwable {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
runTestOnUiThread(new Runnable() {
#Override
public void run() {
java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = com.google.common.collect.Iterables.getOnlyElement(activites);
}});
return activity[0];
}
This class is implemented in the test class.
There are a few problems here:
Your isIdleNow() calls getCurrentActivity() which calls waitForIdleSync() and runTestOnUiThread(). isIdleNow Javadoc says: "Espresso will always call this method from the main thread, therefore it should be non-blocking and return immediately." So this won't work as is, but you could call getActivitiesInStage directly from isIdleNow.
Your other issue is that you store the reference to ResourceCallback but never invoke onTransitionToIdle, also you should allow for the possibility of more than one ResourceCallback being registered and call onTransitionToIdle on all of the callbacks.
You can do the following:
Copy/Paste IdlingResource into your app as com.mycompany.IdlingResource.
Then have your Activity implement that interface and make sure to call onTransitionToIdle when the dialog goes away and make sure isIdleNow returns false iff the dialog is showing.
In your test code, write a "IdlingResourceAdapter" that wraps com.mycompany.IdlingResource and turns it into an Espresso IdlingResource and register that with Espresso.
This will be simpler once this issue is implemented: https://code.google.com/p/android-test-kit/issues/detail?id=71
I stumbled upon this question in my search for a similar answer. Using concepts from Stefano Dacchille's article on IdlingResources, I built the following idling resource that waits for a specific Activity to be active before firing. In my case, I know the dialog is showing when a fragment with a specific tag exists. This isn't the same as the OP's test, but the concepts should translate well.
public class BusyWhenFragmentExistsInActivityIdlingResource implements IdlingResource {
private FragmentActivity activity = null;
private final String fragmentTag;
private ResourceCallback resourceCallback;
private boolean wasIdleLastTime = true; // Start off as idle
private final String name;
// Need this strong reference because ActivityLifecycleMonitorRegistry won't hold one
private final ActivityLifecycleCallback activityLifecycleCallback;
public BusyWhenFragmentExistsInActivityIdlingResource(
final Class<? extends FragmentActivity> clazz,
final String fragmentTag
){
name = BusyWhenFragmentExistsInActivityIdlingResource.class.getSimpleName()+" "+clazz.getSimpleName();
this.fragmentTag = fragmentTag;
activityLifecycleCallback = new ActivityLifecycleCallback() {
#Override
public void onActivityLifecycleChanged(Activity activity, Stage stage) {
if (!FragmentActivity.class.isAssignableFrom(activity.getClass())) {
return;
}
FragmentActivity fragmentActivity = (FragmentActivity) activity;
if (!clazz.isAssignableFrom(fragmentActivity.getClass())) {
return;
}
switch (stage){
case RESUMED:
BusyWhenFragmentExistsInActivityIdlingResource.this.activity = fragmentActivity;
break;
case STOPPED:
BusyWhenFragmentExistsInActivityIdlingResource.this.activity = null;
break;
}
}
};
ActivityLifecycleMonitorRegistry.getInstance()
.addLifecycleCallback(activityLifecycleCallback);
}
#Override
public String getName() {
return name;
}
#Override
public boolean isIdleNow() {
if (activity==null) {
return wasIdleLastTime = true;
}
boolean isIdleThisTime = activity
.getSupportFragmentManager()
.findFragmentByTag(fragmentTag)==null;
if (!wasIdleLastTime && isIdleThisTime && resourceCallback!=null){
resourceCallback.onTransitionToIdle();
}
return wasIdleLastTime = isIdleThisTime;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
}
To use it, add something similar to this to your test:
#Before
public void setUp() throws Exception {
registerIdlingResources(new BusyWhenFragmentExistsInActivityIdlingResource(
SomeOtherActivity.class,
BaseActivity.LOADING_DIALOG
));
}

Perform requests with Retrofit inside custom Runnable

I am migrating from Volley to a custom implementation using Retrofit, but I'm trying to add to my implementation some of the Volley features that I liked, for example
RequestQueue.cancel(String tag)
If the Request has the requested tag, then it's canceled by setting a boolean value, mCanceled, to true. The run method checks this value and returns if it's true.
To be able to reproduce this with Retrofit I should be able to use my custom class implementing Runnable instead of the default one, where I have a mTag and a mCanceled field.
Moreover, Volley was also able to set such flag inside the active Threads and immediately stop them. My cancelAll method, that I've already implemented, just drains the queue to another queue, but isn't able to access the active threads.
Is it possible to achieve the same results with Retrofit and ThreadPoolExecutor?
I think I've found a nicer solution: instead of blocking the Runnable of the requests, I am blocking the Callback execution.
I have extended the Callback interface:
public interface CustomCallbackInterface<T> extends Callback<T> {
public String getTag();
public String setTag(String tag);
public void cancel();
public boolean isCanceled();
}
so that each Callback has a tag and a cancel flag. Then the success method starts with:
public class CustomCallback<ConvertedData> implements CustomCallbackInterface<ConvertedData>{
//failure...
#Override
public void success(ConvertedData cd, Response response) {
if(isCanceled()) return;
// ....
}
}
Every time I make a new request, I store the created CustomCallback inside a List cancel just iterates the list and calls cancel() on the items with the same tag.
I've implemented an easy to use class based on Vektor88 answer
public abstract class CancelableCallback<T> implements Callback<T> {
private static List<CancelableCallback> mList = new ArrayList<>();
private boolean isCanceled = false;
private Object mTag = null;
public static void cancelAll() {
Iterator<CancelableCallback> iterator = mList.iterator();
while (iterator.hasNext()){
iterator.next().isCanceled = true;
iterator.remove();
}
}
public static void cancel(Object tag) {
if (tag != null) {
Iterator<CancelableCallback> iterator = mList.iterator();
CancelableCallback item;
while (iterator.hasNext()) {
item = iterator.next();
if (tag.equals(item.mTag)) {
item.isCanceled = true;
iterator.remove();
}
}
}
}
public CancelableCallback() {
mList.add(this);
}
public CancelableCallback(Object tag) {
mTag = tag;
mList.add(this);
}
public void cancel() {
isCanceled = true;
mList.remove(this);
}
#Override
public final void success(T t, Response response) {
if (!isCanceled)
onSuccess(t, response);
mList.remove(this);
}
#Override
public final void failure(RetrofitError error) {
if (!isCanceled)
onFailure(error);
mList.remove(this);
}
public abstract void onSuccess(T t, Response response);
public abstract void onFailure(RetrofitError error);
}
Usage example
rest.request(..., new CancelableCallback<MyResponse>(TAG) {
#Override
public void onSuccess(MyResponse myResponse, Response response) {
...
}
#Override
public void onFailure(RetrofitError error) {
...
}
});
// if u need to cancel all
CancelableCallback.cancelAll();
// or cancel by tag
CancelableCallback.cancel(TAG);

Categories

Resources