• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.internal;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static org.junit.Assert.assertThrows;
20 
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import androidx.test.core.app.ApplicationProvider;
24 import com.google.android.libraries.mobiledatadownload.SilentFeedback;
25 import com.google.android.libraries.mobiledatadownload.internal.Migrations.FileKeyVersion;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 
32 @RunWith(RobolectricTestRunner.class)
33 public class MigrationsTest {
34 
35   private Context context;
36   private SilentFeedback mockSilentFeedback;
37 
38   @Before
setUp()39   public void setUp() throws Exception {
40     context = ApplicationProvider.getApplicationContext();
41     mockSilentFeedback =
42         new SilentFeedback() {
43           @Override
44           public void send(Throwable throwable, String messageFormat, Object... args) {}
45         };
46   }
47 
48   @After
tearDown()49   public void tearDown() throws Exception {
50     Migrations.clear(context);
51   }
52 
53   @Test
testDefaultVersion()54   public void testDefaultVersion() {
55     // Make sure the default version is FileKeyVersion.NewFileKey
56     assertThat(Migrations.getCurrentVersion(context, mockSilentFeedback))
57         .isEqualTo(FileKeyVersion.NEW_FILE_KEY);
58   }
59 
60   @Test
testSetAndGetVersion()61   public void testSetAndGetVersion() {
62     Migrations.setCurrentVersion(context, FileKeyVersion.ADD_DOWNLOAD_TRANSFORM);
63     assertThat(Migrations.getCurrentVersion(context, mockSilentFeedback))
64         .isEqualTo(FileKeyVersion.ADD_DOWNLOAD_TRANSFORM);
65 
66     Migrations.setCurrentVersion(context, FileKeyVersion.NEW_FILE_KEY);
67     assertThat(Migrations.getCurrentVersion(context, mockSilentFeedback))
68         .isEqualTo(FileKeyVersion.NEW_FILE_KEY);
69 
70     Migrations.setCurrentVersion(context, FileKeyVersion.USE_CHECKSUM_ONLY);
71     assertThat(Migrations.getCurrentVersion(context, mockSilentFeedback))
72         .isEqualTo(FileKeyVersion.USE_CHECKSUM_ONLY);
73   }
74 
75   @Test
testMddFileKeyEnum()76   public void testMddFileKeyEnum() {
77     assertThat(FileKeyVersion.getVersion(0)).isEqualTo(FileKeyVersion.NEW_FILE_KEY);
78     assertThat(FileKeyVersion.getVersion(1)).isEqualTo(FileKeyVersion.ADD_DOWNLOAD_TRANSFORM);
79     assertThat(FileKeyVersion.getVersion(2)).isEqualTo(FileKeyVersion.USE_CHECKSUM_ONLY);
80     assertThrows(RuntimeException.class, () -> FileKeyVersion.getVersion(3));
81   }
82 
83   @Test
testCorruptedVersion()84   public void testCorruptedVersion() {
85     // Set invalid value to file key migration metadata.
86     SharedPreferences migrationPrefs =
87         context.getSharedPreferences("gms_icing_mdd_migrations", Context.MODE_PRIVATE);
88     migrationPrefs.edit().putInt("mdd_file_key_version", 100).commit();
89     // when(mockSilentFeedback.send(anyString(), an))
90     assertThat(Migrations.getCurrentVersion(context, mockSilentFeedback))
91         .isEqualTo(FileKeyVersion.USE_CHECKSUM_ONLY);
92   }
93 }
94