blob: 3071162ed2e5ece90cde4d7d0f75de0a81440a80 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
package com.xtremelabs.robolectric.shadows;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;
import com.xtremelabs.robolectric.tester.android.util.TestFragmentManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
@Implements(FragmentActivity.class)
public class ShadowFragmentActivity extends ShadowActivity {
@RealObject
FragmentActivity realObject;
private TestFragmentManager fragmentManager;
public static final String FRAGMENTS_TAG = "android:fragments";
public void __constructor__() {
fragmentManager = new TestFragmentManager(realObject);
}
@Implementation
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (bundle != null && bundle.containsKey(FRAGMENTS_TAG)) {
Object[] fragments = (Object[]) bundle.getSerializable(FRAGMENTS_TAG);
for (Object o : fragments) {
SerializedFragmentState fragmentState = (SerializedFragmentState) o;
try {
Fragment fragment = fragmentState.fragmentClass.newInstance();
shadowOf(fragment).setSavedInstanceState(bundle);
fragmentManager.addFragment(fragmentState.containerId, fragmentState.tag, fragment, true);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
@Implementation
public void onStart() {
for (Fragment fragment : fragmentManager.getFragments().values()) {
fragmentManager.startFragment(fragment);
}
}
@Implementation
public void onPause() {
for(Fragment fragment : fragmentManager.getFragments().values()) {
fragment.onPause();
}
}
@Implementation
public FragmentManager getSupportFragmentManager() {
return fragmentManager;
}
@Implementation
public void onSaveInstanceState(Bundle outState) {
// We cannot figure out how to pass the RobolectricWiring test without doing this incredibly
// terrible looking hack. I am very sorry.
List<SerializedFragmentState> fragmentStates = new ArrayList<SerializedFragmentState>();
for (Map.Entry<Integer, Fragment> entry : fragmentManager.getFragments().entrySet()) {
Fragment fragment = entry.getValue();
fragment.onSaveInstanceState(outState);
fragmentStates.add(new SerializedFragmentState(entry.getKey(), fragment));
}
outState.putSerializable(FRAGMENTS_TAG, fragmentStates.toArray());
}
@Implementation
@Override
public View getCurrentFocus() {
View focusedView = super.getCurrentFocus();
if (focusedView != null) {
return focusedView;
}
for (Fragment fragment : fragmentManager.getFragments().values()) {
View view = shadowOf(fragment).view;
if (view != null) {
focusedView = view.findFocus();
if (focusedView != null) {
return focusedView;
}
}
}
return null;
}
@Override
public void clearFocus() {
super.clearFocus();
for (Fragment fragment : fragmentManager.getFragments().values()) {
View view = shadowOf(fragment).view;
if (view != null) {
view.clearFocus();
}
}
}
}
|