Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support partial AppComponentFactory in Robolectric
Support to use AppComponentFactory in Robolectric from SDK 28. This CL only supports BroadcastReceiver with custom AppComponentFactory. In Robolectric, developers use Robolectric#setupService or Robolectric#buildService to initialize Service instance, and these methods are static, so it's difficult to leverage custom AppComponentFactory to initialize Service instance with custom constructor. And I don't find proper usage scenarios to use custom AppComponentFactory to initialize ContentProvider, Application, Activity and ClassLoader. So I leave them as not implemented state. Signed-off-by: utzcoz <utzcoz@outlook.com>
- Loading branch information
Showing
8 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
robolectric/src/test/java/org/robolectric/CustomAppComponentFactory.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,22 @@ | ||
package org.robolectric; | ||
|
||
import android.app.AppComponentFactory; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Intent; | ||
import org.robolectric.CustomConstructorReceiverWrapper.CustomConstructorWithEmptyActionReceiver; | ||
import org.robolectric.CustomConstructorReceiverWrapper.CustomConstructorWithOneActionReceiver; | ||
|
||
public final class CustomAppComponentFactory extends AppComponentFactory { | ||
@Override | ||
public BroadcastReceiver instantiateReceiver(ClassLoader cl, String className, Intent intent) | ||
throws InstantiationException, IllegalAccessException, ClassNotFoundException { | ||
if (className != null) { | ||
if (className.contains(CustomConstructorWithOneActionReceiver.class.getName())) { | ||
return new CustomConstructorWithOneActionReceiver(100); | ||
} else if (className.contains(CustomConstructorWithEmptyActionReceiver.class.getName())) { | ||
return new CustomConstructorWithEmptyActionReceiver(100); | ||
} | ||
} | ||
return super.instantiateReceiver(cl, className, intent); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
robolectric/src/test/java/org/robolectric/CustomConstructorReceiverWrapper.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,32 @@ | ||
package org.robolectric; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
|
||
public class CustomConstructorReceiverWrapper { | ||
private static class CustomConstructorReceiver extends BroadcastReceiver { | ||
private final int intValue; | ||
|
||
public CustomConstructorReceiver(int intValue) { | ||
// We don't use intValue actually, and we only want to use this class to test the | ||
// initialization of BroadcastReceiver with a custom constructor. | ||
this.intValue = intValue; | ||
} | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) {} | ||
} | ||
|
||
public static class CustomConstructorWithOneActionReceiver extends CustomConstructorReceiver { | ||
public CustomConstructorWithOneActionReceiver(int intValue) { | ||
super(intValue); | ||
} | ||
} | ||
|
||
public static class CustomConstructorWithEmptyActionReceiver extends CustomConstructorReceiver { | ||
public CustomConstructorWithEmptyActionReceiver(int intValue) { | ||
super(intValue); | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
robolectric/src/test/resources/TestAndroidManifestWithAppComponentFactory.xml
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,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.robolectric"> | ||
<uses-sdk android:targetSdkVersion="18"/> | ||
|
||
<application | ||
android:appComponentFactory="org.robolectric.CustomAppComponentFactory"> | ||
<receiver | ||
android:name=".CustomConstructorReceiverWrapper$CustomConstructorWithOneActionReceiver"> | ||
<intent-filter> | ||
<action android:name="org.robolectric.ACTION_CUSTOM_CONSTRUCTOR"/> | ||
</intent-filter> | ||
</receiver> | ||
<receiver | ||
android:name=".CustomConstructorReceiverWrapper$CustomConstructorWithEmptyActionReceiver" /> | ||
</application> | ||
</manifest> |
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