• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.backup.cts;
18 
19 import android.app.GrammaticalInflectionManager;
20 import android.app.Instrumentation;
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.os.ParcelFileDescriptor;
24 
25 import androidx.test.InstrumentationRegistry;
26 
27 import com.android.compatibility.common.util.BackupUtils;
28 
29 import org.junit.Before;
30 
31 import java.io.IOException;
32 import java.io.InputStream;
33 
34 public class AppGrammaticalGenderBackupTest extends BaseBackupCtsTest {
35     private static final String RESTORE_TOKEN = "1";
36     private static final String SYSTEM_PACKAGE = "android";
37 
38     private final BackupUtils mBackupUtils =
39             new BackupUtils() {
40                 @Override
41                 protected InputStream executeShellCommand(String command) {
42                     return executeInstrumentationShellCommand(getInstrumentation(), command);
43                 }
44             };
45 
46     private GrammaticalInflectionManager mGrammaticalInflectionManager;
47 
48     @Before
setUp()49     public void setUp() {
50         final Context context = InstrumentationRegistry.getContext();
51         mGrammaticalInflectionManager = context.getSystemService(
52                 GrammaticalInflectionManager.class);
53     }
54 
55     /** Test installed app set gender before restoring */
testBackupRestore_setGenderBeforeRestoring_doesNotRestore()56     public void testBackupRestore_setGenderBeforeRestoring_doesNotRestore()
57             throws IOException {
58         if (!isBackupSupported()) {
59             return;
60         }
61         setAndBackupAppGender(Configuration.GRAMMATICAL_GENDER_NEUTRAL);
62         // fake the behavior that user set the gender before restoring
63         setGrammaticalGender(Configuration.GRAMMATICAL_GENDER_FEMININE);
64 
65         mBackupUtils.restoreAndAssertSuccess(RESTORE_TOKEN, SYSTEM_PACKAGE);
66 
67         // verify that device won't be restored
68         assertEquals(Configuration.GRAMMATICAL_GENDER_FEMININE,
69                 mGrammaticalInflectionManager.getApplicationGrammaticalGender());
70     }
71 
setGrammaticalGender(int gender)72     private void setGrammaticalGender(int gender) {
73         mGrammaticalInflectionManager.setRequestedApplicationGrammaticalGender(gender);
74     }
75 
setAndBackupAppGender(int gender)76     private void setAndBackupAppGender(int gender) throws IOException {
77         mGrammaticalInflectionManager.setRequestedApplicationGrammaticalGender(gender);
78         mBackupUtils.backupNowAndAssertSuccess(SYSTEM_PACKAGE);
79     }
80 
executeInstrumentationShellCommand( Instrumentation instrumentation, String command)81     private static InputStream executeInstrumentationShellCommand(
82             Instrumentation instrumentation, String command) {
83         final ParcelFileDescriptor pfd =
84                 instrumentation.getUiAutomation().executeShellCommand(command);
85         return new ParcelFileDescriptor.AutoCloseInputStream(pfd);
86     }
87 }
88