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

Commit b74b0533 authored by Shamali Patwa's avatar Shamali Patwa Committed by Android (Google) Code Review
Browse files

Merge "Fix the getWidgetsByPackageItem method to return a copy" into main

parents 7eaf2484 74862f99
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -89,7 +89,7 @@ public class WidgetsModel {
        if (!WIDGETS_ENABLED) {
            return Collections.emptyMap();
        }
        return mWidgetsByPackageItem;
        return new HashMap<>(mWidgetsByPackageItem);
    }

    /**
+29 −0
Original line number Diff line number Diff line
@@ -186,6 +186,35 @@ class WidgetsModelTest {
        assertThat(underTest.widgetsByComponentKey).isEmpty()
    }

    @Test
    fun getWidgetsByPackageItem_returnsACopyOfMap() {
        loadWidgets()

        val latch = CountDownLatch(1)
        Executors.MODEL_EXECUTOR.execute {
            var update = true

            // each "widgetsByPackageItem" read returns a different copy of the map held internally.
            // Modifying one shouldn't impact another.
            for ((_, _) in underTest.widgetsByPackageItem.entries) {
                underTest.widgetsByPackageItem.clear()
                if (update) { // trigger update
                    update = false
                    // Similarly, model could update its code independently while a client is
                    // iterating on the list.
                    underTest.update(app, /* packageUser= */ null)
                }
            }

            latch.countDown()
        }
        if (!latch.await(LOAD_WIDGETS_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
            fail("Timed out waiting for test")
        }

        // No exception
    }

    private fun loadWidgets() {
        val latch = CountDownLatch(1)
        Executors.MODEL_EXECUTOR.execute {