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         composeTestRule.onNodeWithContentDescription("6.75 kB").assertIsDisplayed()
60         composeTestRule.onNodeWithContentDescription("5.54 kB").assertIsDisplayed()
61         composeTestRule.onNodeWithContentDescription("1.21 kB").assertIsDisplayed()
62     }
63 
64     @Test
summary_zeronull65     fun summary_zero() {
66         val appUsage = NetworkUsageDetailsData(
67             range = Range(1L, 2L),
68             totalUsage = 3,
69             foregroundUsage = 1,
70             backgroundUsage = 2,
71         )
72 
73         controller.update(appUsage)
74         composeTestRule.setContent {
75             controller.Content()
76         }
77 
78         composeTestRule.onNode(hasTextExactly("Total", "3 B")).assertIsDisplayed()
79         composeTestRule.onNode(hasTextExactly("Foreground", "1 B")).assertIsDisplayed()
80         composeTestRule.onNode(hasTextExactly("Background", "2 B")).assertIsDisplayed()
81         composeTestRule.onNodeWithContentDescription("3 byte").assertIsDisplayed()
82         composeTestRule.onNodeWithContentDescription("1 byte").assertIsDisplayed()
83         composeTestRule.onNodeWithContentDescription("2 byte").assertIsDisplayed()
84     }
85 
86     private companion object {
87         const val TEST_KEY = "test_key"
88         const val BACKGROUND_BYTES = 1234L
89         const val FOREGROUND_BYTES = 5678L
90     }
91 }
92