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_deselect_all,
51                 R.id.dir_menu_rename,
52                 R.id.dir_menu_delete,
53                 R.id.dir_menu_view_in_owner,
54                 R.id.dir_menu_paste_into_folder,
55                 R.id.dir_menu_inspect,
56                 R.id.dir_menu_open_in_new_window,
57                 R.id.root_menu_eject_root,
58                 R.id.root_menu_open_in_new_window,
59                 R.id.root_menu_paste_into_folder,
60                 R.id.root_menu_settings,
61                 R.id.action_menu_open_with,
62                 R.id.action_menu_share,
63                 R.id.action_menu_delete,
64                 R.id.action_menu_select,
65                 R.id.action_menu_select_all,
66                 R.id.action_menu_deselect_all,
67                 R.id.action_menu_copy_to,
68                 R.id.action_menu_extract_to,
69                 R.id.action_menu_move_to,
70                 R.id.action_menu_compress,
71                 R.id.action_menu_rename,
72                 R.id.action_menu_inspect,
73                 R.id.action_menu_view_in_owner,
74                 R.id.action_menu_sort,
75                 R.id.option_menu_search,
76                 R.id.option_menu_debug,
77                 R.id.option_menu_new_window,
78                 R.id.option_menu_create_dir,
79                 R.id.option_menu_select_all,
80                 R.id.option_menu_settings,
81                 R.id.option_menu_inspect,
82                 R.id.option_menu_sort,
83                 R.id.option_menu_show_hidden_files,
84                 R.id.option_menu_launcher,
85                 R.id.sub_menu_grid,
86                 R.id.sub_menu_list);
87     }
88 
89 
90 
create(int... ids)91     public static TestMenu create(int... ids) {
92         final TestMenu menu = Mockito.mock(TestMenu.class,
93                 Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
94         menu.items = new SparseArray<>();
95         for (int id : ids) {
96             TestMenuItem item = TestMenuItem.create(id);
97             menu.addMenuItem(id, item);
98 
99             // Used by SearchViewManager
100             if (id == R.id.option_menu_search) {
101                 item.setActionView(Mockito.mock(SearchView.class));
102             }
103         }
104         return menu;
105     }
106 
addMenuItem(int id, TestMenuItem item)107     public void addMenuItem(int id, TestMenuItem item) {
108         items.put(id, item);
109     }
110 
111     @Override
findItem(int id)112     public TestMenuItem findItem(int id) {
113         return items.get(id);
114     }
115 
116     @Override
size()117     public int size() {
118         return items.size();
119     }
120 
121     @Override
getItem(int index)122     public TestMenuItem getItem(int index) {
123         return items.valueAt(index);
124     }
125 }
126