| 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; |
| 53 | private FrameLayout mView; |
| 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 | |
| 64 | @Before |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 65 | public void setupFragment() throws Exception { |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 66 | mView = new FrameLayout(mContext); |
| 67 | mView.setId(VIEW_ID); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 68 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 69 | assertNotNull("BaseFragmentTest must be tagged with @RunWithLooper", |
| 70 | TestableLooper.get(this)); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 71 | TestableLooper.get(this).runWithLooper(() -> { |
| 72 | mHandler = new Handler(); |
| 73 | |
| 74 | mFragment = mCls.newInstance(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 75 | mFragments = FragmentController.createController(new HostCallbacks()); |
| 76 | mFragments.attachHost(null); |
| 77 | mFragments.getFragmentManager().beginTransaction() |
| 78 | .replace(VIEW_ID, mFragment) |
| 79 | .commit(); |
| 80 | }); |
| 81 | } |
| 82 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 83 | protected TestableContext getContext() { |
| 84 | return new TestableContext(InstrumentationRegistry.getContext()); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 87 | @After |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 88 | public void tearDown() throws Exception { |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 89 | if (mFragments != null) { |
| 90 | // Set mFragments to null to let it know not to destroy. |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 91 | TestableLooper.get(this).runWithLooper(() -> mFragments.dispatchDestroy()); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 92 | } |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testCreateDestroy() { |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 97 | mFragments.dispatchCreate(); |
| 98 | processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 99 | destroyFragments(); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testStartStop() { |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 104 | mFragments.dispatchStart(); |
| 105 | processAllMessages(); |
| 106 | mFragments.dispatchStop(); |
| 107 | processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testResumePause() { |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 112 | mFragments.dispatchResume(); |
| 113 | processAllMessages(); |
| 114 | mFragments.dispatchPause(); |
| 115 | processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
| Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 119 | public void testAttachDetach() { |
| 120 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 121 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, |
| 122 | LayoutParams.TYPE_SYSTEM_ALERT, |
| 123 | 0, PixelFormat.TRANSLUCENT); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 124 | mFragments.dispatchResume(); |
| 125 | processAllMessages(); |
| Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 126 | attachFragmentToWindow(); |
| 127 | detachFragmentToWindow(); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 128 | mFragments.dispatchPause(); |
| 129 | processAllMessages(); |
| Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 133 | public void testRecreate() { |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 134 | mFragments.dispatchResume(); |
| 135 | processAllMessages(); |
| Jason Monk | 64b214e | 2017-03-27 13:40:59 -0400 | [diff] [blame] | 136 | recreateFragment(); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 137 | processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testMultipleResumes() { |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 142 | mFragments.dispatchResume(); |
| 143 | processAllMessages(); |
| 144 | mFragments.dispatchStop(); |
| 145 | processAllMessages(); |
| 146 | mFragments.dispatchResume(); |
| 147 | processAllMessages(); |
| 148 | } |
| 149 | |
| Jason Monk | 64b214e | 2017-03-27 13:40:59 -0400 | [diff] [blame] | 150 | protected void recreateFragment() { |
| 151 | mFragments.dispatchPause(); |
| 152 | Parcelable p = mFragments.saveAllState(); |
| 153 | mFragments.dispatchDestroy(); |
| 154 | |
| 155 | mFragments = FragmentController.createController(new HostCallbacks()); |
| 156 | mFragments.attachHost(null); |
| 157 | mFragments.restoreAllState(p, (FragmentManagerNonConfig) null); |
| 158 | mFragments.dispatchResume(); |
| 159 | mFragment = mFragments.getFragmentManager().findFragmentById(VIEW_ID); |
| 160 | } |
| 161 | |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 162 | protected void attachFragmentToWindow() { |
| 163 | ViewUtils.attachView(mView); |
| Jason Monk | 1660a27 | 2017-04-28 15:53:59 -0400 | [diff] [blame^] | 164 | TestableLooper.get(this).processAllMessages(); |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | protected void detachFragmentToWindow() { |
| 168 | ViewUtils.detachView(mView); |
| Jason Monk | 1660a27 | 2017-04-28 15:53:59 -0400 | [diff] [blame^] | 169 | TestableLooper.get(this).processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | protected void destroyFragments() { |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 173 | mFragments.dispatchDestroy(); |
| 174 | processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 175 | mFragments = null; |
| 176 | } |
| 177 | |
| Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 178 | protected void processAllMessages() { |
| 179 | TestableLooper.get(this).processAllMessages(); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private View findViewById(int id) { |
| 183 | return mView.findViewById(id); |
| 184 | } |
| 185 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 186 | private class HostCallbacks extends FragmentHostCallback<BaseFragmentTest> { |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 187 | public HostCallbacks() { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 188 | super(mContext, BaseFragmentTest.this.mHandler, 0); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 192 | public BaseFragmentTest onGetHost() { |
| 193 | return BaseFragmentTest.this; |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean onShouldSaveFragmentState(Fragment fragment) { |
| 202 | return true; // True for now. |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public LayoutInflater onGetLayoutInflater() { |
| 207 | return LayoutInflater.from(mContext); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean onUseFragmentManagerInflaterFactory() { |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public boolean onHasWindowAnimations() { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public int onGetWindowAnimations() { |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void onAttachFragment(Fragment fragment) { |
| 227 | } |
| 228 | |
| 229 | @Nullable |
| 230 | @Override |
| 231 | public View onFindViewById(int id) { |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 232 | return BaseFragmentTest.this.findViewById(id); |
| Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean onHasView() { |
| 237 | return true; |
| 238 | } |
| 239 | } |
| 240 | } |