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

Commit 5329540c authored by Marcin Oczeretko's avatar Marcin Oczeretko
Browse files

Add WorkSource.getAttributionUid()

Bug: 119802232
Test: atest WorkSourceTest
Change-Id: Iafbe6d516b93315f58466385c2dc2031073d8573
parent c2888cbc
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -140,6 +140,21 @@ public class WorkSource implements Parcelable {
        return mUids[index];
    }

    /**
     * Return the UID to which this WorkSource should be attributed to, i.e, the UID that
     * initiated the work and not the UID performing it. If the WorkSource has no UIDs, returns -1
     * instead.
     *
     * @hide
     */
    public int getAttributionUid() {
        if (isEmpty()) {
            return -1;
        }

        return mNum > 0 ? mUids[0] : mChains.get(0).getAttributionUid();
    }

    /** @hide */
    @TestApi
    public String getName(int index) {
@@ -912,17 +927,18 @@ public class WorkSource implements Parcelable {

        /**
         * Return the UID to which this WorkChain should be attributed to, i.e, the UID that
         * initiated the work and not the UID performing it.
         * initiated the work and not the UID performing it. Returns -1 if the chain is empty.
         */
        public int getAttributionUid() {
            return mUids[0];
            return mSize > 0 ? mUids[0] : -1;
        }

        /**
         * Return the tag associated with the attribution UID. See (@link #getAttributionUid}.
         * Returns null if the chain is empty.
         */
        public String getAttributionTag() {
            return mTags[0];
            return mTags.length > 0 ? mTags[0] : null;
        }

        // TODO: The following three trivial getters are purely for testing and will be removed
+31 −6
Original line number Diff line number Diff line
@@ -341,12 +341,37 @@ public class WorkSourceTest extends TestCase {
    }

    public void testGetAttributionId() {
        WorkSource ws1 = new WorkSource();
        WorkChain wc = ws1.createWorkChain();
        wc.addNode(100, "tag");
        assertEquals(100, wc.getAttributionUid());
        wc.addNode(200, "tag2");
        assertEquals(100, wc.getAttributionUid());
        WorkSource ws = new WorkSource();
        WorkChain wc1 = ws.createWorkChain();
        wc1.addNode(100, "tag");
        assertEquals(100, wc1.getAttributionUid());
        assertEquals(100, ws.getAttributionUid());
        wc1.addNode(200, "tag2");
        assertEquals(100, wc1.getAttributionUid());
        assertEquals(100, ws.getAttributionUid());
        WorkChain wc2 = ws.createWorkChain();
        wc2.addNode(300, "tag3");
        assertEquals(300, wc2.getAttributionUid());
        assertEquals(100, ws.getAttributionUid());
    }

    public void testGetAttributionIdWithoutWorkChain() {
        WorkSource ws1 = new WorkSource(100);
        ws1.add(200);
        WorkSource ws2 = new WorkSource();
        ws2.add(100);
        ws2.add(200);
        assertEquals(100, ws1.getAttributionUid());
        assertEquals(100, ws2.getAttributionUid());
    }

    public void testGetAttributionWhenEmpty() {
        WorkSource ws = new WorkSource();
        assertEquals(-1, ws.getAttributionUid());
        WorkChain wc = ws.createWorkChain();
        assertEquals(-1, ws.getAttributionUid());
        assertEquals(-1, wc.getAttributionUid());
        assertNull(wc.getAttributionTag());
    }

    public void testGetAttributionTag() {