-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
app/src/androidTest/java/channel/helper/ChannelFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package channel.helper; | ||
|
||
import android.os.HandlerThread; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import channel.helper.pipe.HandlerPipe; | ||
import channel.helper.test.Bar; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class ChannelFactoryTest { | ||
@Test(timeout = 3000) | ||
public void factoryTest() throws InterruptedException { | ||
final String value = "Hello"; | ||
|
||
final CountDownLatch countDownLatch = new CountDownLatch(1); | ||
|
||
Bar receiver = new Bar() { | ||
@Override | ||
public void noParam() { | ||
|
||
} | ||
|
||
@Override | ||
public void byteParam(byte aByte) { | ||
|
||
} | ||
|
||
@Override | ||
public void shortParam(short aShort) { | ||
|
||
} | ||
|
||
@Override | ||
public void intParam(int aInt) { | ||
|
||
} | ||
|
||
@Override | ||
public void longParam(long aLong) { | ||
|
||
} | ||
|
||
@Override | ||
public void floatParam(float aFloat) { | ||
|
||
} | ||
|
||
@Override | ||
public void doubleParam(double aDouble) { | ||
|
||
} | ||
|
||
@Override | ||
public void stringParam(String aString) { | ||
assertEquals(value, aString); | ||
countDownLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void enumParam(TimeUnit aEnum1, TimeUnit aEnum2) { | ||
|
||
} | ||
|
||
@Override | ||
public void manyParam(byte aByte, short aShort, int aInt, long aLong, float aFloat, double aDouble, String aString, TimeUnit aEnum1, TimeUnit aEnum2) { | ||
|
||
} | ||
|
||
@Override | ||
public void extendsTest(String value) { | ||
|
||
} | ||
}; | ||
|
||
HandlerThread handlerThread = new HandlerThread("ChannelFactoryTest"); | ||
handlerThread.start(); | ||
|
||
Dispatcher barDispatcher = ChannelFactory.newDispatcher(Bar.class, receiver); | ||
Bar emitter = ChannelFactory.newEmitter(Bar.class, new HandlerPipe(handlerThread.getLooper(), barDispatcher)); | ||
|
||
emitter.stringParam(value); | ||
|
||
countDownLatch.await(); | ||
handlerThread.quit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package channel.helper; | ||
|
||
import java.lang.reflect.Constructor; | ||
|
||
public class ChannelFactory { | ||
private ChannelFactory() { | ||
throw new AssertionError(); | ||
} | ||
|
||
@SuppressWarnings("unchecked cast") | ||
public static <T> T newEmitter(Class<T> clazz, Emitter pipe) { | ||
String emitterName = clazz.getPackage().getName() + "." | ||
+ clazz.getSimpleName() | ||
+ "__ChannelWrapper$Emitter"; | ||
|
||
try { | ||
Class<? extends T> emitter = (Class<? extends T>) Class.forName(emitterName); | ||
Constructor<? extends T> constructor = emitter.getConstructor(Emitter.class); | ||
return (T) constructor.newInstance(pipe); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("emitter create failed", e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked cast") | ||
public static <T> Dispatcher newDispatcher(Class<T> clazz, T receiver) { | ||
String dispatcherName = clazz.getPackage().getName() + "." | ||
+ clazz.getSimpleName() | ||
+ "__ChannelWrapper$Dispatcher"; | ||
|
||
try { | ||
Class<? extends Dispatcher> dispatcher = (Class<? extends Dispatcher>) Class.forName(dispatcherName); | ||
Constructor<? extends Dispatcher> constructor = dispatcher.getConstructor(clazz); | ||
return constructor.newInstance(receiver); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("dispatcher create failed", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters