1 /* 2 * Copyright (C) 2014 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.dialer.widget; 18 19 import android.content.Context; 20 import android.test.InstrumentationTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 import com.android.dialer.widget.ActionBarController.ActivityUi; 24 25 @SmallTest 26 public class ActionBarControllerTest extends InstrumentationTestCase { 27 28 private static final int ACTION_BAR_HEIGHT = 100; 29 private ActionBarController mActionBarController; 30 private SearchEditTextLayout mSearchBox; 31 private MockActivityUi mActivityUi; 32 33 private class MockActivityUi implements ActivityUi { 34 boolean isInSearchUi; 35 boolean hasSearchQuery; 36 boolean shouldShowActionBar; 37 int actionBarHideOffset; 38 39 @Override 40 public boolean isInSearchUi() { 41 return isInSearchUi; 42 } 43 44 @Override 45 public boolean hasSearchQuery() { 46 return hasSearchQuery; 47 } 48 49 @Override 50 public boolean shouldShowActionBar() { 51 return shouldShowActionBar; 52 } 53 54 @Override 55 public int getActionBarHeight() { 56 return ACTION_BAR_HEIGHT; 57 } 58 59 @Override 60 public int getActionBarHideOffset() { 61 return actionBarHideOffset; 62 } 63 64 @Override 65 public void setActionBarHideOffset(int offset) { 66 actionBarHideOffset = offset; 67 } 68 } 69 70 /** 71 * Mock version of the searchbox, that updates its state immediately instead of animating 72 */ 73 private class MockSearchBox extends SearchEditTextLayout { 74 75 public MockSearchBox(Context context) { 76 super(context, null); 77 } 78 79 @Override 80 public void expand(boolean animate, boolean requestFocus) { 81 mIsExpanded = true; 82 } 83 84 @Override 85 public void collapse(boolean animate) { 86 mIsExpanded = false; 87 } 88 } 89 90 @Override 91 protected void setUp() { 92 mActivityUi = new MockActivityUi(); 93 mSearchBox = new MockSearchBox(this.getInstrumentation().getContext()); 94 mActionBarController = new ActionBarController(mActivityUi, mSearchBox); 95 } 96 97 // Tapping the search box should only do something when the activity is not in the search UI 98 public void testSearchBoxTapped() { 99 mSearchBox.collapse(false); 100 mActivityUi.isInSearchUi = false; 101 mActionBarController.onSearchBoxTapped(); 102 assertActionBarState(true, false, false); 103 104 // Collapse the search box manually again. This time tapping on the search box should not 105 // expand the search box because isInSearchUi is not true. 106 mSearchBox.collapse(false); 107 mActivityUi.isInSearchUi = true; 108 mActionBarController.onSearchBoxTapped(); 109 assertActionBarState(false, false, false); 110 } 111 112 // The search box should always end up being faded in and collapsed. If necessary, it should 113 // be slid down or up depending on what the state of the action bar was before that. 114 public void testOnSearchUiExited() { 115 // ActionBar shown previously before entering searchUI 116 mSearchBox.expand(true, false); 117 mSearchBox.setVisible(false); 118 mActivityUi.shouldShowActionBar = true; 119 mActionBarController.onSearchUiExited(); 120 assertActionBarState(false, false, false); 121 122 // ActionBar slid up previously before entering searchUI 123 mSearchBox.collapse(false); 124 mSearchBox.setVisible(false); 125 mActivityUi.shouldShowActionBar = false; 126 mActionBarController.onSearchUiExited(); 127 assertActionBarState(false, false, true); 128 } 129 130 // Depending on what state the UI was in previously, sliding the dialpad down can mean either 131 // displaying the expanded search box by sliding it down, displaying the unexpanded search box, 132 // or nothing at all. 133 public void testOnDialpadDown() { 134 // No search query typed in the dialpad and action bar was showing before 135 mActivityUi.shouldShowActionBar = true; 136 mActivityUi.isInSearchUi = true; 137 mSearchBox.setVisible(false); 138 mActionBarController.onDialpadDown(); 139 assertActionBarState(false, false, false); 140 141 // No search query typed in the dialpad, but action bar was not showing before 142 mActionBarController.slideActionBar(true /* slideUp */, false /* animate */); 143 mActivityUi.shouldShowActionBar = false; 144 mSearchBox.setVisible(false); 145 mActionBarController.onDialpadDown(); 146 assertActionBarState(false, false, true); 147 148 // Something typed in the dialpad - so remain in search UI and slide the expanded search 149 // box down 150 mActionBarController.slideActionBar(true /* slideUp */, false /* animate */); 151 mActivityUi.shouldShowActionBar = true; 152 mActivityUi.hasSearchQuery= true; 153 mSearchBox.setVisible(false); 154 mSearchBox.expand(false, false); 155 mActionBarController.onDialpadDown(); 156 assertActionBarState(true, false, false); 157 } 158 159 // Sliding the dialpad up should fade out the search box if we weren't already in search, or 160 // slide up the search box otherwise 161 public void testOnDialpadUp() { 162 mActivityUi.isInSearchUi = false; 163 mActionBarController.onDialpadUp(); 164 assertActionBarState(false, true, false); 165 166 // In Search UI, with expanded search box and something currently typed in the search box 167 mActivityUi.isInSearchUi = true; 168 mActivityUi.hasSearchQuery = true; 169 mSearchBox.expand(true, false); 170 mSearchBox.setVisible(true); 171 mActionBarController.slideActionBar(true /* slideUp */, false /* animate */); 172 mActionBarController.onDialpadUp(); 173 assertActionBarState(true, false, true); 174 } 175 176 private void assertActionBarState(boolean isExpanded, boolean isFadedOut, boolean isSlidUp) { 177 assertEquals(isExpanded, mSearchBox.isExpanded()); 178 assertEquals(isFadedOut, mSearchBox.isFadedOut()); 179 assertEquals(isSlidUp, mActionBarController.getIsActionBarSlidUp()); 180 } 181 } 182