blob: 4150602442431112f1c5d97a9062433f08888947 [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.keyguard;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextClock;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.plugins.ClockPlugin;
import com.android.systemui.plugins.PluginListener;
import com.android.systemui.shared.plugins.PluginManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@SmallTest
@RunWith(AndroidTestingRunner.class)
// Need to run on the main thread because KeyguardSliceView$Row init checks for
// the main thread before acquiring a wake lock. This class is constructed when
// the keyguard_clcok_switch layout is inflated.
@RunWithLooper(setAsMainLooper = true)
public class KeyguardClockSwitchTest extends SysuiTestCase {
private PluginManager mPluginManager;
private FrameLayout mClockContainer;
@Mock
TextClock mClockView;
@InjectMocks
KeyguardClockSwitch mKeyguardClockSwitch;
@Before
public void setUp() {
mPluginManager = mDependency.injectMockDependency(PluginManager.class);
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
mKeyguardClockSwitch =
(KeyguardClockSwitch) layoutInflater.inflate(R.layout.keyguard_clock_switch, null);
mClockContainer = mKeyguardClockSwitch.findViewById(R.id.clock_view);
MockitoAnnotations.initMocks(this);
when(mClockView.getPaint()).thenReturn(mock(TextPaint.class));
}
@Test
public void onAttachToWindow_addPluginListener() {
mKeyguardClockSwitch.onAttachedToWindow();
ArgumentCaptor<PluginListener> listener = ArgumentCaptor.forClass(PluginListener.class);
verify(mPluginManager).addPluginListener(listener.capture(), eq(ClockPlugin.class));
}
@Test
public void onDetachToWindow_removePluginListener() {
mKeyguardClockSwitch.onDetachedFromWindow();
ArgumentCaptor<PluginListener> listener = ArgumentCaptor.forClass(PluginListener.class);
verify(mPluginManager).removePluginListener(listener.capture());
}
@Test
public void onPluginConnected_showPluginClock() {
ClockPlugin plugin = mock(ClockPlugin.class);
TextClock pluginView = new TextClock(getContext());
when(plugin.getView()).thenReturn(pluginView);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
verify(mClockView).setVisibility(GONE);
assertThat(plugin.getView().getParent()).isEqualTo(mClockContainer);
}
@Test
public void onPluginConnected_showPluginBigClock() {
// GIVEN that the container for the big clock has visibility GONE
FrameLayout bigClockContainer = new FrameLayout(getContext());
bigClockContainer.setVisibility(GONE);
mKeyguardClockSwitch.setBigClockContainer(bigClockContainer);
// AND the plugin returns a view for the big clock
ClockPlugin plugin = mock(ClockPlugin.class);
TextClock pluginView = new TextClock(getContext());
when(plugin.getBigClockView()).thenReturn(pluginView);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
// WHEN the plugin is connected
listener.onPluginConnected(plugin, null);
// THEN the big clock container is visible and it is the parent of the
// big clock view.
assertThat(bigClockContainer.getVisibility()).isEqualTo(VISIBLE);
assertThat(pluginView.getParent()).isEqualTo(bigClockContainer);
}
@Test
public void onPluginConnected_nullView() {
ClockPlugin plugin = mock(ClockPlugin.class);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
verify(mClockView, never()).setVisibility(GONE);
}
@Test
public void onPluginConnected_showSecondPluginClock() {
// GIVEN a plugin has already connected
ClockPlugin plugin1 = mock(ClockPlugin.class);
when(plugin1.getView()).thenReturn(new TextClock(getContext()));
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin1, null);
// WHEN a second plugin is connected
ClockPlugin plugin2 = mock(ClockPlugin.class);
when(plugin2.getView()).thenReturn(new TextClock(getContext()));
listener.onPluginConnected(plugin2, null);
// THEN only the view from the second plugin should be a child of KeyguardClockSwitch.
assertThat(plugin2.getView().getParent()).isEqualTo(mClockContainer);
assertThat(plugin1.getView().getParent()).isNull();
}
@Test
public void onPluginDisconnected_showDefaultClock() {
ClockPlugin plugin = mock(ClockPlugin.class);
TextClock pluginView = new TextClock(getContext());
when(plugin.getView()).thenReturn(pluginView);
mClockView.setVisibility(GONE);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
listener.onPluginDisconnected(plugin);
verify(mClockView).setVisibility(VISIBLE);
assertThat(plugin.getView().getParent()).isNull();
}
@Test
public void onPluginDisconnected_hidePluginBigClock() {
// GIVEN that the big clock container is visible
FrameLayout bigClockContainer = new FrameLayout(getContext());
bigClockContainer.setVisibility(VISIBLE);
mKeyguardClockSwitch.setBigClockContainer(bigClockContainer);
// AND the plugin returns a view for the big clock
ClockPlugin plugin = mock(ClockPlugin.class);
TextClock pluginView = new TextClock(getContext());
when(plugin.getBigClockView()).thenReturn(pluginView);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
// WHEN the plugin is disconnected
listener.onPluginDisconnected(plugin);
// THEN the big lock container is GONE and the big clock view doesn't have
// a parent.
assertThat(bigClockContainer.getVisibility()).isEqualTo(GONE);
assertThat(pluginView.getParent()).isNull();
}
@Test
public void onPluginDisconnected_nullView() {
ClockPlugin plugin = mock(ClockPlugin.class);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
listener.onPluginDisconnected(plugin);
verify(mClockView, never()).setVisibility(GONE);
}
@Test
public void onPluginDisconnected_firstOfTwoDisconnected() {
// GIVEN two plugins are connected
ClockPlugin plugin1 = mock(ClockPlugin.class);
when(plugin1.getView()).thenReturn(new TextClock(getContext()));
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin1, null);
ClockPlugin plugin2 = mock(ClockPlugin.class);
when(plugin2.getView()).thenReturn(new TextClock(getContext()));
listener.onPluginConnected(plugin2, null);
// WHEN the first plugin is disconnected
listener.onPluginDisconnected(plugin1);
// THEN the view from the second plugin is still a child of KeyguardClockSwitch.
assertThat(plugin2.getView().getParent()).isEqualTo(mClockContainer);
assertThat(plugin1.getView().getParent()).isNull();
}
@Test
public void onPluginDisconnected_secondOfTwoDisconnected() {
// GIVEN two plugins are connected
ClockPlugin plugin1 = mock(ClockPlugin.class);
when(plugin1.getView()).thenReturn(new TextClock(getContext()));
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin1, null);
ClockPlugin plugin2 = mock(ClockPlugin.class);
when(plugin2.getView()).thenReturn(new TextClock(getContext()));
listener.onPluginConnected(plugin2, null);
// WHEN the second plugin is disconnected
listener.onPluginDisconnected(plugin2);
// THEN the default clock should be shown.
verify(mClockView).setVisibility(VISIBLE);
assertThat(plugin1.getView().getParent()).isNull();
assertThat(plugin2.getView().getParent()).isNull();
}
@Test
public void setTextColor_defaultClockSetTextColor() {
mKeyguardClockSwitch.setTextColor(Color.YELLOW);
verify(mClockView).setTextColor(Color.YELLOW);
}
@Test
public void setTextColor_pluginClockSetTextColor() {
ClockPlugin plugin = mock(ClockPlugin.class);
TextClock pluginView = new TextClock(getContext());
when(plugin.getView()).thenReturn(pluginView);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
mKeyguardClockSwitch.setTextColor(Color.WHITE);
verify(plugin).setTextColor(Color.WHITE);
}
@Test
public void setStyle_defaultClockSetStyle() {
TextPaint paint = mock(TextPaint.class);
Style style = mock(Style.class);
doReturn(paint).when(mClockView).getPaint();
mKeyguardClockSwitch.setStyle(style);
verify(paint).setStyle(style);
}
@Test
public void setStyle_pluginClockSetStyle() {
ClockPlugin plugin = mock(ClockPlugin.class);
TextClock pluginView = new TextClock(getContext());
when(plugin.getView()).thenReturn(pluginView);
Style style = mock(Style.class);
PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
listener.onPluginConnected(plugin, null);
mKeyguardClockSwitch.setStyle(style);
verify(plugin).setStyle(style);
}
}