1 /* 2 * Copyright (C) 2016 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 com.android.documentsui.testing; 18 19 import android.util.SparseArray; 20 import android.view.Menu; 21 22 import androidx.appcompat.widget.SearchView; 23 24 import com.android.documentsui.R; 25 26 import org.mockito.Mockito; 27 28 /** 29 * 30 * Test copy of {@link android.view.Menu}. 31 * 32 * We use abstract so we don't have to implement all the necessary methods from the interface, 33 * and we use Mockito to just mock out the methods we need. 34 * To get an instance, use {@link #create(int...)}. 35 */ 36 public abstract class TestMenu implements Menu { 37 38 private SparseArray<TestMenuItem> items = new SparseArray<>(); 39 create()40 public static TestMenu create() { 41 return create( 42 R.id.dir_menu_share, 43 R.id.dir_menu_open, 44 R.id.dir_menu_open_with, 45 R.id.dir_menu_cut_to_clipboard, 46 R.id.dir_menu_copy_to_clipboard, 47 R.id.dir_menu_paste_from_clipboard, 48 R.id.dir_menu_create_dir, 49 R.id.dir_menu_select_all, 50 R.id.dir_menu_rename, 51 R.id.dir_menu_delete, 52 R.id.dir_menu_view_in_owner, 53 R.id.dir_menu_paste_into_folder, 54 R.id.dir_menu_inspect, 55 R.id.dir_menu_open_in_new_window, 56 R.id.root_menu_eject_root, 57 R.id.root_menu_open_in_new_window, 58 R.id.root_menu_paste_into_folder, 59 R.id.root_menu_settings, 60 R.id.action_menu_open_with, 61 R.id.action_menu_share, 62 R.id.action_menu_delete, 63 R.id.action_menu_select, 64 R.id.action_menu_select_all, 65 R.id.action_menu_copy_to, 66 R.id.action_menu_extract_to, 67 R.id.action_menu_move_to, 68 R.id.action_menu_compress, 69 R.id.action_menu_rename, 70 R.id.action_menu_inspect, 71 R.id.action_menu_view_in_owner, 72 R.id.action_menu_sort, 73 R.id.option_menu_search, 74 R.id.option_menu_debug, 75 R.id.option_menu_new_window, 76 R.id.option_menu_create_dir, 77 R.id.option_menu_select_all, 78 R.id.option_menu_advanced, 79 R.id.option_menu_settings, 80 R.id.option_menu_inspect, 81 R.id.option_menu_sort, 82 R.id.sub_menu_grid, 83 R.id.sub_menu_list); 84 } 85 86 87 create(int... ids)88 public static TestMenu create(int... ids) { 89 final TestMenu menu = Mockito.mock(TestMenu.class, 90 Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS)); 91 menu.items = new SparseArray<>(); 92 for (int id : ids) { 93 TestMenuItem item = TestMenuItem.create(id); 94 menu.addMenuItem(id, item); 95 96 // Used by SearchViewManager 97 if (id == R.id.option_menu_search) { 98 item.setActionView(Mockito.mock(SearchView.class)); 99 } 100 } 101 return menu; 102 } 103 addMenuItem(int id, TestMenuItem item)104 public void addMenuItem(int id, TestMenuItem item) { 105 items.put(id, item); 106 } 107 108 @Override findItem(int id)109 public TestMenuItem findItem(int id) { 110 return items.get(id); 111 } 112 113 @Override size()114 public int size() { 115 return items.size(); 116 } 117 118 @Override getItem(int index)119 public TestMenuItem getItem(int index) { 120 return items.valueAt(index); 121 } 122 } 123