Robert Snoeberger | 06d2500 | 2019-06-13 16:19:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.keyguard.clock |
| 18 | |
| 19 | import android.testing.AndroidTestingRunner |
| 20 | import androidx.test.filters.SmallTest |
| 21 | import com.android.systemui.SysuiTestCase |
| 22 | import com.google.common.truth.Truth.assertThat |
| 23 | import org.json.JSONObject |
| 24 | import org.junit.Before |
| 25 | import org.junit.Test |
| 26 | import org.junit.runner.RunWith |
| 27 | import org.mockito.Mockito.mock |
| 28 | import org.mockito.Mockito.never |
| 29 | import org.mockito.Mockito.verify |
| 30 | |
| 31 | private const val PACKAGE = "com.android.keyguard.clock.Clock" |
| 32 | private const val CLOCK_FIELD = "clock" |
| 33 | private const val TIMESTAMP_FIELD = "_applied_timestamp" |
| 34 | private const val USER_ID = 0 |
| 35 | |
| 36 | @RunWith(AndroidTestingRunner::class) |
| 37 | @SmallTest |
| 38 | class SettingsWrapperTest : SysuiTestCase() { |
| 39 | |
| 40 | private lateinit var wrapper: SettingsWrapper |
| 41 | private lateinit var migration: SettingsWrapper.Migration |
| 42 | |
| 43 | @Before |
| 44 | fun setUp() { |
| 45 | migration = mock(SettingsWrapper.Migration::class.java) |
| 46 | wrapper = SettingsWrapper(getContext().contentResolver, migration) |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | fun testDecodeUnnecessary() { |
| 51 | // GIVEN a settings value that doesn't need to be decoded |
| 52 | val value = PACKAGE |
| 53 | // WHEN the value is decoded |
| 54 | val decoded = wrapper.decode(value, USER_ID) |
| 55 | // THEN the same value is returned, because decoding isn't necessary. |
| 56 | // TODO(b/135674383): Null should be returned when the migration code in removed. |
| 57 | assertThat(decoded).isEqualTo(value) |
| 58 | // AND the value is migrated to JSON format |
| 59 | verify(migration).migrate(value, USER_ID) |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | fun testDecodeJSON() { |
| 64 | // GIVEN a settings value that is encoded in JSON |
| 65 | val json: JSONObject = JSONObject() |
| 66 | json.put(CLOCK_FIELD, PACKAGE) |
| 67 | json.put(TIMESTAMP_FIELD, System.currentTimeMillis()) |
| 68 | val value = json.toString() |
| 69 | // WHEN the value is decoded |
| 70 | val decoded = wrapper.decode(value, USER_ID) |
| 71 | // THEN the clock field should have been extracted |
| 72 | assertThat(decoded).isEqualTo(PACKAGE) |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | fun testDecodeJSONWithoutClockField() { |
| 77 | // GIVEN a settings value that doesn't contain the CLOCK_FIELD |
| 78 | val json: JSONObject = JSONObject() |
| 79 | json.put(TIMESTAMP_FIELD, System.currentTimeMillis()) |
| 80 | val value = json.toString() |
| 81 | // WHEN the value is decoded |
| 82 | val decoded = wrapper.decode(value, USER_ID) |
| 83 | // THEN null is returned |
| 84 | assertThat(decoded).isNull() |
| 85 | // AND the value is not migrated to JSON format |
| 86 | verify(migration, never()).migrate(value, USER_ID) |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun testDecodeNullJSON() { |
| 91 | assertThat(wrapper.decode(null, USER_ID)).isNull() |
| 92 | } |
| 93 | } |