blob: 657a7fc7b7984d2e57d22e719e0ec13d3b7a8a5e [file] [log] [blame]
Vladislav Kaznacheevd959c9d2018-01-23 14:03:36 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view.menu;
18
19import android.content.Context;
20import android.graphics.Point;
21import android.support.test.filters.MediumTest;
22import android.test.ActivityInstrumentationTestCase;
23import android.util.PollingCheck;
24import android.view.Display;
25import android.view.View;
26import android.view.WindowManager;
27import android.widget.espresso.ContextMenuUtils;
28
29@MediumTest
30public class ContextMenuTest extends ActivityInstrumentationTestCase<ContextMenuActivity> {
31
32 public ContextMenuTest() {
33 super("com.android.frameworks.coretests", ContextMenuActivity.class);
34 }
35
36 public void testContextMenuPositionLtr() throws InterruptedException {
37 testMenuPosition(getActivity().getTargetLtr());
38 }
39
40 public void testContextMenuPositionRtl() throws InterruptedException {
41 testMenuPosition(getActivity().getTargetRtl());
42 }
43
Vladislav Kaznacheeva6dfa742018-01-25 13:59:35 -080044 public void testContextMenuPositionRepetitive() throws InterruptedException {
45 // Regression test for b/72507876
46 testMenuPosition(getActivity().getTargetLtr());
47 testMenuPosition(getActivity().getTargetRtl());
48 testMenuPosition(getActivity().getTargetLtr());
49 }
50
Vladislav Kaznacheevd959c9d2018-01-23 14:03:36 -080051 private void testMenuPosition(View target) throws InterruptedException {
52 final int minScreenDimension = getMinScreenDimension();
53 if (minScreenDimension < 320) {
54 // Assume there is insufficient room for the context menu to be aligned properly.
55 return;
56 }
57
58 int offsetX = target.getWidth() / 2;
59 int offsetY = target.getHeight() / 2;
60
61 getInstrumentation().runOnMainSync(() -> target.performLongClick(offsetX, offsetY));
62
63 PollingCheck.waitFor(
64 () -> ContextMenuUtils.isMenuItemClickable(ContextMenuActivity.LABEL_SUBMENU));
65
66 ContextMenuUtils.assertContextMenuAlignment(target, offsetX, offsetY);
67
68 ContextMenuUtils.clickMenuItem(ContextMenuActivity.LABEL_SUBMENU);
69
70 PollingCheck.waitFor(
71 () -> ContextMenuUtils.isMenuItemClickable(ContextMenuActivity.LABEL_SUBITEM));
72
73 if (minScreenDimension < getCascadingMenuTreshold()) {
74 // A non-cascading submenu should be displayed at the same location as its parent.
75 // Not testing cascading submenu position, as it is positioned differently.
76 ContextMenuUtils.assertContextMenuAlignment(target, offsetX, offsetY);
77 }
78 }
79
80 /**
81 * Returns the minimum of the default display's width and height.
82 */
83 private int getMinScreenDimension() {
84 final WindowManager windowManager = (WindowManager) getActivity().getSystemService(
85 Context.WINDOW_SERVICE);
86 final Display display = windowManager.getDefaultDisplay();
87 final Point displaySize = new Point();
88 display.getRealSize(displaySize);
89 return Math.min(displaySize.x, displaySize.y);
90 }
91
92 /**
93 * Returns the minimum display size where cascading submenus are supported.
94 */
95 private int getCascadingMenuTreshold() {
96 // Use the same dimension resource as in MenuPopupHelper.createPopup().
97 return getActivity().getResources().getDimensionPixelSize(
98 com.android.internal.R.dimen.cascading_menus_min_smallest_width);
99 }
100}