Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5ea09ee authored by Matt Pietal's avatar Matt Pietal Committed by Android (Google) Code Review
Browse files

Merge "Remove clock migration: the big one" into main

parents f2f0688e 1f73b12d
Loading
Loading
Loading
Loading
+0 −117
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.TextView;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.systemui.SysuiTestCase;
import com.android.systemui.res.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class KeyguardClockAccessibilityDelegateTest extends SysuiTestCase {

    private TextView mView;
    private String m12HoursFormat;
    private String m24HoursFormat;

    @Before
    public void setUp() throws Exception {
        m12HoursFormat = mContext.getString(R.string.keyguard_widget_12_hours_format);
        m24HoursFormat = mContext.getString(R.string.keyguard_widget_24_hours_format);

        mView = new TextView(mContext);
        mView.setText(m12HoursFormat);
        mView.setContentDescription(m12HoursFormat);
        mView.setAccessibilityDelegate(new KeyguardClockAccessibilityDelegate(mContext));
    }

    @Test
    public void onInitializeAccessibilityEvent_producesNonEmptyAsciiContentDesc() throws Exception {
        AccessibilityEvent ev = AccessibilityEvent.obtain();
        mView.onInitializeAccessibilityEvent(ev);

        assertFalse(TextUtils.isEmpty(ev.getContentDescription()));
        assertTrue(isAscii(ev.getContentDescription()));
    }

    @Test
    public void onPopulateAccessibilityEvent_producesNonEmptyAsciiText() throws Exception {
        AccessibilityEvent ev = AccessibilityEvent.obtain();
        mView.onPopulateAccessibilityEvent(ev);

        assertFalse(isEmpty(ev.getText()));
        assertTrue(isAscii(ev.getText()));
    }

    @Test
    public void onInitializeAccessibilityNodeInfo_producesNonEmptyAsciiText() throws Exception {
        AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain();
        // Usually done in View.onInitializeAccessibilityNodeInfoInternal, but only when attached.
        info.setContentDescription(mView.getContentDescription());
        mView.onInitializeAccessibilityNodeInfo(info);

        assertFalse(TextUtils.isEmpty(info.getText()));
        assertTrue(isAscii(info.getText()));

        assertFalse(TextUtils.isEmpty(info.getContentDescription()));
        assertTrue(isAscii(info.getContentDescription()));
    }

    @Test
    public void isNeeded_returnsTrueIfDateFormatsContainNonAscii() {
        if (!isAscii(m12HoursFormat) || !isAscii(m24HoursFormat)) {
            assertTrue(KeyguardClockAccessibilityDelegate.isNeeded(mContext));
        }
    }

    @Test
    public void isNeeded_returnsWhetherFancyColonExists() {
        boolean hasFancyColon = !TextUtils.isEmpty(mContext.getString(
                R.string.keyguard_fancy_colon));

        assertEquals(hasFancyColon, KeyguardClockAccessibilityDelegate.isNeeded(mContext));
    }

    private boolean isAscii(CharSequence text) {
        return text.chars().allMatch((i) -> i < 128);
    }

    private boolean isAscii(List<CharSequence> texts) {
        return texts.stream().allMatch(this::isAscii);
    }

    private boolean isEmpty(List<CharSequence> texts) {
        return texts.stream().allMatch(TextUtils::isEmpty);
    }
}
+1 −7
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ package com.android.keyguard;

import static org.junit.Assume.assumeFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -36,7 +35,6 @@ import com.android.systemui.keyguard.KeyguardSliceProvider;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.settings.FakeDisplayTracker;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.tuner.TunerService;

import org.junit.After;
import org.junit.Before;
@@ -53,8 +51,6 @@ public class KeyguardSliceViewControllerTest extends SysuiTestCase {
    @Mock
    private KeyguardSliceView mView;
    @Mock
    private TunerService mTunerService;
    @Mock
    private ConfigurationController mConfigurationController;
    @Mock
    private ActivityStarter mActivityStarter;
@@ -74,7 +70,7 @@ public class KeyguardSliceViewControllerTest extends SysuiTestCase {
        when(mView.isAttachedToWindow()).thenReturn(true);
        when(mView.getContext()).thenReturn(mContext);
        mController = new KeyguardSliceViewController(mHandler, mBgHandler, mView,
                mActivityStarter, mConfigurationController, mTunerService, mDumpManager,
                mActivityStarter, mConfigurationController, mDumpManager,
                mDisplayTracker);
        mController.setupUri(KeyguardSliceProvider.KEYGUARD_SLICE_URI);
    }
@@ -101,7 +97,6 @@ public class KeyguardSliceViewControllerTest extends SysuiTestCase {
        assumeFalse(isWatch());

        mController.init();
        verify(mTunerService).addTunable(any(TunerService.Tunable.class), anyString());
        verify(mConfigurationController).addCallback(
                any(ConfigurationController.ConfigurationListener.class));
    }
@@ -120,7 +115,6 @@ public class KeyguardSliceViewControllerTest extends SysuiTestCase {

        attachListenerArgumentCaptor.getValue().onViewDetachedFromWindow(mView);

        verify(mTunerService).removeTunable(any(TunerService.Tunable.class));
        verify(mConfigurationController).removeCallback(
                any(ConfigurationController.ConfigurationListener.class));
    }
+0 −44
Original line number Diff line number Diff line
package com.android.keyguard

import android.testing.TestableLooper.RunWithLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
@RunWithLooper(setAsMainLooper = true)
class KeyguardStatusAreaViewTest : SysuiTestCase() {

    private lateinit var view: KeyguardStatusAreaView

    @Before
    fun setUp() {
        view = KeyguardStatusAreaView(context)
    }

    @Test
    fun checkTranslationX_AddedTotals() {
        view.translateXFromClockDesign = 10f
        assertEquals(10f, view.translationX)

        view.translateXFromAod = 20f
        assertEquals(30f, view.translationX)

        view.translateXFromUnfold = 30f
        assertEquals(60f, view.translationX)
    }

    @Test
    fun checkTranslationY_AddedTotals() {
        view.translateYFromClockSize = 10f
        assertEquals(10f, view.translationY)

        view.translateYFromClockDesign = 20f
        assertEquals(30f, view.translationY)
    }
}
+0 −119
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.android.keyguard.logging.KeyguardLogger;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.keyguard.data.repository.FakeKeyguardRepository;
import com.android.systemui.keyguard.domain.interactor.KeyguardInteractorFactory;
import com.android.systemui.kosmos.KosmosJavaAdapter;
import com.android.systemui.power.data.repository.FakePowerRepository;
import com.android.systemui.power.domain.interactor.PowerInteractorFactory;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.notification.AnimatableProperty;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.statusbar.phone.ScreenOffAnimationController;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.statusbar.policy.KeyguardStateController;

import org.junit.Before;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class KeyguardStatusViewControllerBaseTest extends SysuiTestCase {

    private KosmosJavaAdapter mKosmos;
    @Mock protected KeyguardStatusView mKeyguardStatusView;

    @Mock protected KeyguardSliceViewController mKeyguardSliceViewController;
    @Mock protected KeyguardClockSwitchController mKeyguardClockSwitchController;
    @Mock protected KeyguardStateController mKeyguardStateController;
    @Mock protected KeyguardUpdateMonitor mKeyguardUpdateMonitor;
    @Mock protected ConfigurationController mConfigurationController;
    @Mock protected DozeParameters mDozeParameters;
    @Mock protected ScreenOffAnimationController mScreenOffAnimationController;
    @Mock protected KeyguardLogger mKeyguardLogger;
    @Mock protected KeyguardStatusViewController mControllerMock;
    @Mock protected ViewTreeObserver mViewTreeObserver;
    protected FakeKeyguardRepository mFakeKeyguardRepository;
    protected FakePowerRepository mFakePowerRepository;

    protected KeyguardStatusViewController mController;

    @Mock protected KeyguardClockSwitch mKeyguardClockSwitch;
    @Mock protected FrameLayout mMediaHostContainer;
    @Mock protected KeyguardStatusAreaView mKeyguardStatusAreaView;

    @Before
    public void setup() {
        mKosmos = new KosmosJavaAdapter(this);
        MockitoAnnotations.initMocks(this);

        KeyguardInteractorFactory.WithDependencies deps = KeyguardInteractorFactory.create();
        mFakeKeyguardRepository = deps.getRepository();
        mFakePowerRepository = new FakePowerRepository();

        mController = new KeyguardStatusViewController(
                mKeyguardStatusView,
                mKeyguardSliceViewController,
                mKeyguardClockSwitchController,
                mKeyguardStateController,
                mKeyguardUpdateMonitor,
                mConfigurationController,
                mDozeParameters,
                mScreenOffAnimationController,
                mKeyguardLogger,
                mKosmos.getInteractionJankMonitor(),
                deps.getKeyguardInteractor(),
                PowerInteractorFactory.create(
                        mFakePowerRepository
                ).getPowerInteractor()) {
                    @Override
                    void setProperty(
                            AnimatableProperty property,
                            float value,
                            boolean animate) {
                        // Route into the mock version for verification
                        mControllerMock.setProperty(property, value, animate);
                    }
                };

        when(mKeyguardStatusView.getViewTreeObserver()).thenReturn(mViewTreeObserver);
        when(mKeyguardClockSwitchController.getView()).thenReturn(mKeyguardClockSwitch);
        when(mKeyguardStatusView.findViewById(R.id.keyguard_status_area))
                .thenReturn(mKeyguardStatusAreaView);
    }

    protected void givenViewAttached() {
        ArgumentCaptor<View.OnAttachStateChangeListener> captor =
                ArgumentCaptor.forClass(View.OnAttachStateChangeListener.class);
        verify(mKeyguardStatusView, atLeast(1)).addOnAttachStateChangeListener(captor.capture());

        for (View.OnAttachStateChangeListener listener : captor.getAllValues()) {
            listener.onViewAttachedToWindow(mKeyguardStatusView);
        }
    }
}
+0 −69
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.power.shared.model.ScreenPowerState
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.clearInvocations
import org.mockito.Mockito.never
import org.mockito.Mockito.verify

@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@SmallTest
class KeyguardStatusViewControllerWithCoroutinesTest : KeyguardStatusViewControllerBaseTest() {

    @Test
    fun dozeTimeTickUpdatesSlices() = runTest {
        mController.startCoroutines(coroutineContext)
        givenViewAttached()
        runCurrent()
        clearInvocations(mKeyguardSliceViewController)

        mFakeKeyguardRepository.dozeTimeTick()
        runCurrent()
        verify(mKeyguardSliceViewController).refresh()

        coroutineContext.cancelChildren()
    }

    @Test
    fun onScreenTurningOnUpdatesSlices() = runTest {
        mController.startCoroutines(coroutineContext)
        givenViewAttached()
        runCurrent()
        clearInvocations(mKeyguardSliceViewController)

        mFakePowerRepository.setScreenPowerState(ScreenPowerState.SCREEN_ON)
        runCurrent()
        verify(mKeyguardSliceViewController, never()).refresh()

        // Should only be called during a 'turning on' event
        mFakePowerRepository.setScreenPowerState(ScreenPowerState.SCREEN_TURNING_ON)
        runCurrent()
        verify(mKeyguardSliceViewController).refresh()

        coroutineContext.cancelChildren()
    }
}
Loading