| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import static junit.framework.Assert.assertTrue; |
| 20 |
|
| 21 |
import android.content.Context; |
| 22 |
import android.perftests.utils.PerfTestActivity; |
| 23 |
import android.widget.FrameLayout; |
| 24 |
|
| 25 |
import androidx.benchmark.BenchmarkState; |
| 26 |
import androidx.benchmark.junit4.BenchmarkRule; |
| 27 |
import androidx.test.InstrumentationRegistry; |
| 28 |
import androidx.test.filters.LargeTest; |
| 29 |
import androidx.test.rule.ActivityTestRule; |
| 30 |
|
| 31 |
import com.android.perftests.core.R; |
| 32 |
|
| 33 |
import org.junit.Before; |
| 34 |
import org.junit.Rule; |
| 35 |
import org.junit.Test; |
| 36 |
|
| 37 |
@LargeTest |
| 38 |
public class ViewPerfTest { |
| 39 |
@Rule |
| 40 |
public final BenchmarkRule mBenchmarkRule = new BenchmarkRule(); |
| 41 |
|
| 42 |
@Rule |
| 43 |
public final ActivityTestRule<PerfTestActivity> mActivityRule = |
| 44 |
new ActivityTestRule<>(PerfTestActivity.class); |
| 45 |
|
| 46 |
private Context mContext; |
| 47 |
|
| 48 |
@Before |
| 49 |
public void setUp() { |
| 50 |
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void testSimpleViewInflate() { |
| 55 |
final BenchmarkState state = mBenchmarkRule.getState(); |
| 56 |
LayoutInflater inflater = LayoutInflater.from(mContext); |
| 57 |
FrameLayout root = new FrameLayout(mContext); |
| 58 |
while (state.keepRunning()) { |
| 59 |
inflater.inflate(R.layout.test_simple_view, root, false); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void testTwelveKeyInflate() { |
| 65 |
final BenchmarkState state = mBenchmarkRule.getState(); |
| 66 |
LayoutInflater inflater = LayoutInflater.from(mContext); |
| 67 |
FrameLayout root = new FrameLayout(mContext); |
| 68 |
while (state.keepRunning()) { |
| 69 |
inflater.inflate(R.layout.twelve_key_entry, root, false); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void testPerformHapticFeedback() throws Throwable { |
| 75 |
final BenchmarkState state = mBenchmarkRule.getState(); |
| 76 |
mActivityRule.runOnUiThread(() -> { |
| 77 |
state.pauseTiming(); |
| 78 |
View view = new View(mContext); |
| 79 |
mActivityRule.getActivity().setContentView(view); |
| 80 |
assertTrue("View needs to be attached to Window to perform haptic feedback", |
| 81 |
view.isAttachedToWindow()); |
| 82 |
state.resumeTiming(); |
| 83 |
|
| 84 |
|
| 85 |
int flags = HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING |
| 86 |
| HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING; |
| 87 |
|
| 88 |
while (state.keepRunning()) { |
| 89 |
assertTrue("Call to performHapticFeedback was ignored", |
| 90 |
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_PRESS, flags)); |
| 91 |
} |
| 92 |
}); |
| 93 |
} |
| 94 |
} |