GuidedStepTestFragment.java revision b8706fd99b0a3724ed8e0c4b97bf37f3cdf389da
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.support.v17.leanback.app;
16
17import android.app.Activity;
18import android.app.FragmentManager;
19import android.os.Bundle;
20import android.view.ViewGroup;
21import android.view.View;
22import android.view.LayoutInflater;
23
24
25import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
26import android.support.v17.leanback.widget.GuidedAction;
27
28import java.util.List;
29import java.util.HashMap;
30
31/**
32 * @hide from javadoc
33 */
34public class GuidedStepTestFragment extends GuidedStepFragment {
35
36    private static final String KEY_TEST_NAME = "key_test_name";
37
38    private static final HashMap<String, Provider> sTestMap = new HashMap<String, Provider>();
39
40    public static class Provider {
41
42        GuidedStepTestFragment mFragment;
43
44        public void onCreate(Bundle savedInstanceState) {
45        }
46
47        public void onSaveInstanceState(Bundle outState) {
48        }
49
50        public Guidance onCreateGuidance(Bundle savedInstanceState) {
51            return new Guidance("", "", "", null);
52        }
53
54        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
55        }
56
57        public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
58        }
59
60        public void onGuidedActionClicked(GuidedAction action) {
61        }
62
63        public boolean onSubGuidedActionClicked(GuidedAction action) {
64            return true;
65        }
66
67        public void onCreateView(LayoutInflater inflater, ViewGroup container,
68                Bundle savedInstanceState, View result) {
69        }
70
71        public void onDestroyView() {
72        }
73
74        public void onDestroy() {
75        }
76
77        public void onStart() {
78        }
79
80        public void onStop() {
81        }
82
83        public void onResume() {
84        }
85
86        public void onPause() {
87        }
88
89        public void onViewStateRestored(Bundle bundle) {
90        }
91
92        public void onDetach() {
93        }
94
95        public GuidedStepTestFragment getFragment() {
96            return mFragment;
97        }
98
99        public Activity getActivity() {
100            return mFragment.getActivity();
101        }
102
103        public FragmentManager getFragmentManager() {
104            return mFragment.getFragmentManager();
105        }
106    }
107
108    public static void setupTest(String testName, Provider provider) {
109        sTestMap.put(testName, provider);
110    }
111
112    public static void clearTests() {
113        sTestMap.clear();
114    }
115
116    CharSequence mTestName;
117    Provider mProvider;
118
119    public GuidedStepTestFragment() {
120    }
121
122    public GuidedStepTestFragment(String testName) {
123        setTestName(testName);
124    }
125
126    public void setTestName(CharSequence testName) {
127        mTestName = testName;
128    }
129
130    public CharSequence getTestName() {
131        return mTestName;
132    }
133
134    @Override
135    public void onCreate(Bundle savedInstanceState) {
136        if (savedInstanceState != null) {
137            mTestName = savedInstanceState.getCharSequence(KEY_TEST_NAME, null);
138        }
139        mProvider = sTestMap.get(mTestName);
140        if (mProvider == null) {
141            throw new IllegalArgumentException("you must setupTest()");
142        }
143        mProvider.mFragment = this;
144        super.onCreate(savedInstanceState);
145        mProvider.onCreate(savedInstanceState);
146    }
147
148    @Override
149    public void onSaveInstanceState(Bundle outState) {
150        super.onSaveInstanceState(outState);
151        outState.putCharSequence(KEY_TEST_NAME, mTestName);
152        mProvider.onSaveInstanceState(outState);
153    }
154
155    @Override
156    public Guidance onCreateGuidance(Bundle savedInstanceState) {
157        Guidance g = mProvider.onCreateGuidance(savedInstanceState);
158        if (g == null) {
159            g = new Guidance("", "", "", null);
160        }
161        return g;
162    }
163
164    @Override
165    public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
166        mProvider.onCreateActions(actions, savedInstanceState);
167    }
168
169    @Override
170    public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
171        mProvider.onCreateButtonActions(actions, savedInstanceState);
172    }
173
174    @Override
175    public void onGuidedActionClicked(GuidedAction action) {
176        mProvider.onGuidedActionClicked(action);
177    }
178
179    @Override
180    public boolean onSubGuidedActionClicked(GuidedAction action) {
181        return mProvider.onSubGuidedActionClicked(action);
182    }
183
184    @Override
185    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
186        View view = super.onCreateView(inflater, container, state);
187        mProvider.onCreateView(inflater, container, state, view);
188        return view;
189    }
190
191    @Override
192    public void onDestroyView() {
193        mProvider.onDestroyView();
194        super.onDestroyView();
195    }
196
197    @Override
198    public void onDestroy() {
199        mProvider.onDestroy();
200        super.onDestroy();
201    }
202
203    @Override
204    public void onPause() {
205        mProvider.onPause();
206        super.onPause();
207    }
208
209    @Override
210    public void onResume() {
211        super.onResume();
212        mProvider.onResume();
213    }
214
215    @Override
216    public void onStart() {
217        super.onStart();
218        mProvider.onStart();
219    }
220
221    @Override
222    public void onStop() {
223        mProvider.onStop();
224        super.onStop();
225    }
226
227    @Override
228    public void onDetach() {
229        mProvider.onDetach();
230        super.onDetach();
231    }
232
233    @Override
234    public void onViewStateRestored(Bundle bundle) {
235        super.onViewStateRestored(bundle);
236        mProvider.onViewStateRestored(bundle);
237    }
238}
239
240