Make sure uibench test app load list content
If the test app is relaunched by configuration change (e.g. the
testing device is put in landscape), the fragment will be kept
but its content needs to reload. Otherwise it will show a loading
animation that causes unexpected frame count.
This change makes ShadowGridActivity, ClippedListActivity,
MainActivity inherit CompatListActivity that sets new list adapter
to the existing fragment. So the list can show correct content.
Bug: 156473870
Test: atest UiBenchJankTests
Change-Id: Ie608baa7362642554c014f805c797d09f4cf3117
diff --git a/tests/UiBench/src/com/android/test/uibench/ClippedListActivity.java b/tests/UiBench/src/com/android/test/uibench/ClippedListActivity.java
index 2bf6040..e21dec3 100644
--- a/tests/UiBench/src/com/android/test/uibench/ClippedListActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/ClippedListActivity.java
@@ -15,27 +15,24 @@
*/
package com.android.test.uibench;
-import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import androidx.appcompat.app.ActionBarDrawerToggle;
-import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
-import androidx.fragment.app.FragmentManager;
-import androidx.fragment.app.ListFragment;
+
+import com.android.test.uibench.listview.CompatListActivity;
import com.google.android.material.navigation.NavigationView;
-public class ClippedListActivity extends AppCompatActivity
+public class ClippedListActivity extends CompatListActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
+ protected void initializeActivity() {
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@@ -48,15 +45,17 @@
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
+ }
- FragmentManager fm = getSupportFragmentManager();
- if (fm.findFragmentById(android.R.id.content) == null) {
- ListFragment listFragment = new ListFragment();
- ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
- TextUtils.buildSimpleStringList(40));
- listFragment.setListAdapter(adapter);
- fm.beginTransaction().add(R.id.app_bar_layout, listFragment).commit();
- }
+ @Override
+ protected ListAdapter createListAdapter() {
+ return new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
+ TextUtils.buildSimpleStringList(40));
+ }
+
+ @Override
+ protected int getListFragmentContainerViewId() {
+ return R.id.app_bar_layout;
}
@Override
diff --git a/tests/UiBench/src/com/android/test/uibench/MainActivity.java b/tests/UiBench/src/com/android/test/uibench/MainActivity.java
index 0a7aa42..77a6c32 100644
--- a/tests/UiBench/src/com/android/test/uibench/MainActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/MainActivity.java
@@ -19,13 +19,15 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
-import androidx.fragment.app.FragmentManager;
-import androidx.fragment.app.ListFragment;
-import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
+import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
+import androidx.fragment.app.ListFragment;
+
+import com.android.test.uibench.listview.CompatListActivity;
+
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
@@ -34,10 +36,12 @@
import java.util.List;
import java.util.Map;
-public class MainActivity extends AppCompatActivity {
+public class MainActivity extends CompatListActivity {
private static final String EXTRA_PATH = "activity_path";
private static final String CATEGORY_HWUI_TEST = "com.android.test.uibench.TEST";
+ private String mActivityPath = "";
+
public static class TestListFragment extends ListFragment {
@Override
@SuppressWarnings("unchecked")
@@ -56,9 +60,7 @@
}
@Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
+ protected void initializeActivity() {
Intent intent = getIntent();
String path = intent.getStringExtra(EXTRA_PATH);
@@ -68,15 +70,19 @@
// not root level, display where we are in the hierarchy
setTitle(path);
}
+ mActivityPath = path;
+ }
- FragmentManager fm = getSupportFragmentManager();
- if (fm.findFragmentById(android.R.id.content) == null) {
- ListFragment listFragment = new TestListFragment();
- listFragment.setListAdapter(new SimpleAdapter(this, getData(path),
- android.R.layout.simple_list_item_1, new String[] { "title" },
- new int[] { android.R.id.text1 }));
- fm.beginTransaction().add(android.R.id.content, listFragment).commit();
- }
+ @Override
+ protected ListAdapter createListAdapter() {
+ return new SimpleAdapter(this, getData(mActivityPath),
+ android.R.layout.simple_list_item_1, new String[] { "title" },
+ new int[] { android.R.id.text1 });
+ }
+
+ @Override
+ protected ListFragment createListFragment() {
+ return new TestListFragment();
}
protected List<Map<String, Object>> getData(String prefix) {
diff --git a/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java b/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
index af7c65a..d6e1d06 100644
--- a/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
@@ -16,13 +16,15 @@
package com.android.test.uibench;
import android.os.Bundle;
-import androidx.fragment.app.FragmentManager;
-import androidx.fragment.app.ListFragment;
-import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
+import android.widget.ListAdapter;
-public class ShadowGridActivity extends AppCompatActivity {
+import androidx.fragment.app.ListFragment;
+
+import com.android.test.uibench.listview.CompatListActivity;
+
+public class ShadowGridActivity extends CompatListActivity {
public static class NoDividerListFragment extends ListFragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
@@ -31,18 +33,14 @@
}
};
+ @Override
+ protected ListAdapter createListAdapter() {
+ return new ArrayAdapter<>(this, R.layout.card_row, R.id.card_text,
+ TextUtils.buildSimpleStringList());
+ }
@Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- FragmentManager fm = getSupportFragmentManager();
- if (fm.findFragmentById(android.R.id.content) == null) {
- ListFragment listFragment = new NoDividerListFragment();
-
- listFragment.setListAdapter(new ArrayAdapter<>(this,
- R.layout.card_row, R.id.card_text, TextUtils.buildSimpleStringList()));
- fm.beginTransaction().add(android.R.id.content, listFragment).commit();
- }
+ protected ListFragment createListFragment() {
+ return new NoDividerListFragment();
}
}
diff --git a/tests/UiBench/src/com/android/test/uibench/listview/CompatListActivity.java b/tests/UiBench/src/com/android/test/uibench/listview/CompatListActivity.java
index 6659558..9a4b527 100644
--- a/tests/UiBench/src/com/android/test/uibench/listview/CompatListActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/listview/CompatListActivity.java
@@ -16,22 +16,29 @@
package com.android.test.uibench.listview;
import android.os.Bundle;
+import android.widget.ListAdapter;
+
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.ListFragment;
-import androidx.appcompat.app.AppCompatActivity;
-import android.widget.ListAdapter;
public abstract class CompatListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ initializeActivity();
+ int containerViewId = getListFragmentContainerViewId();
FragmentManager fm = getSupportFragmentManager();
- if (fm.findFragmentById(android.R.id.content) == null) {
+ Fragment fragment = fm.findFragmentById(containerViewId);
+ if (fragment == null) {
ListFragment listFragment = createListFragment();
listFragment.setListAdapter(createListAdapter());
- fm.beginTransaction().add(android.R.id.content, listFragment).commit();
+ fm.beginTransaction().add(containerViewId, listFragment).commit();
+ } else if (fragment instanceof ListFragment) {
+ ((ListFragment) fragment).setListAdapter(createListAdapter());
}
}
@@ -40,4 +47,11 @@
protected ListFragment createListFragment() {
return new ListFragment();
}
+
+ protected int getListFragmentContainerViewId() {
+ return android.R.id.content;
+ }
+
+ protected void initializeActivity() {
+ }
}
diff --git a/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java b/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java
index bd313ad..2f0d6ab 100644
--- a/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java
@@ -17,16 +17,17 @@
import android.content.Context;
import android.os.Bundle;
-import androidx.annotation.Nullable;
-import androidx.fragment.app.Fragment;
-import androidx.fragment.app.FragmentManager;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.recyclerview.widget.LinearLayoutManager;
-import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.fragment.app.Fragment;
+import androidx.fragment.app.FragmentManager;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+
import com.android.test.uibench.R;
public abstract class RvCompatListActivity extends AppCompatActivity {
@@ -51,14 +52,21 @@
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
- if (fm.findFragmentById(android.R.id.content) == null) {
+ Fragment existingFragment = fm.findFragmentById(android.R.id.content);
+ if (existingFragment == null) {
RecyclerViewFragment fragment = new RecyclerViewFragment();
- fragment.layoutManager = createLayoutManager(this);
- fragment.adapter = createAdapter();
+ initializeRecyclerViewFragment(fragment);
fm.beginTransaction().add(android.R.id.content, fragment).commit();
+ } else if (existingFragment instanceof RecyclerViewFragment) {
+ initializeRecyclerViewFragment((RecyclerViewFragment) existingFragment);
}
}
+ private void initializeRecyclerViewFragment(RecyclerViewFragment fragment) {
+ fragment.layoutManager = createLayoutManager(this);
+ fragment.adapter = createAdapter();
+ }
+
protected RecyclerView.LayoutManager createLayoutManager(Context context) {
return new LinearLayoutManager(context);
}