1959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar/* 2959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * Copyright (C) 2016 The Android Open Source Project 3959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * 4959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 5959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * you may not use this file except in compliance with the License. 6959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * You may obtain a copy of the License at 7959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * 8959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 9959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * 10959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * Unless required by applicable law or agreed to in writing, software 11959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 12959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * See the License for the specific language governing permissions and 14959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * limitations under the License. 15959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar */ 16959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyarpackage android.support.v7.recyclerview.test; 17959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 18959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyarimport android.support.test.rule.ActivityTestRule; 19959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyarimport android.support.v7.widget.TestActivity; 20959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 21959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyarimport org.junit.runner.Description; 22959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyarimport org.junit.runners.model.Statement; 23959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 24959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar/** 25959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar * Like ActivityTestRule but re-uses the same activity 26959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar */ 27959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyarpublic class SameActivityTestRule extends ActivityTestRule<TestActivity> { 28959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar static TestActivity sTestActivity; 29959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar public SameActivityTestRule() { 30959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar super(TestActivity.class, false, false); 31959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 32959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 33959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar public boolean canReUseActivity() { 34959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar return true; 35959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 36959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 37959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar @Override 38959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar public Statement apply(final Statement base, Description description) { 39959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar return new ReUsedActivityStatement(base); 40959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 41959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 42959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar @Override 43959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar public TestActivity getActivity() { 44959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar if (sTestActivity != null) { 45959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar return sTestActivity; 46959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 47959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar return super.getActivity(); 48959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 49959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 50959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar private class ReUsedActivityStatement extends Statement { 51959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 52959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar private final Statement mBase; 53959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 54959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar public ReUsedActivityStatement(Statement base) { 55959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar mBase = base; 56959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 57959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar 58959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar @Override 59959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar public void evaluate() throws Throwable { 60959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar try { 61959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar if (sTestActivity == null || !sTestActivity.canBeReUsed() || !canReUseActivity()) { 62959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar launchActivity(getActivityIntent()); 63959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar sTestActivity = getActivity(); 64959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar sTestActivity.setAllowFinish(!canReUseActivity()); 65959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar if (!canReUseActivity()) { 66959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar sTestActivity = null; 67959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 68959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } else { 69959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar sTestActivity.resetContent(); 70959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 71959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar mBase.evaluate(); 72959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } finally { 73959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar afterActivityFinished(); 74959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 75959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 76959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar } 77959f4c0fac89425a8a9842e82fc180ec736fffacYigit Boyar} 78