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