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