Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 7f374b9

Browse files
committed
Implement @rootfragment annotation with its Tests
1 parent 63e6d58 commit 7f374b9

30 files changed

Lines changed: 956 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* <p>
25+
* Use it on Fragment fields in an {@link EBean} annotated classes to inject the
26+
* Fragment which originally injected the bean instance.
27+
* </p>
28+
* <p>
29+
* This field may not be injected at runtime if the fragment used to create the
30+
* bean is not of the appropriate type, or if the bean is injected in an
31+
* activity.
32+
* </p>
33+
* <blockquote>
34+
*
35+
* Example :
36+
*
37+
* <pre>
38+
* &#064;EBean
39+
* public class MyClass {
40+
*
41+
* &#064;RootFragment
42+
* Fragment fragment;
43+
*
44+
* // Only injected if the root fragment implements a given interface
45+
* &#064;RootFragment
46+
* MyInterface fragment;
47+
* }
48+
* </pre>
49+
*
50+
* </blockquote>
51+
*/
52+
@Retention(RetentionPolicy.CLASS)
53+
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
54+
public @interface RootFragment {
55+
}

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<activity android:name=".afterextras.AfterExtrasActivity_"/>
9292
<activity android:name=".FullscreenActivity_"/>
9393
<activity android:name=".PageChangeListenerActivity_"/>
94+
<activity android:name=".ebean.ActivityWithBeansWithRootFragment_"/>
9495

9596
<receiver android:name=".ereceiver.MyReceiver_"/>
9697

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.Bean;
19+
import org.androidannotations.annotations.EActivity;
20+
21+
import android.app.Activity;
22+
23+
@EActivity
24+
public class ActivityWithBeansWithRootFragment extends Activity {
25+
26+
@Bean
27+
SomeBeanWithRootFragment someBean;
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.Bean;
19+
import org.androidannotations.annotations.EFragment;
20+
21+
import android.app.Fragment;
22+
23+
@EFragment
24+
public class CustomFragment extends Fragment {
25+
26+
@Bean
27+
SomeBeanWithRootFragment someBean;
28+
29+
@Bean
30+
SomeBeanWithRootFragmentWithCustomFragment someBeanWithCustomFragment;
31+
32+
@Bean
33+
SomeBeanWithRootFragmentWithDifferentFragment someBeanWithDifferentFragment;
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.EFragment;
19+
20+
import android.app.Fragment;
21+
22+
@EFragment
23+
public class DifferentFragment extends Fragment {
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.EBean;
19+
import org.androidannotations.annotations.RootFragment;
20+
21+
import android.app.Fragment;
22+
23+
@EBean
24+
public class SomeBeanWithRootFragment {
25+
26+
@RootFragment
27+
Fragment fragment;
28+
29+
Fragment fragmentByMethod;
30+
31+
Fragment fragmentByParam1;
32+
33+
Fragment fragmentByParam2;
34+
35+
@RootFragment
36+
void setFragment(Fragment fragment) {
37+
this.fragmentByMethod = fragment;
38+
}
39+
40+
void setFragment2(@RootFragment Fragment fragment1, @RootFragment Fragment fragment2) {
41+
this.fragmentByParam1 = fragment1;
42+
this.fragmentByParam2 = fragment2;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.EBean;
19+
import org.androidannotations.annotations.RootFragment;
20+
21+
@EBean
22+
public class SomeBeanWithRootFragmentWithCustomFragment {
23+
24+
@RootFragment
25+
CustomFragment fragment;
26+
27+
CustomFragment fragmentByMethod;
28+
29+
CustomFragment fragmentByParam1;
30+
31+
CustomFragment fragmentByParam2;
32+
33+
@RootFragment
34+
void setFragment(CustomFragment fragment) {
35+
this.fragmentByMethod = fragment;
36+
}
37+
38+
void setFragment2(@RootFragment CustomFragment fragment1, @RootFragment CustomFragment fragment2) {
39+
this.fragmentByParam1 = fragment1;
40+
this.fragmentByParam2 = fragment2;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import org.androidannotations.annotations.EBean;
19+
import org.androidannotations.annotations.RootFragment;
20+
21+
@EBean
22+
public class SomeBeanWithRootFragmentWithDifferentFragment {
23+
24+
@RootFragment
25+
DifferentFragment fragment;
26+
27+
DifferentFragment fragmentByMethod;
28+
29+
DifferentFragment fragmentByParam1;
30+
31+
DifferentFragment fragmentByParam2;
32+
33+
@RootFragment
34+
void setFragment(DifferentFragment fragment) {
35+
this.fragmentByMethod = fragment;
36+
}
37+
38+
void setFragment2(@RootFragment DifferentFragment fragment1, @RootFragment DifferentFragment fragment2) {
39+
this.fragmentByParam1 = fragment1;
40+
this.fragmentByParam2 = fragment2;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (C) 2016-2019 the AndroidAnnotations project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test.ebean;
17+
18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.robolectric.Robolectric;
23+
import org.robolectric.RobolectricTestRunner;
24+
25+
@RunWith(RobolectricTestRunner.class)
26+
public class RootFragmentTest {
27+
28+
@Test
29+
public void injectFragmentWhichInjectedBean() {
30+
CustomFragment fragment = Robolectric.buildFragment(CustomFragment_.class).create().get();
31+
32+
fragment.onCreate(null);
33+
34+
assertThat(fragment.someBean.fragment).isEqualTo(fragment);
35+
assertThat(fragment.someBean.fragmentByMethod).isEqualTo(fragment);
36+
assertThat(fragment.someBean.fragmentByParam1).isEqualTo(fragment);
37+
assertThat(fragment.someBean.fragmentByParam2).isEqualTo(fragment);
38+
}
39+
40+
@Test
41+
public void injectSomeFragmentWhichInjectedBean() {
42+
CustomFragment fragment = Robolectric.buildFragment(CustomFragment_.class).create().get();
43+
44+
fragment.onCreate(null);
45+
46+
assertThat(fragment.someBeanWithCustomFragment.fragment).isEqualTo(fragment);
47+
assertThat(fragment.someBeanWithCustomFragment.fragmentByMethod).isEqualTo(fragment);
48+
assertThat(fragment.someBeanWithCustomFragment.fragmentByParam1).isEqualTo(fragment);
49+
assertThat(fragment.someBeanWithCustomFragment.fragmentByParam2).isEqualTo(fragment);
50+
}
51+
52+
@Test
53+
public void nothingInjectedIfDifferentFragmentThanInjectingFragmentIsUsedOnBean() {
54+
CustomFragment fragment = Robolectric.buildFragment(CustomFragment_.class).create().get();
55+
56+
fragment.onCreate(null);
57+
58+
assertThat(fragment.someBeanWithDifferentFragment.fragment).isNull();
59+
assertThat(fragment.someBeanWithDifferentFragment.fragmentByMethod).isNull();
60+
assertThat(fragment.someBeanWithDifferentFragment.fragmentByParam1).isNull();
61+
assertThat(fragment.someBeanWithDifferentFragment.fragmentByParam2).isNull();
62+
}
63+
64+
@Test
65+
public void nothingInjectedIfActivityInjectedBean() {
66+
ActivityWithBeansWithRootFragment_ activity = Robolectric.setupActivity(ActivityWithBeansWithRootFragment_.class);
67+
68+
assertThat(activity.someBean.fragment).isNull();
69+
assertThat(activity.someBean.fragmentByMethod).isNull();
70+
assertThat(activity.someBean.fragmentByParam1).isNull();
71+
assertThat(activity.someBean.fragmentByParam2).isNull();
72+
}
73+
74+
}

‎AndroidAnnotations/androidannotations-core/androidannotations-testutils/src/main/java/org/androidannotations/testutils/AAProcessorTestHelper.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public String toPath(Class<?> classOfPackagingContainingFile, String filename) {
4343
return classOfPackagingContainingFile.getResource(filename).getPath();
4444
}
4545

46+
public String[] defPath(String... filesName) {
47+
String[] paths = new String[filesName.length];
48+
for (int i = 0; i < filesName.length; i++) {
49+
paths[i] = toPath(this.getClass(), filesName[i]);
50+
}
51+
return paths;
52+
}
53+
4654
/**
4755
* This module cannot depend on androidannotations module, because that would
4856
* introduce a cycle in the dependency graph. That is why we cannot directly

0 commit comments

Comments
 (0)