1 /*
2  * Copyright 2022 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 platform.test.screenshot
18 
19 import android.content.Context
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.MediumTest
22 import androidx.test.platform.app.InstrumentationRegistry
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 
27 @RunWith(AndroidJUnit4::class)
28 @MediumTest
29 class GoldenPathManagerTest {
30 
31     @Test
goldenImageIdentifierResolver_appendsPngnull32     fun goldenImageIdentifierResolver_appendsPng() {
33         val subject = GoldenPathManager(InstrumentationRegistry.getInstrumentation().context)
34         // Test for resolving device local paths.
35         val localGoldenFullImagePath = subject.goldenImageIdentifierResolver(testName = "test1")
36         assertThat(localGoldenFullImagePath).endsWith("/test1.png")
37         assertThat(localGoldenFullImagePath.split("/").size).isEqualTo(2)
38     }
39 
40     @Test
goldenIdentifierResolver_includesPathConfignull41     fun goldenIdentifierResolver_includesPathConfig() {
42         val subject =
43             GoldenPathManager(
44                 InstrumentationRegistry.getInstrumentation().context,
45                 pathConfig = PathConfig(PathElementNoContext("something", true) { "mydevice" })
46             )
47         val pathSegments =
48             subject.goldenIdentifierResolver(testName = "test1", extension = "png").split("/")
49         assertThat(pathSegments).containsExactly("mydevice", "test1.png").inOrder()
50     }
51 
52     @Test
goldenIdentifierResolver_allowsOverrideFileExtensionnull53     fun goldenIdentifierResolver_allowsOverrideFileExtension() {
54         val context = InstrumentationRegistry.getInstrumentation().context
55         val subject = GoldenPathManager(context)
56         val result = subject.goldenIdentifierResolver(testName = "test1", extension = "proto")
57         assertThat(result).endsWith("/test1.proto")
58     }
59 
pathContextExtractornull60     private fun pathContextExtractor(context: Context): String {
61         return when {
62             (context.resources.displayMetrics.densityDpi.toString().length > 0) -> "context"
63             else -> "invalidcontext"
64         }
65     }
66 
pathNoContextExtractor1null67     private fun pathNoContextExtractor1() = "nocontext1"
68     private fun pathNoContextExtractor2() = "nocontext2"
69 
70     @Test
71     fun pathConfigTest() {
72         val pc =
73             PathConfig(
74                 PathElementNoContext("nocontext1", true, ::pathNoContextExtractor1),
75                 PathElementNoContext("nocontext2", true, ::pathNoContextExtractor2),
76                 PathElementWithContext("context1", true, ::pathContextExtractor),
77                 PathElementWithContext("context2", true, ::pathContextExtractor)
78             )
79         val context = InstrumentationRegistry.getInstrumentation().getContext()
80         val pcResolvedRelativePath = pc.resolveRelativePath(context)
81         assertThat(pcResolvedRelativePath).isEqualTo("nocontext1/nocontext2/context/context/")
82 
83         val pc2 = getSimplePathConfig()
84         val pcResolvedRelativePath2 = pc2.resolveRelativePath(context)
85         assertThat(pcResolvedRelativePath2).startsWith("cuttlefish")
86     }
87 
88     @Test
emulatedDevicePathConfigTestnull89     fun emulatedDevicePathConfigTest() {
90         val context = InstrumentationRegistry.getInstrumentation().context
91 
92         val pc1 =
93             getEmulatedDevicePathConfig(
94                 DeviceEmulationSpec(
95                     DisplaySpec("phone", width = 100, height = 200, densityDpi = 180),
96                     isDarkTheme = false,
97                     isLandscape = false,
98                 )
99             )
100         assertThat(pc1.resolveRelativePath(context)).isEqualTo("phone/light_portrait_")
101 
102         val pc2 =
103             getEmulatedDevicePathConfig(
104                 DeviceEmulationSpec(
105                     DisplaySpec("tablet", width = 100, height = 200, densityDpi = 180),
106                     isDarkTheme = true,
107                     isLandscape = true,
108                 )
109             )
110         assertThat(pc2.resolveRelativePath(context)).isEqualTo("tablet/dark_landscape_")
111     }
112 }
113