Add classloader for RtlCompatibleViewPager

To avoid BadParcelableException when reload the activity after it
is killed by OS.

Bug: 31707097
Test: make SettingsTests && adb install -r \
${OUT}/data/app/SettingsTests/SettingsTests.apk && \
adb shell am instrument -w \
'com.android.settings.tests/android.support.test.runner.AndroidJUnitRunner'

Change-Id: Ic8f20d9e2d236f0bea96071a88fbc6e7ba856f54
This commit is contained in:
jackqdyulei
2016-09-28 14:57:26 -07:00
parent eaec1622f5
commit 0bfee2799d
2 changed files with 93 additions and 5 deletions

View File

@@ -97,8 +97,8 @@ public final class RtlCompatibleViewPager extends ViewPager {
super(superState);
}
private RtlSavedState(Parcel in) {
super(in);
private RtlSavedState(Parcel in, ClassLoader loader) {
super(in, loader);
position = in.readInt();
}
@@ -108,11 +108,17 @@ public final class RtlCompatibleViewPager extends ViewPager {
out.writeInt(position);
}
public static final Parcelable.Creator<RtlSavedState> CREATOR
= new Parcelable.Creator<RtlSavedState>() {
public static final Parcelable.ClassLoaderCreator<RtlSavedState> CREATOR
= new Parcelable.ClassLoaderCreator<RtlSavedState>() {
@Override
public RtlSavedState createFromParcel(Parcel source,
ClassLoader loader) {
return new RtlSavedState(source, loader);
}
@Override
public RtlSavedState createFromParcel(Parcel in) {
return new RtlSavedState(in);
return new RtlSavedState(in, null);
}
@Override

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.tests;
import android.app.ActivityManagerNative;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class SettingsRestoreAfterCloseTest {
private static final String PACKAGE_SETTINGS = "com.android.settings";
private static final int TIME_OUT = 2000;
private boolean mAlwaysFinish;
@Before
public void setUp() throws Exception {
// To make sure when we press home button, the activity will be destroyed by OS
Context context = InstrumentationRegistry.getContext();
mAlwaysFinish = Settings.Global.getInt(
context.getContentResolver(), Settings.Global
.ALWAYS_FINISH_ACTIVITIES, 0)
!= 0;
ActivityManagerNative.getDefault().setAlwaysFinish(true);
}
@After
public void tearDown() throws Exception {
ActivityManagerNative.getDefault().setAlwaysFinish(mAlwaysFinish);
}
@Test
public void testRtlStability_AppCloseAndReOpen_shouldNotCrash() throws Exception {
final UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation
());
uiDevice.pressHome();
// Open the settings app
startSettingsMainActivity(uiDevice);
// Press home button
uiDevice.pressHome();
final String launcherPackage = uiDevice.getLauncherPackageName();
uiDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), TIME_OUT);
// Open the settings again
startSettingsMainActivity(uiDevice);
}
private void startSettingsMainActivity(UiDevice uiDevice) {
Context context = InstrumentationRegistry.getContext();
context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
uiDevice.wait(Until.hasObject(By.pkg(PACKAGE_SETTINGS).depth(0)), TIME_OUT);
}
}