1 /* 2 * Copyright (C) 2010 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 17 package android.widget.cts; 18 19 import android.view.ContextMenu; 20 import android.view.View; 21 import android.view.ContextMenu.ContextMenuInfo; 22 import android.view.View.OnCreateContextMenuListener; 23 import android.widget.ExpandableListView; 24 import android.widget.AdapterView.AdapterContextMenuInfo; 25 26 public class PositionTesterContextMenuListener implements OnCreateContextMenuListener { 27 28 private int groupPosition, childPosition; 29 30 // Fake constant to store in testType a test type specific to headers and footers 31 private static final int ADAPTER_TYPE = -1; 32 private int testType; // as returned by getPackedPositionType 33 34 // Will be set to null by each call to onCreateContextMenu, unless an error occurred. 35 private String errorMessage; 36 37 public void expectGroupContextMenu(int groupPosition) { 38 this.groupPosition = groupPosition; 39 testType = ExpandableListView.PACKED_POSITION_TYPE_GROUP; 40 } 41 42 public void expectChildContextMenu(int groupPosition, int childPosition) { 43 this.groupPosition = groupPosition; 44 this.childPosition = childPosition; 45 testType = ExpandableListView.PACKED_POSITION_TYPE_CHILD; 46 } 47 48 public void expectAdapterContextMenu(int flatPosition) { 49 this.groupPosition = flatPosition; 50 testType = ADAPTER_TYPE; 51 } 52 53 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 54 errorMessage = null; 55 if (testType == ADAPTER_TYPE) { 56 if (!isTrue("MenuInfo is not an AdapterContextMenuInfo", 57 menuInfo instanceof AdapterContextMenuInfo)) { 58 return; 59 } 60 AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo; 61 if (!areEqual("Wrong flat position", groupPosition, adapterContextMenuInfo.position)) { 62 return; 63 } 64 } else { 65 if (!isTrue("MenuInfo is not an ExpandableListContextMenuInfo", 66 menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) { 67 return; 68 } 69 ExpandableListView.ExpandableListContextMenuInfo elvMenuInfo = 70 (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; 71 long packedPosition = elvMenuInfo.packedPosition; 72 73 int packedPositionType = ExpandableListView.getPackedPositionType(packedPosition); 74 if (!areEqual("Wrong packed position type", testType, packedPositionType)) { 75 return; 76 } 77 78 int packedPositionGroup = ExpandableListView.getPackedPositionGroup(packedPosition); 79 if (!areEqual("Wrong group position", groupPosition, packedPositionGroup)) { 80 return; 81 } 82 83 if (testType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 84 int packedPositionChild = ExpandableListView.getPackedPositionChild(packedPosition); 85 if (!areEqual("Wrong child position", childPosition, packedPositionChild)) { 86 return; 87 } 88 } 89 } 90 } 91 92 private boolean areEqual(String message, int expected, int actual) { 93 if (expected != actual) { 94 errorMessage = String.format(message + " (%d vs %d", expected, actual); 95 return false; 96 } 97 return true; 98 } 99 100 private boolean isTrue(String message, boolean value) { 101 if (!value) { 102 errorMessage = message; 103 return false; 104 } 105 return true; 106 } 107 108 public String getErrorMessage() { 109 return errorMessage; 110 } 111 } 112