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

Commit 0a2f267f authored by Junyu Lai's avatar Junyu Lai
Browse files

[SP26.1] Add assert function for parceling NetworkStats

In ParcelUtils there are methods that help to verify parceling
correctness. However, they can only be applied to objects which
implement equals.

For NetworkStats, equals are not overrided due to performace
might be impacted if everyone use it. Hence specfic function
to verify parceling correectness is needed.

Test: atest NetworkStatsApiTest NetworkStackTests
Bug: 150644692
Change-Id: I48aa73117bc7db981ffaef5365f96e46d431bd6e
Merged-In: I48aa73117bc7db981ffaef5365f96e46d431bd6e
parent 74d48f72
Loading
Loading
Loading
Loading
+22 −6
Original line number Original line Diff line number Diff line
@@ -29,16 +29,24 @@ fun orderInsensitiveEquals(
    if (compareTime && leftStats.getElapsedRealtime() != rightStats.getElapsedRealtime()) {
    if (compareTime && leftStats.getElapsedRealtime() != rightStats.getElapsedRealtime()) {
        return false
        return false
    }
    }
    if (leftStats.size() != rightStats.size()) return false

    // While operations such as add/subtract will preserve empty entries. This will make
    // the result be hard to verify during test. Remove them before comparing since they
    // are not really affect correctness.
    // TODO (b/152827872): Remove empty entries after addition/subtraction.
    val leftTrimmedEmpty = leftStats.removeEmptyEntries()
    val rightTrimmedEmpty = rightStats.removeEmptyEntries()

    if (leftTrimmedEmpty.size() != rightTrimmedEmpty.size()) return false
    val left = NetworkStats.Entry()
    val left = NetworkStats.Entry()
    val right = NetworkStats.Entry()
    val right = NetworkStats.Entry()
    // Order insensitive compare.
    // Order insensitive compare.
    for (i in 0 until leftStats.size()) {
    for (i in 0 until leftTrimmedEmpty.size()) {
        leftStats.getValues(i, left)
        leftTrimmedEmpty.getValues(i, left)
        val j: Int = rightStats.findIndexHinted(left.iface, left.uid, left.set, left.tag,
        val j: Int = rightTrimmedEmpty.findIndexHinted(left.iface, left.uid, left.set, left.tag,
                left.metered, left.roaming, left.defaultNetwork, i)
                left.metered, left.roaming, left.defaultNetwork, i)
        if (j == -1) return false
        if (j == -1) return false
        rightStats.getValues(j, right)
        rightTrimmedEmpty.getValues(j, right)
        if (left != right) return false
        if (left != right) return false
    }
    }
    return true
    return true
@@ -60,3 +68,11 @@ fun assertNetworkStatsEquals(
    assertTrue(orderInsensitiveEquals(expected, actual, compareTime),
    assertTrue(orderInsensitiveEquals(expected, actual, compareTime),
            "expected: " + expected + " but was: " + actual)
            "expected: " + expected + " but was: " + actual)
}
}

/**
 * Assert that after being parceled then unparceled, {@link NetworkStats} is equal to the original
 * object.
 */
fun assertParcelingIsLossless(stats: NetworkStats) {
    assertParcelingIsLossless(stats, { a, b -> orderInsensitiveEquals(a, b) })
}
+17 −7
Original line number Original line Diff line number Diff line
@@ -18,7 +18,7 @@ package com.android.testutils


import android.os.Parcel
import android.os.Parcel
import android.os.Parcelable
import android.os.Parcelable
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
import kotlin.test.fail


/**
/**
@@ -46,13 +46,23 @@ fun <T: Parcelable> parcelingRoundTrip(source: T): T {


/**
/**
 * Assert that after being parceled then unparceled, `source` is equal to the original
 * Assert that after being parceled then unparceled, `source` is equal to the original
 * object.
 * object. If a customized equals function is provided, uses the provided one.
 */
 */
fun <T: Parcelable> assertParcelingIsLossless(source: T) {
@JvmOverloads
    assertEquals(source, parcelingRoundTrip(source))
fun <T : Parcelable> assertParcelingIsLossless(
    source: T,
    equals: (T, T) -> Boolean = { a, b -> a == b }
) {
    val actual = parcelingRoundTrip(source)
    assertTrue(equals(source, actual), "Expected $source, but was $actual")
}
}


fun <T: Parcelable> assertParcelSane(obj: T, fieldCount: Int) {
@JvmOverloads
fun <T : Parcelable> assertParcelSane(
    obj: T,
    fieldCount: Int,
    equals: (T, T) -> Boolean = { a, b -> a == b }
) {
    assertFieldCountEquals(fieldCount, obj::class.java)
    assertFieldCountEquals(fieldCount, obj::class.java)
    assertParcelingIsLossless(obj)
    assertParcelingIsLossless(obj, equals)
}
}