• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.ui.conversation;
17 
18 import android.app.FragmentManager;
19 import android.app.FragmentTransaction;
20 import android.os.Bundle;
21 import android.test.suitebuilder.annotation.SmallTest;
22 import android.widget.EditText;
23 
24 import com.android.messaging.BugleTestCase;
25 import com.android.messaging.FakeFactory;
26 import com.android.messaging.R;
27 import com.android.messaging.datamodel.binding.Binding;
28 import com.android.messaging.datamodel.binding.BindingBase;
29 import com.android.messaging.datamodel.data.ConversationData;
30 import com.android.messaging.datamodel.data.DraftMessageData;
31 import com.android.messaging.datamodel.data.SubscriptionListData;
32 import com.android.messaging.ui.conversation.ConversationInputManager.ConversationInputHost;
33 import com.android.messaging.ui.conversation.ConversationInputManager.ConversationInputSink;
34 import com.android.messaging.ui.mediapicker.MediaPicker;
35 import com.android.messaging.util.BugleGservices;
36 import com.android.messaging.util.ImeUtil;
37 import com.android.messaging.util.ImeUtil.ImeStateHost;
38 
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.Spy;
42 
43 @SmallTest
44 public class ConversationInputManagerTest extends BugleTestCase {
45     @Spy protected ImeUtil spyImeUtil;
46     @Mock protected BugleGservices mockBugleGservices;
47     @Mock protected FragmentManager mockFragmentManager;
48     @Mock protected ConversationInputHost mockConversationInputHost;
49     @Mock protected ConversationInputSink mockConversationInputSink;
50     @Mock protected ImeStateHost mockImeStateHost;
51     @Mock protected ConversationData mockConversationData;
52     @Mock protected DraftMessageData mockDraftMessageData;
53     @Mock protected MediaPicker mockMediaPicker;
54     @Mock protected SubscriptionListData mockSubscriptionListData;
55     @Mock protected FragmentTransaction mockFragmentTransaction;
56 
57     @Override
setUp()58     protected void setUp() throws Exception {
59         super.setUp();
60         FakeFactory.register(getTestContext())
61                 .withBugleGservices(mockBugleGservices);
62         spyImeUtil = Mockito.spy(new ImeUtil());
63         ImeUtil.set(spyImeUtil);
64     }
65 
initNewInputManager(final Bundle savedState)66     private ConversationInputManager initNewInputManager(final Bundle savedState) {
67         // Set up the mocks.
68         Mockito.when(mockConversationInputHost.getSimSelectorView())
69                 .thenReturn(new SimSelectorView(getTestContext(), null));
70         Mockito.when(mockConversationInputHost.createMediaPicker()).thenReturn(mockMediaPicker);
71         Mockito.when(mockConversationInputSink.getComposeEditText())
72                 .thenReturn(new EditText(getTestContext()));
73         Mockito.doReturn(mockFragmentTransaction).when(mockFragmentTransaction).replace(
74                 Mockito.eq(R.id.mediapicker_container), Mockito.any(MediaPicker.class),
75                 Mockito.anyString());
76         Mockito.when(mockFragmentManager.findFragmentByTag(MediaPicker.FRAGMENT_TAG))
77                 .thenReturn(null);
78         Mockito.when(mockFragmentManager.beginTransaction()).thenReturn(mockFragmentTransaction);
79         Mockito.when(mockSubscriptionListData.hasData()).thenReturn(true);
80         Mockito.when(mockConversationData.getSubscriptionListData())
81                 .thenReturn(mockSubscriptionListData);
82         Mockito.doReturn(true).when(mockConversationData).isBound(Mockito.anyString());
83         Mockito.doReturn(true).when(mockDraftMessageData).isBound(Mockito.anyString());
84 
85         final Binding<ConversationData> dataBinding = BindingBase.createBinding(this);
86         dataBinding.bind(mockConversationData);
87         final Binding<DraftMessageData> draftBinding = BindingBase.createBinding(this);
88         draftBinding.bind(mockDraftMessageData);
89         final ConversationInputManager inputManager = new ConversationInputManager(getTestContext(),
90                 mockConversationInputHost, mockConversationInputSink, mockImeStateHost,
91                 mockFragmentManager, dataBinding, draftBinding, savedState);
92         return inputManager;
93     }
94 
testShowHideInputs()95     public void testShowHideInputs() {
96         final ConversationInputManager inputManager = initNewInputManager(new Bundle());
97         Mockito.when(mockMediaPicker.isOpen()).thenReturn(true);
98         inputManager.showHideMediaPicker(true /* show */, true /* animate */);
99         Mockito.verify(mockFragmentTransaction).replace(
100                 Mockito.eq(R.id.mediapicker_container), Mockito.any(MediaPicker.class),
101                 Mockito.anyString());
102         Mockito.verify(mockMediaPicker).open(Mockito.anyInt(), Mockito.eq(true /* animate */));
103 
104         assertEquals(true, inputManager.isMediaPickerVisible());
105         assertEquals(false, inputManager.isSimSelectorVisible());
106         assertEquals(false, inputManager.isImeKeyboardVisible());
107 
108         Mockito.when(mockMediaPicker.isOpen()).thenReturn(false);
109         inputManager.showHideMediaPicker(false /* show */, true /* animate */);
110         Mockito.verify(mockMediaPicker).dismiss(Mockito.eq(true /* animate */));
111 
112         assertEquals(false, inputManager.isMediaPickerVisible());
113         assertEquals(false, inputManager.isSimSelectorVisible());
114         assertEquals(false, inputManager.isImeKeyboardVisible());
115     }
116 
testShowTwoInputsSequentially()117     public void testShowTwoInputsSequentially() {
118         // First show the media picker, then show the IME keyboard.
119         final ConversationInputManager inputManager = initNewInputManager(new Bundle());
120         Mockito.when(mockMediaPicker.isOpen()).thenReturn(true);
121         inputManager.showHideMediaPicker(true /* show */, true /* animate */);
122 
123         assertEquals(true, inputManager.isMediaPickerVisible());
124         assertEquals(false, inputManager.isSimSelectorVisible());
125         assertEquals(false, inputManager.isImeKeyboardVisible());
126 
127         Mockito.when(mockMediaPicker.isOpen()).thenReturn(false);
128         inputManager.showHideImeKeyboard(true /* show */, true /* animate */);
129 
130         assertEquals(false, inputManager.isMediaPickerVisible());
131         assertEquals(false, inputManager.isSimSelectorVisible());
132         assertEquals(true, inputManager.isImeKeyboardVisible());
133     }
134 
testOnKeyboardShow()135     public void testOnKeyboardShow() {
136         final ConversationInputManager inputManager = initNewInputManager(new Bundle());
137         Mockito.when(mockMediaPicker.isOpen()).thenReturn(true);
138         inputManager.showHideMediaPicker(true /* show */, true /* animate */);
139 
140         assertEquals(true, inputManager.isMediaPickerVisible());
141         assertEquals(false, inputManager.isSimSelectorVisible());
142         assertEquals(false, inputManager.isImeKeyboardVisible());
143 
144         Mockito.when(mockMediaPicker.isOpen()).thenReturn(false);
145         inputManager.testNotifyImeStateChanged(true /* imeOpen */);
146 
147         assertEquals(false, inputManager.isMediaPickerVisible());
148         assertEquals(false, inputManager.isSimSelectorVisible());
149         assertEquals(true, inputManager.isImeKeyboardVisible());
150     }
151 
testRestoreState()152     public void testRestoreState() {
153         final ConversationInputManager inputManager = initNewInputManager(new Bundle());
154         Mockito.when(mockMediaPicker.isOpen()).thenReturn(true);
155         inputManager.showHideMediaPicker(true /* show */, true /* animate */);
156 
157         assertEquals(true, inputManager.isMediaPickerVisible());
158         assertEquals(false, inputManager.isSimSelectorVisible());
159         assertEquals(false, inputManager.isImeKeyboardVisible());
160 
161         Bundle savedInstanceState = new Bundle();
162         inputManager.onSaveInputState(savedInstanceState);
163 
164         // Now try to restore the state
165         final ConversationInputManager restoredInputManager =
166                 initNewInputManager(savedInstanceState);
167 
168         // Make sure the state is preserved.
169         assertEquals(true, restoredInputManager.isMediaPickerVisible());
170         assertEquals(false, restoredInputManager.isSimSelectorVisible());
171         assertEquals(false, restoredInputManager.isImeKeyboardVisible());
172     }
173 
testBackPress()174     public void testBackPress() {
175         final ConversationInputManager inputManager = initNewInputManager(new Bundle());
176         Mockito.when(mockMediaPicker.isOpen()).thenReturn(true);
177         inputManager.showHideMediaPicker(true /* show */, true /* animate */);
178 
179         assertEquals(true, inputManager.isMediaPickerVisible());
180         assertEquals(false, inputManager.isSimSelectorVisible());
181         assertEquals(false, inputManager.isImeKeyboardVisible());
182 
183         Mockito.when(mockMediaPicker.isOpen()).thenReturn(false);
184         assertEquals(true, inputManager.onBackPressed());
185 
186         assertEquals(false, inputManager.isMediaPickerVisible());
187         assertEquals(false, inputManager.isSimSelectorVisible());
188         assertEquals(false, inputManager.isImeKeyboardVisible());
189     }
190 }
191