1 /*
2  * Copyright 2021 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  *      https://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.accompanist.sample.testharness
18 
19 import android.os.Bundle
20 import androidx.activity.ComponentActivity
21 import androidx.activity.compose.setContent
22 import androidx.compose.foundation.border
23 import androidx.compose.foundation.layout.Arrangement
24 import androidx.compose.foundation.layout.Column
25 import androidx.compose.foundation.layout.fillMaxWidth
26 import androidx.compose.foundation.layout.height
27 import androidx.compose.foundation.layout.padding
28 import androidx.compose.foundation.rememberScrollState
29 import androidx.compose.foundation.verticalScroll
30 import androidx.compose.material.Surface
31 import androidx.compose.material.Text
32 import androidx.compose.runtime.Composable
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.graphics.Color
35 import androidx.compose.ui.res.stringResource
36 import androidx.compose.ui.tooling.preview.Preview
37 import androidx.compose.ui.unit.DpSize
38 import androidx.compose.ui.unit.LayoutDirection
39 import androidx.compose.ui.unit.dp
40 import androidx.core.os.LocaleListCompat
41 import com.google.accompanist.sample.AccompanistSampleTheme
42 import com.google.accompanist.sample.R
43 import com.google.accompanist.testharness.TestHarness
44 import java.util.Locale
45 
46 /**
47  * A visual sample for the TestHarness Composable. Note that it should not be used in production.
48  */
49 class TestHarnessSample : ComponentActivity() {
onCreatenull50     override fun onCreate(savedInstanceState: Bundle?) {
51         super.onCreate(savedInstanceState)
52         setContent {
53             TestHarnessSampleScreen()
54         }
55     }
56 }
57 
58 @Preview
59 @Composable
TestHarnessSampleScreennull60 fun TestHarnessSampleScreen() {
61     Column(
62         modifier = Modifier
63             .padding(16.dp)
64             .verticalScroll(rememberScrollState()),
65         verticalArrangement = Arrangement.spacedBy(8.dp)
66     ) {
67         TestHarnessScreen()
68         TestHarness(size = DpSize(100.dp, 100.dp)) {
69             TestHarnessScreen("with a set size")
70         }
71         TestHarness(darkMode = true) {
72             TestHarnessScreen("with darkMode enabled")
73         }
74         TestHarness(fontScale = 2f) {
75             TestHarnessScreen("with a big font scale")
76         }
77         TestHarness(layoutDirection = LayoutDirection.Rtl) {
78             TestHarnessScreen("in RTL")
79         }
80         TestHarness(locales = LocaleListCompat.create(Locale("ar"))) {
81             TestHarnessScreen("in Arabic")
82         }
83     }
84 }
85 
86 @Preview
87 @Composable
TestHarnessScreennull88 fun TestHarnessScreen(text: String = "") {
89     AccompanistSampleTheme {
90         Surface(
91             modifier = Modifier
92                 .border(1.dp, Color.LightGray)
93                 .height(100.dp)
94                 .fillMaxWidth()
95         ) {
96             Text(
97                 stringResource(R.string.this_is_content, text),
98                 modifier = Modifier.padding(8.dp)
99             )
100         }
101     }
102 }
103