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