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

Commit 37a6926b authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[SB][ComposeIcons] Add HotspotStatusInteractor" into main

parents 0f9957ff 1ec74cdd
Loading
Loading
Loading
Loading
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.policy.fakeHotspotController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class HotspotStatusInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()

    private val fakeController = kosmos.fakeHotspotController
    private val underTest = kosmos.hotspotStatusInteractor

    @Test
    fun isEnabled_initial_matchesFakeControllerDefaults() =
        kosmos.runTest {
            val enabled by collectLastValue(underTest.isEnabled)
            assertThat(enabled).isEqualTo(fakeController.isHotspotEnabled)
        }

    @Test
    fun isEnabled_updatesOnHotspotChanged() =
        kosmos.runTest {
            val state by collectLastValue(underTest.isEnabled)

            fakeController.isHotspotEnabled = true
            assertThat(state).isEqualTo(true)

            fakeController.isHotspotEnabled = false
            assertThat(state).isEqualTo(false)
        }
}
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.statusbar.policy.HotspotController
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn

/** Interactor responsible for providing hotspot status. */
@SysUISingleton
class HotspotStatusInteractor
@Inject
constructor(
    @Background private val bgDispatcher: CoroutineDispatcher,
    @Background private val scope: CoroutineScope,
    private val controller: HotspotController,
) {

    /** Flow emitting the current hotspot enabled status. */
    val isEnabled: StateFlow<Boolean> =
        conflatedCallbackFlow {
                val callback = HotspotController.Callback { enabled, _ -> trySend(enabled) }
                controller.addCallback(callback)
                awaitClose { controller.removeCallback(callback) }
            }
            .flowOn(bgDispatcher)
            .stateIn(
                scope = scope,
                started = SharingStarted.WhileSubscribed(),
                initialValue = controller.isHotspotEnabled(),
            )
}
+22 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture
import com.android.systemui.utils.leaks.FakeHotspotController

val Kosmos.fakeHotspotController by Fixture { FakeHotspotController(leakCheck) }
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.systemui.statusbar.policy.domain.interactor

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.statusbar.policy.fakeHotspotController

val Kosmos.hotspotStatusInteractor by
    Kosmos.Fixture {
        HotspotStatusInteractor(
            scope = testScope.backgroundScope,
            bgDispatcher = testDispatcher,
            controller = fakeHotspotController,
        )
    }
+36 −3
Original line number Diff line number Diff line
@@ -16,10 +16,17 @@ package com.android.systemui.utils.leaks;

import android.testing.LeakCheck;

import androidx.annotation.NonNull;

import com.android.systemui.statusbar.policy.HotspotController;
import com.android.systemui.statusbar.policy.HotspotController.Callback;

import java.util.ArrayList;
import java.util.List;

public class FakeHotspotController extends BaseLeakChecker<Callback> implements HotspotController {
    private boolean mHotspotEnabled = false;
    private final List<Callback> mCallbacks = new ArrayList<>();

    public FakeHotspotController(LeakCheck test) {
        super(test, "hotspot");
@@ -27,7 +34,7 @@ public class FakeHotspotController extends BaseLeakChecker<Callback> implements

    @Override
    public boolean isHotspotEnabled() {
        return false;
        return mHotspotEnabled;
    }

    @Override
@@ -37,7 +44,10 @@ public class FakeHotspotController extends BaseLeakChecker<Callback> implements

    @Override
    public void setHotspotEnabled(boolean enabled) {

        if (mHotspotEnabled != enabled) {
            mHotspotEnabled = enabled;
            fireHotspotEnabledChanged(mHotspotEnabled);
        }
    }

    @Override
@@ -49,4 +59,27 @@ public class FakeHotspotController extends BaseLeakChecker<Callback> implements
    public int getNumConnectedDevices() {
        return 0;
    }

    @Override
    public void addCallback(@NonNull HotspotController.Callback listener) {
        if (mCallbacks.contains(listener)) {
            return;
        }
        mCallbacks.add(listener);

        listener.onHotspotChanged(mHotspotEnabled, /* numDevices= */0);
    }

    @Override
    public void removeCallback(@NonNull HotspotController.Callback listener) {
        mCallbacks.remove(listener);
    }

    private void fireHotspotEnabledChanged(boolean enabled) {
        // Iterate over a copy in case of concurrent modification or reentrancy.
        for (HotspotController.Callback callback : new ArrayList<>(mCallbacks)) {
            callback.onHotspotChanged(enabled, /* numDevices= */ 0);
        }
    }

}
 No newline at end of file