blob: 59d4e55d8d45fcc34935dabd26667b686d6527b3 [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
44 private void testMenuPosition(View target) throws InterruptedException {
45 final int minScreenDimension = getMinScreenDimension();
46 if (minScreenDimension < 320) {
47 // Assume there is insufficient room for the context menu to be aligned properly.
48 return;
49 }
50
51 int offsetX = target.getWidth() / 2;
52 int offsetY = target.getHeight() / 2;
53
54 getInstrumentation().runOnMainSync(() -> target.performLongClick(offsetX, offsetY));
55
56 PollingCheck.waitFor(
57 () -> ContextMenuUtils.isMenuItemClickable(ContextMenuActivity.LABEL_SUBMENU));
58
59 ContextMenuUtils.assertContextMenuAlignment(target, offsetX, offsetY);
60
61 ContextMenuUtils.clickMenuItem(ContextMenuActivity.LABEL_SUBMENU);
62
63 PollingCheck.waitFor(
64 () -> ContextMenuUtils.isMenuItemClickable(ContextMenuActivity.LABEL_SUBITEM));
65
66 if (minScreenDimension < getCascadingMenuTreshold()) {
67 // A non-cascading submenu should be displayed at the same location as its parent.
68 // Not testing cascading submenu position, as it is positioned differently.
69 ContextMenuUtils.assertContextMenuAlignment(target, offsetX, offsetY);
70 }
71 }
72
73 /**
74 * Returns the minimum of the default display's width and height.
75 */
76 private int getMinScreenDimension() {
77 final WindowManager windowManager = (WindowManager) getActivity().getSystemService(
78 Context.WINDOW_SERVICE);
79 final Display display = windowManager.getDefaultDisplay();
80 final Point displaySize = new Point();
81 display.getRealSize(displaySize);
82 return Math.min(displaySize.x, displaySize.y);
83 }
84
85 /**
86 * Returns the minimum display size where cascading submenus are supported.
87 */
88 private int getCascadingMenuTreshold() {
89 // Use the same dimension resource as in MenuPopupHelper.createPopup().
90 return getActivity().getResources().getDimensionPixelSize(
91 com.android.internal.R.dimen.cascading_menus_min_smallest_width);
92 }
93}