Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a method was called on UI-Thread with robolectric [closed]

Anyone here knows how to write a test ( or ideally has an example for this ) to check if a method was called on the UI-Thread?

like image 951
ligi Avatar asked Mar 22 '23 13:03

ligi


1 Answers

Answer refered from the following links :

How to check if current thread is not main thread

How to know if this thread is a UI Thread

1) Looper.myLooper() == Looper.getMainLooper()

2) Android app has only one UI thread, so you could somewhere in the Activity callback like onCreate() check and store its ID and later just compare that thread's ID to the stored one.

 mMainThreadId = Thread.currentThread().getId();

3) if you want to do something on the UI thread and have any reference to Activity by using

mActivity.runOnUiThread( new Runnable() {
    @Override 
    public void run() {
    ...
    }
});

Hope it helps

like image 186
Rachita Nanda Avatar answered Apr 06 '23 20:04

Rachita Nanda