Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 1 | /* |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except 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 |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 15 | package android.testing; |
| 16 | |
| 17 | import static org.junit.Assert.assertNotNull; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.app.Fragment; |
| 21 | import android.app.FragmentController; |
| 22 | import android.app.FragmentHostCallback; |
| 23 | import android.app.FragmentManagerNonConfig; |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 24 | import android.content.Context; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 25 | import android.graphics.PixelFormat; |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 26 | import android.os.Bundle; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 28 | import android.os.Parcelable; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 29 | import android.support.test.InstrumentationRegistry; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 32 | import android.view.WindowManager; |
| 33 | import android.view.WindowManager.LayoutParams; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 34 | import android.widget.FrameLayout; |
| 35 | |
| 36 | import org.junit.After; |
| 37 | import org.junit.Before; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 38 | import org.junit.Rule; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 39 | import org.junit.Test; |
| 40 | |
| 41 | import java.io.FileDescriptor; |
| 42 | import java.io.PrintWriter; |
| 43 | |
| 44 | /** |
| 45 | * Base class for fragment class tests. Just adding one for any fragment will push it through |
| 46 | * general lifecycle events and ensure no basic leaks are happening. This class also implements |
| 47 | * the host for subclasses, so they can push it into desired states and do any unit testing |
| 48 | * required. |
| 49 | */ |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 50 | public abstract class BaseFragmentTest { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 51 | |
| 52 | private static final int VIEW_ID = 42; |
| 53 | private final Class<? extends Fragment> mCls; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 54 | private Handler mHandler; |
Siarhei Vishniakou | 6ad0e39 | 2017-06-02 17:20:34 -0700 | [diff] [blame] | 55 | protected FrameLayout mView; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 56 | protected FragmentController mFragments; |
| 57 | protected Fragment mFragment; |
| 58 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 59 | @Rule |
| 60 | public final TestableContext mContext = getContext(); |
| 61 | |
| 62 | public BaseFragmentTest(Class<? extends Fragment> cls) { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 63 | mCls = cls; |
| 64 | } |
| 65 | |
Siarhei Vishniakou | 6ad0e39 | 2017-06-02 17:20:34 -0700 | [diff] [blame] | 66 | protected void createRootView() { |
| 67 | mView = new FrameLayout(mContext); |
| 68 | } |
| 69 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 70 | @Before |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 71 | public void setupFragment() throws Exception { |
Siarhei Vishniakou | 6ad0e39 | 2017-06-02 17:20:34 -0700 | [diff] [blame] | 72 | createRootView(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 73 | mView.setId(VIEW_ID); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 74 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 75 | assertNotNull("BaseFragmentTest must be tagged with @RunWithLooper", |
| 76 | TestableLooper.get(this)); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 77 | TestableLooper.get(this).runWithLooper(() -> { |
| 78 | mHandler = new Handler(); |
| 79 | |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 80 | mFragment = instantiate(mContext, mCls.getName(), null); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 81 | mFragments = FragmentController.createController(new HostCallbacks()); |
| 82 | mFragments.attachHost(null); |
| 83 | mFragments.getFragmentManager().beginTransaction() |
| 84 | .replace(VIEW_ID, mFragment) |
| 85 | .commit(); |
| 86 | }); |
| 87 | } |
| 88 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 89 | /** |
| 90 | * Allows tests to sub-class TestableContext if they want to provide any extended functionality |
| 91 | * or provide a {@link LeakCheck} to the TestableContext upon instantiation. |
| 92 | */ |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 93 | protected TestableContext getContext() { |
| 94 | return new TestableContext(InstrumentationRegistry.getContext()); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 97 | @After |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 98 | public void tearDown() throws Exception { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 99 | if (mFragments != null) { |
| 100 | // Set mFragments to null to let it know not to destroy. |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 101 | TestableLooper.get(this).runWithLooper(() -> mFragments.dispatchDestroy()); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 102 | } |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testCreateDestroy() { |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 107 | mFragments.dispatchCreate(); |
| 108 | processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 109 | destroyFragments(); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testStartStop() { |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 114 | mFragments.dispatchStart(); |
| 115 | processAllMessages(); |
| 116 | mFragments.dispatchStop(); |
| 117 | processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testResumePause() { |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 122 | mFragments.dispatchResume(); |
| 123 | processAllMessages(); |
| 124 | mFragments.dispatchPause(); |
| 125 | processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 129 | public void testAttachDetach() { |
| 130 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 131 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, |
| 132 | LayoutParams.TYPE_SYSTEM_ALERT, |
| 133 | 0, PixelFormat.TRANSLUCENT); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 134 | mFragments.dispatchResume(); |
| 135 | processAllMessages(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 136 | attachFragmentToWindow(); |
| 137 | detachFragmentToWindow(); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 138 | mFragments.dispatchPause(); |
| 139 | processAllMessages(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 143 | public void testRecreate() { |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 144 | mFragments.dispatchResume(); |
| 145 | processAllMessages(); |
Jason Monk | 64b214e | 2017-03-27 13:40:59 -0400 | [diff] [blame] | 146 | recreateFragment(); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 147 | processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testMultipleResumes() { |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 152 | mFragments.dispatchResume(); |
| 153 | processAllMessages(); |
| 154 | mFragments.dispatchStop(); |
| 155 | processAllMessages(); |
| 156 | mFragments.dispatchResume(); |
| 157 | processAllMessages(); |
| 158 | } |
| 159 | |
Jason Monk | 64b214e | 2017-03-27 13:40:59 -0400 | [diff] [blame] | 160 | protected void recreateFragment() { |
| 161 | mFragments.dispatchPause(); |
| 162 | Parcelable p = mFragments.saveAllState(); |
| 163 | mFragments.dispatchDestroy(); |
| 164 | |
| 165 | mFragments = FragmentController.createController(new HostCallbacks()); |
| 166 | mFragments.attachHost(null); |
| 167 | mFragments.restoreAllState(p, (FragmentManagerNonConfig) null); |
| 168 | mFragments.dispatchResume(); |
| 169 | mFragment = mFragments.getFragmentManager().findFragmentById(VIEW_ID); |
| 170 | } |
| 171 | |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 172 | protected void attachFragmentToWindow() { |
| 173 | ViewUtils.attachView(mView); |
Jason Monk | 1660a27 | 2017-04-28 15:53:59 -0400 | [diff] [blame] | 174 | TestableLooper.get(this).processAllMessages(); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | protected void detachFragmentToWindow() { |
| 178 | ViewUtils.detachView(mView); |
Jason Monk | 1660a27 | 2017-04-28 15:53:59 -0400 | [diff] [blame] | 179 | TestableLooper.get(this).processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | protected void destroyFragments() { |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 183 | mFragments.dispatchDestroy(); |
| 184 | processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 185 | mFragments = null; |
| 186 | } |
| 187 | |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 188 | protected void processAllMessages() { |
| 189 | TestableLooper.get(this).processAllMessages(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 192 | /** |
| 193 | * Method available for override to replace fragment instantiation. |
| 194 | */ |
| 195 | protected Fragment instantiate(Context context, String className, @Nullable Bundle arguments) { |
| 196 | return Fragment.instantiate(context, className, arguments); |
| 197 | } |
| 198 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 199 | private View findViewById(int id) { |
| 200 | return mView.findViewById(id); |
| 201 | } |
| 202 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 203 | private class HostCallbacks extends FragmentHostCallback<BaseFragmentTest> { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 204 | public HostCallbacks() { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 205 | super(mContext, BaseFragmentTest.this.mHandler, 0); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 209 | public BaseFragmentTest onGetHost() { |
| 210 | return BaseFragmentTest.this; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 215 | } |
| 216 | |
| 217 | @Override |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 218 | public Fragment instantiate(Context context, String className, Bundle arguments) { |
| 219 | return BaseFragmentTest.this.instantiate(context, className, arguments); |
| 220 | } |
| 221 | |
| 222 | @Override |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 223 | public boolean onShouldSaveFragmentState(Fragment fragment) { |
| 224 | return true; // True for now. |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public LayoutInflater onGetLayoutInflater() { |
| 229 | return LayoutInflater.from(mContext); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean onUseFragmentManagerInflaterFactory() { |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public boolean onHasWindowAnimations() { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public int onGetWindowAnimations() { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onAttachFragment(Fragment fragment) { |
| 249 | } |
| 250 | |
| 251 | @Nullable |
| 252 | @Override |
| 253 | public View onFindViewById(int id) { |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 254 | return BaseFragmentTest.this.findViewById(id); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean onHasView() { |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | } |