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