• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.documentsui.R;
23 
24 import 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  */
34 public abstract class TestMenu implements Menu {
35 
36     private SparseArray<TestMenuItem> items = new SparseArray<>();
37 
create()38     public static TestMenu create() {
39         return create(
40                 R.id.menu_open,
41                 R.id.menu_open_in_new_window,
42                 R.id.menu_open_with,
43                 R.id.menu_rename,
44                 R.id.menu_move_to,
45                 R.id.menu_copy_to,
46                 R.id.menu_compress,
47                 R.id.menu_extract_to,
48                 R.id.menu_cut_to_clipboard,
49                 R.id.menu_copy_to_clipboard,
50                 R.id.menu_paste_from_clipboard,
51                 R.id.menu_paste_into_folder,
52                 R.id.menu_share,
53                 R.id.menu_delete,
54                 R.id.menu_create_dir,
55                 R.id.menu_settings,
56                 R.id.menu_new_window,
57                 R.id.menu_select_all,
58                 R.id.menu_grid,
59                 R.id.menu_list,
60                 R.id.menu_advanced,
61                 R.id.menu_debug,
62                 R.id.menu_eject_root,
63                 R.id.menu_view_in_owner);
64     }
65 
create(int... ids)66     public static TestMenu create(int... ids) {
67         final TestMenu menu = Mockito.mock(TestMenu.class,
68                 Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
69         menu.items = new SparseArray<>();
70         for (int id : ids) {
71             TestMenuItem item = TestMenuItem.create(id);
72             menu.addMenuItem(id, item);
73         }
74         return menu;
75     }
76 
addMenuItem(int id, TestMenuItem item)77     public void addMenuItem(int id, TestMenuItem item) {
78         items.put(id, item);
79     }
80 
81     @Override
findItem(int id)82     public TestMenuItem findItem(int id) {
83         return items.get(id);
84     }
85 
86     @Override
size()87     public int size() {
88         return items.size();
89     }
90 
91     @Override
getItem(int index)92     public TestMenuItem getItem(int index) {
93         return items.valueAt(index);
94     }
95 }
96