Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JOp;
Expand Down Expand Up @@ -67,7 +68,9 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t

if (holder.handler == null) {
JClass handlerClass = holder.classes().HANDLER;
holder.handler = holder.generatedClass.field(JMod.PRIVATE, handlerClass, "handler_", JExpr._new(handlerClass));
JClass lClass = holder.classes().LOOPER;
JInvocation arg = JExpr._new(handlerClass).arg(lClass.staticInvoke(METHOD_MAIN_LOOPER));
holder.handler = holder.generatedClass.field(JMod.PRIVATE, handlerClass, "handler_", arg);
}

if (delay == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -35,6 +36,9 @@
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import android.os.Handler;
import android.os.Looper;

@RunWith(AndroidAnnotationsTestRunner.class)
public class ThreadActivityTest {

Expand Down Expand Up @@ -285,4 +289,35 @@ public void execute(Runnable command) {
}
}

@Test
public void assertHandlerWithMainThread() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
/*
* For this test we need to recreate the activity in a separate thread,
* in order to check the handler is well associated to the main thread.
*/
final ThreadActivity_[] threadActivityHolder = new ThreadActivity_[1];

new Thread(new Runnable() {
@Override
public void run() {
synchronized (threadActivityHolder) {
threadActivityHolder[0] = new ThreadActivity_();
threadActivityHolder[0].onCreate(null);
threadActivityHolder.notify();
}
}
}).start();
synchronized (threadActivityHolder) {
do {
threadActivityHolder.wait();
} while(threadActivityHolder[0] == null);
}

Field handlerField = ThreadActivity_.class.getDeclaredField("handler_");
handlerField.setAccessible(true);

Handler handler = (Handler) handlerField.get(threadActivityHolder[0]);
Assert.assertTrue("Handler field not associated to the main thread", handler.getLooper() == Looper.getMainLooper());
}

}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.