1 /* 2 * Copyright (C) 2024 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.settings.datausage 18 19 import android.content.Context 20 import android.util.Range 21 import androidx.compose.ui.test.assertIsDisplayed 22 import androidx.compose.ui.test.hasTextExactly 23 import androidx.compose.ui.test.junit4.createComposeRule 24 import androidx.compose.ui.test.onNodeWithContentDescription 25 import androidx.test.core.app.ApplicationProvider 26 import androidx.test.ext.junit.runners.AndroidJUnit4 27 import com.android.settings.datausage.lib.NetworkUsageDetailsData 28 import org.junit.Rule 29 import org.junit.Test 30 import org.junit.runner.RunWith 31 32 @RunWith(AndroidJUnit4::class) 33 class AppDataUsageSummaryControllerTest { 34 35 @get:Rule 36 val composeTestRule = createComposeRule() 37 38 private val context: Context = ApplicationProvider.getApplicationContext() 39 40 private val controller = AppDataUsageSummaryController(context, TEST_KEY) 41 42 @Test summarynull43 fun summary() { 44 val appUsage = NetworkUsageDetailsData( 45 range = Range(1L, 2L), 46 totalUsage = BACKGROUND_BYTES + FOREGROUND_BYTES, 47 foregroundUsage = FOREGROUND_BYTES, 48 backgroundUsage = BACKGROUND_BYTES, 49 ) 50 51 controller.update(appUsage) 52 composeTestRule.setContent { 53 controller.Content() 54 } 55 56 composeTestRule.onNode(hasTextExactly("Total", "6.75 kB")).assertIsDisplayed() 57 composeTestRule.onNode(hasTextExactly("Foreground", "5.54 kB")).assertIsDisplayed() 58 composeTestRule.onNode(hasTextExactly("Background", "1.21 kB")).assertIsDisplayed() 59 } 60 61 @Test summary_smallBytenull62 fun summary_smallByte() { 63 val appUsage = NetworkUsageDetailsData( 64 range = Range(1L, 2L), 65 totalUsage = 3, 66 foregroundUsage = 1, 67 backgroundUsage = 2, 68 ) 69 70 controller.update(appUsage) 71 composeTestRule.setContent { 72 controller.Content() 73 } 74 75 composeTestRule.onNode(hasTextExactly("Total", "3 byte")).assertIsDisplayed() 76 composeTestRule.onNode(hasTextExactly("Foreground", "1 byte")).assertIsDisplayed() 77 composeTestRule.onNode(hasTextExactly("Background", "2 byte")).assertIsDisplayed() 78 } 79 80 private companion object { 81 const val TEST_KEY = "test_key" 82 const val BACKGROUND_BYTES = 1234L 83 const val FOREGROUND_BYTES = 5678L 84 } 85 } 86