1 /*
2  * Copyright (C) 2018 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 com.android.server.backup;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.os.Handler;
24 import android.platform.test.annotations.Presubmit;
25 import android.provider.Settings;
26 import android.util.KeyValueSettingObserver;
27 import com.android.server.testing.FrameworkRobolectricTestRunner;
28 import com.android.server.testing.SystemLoaderClasses;
29 import com.android.server.testing.SystemLoaderPackages;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RuntimeEnvironment;
35 import org.robolectric.annotation.Config;
36 
37 /** Tests for {@link BackupAgentTimeoutParameters}. */
38 @RunWith(FrameworkRobolectricTestRunner.class)
39 @Config(manifest = Config.NONE, sdk = 26)
40 @SystemLoaderPackages({"com.android.server.backup"})
41 @SystemLoaderClasses({KeyValueSettingObserver.class})
42 @Presubmit
43 public class BackupAgentTimeoutParametersTest {
44     private ContentResolver mContentResolver;
45     private BackupAgentTimeoutParameters mParameters;
46 
47     /** Initialize timeout parameters and start observing changes. */
48     @Before
setUp()49     public void setUp() {
50         Context context = RuntimeEnvironment.application.getApplicationContext();
51 
52         mContentResolver = context.getContentResolver();
53         mParameters = new BackupAgentTimeoutParameters(new Handler(), mContentResolver);
54         mParameters.start();
55     }
56 
57     /** Stop observing changes to the setting. */
58     @After
tearDown()59     public void tearDown() {
60         mParameters.stop();
61     }
62 
63     /** Tests that timeout parameters are initialized with default values on creation. */
64     @Test
testGetParameters_afterConstructorWithStart_returnsDefaultValues()65     public void testGetParameters_afterConstructorWithStart_returnsDefaultValues() {
66         long kvBackupAgentTimeoutMillis = mParameters.getKvBackupAgentTimeoutMillis();
67         long fullBackupAgentTimeoutMillis = mParameters.getFullBackupAgentTimeoutMillis();
68         long sharedBackupAgentTimeoutMillis = mParameters.getSharedBackupAgentTimeoutMillis();
69         long restoreAgentTimeoutMillis = mParameters.getRestoreAgentTimeoutMillis();
70         long restoreAgentFinishedTimeoutMillis = mParameters.getRestoreAgentFinishedTimeoutMillis();
71 
72         assertEquals(
73                 BackupAgentTimeoutParameters.DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS,
74                 kvBackupAgentTimeoutMillis);
75         assertEquals(
76                 BackupAgentTimeoutParameters.DEFAULT_FULL_BACKUP_AGENT_TIMEOUT_MILLIS,
77                 fullBackupAgentTimeoutMillis);
78         assertEquals(
79                 BackupAgentTimeoutParameters.DEFAULT_SHARED_BACKUP_AGENT_TIMEOUT_MILLIS,
80                 sharedBackupAgentTimeoutMillis);
81         assertEquals(
82                 BackupAgentTimeoutParameters.DEFAULT_RESTORE_AGENT_TIMEOUT_MILLIS,
83                 restoreAgentTimeoutMillis);
84         assertEquals(
85                 BackupAgentTimeoutParameters.DEFAULT_RESTORE_AGENT_FINISHED_TIMEOUT_MILLIS,
86                 restoreAgentFinishedTimeoutMillis);
87     }
88 
89     /**
90      * Tests that timeout parameters are updated when we call start, even when a setting change
91      * occurs while we are not observing.
92      */
93     @Test
testGetParameters_withSettingChangeBeforeStart_updatesValues()94     public void testGetParameters_withSettingChangeBeforeStart_updatesValues() {
95         mParameters.stop();
96         long testTimeout = BackupAgentTimeoutParameters.DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS * 2;
97         final String setting =
98                 BackupAgentTimeoutParameters.SETTING_KV_BACKUP_AGENT_TIMEOUT_MILLIS
99                         + "="
100                         + testTimeout;
101         putStringAndNotify(setting);
102         mParameters.start();
103 
104         long kvBackupAgentTimeoutMillis = mParameters.getKvBackupAgentTimeoutMillis();
105 
106         assertEquals(testTimeout, kvBackupAgentTimeoutMillis);
107     }
108 
109     /**
110      * Tests that timeout parameters are updated when a setting change occurs while we are observing
111      * changes.
112      */
113     @Test
testGetParameters_withSettingChangeAfterStart_updatesValues()114     public void testGetParameters_withSettingChangeAfterStart_updatesValues() {
115         long testTimeout = BackupAgentTimeoutParameters.DEFAULT_KV_BACKUP_AGENT_TIMEOUT_MILLIS * 2;
116         final String setting =
117                 BackupAgentTimeoutParameters.SETTING_KV_BACKUP_AGENT_TIMEOUT_MILLIS
118                         + "="
119                         + testTimeout;
120         putStringAndNotify(setting);
121 
122         long kvBackupAgentTimeoutMillis = mParameters.getKvBackupAgentTimeoutMillis();
123 
124         assertEquals(testTimeout, kvBackupAgentTimeoutMillis);
125     }
126 
127     /**
128      * Robolectric does not notify observers of changes to settings so we have to trigger it here.
129      * Currently, the mock of {@link Settings.Secure#putString(ContentResolver, String, String)}
130      * only stores the value. TODO: Implement properly in ShadowSettings.
131      */
putStringAndNotify(String value)132     private void putStringAndNotify(String value) {
133         Settings.Global.putString(mContentResolver, BackupAgentTimeoutParameters.SETTING, value);
134 
135         // We pass null as the observer since notifyChange iterates over all available observers and
136         // we don't have access to the local observer.
137         mContentResolver.notifyChange(
138                 Settings.Global.getUriFor(BackupAgentTimeoutParameters.SETTING), /*observer*/ null);
139     }
140 }
141