1 /* 2 * Copyright (C) 2023 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.google.android.wallpaper.weathereffects.provider 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import com.google.android.wallpaper.weathereffects.provider.WallpaperInfoContract.WallpaperGenerationData 21 import com.google.android.wallpaper.weathereffects.provider.WallpaperInfoContract.WeatherEffect 22 import com.google.common.truth.Truth.assertThat 23 import kotlinx.coroutines.ExperimentalCoroutinesApi 24 import kotlinx.coroutines.test.TestScope 25 import kotlinx.coroutines.test.UnconfinedTestDispatcher 26 import kotlinx.coroutines.test.runTest 27 import org.junit.Before 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 31 @OptIn(ExperimentalCoroutinesApi::class) 32 @RunWith(AndroidJUnit4::class) 33 class WeatherEffectsContentProviderTest { 34 private val testDispatcher = UnconfinedTestDispatcher() 35 private val testScope = TestScope(testDispatcher) 36 private lateinit var weatherEffectsContentProvider: WeatherEffectsContentProvider 37 38 @Before setupnull39 fun setup() { 40 weatherEffectsContentProvider = WeatherEffectsContentProvider() 41 weatherEffectsContentProvider.onCreate() 42 } 43 44 @Test query_updateWallpaper_returnsCorrectDatanull45 fun query_updateWallpaper_returnsCorrectData() { 46 testScope.runTest { 47 val expectedForegroundPath = "fake_directory/foreground.png" 48 val expectedBackgroundPath = "fake_directory/background.png" 49 val expectedWeatherEffect = WeatherEffect.SNOW.value 50 val uri = WallpaperInfoContract.getUpdateWallpaperUri() 51 .appendQueryParameter( 52 WallpaperInfoContract.FOREGROUND_TEXTURE_PARAM, expectedForegroundPath 53 ) 54 .appendQueryParameter( 55 WallpaperInfoContract.BACKGROUND_TEXTURE_PARAM, expectedBackgroundPath 56 ) 57 .appendQueryParameter( 58 WallpaperInfoContract.WEATHER_EFFECT_PARAM, expectedWeatherEffect 59 ) 60 .build() 61 62 val cursor = weatherEffectsContentProvider.query( 63 uri, 64 projection = null, 65 selection = null, 66 selectionArgs = null, 67 sortOrder = null 68 ) 69 70 assertThat(cursor.count).isEqualTo(1) 71 cursor.moveToFirst() 72 73 assertThat(cursor.getString( 74 cursor.getColumnIndex(WallpaperGenerationData.FOREGROUND_TEXTURE)) 75 ).isEqualTo(expectedForegroundPath) 76 assertThat(cursor.getString( 77 cursor.getColumnIndex(WallpaperGenerationData.BACKGROUND_TEXTURE)) 78 ).isEqualTo(expectedBackgroundPath) 79 assertThat(cursor.getString( 80 cursor.getColumnIndex(WallpaperGenerationData.WEATHER_EFFECT)) 81 ).isEqualTo(expectedWeatherEffect) 82 assertThat(cursor.columnNames).isEqualTo(WallpaperGenerationData.DEFAULT_PROJECTION) 83 } 84 } 85 86 @Test query_updateWallpaper_withNoParams_returnsCorrectDatanull87 fun query_updateWallpaper_withNoParams_returnsCorrectData() { 88 testScope.runTest { 89 val uri = WallpaperInfoContract.getUpdateWallpaperUri().build() 90 91 val cursor = weatherEffectsContentProvider.query( 92 uri, 93 projection = null, 94 selection = null, 95 selectionArgs = null, 96 sortOrder = null 97 ) 98 99 assertThat(cursor.count).isEqualTo(1) 100 cursor.moveToFirst() 101 102 assertThat(cursor.getString( 103 cursor.getColumnIndex(WallpaperGenerationData.FOREGROUND_TEXTURE)) 104 ).isNull() 105 assertThat(cursor.getString( 106 cursor.getColumnIndex(WallpaperGenerationData.BACKGROUND_TEXTURE)) 107 ).isNull() 108 assertThat(cursor.getString( 109 cursor.getColumnIndex(WallpaperGenerationData.WEATHER_EFFECT)) 110 ).isNull() 111 assertThat(cursor.columnNames).isEqualTo(WallpaperGenerationData.DEFAULT_PROJECTION) 112 } 113 } 114 }