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