I have a BaseActivity which is an abstract activity and isn't registered in AndroidManifest. BaseActivity will call getPresenter in activity's lifecycle.
public abstract class BaseActivity extends AppCompatActivity{
public abstract Presenter getPresenter;
public abstract int getLayout();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
getPresenter().attachView(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
getPresenter().detachView();
}
}
I use ActivityTestRule to launch the BaseActivity, but the following error is shown.
java.lang.RuntimeException: Could not launch activity
How to test the getPresenter().attachView(this) and getPresenter().detachView() are called in correct activity's lifecycle?
I don't have quite big experience with Android Testing, especially unit testing, but I've already found this post, which may be useful for you:
Is it possible to test an Abstract activity with Robolectric
Also on Github page of Robolectric I'd found this: https://github.com/robolectric/robolectric/issues/1441
So all I can say according to your question, that yes you can test your abstract class, at least with Robolectric.
Read also: https://gualtierotesta.wordpress.com/2015/01/28/tutorial-java-abstract-classes-testing/
EDIT: Nowadays, Robolectric doesn't support directly API 23, but you can "downgrade" it in configuration of test class, like below:
#RunWith(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, sdk = 21)
public class MainActivityTest {
MainActivity_ activity = Robolectric.setupActivity(MainActivity.class);
}
The error is shown because the BaseActivity isn't registered in Android Manifest. It seems that there are some solutions to add an activity in test package.
However, I finally choose another solution, delegate the activity's lifecycle to others.
This idea is mentioned in Mosby playbook.
http://hannesdorfmann.com/android/mosby-playbook/
New BaseActivity:
public abstract class BaseActivity extends AppCompatActivity implements BaseMvpView, DelegateCallback{
private ActivityMvpDelegate activityDelegate;
protected ActivityMvpDelegate getActivityDelegate() {
if (activityDelegate == null) {
activityDelegate = createActivityDelegate();
}
return activityDelegate;
}
protected ActivityMvpDelegate createActivityDelegate() {
return new ActivityMvpDelegateImpl(this, this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityDelegate().onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
super.onDestroy();
getActivityDelegate().onDestroy();
}
}
Finally, I can test the delegate class without activity's lifecycle.
Related
Hi everyone i want to block the Screenshot in my app. I got the first problem solve from here.
But now the thing is I have more than 10 activity and 10 + fragment.
Is there any way to do this just by writing in the one class and giving it reference to the entire app.
Just like we make one Application class and in the AndroidMainfest.xml give that application class refrence.
You can implement a BaseActivity, and make all your activities extend this BaseActivity. In onCreate() of this activity set the flag. You need to ensure all your activities call super.onCreate() as follows:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your flag here
...
}
}
Activity1.java
public class Activity1 extends BaseActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
}
I am using android annotations and have some code that I need to execute in the onResume() function in my activity.
Is it safe to just override the onResume function from the android annotation activity (ie with #EActivity)?
Yeah, you should use these lifecycle methods just like with plain Android activities. There is one thing though: injected Views are not yet available in your onCreate method, this is why #AfterViews exist:
#EActivity(R.layout.views_injected)
public class ViewsInjectedActivity extends Activity {
#ViewById
Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// myButton is not yet available here
}
#AfterViews
void setupViews() {
// myButton is first available here
myButton.setText("Hello");
}
#Override
protected void onResume() {
super.onResume();
// just as usual
}
}
Yeah. Just call super.onResume() and then add your code.
I'd do it just like their on create example here: https://github.com/excilys/androidannotations/wiki/Enhance-activities
You can bind your custom class with lifecycle component of android. It holds life cycle information of android component so that your custom class observe lifecycle changes.
public class MyObserver implements LifecycleObserver {
#OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void connectListener() {
...
}
#OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void disconnectListener() {
...
}
}
myLifecycleOwner.getLifecycle().addObserver(new MyObserver());
I try to create a Robolectric test (3.0-rc02) for the following Activity:
public class NotificationActivity extends ActionBarActivity {
private NotificationFragment fragment;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
}
}
The test is looking like this:
#Config(manifest = IConfig.MANIFEST_PATH, emulateSdk = IConfig.SDK_VERSION, reportSdk = IConfig.SDK_VERSION)
#RunWith(RobolectricTestRunner.class)
public class AbstractFragmentTest {
#Test
public void test() {
Robolectric.buildActivity(NotificationActivity.class).setup().visible();
}
where SDK_VERSION = 18.
When running the test, I get this error:
java.lang.NullPointerException
at org.robolectric.res.builder.DefaultPackageManager.getActivityInfo(DefaultPackageManager.java:164)
at org.robolectric.util.ActivityController.getActivityInfo(ActivityController.java:65)
at org.robolectric.util.ActivityController.attach(ActivityController.java:51)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:309)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:47)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at com.viae.common.view.AbstractFragmentTest.test(AbstractFragmentTest.java:31)
Anyone knows what I'm doing wrong over here?
Issue solved
Using FragmentActivity in stead of ActionBarActivity did the trick to me.
My main logic is in a fragment (DialogFragment) inside the activity. Both activities are supporting the DialogFragment, so for my functional tests it doesn't matter which of the 2 parent activities I use.
ActionBarActivity is deprecated. Use AppCompatActivity instead
I want to know if Android support the possibility to make a container with some static components and just include my activities in it.
I have a sliding menu with some onClickListener events and I don't want to set these events for each activity.
If I understood you correctly, you have some functionality that is common to several Activities, and you don't want to repeat the same code in all of them. Instead, you want to do that in one place.
One way to achieve this is to create a superclass activity, place your common code in that activity, and then extend it with your other activities. For example,
public class BaseActivity extends Activity implements OnClickListener {
private Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.my_button);
button.setOnClickListener(this);
}
...
#Override
public void onClick(View view) {
int id = view.getId();
switch(id) {
case R.id.my_button:
// perform action
break;
}
}
}
Then you extend it as
public class Activity1 extends BaseActivity {
/...
}
public class Activity2 extends BaseActivity {
/...
}
public class Activity3 extends BaseActivity {
/...
}
I am not exactly sure I understand your question, can you perhaps elaborate some more? maybe even post some sample code that you are using currently.
From what I can tell you should be able to achieve what you want by making your own CustomActivity
public class CustomActivity extends Activity {
//put your slidingmenu stuff here
}
Then inside all of the other Activities where you want to use that shared piece do it like this:
public class AnotherActivity extends CustomActivity {
//...
}
with extends CustomActivity instead of the usual extends Activity
This is how i solved the problem:
First thing i did is creating my main class wich will host common code.
for example :
public abstract class main extends activity(){
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
// Your common code here
}
protected abstract int getLayoutResourceId();
}
Then all what you need is to extend this class in your activity:
public class HelloActivity extends main{
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_hello);
super.onCreate(savedInstanceState);
//make sure to put setcontentview before super.oncreate
}
#Override
protected int getLayoutResourceId() {
return R.layout.activity_hello;
}
}
All the activities needs to be registered in the Manifest. For the common things, e.g. Slide menu, you can use Fragment for the slide menu.
I am writing junit test cases for an android project. I wrote junit for activity classes but I dont know how to write test cases for other classes that doesn't inherit from activity. Also, how can I link these classes (activity and non activity classes)?
examples:
public class A extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.someLayout);
B objectB = new B();
pbjectB.getString();
}
}
public class B{
public b(){
}
public String getString(){
return anyString;
}
}
In this example I am able to write junit test cases for class A but I am confused for class B.
For non-Activity classes, you can use standard junit 3 test classes. So make your test for B extend from junit.framework.TestCase
import junit.framework.TestCase;
public class BTest extends TestCase {
public void testGetStringIsNotNull() {
B subject = new B();
assertNotNull(subject.getString());
}
}