Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TECH: fix tests on API 21 #480

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ interface HackPermissions {
*/
@Deprecated("Private API usage is forbidden when targeting API 30 and above")
fun grant(packageName: String, permission: String): Boolean

fun grantThroughAdb(packageName: String, permission: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kaspersky.kaspresso.device.permissions
import android.app.UiAutomation
import android.os.Process
import android.os.UserHandle
import com.kaspersky.kaspresso.device.server.AdbServer
import com.kaspersky.kaspresso.instrumental.InstrumentalDependencyProvider
import com.kaspersky.kaspresso.logger.UiTestLogger

Expand All @@ -12,6 +13,7 @@ import com.kaspersky.kaspresso.logger.UiTestLogger
class HackPermissionsImpl(
private val logger: UiTestLogger,
private val instrumentalDependencyProvider: InstrumentalDependencyProvider,
private val adbServer: AdbServer
) : HackPermissions {

private val uiAutomation: UiAutomation
Expand Down Expand Up @@ -39,4 +41,8 @@ class HackPermissionsImpl(
false
}
}

override fun grantThroughAdb(packageName: String, permission: String) {
adbServer.performShell("pm grant $packageName $permission")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ internal class SystemLanguage(
}
val attemptToGrantPermissionResult = hackPermissions.grant(context.packageName, Manifest.permission.CHANGE_CONFIGURATION)
if (!attemptToGrantPermissionResult) {
hackPermissions.grantThroughAdb(context.packageName, Manifest.permission.CHANGE_CONFIGURATION)
}

if (PackageManager.PERMISSION_GRANTED != context.checkPermission(Manifest.permission.CHANGE_CONFIGURATION, Process.myPid(), Process.myUid())) {
throw DocLocException(
"SystemLanguage: The attempt to grant Manifest.permission.CHANGE_CONFIGURATION for SystemLanguage failed"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,8 @@ data class Kaspresso(
)
if (!::hackPermissions.isInitialized) hackPermissions = HackPermissionsImpl(
libLogger,
instrumentalDependencyProviderFactory.getComponentProvider<HackPermissionsImpl>(instrumentation)
instrumentalDependencyProviderFactory.getComponentProvider<HackPermissionsImpl>(instrumentation),
adbServer
)
if (!::exploit.isInitialized) exploit = ExploitImpl(
libLogger,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.kaspersky.kaspressample.device_tests

import android.Manifest
import android.os.Build
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.rule.GrantPermissionRule
import com.kaspersky.kaspressample.MainActivity
import com.kaspersky.kaspressample.device_tests.DeviceAppSampleTest.Companion.TEST_APK_FILE_RELATIVE_PATH
import com.kaspersky.kaspresso.device.apps.Apps
import com.kaspersky.kaspresso.device.server.AdbServer
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Rule
import org.junit.Test

Expand Down Expand Up @@ -38,7 +41,10 @@ class DeviceAppSampleTest : TestCase() {

@Test
fun test() {
run {
before {
assumeTrue("Test APK signature fits only API >= 24", Build.VERSION.SDK_INT >= 24)
}.after {
}.run {
step("Install hello world apk") {
device.apps.install(TEST_APK_FILE_RELATIVE_PATH)
assertTrue(isAppInstalled(adbServer, TEST_APK_PACKAGE_NAME))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.kaspersky.kaspressample.device_tests

import android.Manifest
import android.os.Build
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.rule.GrantPermissionRule
import com.kaspersky.kaspressample.MainActivity
import com.kaspersky.kaspressample.screen.MainScreen
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import java.util.Locale
import org.junit.Rule
import org.junit.Test
import java.util.Locale

class DeviceLanguageSampleTest : TestCase() {

Expand All @@ -30,7 +31,11 @@ class DeviceLanguageSampleTest : TestCase() {
@Test
fun languageSampleTest() {
before {
default = device.targetContext.resources.configuration.locales[0]
default = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
device.targetContext.resources.configuration.locales[0]
} else {
device.targetContext.resources.configuration.locale
}
}.after {
device.language.switchInApp(default)
}.run {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kaspersky.kaspressample.device_tests

import android.Manifest
import android.content.Context
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
Expand Down Expand Up @@ -54,7 +55,7 @@ class DeviceLocationSampleTest : TestCase() {
fun locationSampleTest() {
before {
device.location.enableGps()
manager = device.targetContext.getSystemService(LocationManager::class.java)
manager = device.targetContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
}.after {
device.location.enableGps()
}.run {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kaspersky.kaspressample.device_tests

import androidx.test.ext.junit.rules.activityScenarioRule
import android.os.Build
import androidx.test.ext.junit.rules.activityScenarioRule
import com.kaspersky.kaspressample.MainActivity
import com.kaspersky.kaspresso.device.logcat.LogcatBufferSize
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
Expand All @@ -18,6 +18,11 @@ class DeviceLogcatSampleTest : TestCase() {

@Test
fun logcatTest() {
Assume.assumeTrue(
"Logcat can't connect to 'default' buffer on API < 24",
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
)

Assume.assumeTrue(
"Due to Android 8 bug 'logcat -c' fails. To run this test please use another device",
Build.VERSION.SDK_INT != Build.VERSION_CODES.O
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import android.provider.CallLog
import android.provider.Telephony
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.rule.GrantPermissionRule
import io.github.kakaocup.kakao.screen.Screen
import com.kaspersky.kaspressample.device.DeviceSampleActivity
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import com.kaspersky.kaspresso.testcases.core.testcontext.BaseTestContext
import io.github.kakaocup.kakao.screen.Screen
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Rule
Expand All @@ -19,7 +19,7 @@ class DevicePhoneSampleTest : TestCase() {
companion object {
private const val PHONE_NUMBER = "+79111111111"
private const val SMS_MESSAGE_TEXT = "Kaspresso"
private const val CONTENT_UPDATE_DELAY = 1_500L
private const val CONTENT_UPDATE_DELAY = 5_000L
private const val CALL_DURATION = 2_000L
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.kaspersky.kaspressample.docloc_tests

import android.os.Build
import androidx.test.ext.junit.rules.activityScenarioRule
import com.kaspersky.kaspressample.screen.SimpleScreen
import com.kaspersky.kaspressample.simple.SimpleActivity
import com.kaspersky.kaspresso.annotations.ScreenShooterTest
import com.kaspersky.kaspresso.testcases.api.testcase.DocLocScreenshotTestCase
import io.github.kakaocup.kakao.screen.Screen
import org.junit.Assume
import org.junit.Rule
import org.junit.Test

Expand All @@ -20,7 +22,13 @@ class ScreenshotSampleTest : DocLocScreenshotTestCase(locales = "en,ru") {

@ScreenShooterTest
@Test
fun test() = run {
fun test() = before {
Assume.assumeTrue(
"Due to the bug in the UiAutomation this test doesn't work and API <= 22",
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
)
}.after {
}.run {
step("1. Launch activity") {
activityRule.scenario.onActivity { // Test that screenshot shooter works in app's main thread
captureScreenshot("1. Simple screen")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class UiCommonFlakyTest : TestCase() {
// even UiAutomator is using under the hood =)
// the text is changing during 3 seconds
// the default value of flaky safety timeout = 10 seconds
hasText(device.targetContext.getString(R.string.common_flaky_final_button).uppercase())
hasText(device.targetContext.getString(R.string.common_flaky_final_button))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class KautomatorMeasureTest : TestCase(
RANGE.forEach { _ ->
button1 {
click()
hasText(device.targetContext.getString(R.string.measure_fragment_text_button_1).uppercase())
hasText(device.targetContext.getString(R.string.measure_fragment_text_button_1))
}
}
}
Expand All @@ -59,7 +59,7 @@ class KautomatorMeasureTest : TestCase(
RANGE.forEach { index ->
button2 {
click()
hasText(device.targetContext.getString(R.string.measure_fragment_text_button_2).uppercase())
hasText(device.targetContext.getString(R.string.measure_fragment_text_button_2))
}
textView {
hasText(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kaspersky.kaspresso.sanity.docloc

import android.os.Build
import androidx.test.ext.junit.rules.activityScenarioRule
import com.kaspersky.kaspressample.screen.SimpleScreen
import com.kaspersky.kaspressample.simple.SimpleActivity
Expand All @@ -10,9 +11,9 @@ import com.kaspersky.kaspresso.interceptors.watcher.testcase.impl.screenshot.Tes
import com.kaspersky.kaspresso.kaspresso.Kaspresso
import com.kaspersky.kaspresso.testcases.api.testcase.DocLocScreenshotTestCase
import io.github.kakaocup.kakao.screen.Screen
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Assume
import org.junit.Rule
import org.junit.Test
import java.io.File
Expand Down Expand Up @@ -43,18 +44,21 @@ class DocLocSanityTest : DocLocScreenshotTestCase(
testRootDirsProvider = resourcesRootDirsProvider
}
) {
@After
fun after() {
checkSanity()
// cleanUpScreenshotsDir()
}

@get:Rule
val activityRule = activityScenarioRule<SimpleActivity>()

@ScreenShooterTest
@Test
fun test() = run {
fun test() = before {
Assume.assumeTrue(
"Due to the bug in the UiAutomation this test doesn't work and API <= 22",
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
)
}.after {
checkSanity()
cleanUpScreenshotsDir()
}.run {
step("1. Launch activity") {
activityRule.scenario.onActivity { // Test that screenshot shooter works in app's main thread
captureScreenshot("1. Simple screen")
Expand Down
6 changes: 3 additions & 3 deletions samples/kaspresso-sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

<!-- Measure fragment screen -->
<string name="measure_fragment_title" translatable="false">Measure Fragment</string>
<string name="measure_fragment_text_button_1" translatable="false">Button 1</string>
<string name="measure_fragment_text_button_2" translatable="false">Button 2</string>
<string name="measure_fragment_text_button_1" translatable="false">BUTTON 1</string>
<string name="measure_fragment_text_button_2" translatable="false">BUTTON 2</string>
<string name="measure_fragment_text_textview" translatable="false">Button_2 clicks count = </string>
<string name="measure_fragment_text_edittext" translatable="false">Some text</string>
<string name="measure_fragment_text_checkbox" translatable="false">Checkbox</string>
Expand All @@ -53,7 +53,7 @@

<!-- Flakysafety sample test -->
<string name="common_flaky_final_textview" translatable="false">TextView</string>
<string name="common_flaky_final_button" translatable="false">Button</string>
<string name="common_flaky_final_button" translatable="false">BUTTON</string>
<string name="common_flaky_start_1" translatable="false">1</string>
<string name="common_flaky_start_2" translatable="false">2</string>
<string name="common_flaky_start_3" translatable="false">3</string>
Expand Down