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