diff options
author | 2025-03-11 17:57:42 +0000 | |
---|---|---|
committer | 2025-03-19 18:41:50 +0000 | |
commit | 56eefc53ecc7fbe9e23abe3da1b64fac86d648f7 (patch) | |
tree | 75e27da8c0855cfb92b7d9f58db2e394d4dda6be | |
parent | 229e8bc07345c027a639992b3e6c89c20180217f (diff) |
Confirm UID being reported
When JankTracking is first enabled the JankTracker object is passed the
apps UID. When stats are being reported from non platform libraries we
need to confirm the UID in the reported AppJankStat matches what was
passed in when JankTracking was first enabled. This makes sure that all
metrics and frames are associated to the app correctly.
Bug: 383586335
Test: atest CoreAppJankTestCases
Flag: EXEMPT bugfix
Change-Id: I12c827bcfc06e4e4d3de08fce0b65e5c93574c47
3 files changed, 58 insertions, 3 deletions
diff --git a/core/java/android/app/jank/JankTracker.java b/core/java/android/app/jank/JankTracker.java index e3f67811757c..a085701b006a 100644 --- a/core/java/android/app/jank/JankTracker.java +++ b/core/java/android/app/jank/JankTracker.java @@ -143,6 +143,13 @@ public class JankTracker { * stats */ public void mergeAppJankStats(AppJankStats appJankStats) { + if (appJankStats.getUid() != mAppUid) { + if (DEBUG) { + Log.d(DEBUG_KEY, "Reported JankStats AppUID does not match AppUID of " + + "enclosing activity."); + } + return; + } getHandler().post(new Runnable() { @Override public void run() { diff --git a/tests/AppJankTest/src/android/app/jank/tests/IntegrationTests.java b/tests/AppJankTest/src/android/app/jank/tests/IntegrationTests.java index 229c7bfb53e9..a08b650b4c2f 100644 --- a/tests/AppJankTest/src/android/app/jank/tests/IntegrationTests.java +++ b/tests/AppJankTest/src/android/app/jank/tests/IntegrationTests.java @@ -222,4 +222,46 @@ public class IntegrationTests { assertTrue(jankTracker.shouldTrack()); } + + /* + When JankTracker is first instantiated it gets passed the apps UID the same UID should be + passed when reporting AppJankStats. To make sure frames and metrics are all associated with + the same app these UIDs need to match. This test confirms that mismatched IDs are not + counted. + */ + @Test + @RequiresFlagsEnabled(Flags.FLAG_DETAILED_APP_JANK_METRICS_API) + public void reportJankStats_statNotMerged_onMisMatchedAppIds() { + Activity jankTrackerActivity = mJankTrackerActivityRule.launchActivity(null); + mDevice.wait(Until.findObject( + By.text(jankTrackerActivity.getString(R.string.continue_test))), + WAIT_FOR_TIMEOUT_MS); + + EditText editText = jankTrackerActivity.findViewById(R.id.edit_text); + JankTracker jankTracker = editText.getJankTracker(); + + HashMap<String, JankDataProcessor.PendingJankStat> pendingStats = + jankTracker.getPendingJankStats(); + assertEquals(0, pendingStats.size()); + + int mismatchedAppUID = 25; + editText.reportAppJankStats(JankUtils.getAppJankStats(mismatchedAppUID)); + + // reportAppJankStats performs the work on a background thread, check periodically to see + // if the work is complete. + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(100); + if (jankTracker.getPendingJankStats().size() > 0) { + break; + } + } catch (InterruptedException exception) { + //do nothing and continue + } + } + + pendingStats = jankTracker.getPendingJankStats(); + + assertEquals(0, pendingStats.size()); + } } diff --git a/tests/AppJankTest/src/android/app/jank/tests/JankUtils.java b/tests/AppJankTest/src/android/app/jank/tests/JankUtils.java index 9640a84eb9ca..7067b873d4b7 100644 --- a/tests/AppJankTest/src/android/app/jank/tests/JankUtils.java +++ b/tests/AppJankTest/src/android/app/jank/tests/JankUtils.java @@ -18,16 +18,18 @@ package android.app.jank.tests; import android.app.jank.AppJankStats; import android.app.jank.RelativeFrameTimeHistogram; +import android.os.Process; + public class JankUtils { - private static final int APP_ID = 25; + private static final int APP_ID = Process.myUid(); /** * Returns a mock AppJankStats object to be used in tests. */ - public static AppJankStats getAppJankStats() { + public static AppJankStats getAppJankStats(int appUID) { AppJankStats jankStats = new AppJankStats( - /*App Uid*/APP_ID, + /*App Uid*/appUID, /*Widget Id*/"test widget id", /*navigationComponent*/null, /*Widget Category*/AppJankStats.WIDGET_CATEGORY_SCROLL, @@ -39,6 +41,10 @@ public class JankUtils { return jankStats; } + public static AppJankStats getAppJankStats() { + return getAppJankStats(APP_ID); + } + /** * Returns a mock histogram to be used with an AppJankStats object. */ |