Skip to content

Commit 15651ac

Browse files
author
Leo Neat
authored
Merge pull request #19 from Leo-Neat/config-test-suite
[Infra] Add config options
2 parents 0bbad7b + e01c76a commit 15651ac

File tree

6 files changed

+331
-35
lines changed

6 files changed

+331
-35
lines changed

mediacontroller/src/main/java/com/example/android/mediacontroller/MediaAppTestSuite.kt

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ package com.example.android.mediacontroller
1717

1818
import android.app.Dialog
1919
import android.content.Context
20+
import android.content.SharedPreferences
2021
import android.graphics.Color
2122
import android.os.Handler
2223
import android.os.Looper
23-
import android.util.DisplayMetrics
2424
import android.util.Log
2525
import android.view.LayoutInflater
2626
import android.view.View
2727
import android.view.ViewGroup
2828
import android.view.Window
29-
import android.view.WindowManager
3029
import android.widget.Button
3130
import android.widget.LinearLayout
3231
import android.widget.ScrollView
@@ -45,7 +44,9 @@ import java.util.concurrent.Semaphore
4544
import kotlin.concurrent.thread
4645

4746

48-
class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, testList: Array<TestOptionDetails>, private val testSuiteResultsLayout: RecyclerView, context: Context){
47+
class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, testList:
48+
Array<TestOptionDetails>, private val testSuiteResultsLayout: RecyclerView, context: Context) {
49+
4950
val name = testSuiteName
5051
val description = testSuiteDescription
5152
private val singleSuiteTestList = testList
@@ -58,19 +59,14 @@ class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, tes
5859
private var suiteRunning = false
5960
private var screenHeight = 0
6061
private val SLEEP_TIME = 1000L
62+
private val sharedPreferences: SharedPreferences
6163

6264
init {
6365
for (i in testList.indices) {
6466
iDToPositionMap[testList[i].id] = i
6567
}
6668
context.applicationContext
67-
68-
// Get screen height for dialog display purposes.
69-
val displayMetrics = DisplayMetrics()
70-
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
71-
val display = windowManager.defaultDisplay
72-
display.getMetrics(displayMetrics)
73-
screenHeight = displayMetrics.heightPixels
69+
sharedPreferences = context.getSharedPreferences(MediaAppTestingActivity.SHARED_PREF_KEY_SUITE_CONFIG, Context.MODE_PRIVATE)
7470
}
7571

7672
val callback = { result: TestResult, testId: Int, testLogs: ArrayList<String> ->
@@ -82,6 +78,16 @@ class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, tes
8278
testSemaphore.release()
8379
}
8480

81+
fun getConfigurableTests(): ArrayList<TestOptionDetails> {
82+
var configTests = ArrayList<TestOptionDetails>()
83+
for (test in singleSuiteTestList) {
84+
if (test.queryRequired) {
85+
configTests.add(test)
86+
}
87+
}
88+
return configTests
89+
}
90+
8591
fun suiteIsRunning(): Boolean {
8692
return suiteRunning
8793
}
@@ -96,16 +102,19 @@ class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, tes
96102
Thread.sleep(SLEEP_TIME)
97103

98104
// In the event that a query is not specified, don't run the test.
99-
if (test.queryRequired) {
105+
var query = sharedPreferences.getString(test.name, MediaAppTestingActivity.NO_CONFIG)
106+
if (test.queryRequired && query == MediaAppTestingActivity.NO_CONFIG) {
100107
test.testResult = TestResult.CONFIG_REQUIRED
101108
val index = iDToPositionMap[test.id]
102109
mHandler.post { resultsAdapter.notifyItemChanged(index!!) }
103110
continue
104111
}
105112

106-
// For tests that don't require queries, run normally.
113+
if (query == MediaAppTestingActivity.NO_CONFIG){
114+
query = ""
115+
}
107116
testSemaphore.acquire()
108-
test.runTest("", callback, test.id)
117+
test.runTest(query, callback, test.id)
109118
}
110119
suiteRunning = false
111120
}
@@ -182,7 +191,6 @@ class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, tes
182191
inner class OnResultsClickedListener(private val testDetails: TestOptionDetails, val context: Context) : View.OnClickListener {
183192

184193
override fun onClick(p0: View?) {
185-
186194
var dialog = Dialog(context)
187195
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
188196
dialog.setContentView(R.layout.test_suite_results_dialog)
@@ -201,7 +209,7 @@ class MediaAppTestSuite(testSuiteName: String, testSuiteDescription: String, tes
201209
}
202210
val close_button = dialog.findViewById(R.id.close_results_button) as Button
203211
val results_scroll_view = dialog.findViewById(R.id.results_scroll_view) as ScrollView
204-
results_scroll_view.layoutParams.height = (screenHeight / 2).toInt()
212+
results_scroll_view.layoutParams.height = (MediaAppTestingActivity.getScreenHeightPx(context) / 2).toInt()
205213
close_button.setOnClickListener(View.OnClickListener { dialog.dismiss() })
206214
dialog.show()
207215
}

mediacontroller/src/main/java/com/example/android/mediacontroller/MediaAppTestingActivity.kt

Lines changed: 148 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.example.android.mediacontroller
1717

1818
import android.app.Activity
19+
import android.app.Dialog
20+
import android.content.Context
1921
import android.content.Intent
2022
import android.graphics.Bitmap
2123
import android.graphics.Color
@@ -28,12 +30,17 @@ import android.support.v4.media.MediaMetadataCompat
2830
import android.support.v4.media.session.MediaControllerCompat
2931
import android.support.v4.media.session.MediaSessionCompat
3032
import android.support.v4.media.session.PlaybackStateCompat
33+
import android.text.Editable
34+
import android.text.TextWatcher
35+
import android.util.DisplayMetrics
3136
import android.util.Log
3237
import android.view.View
3338
import android.view.ViewGroup
3439
import android.view.MenuItem
3540
import android.view.Menu
3641
import android.view.LayoutInflater
42+
import android.view.Window
43+
import android.view.WindowManager
3744
import android.widget.EditText
3845
import android.widget.LinearLayout
3946
import android.widget.TextView
@@ -48,13 +55,45 @@ import androidx.recyclerview.widget.LinearLayoutManager
4855
import androidx.recyclerview.widget.RecyclerView
4956
import androidx.viewpager.widget.PagerAdapter
5057
import androidx.viewpager.widget.ViewPager
58+
5159
import com.google.android.material.bottomnavigation.BottomNavigationView
52-
import kotlinx.android.synthetic.main.activity_media_app_testing.*
53-
import kotlinx.android.synthetic.main.media_controller_info.*
54-
import kotlinx.android.synthetic.main.media_queue_item.view.*
55-
import kotlinx.android.synthetic.main.media_test_option.view.*
56-
import kotlinx.android.synthetic.main.media_test_suites.*
57-
import kotlinx.android.synthetic.main.media_tests.*
60+
import kotlinx.android.synthetic.main.activity_media_app_testing.media_controller_test_page
61+
import kotlinx.android.synthetic.main.activity_media_app_testing.media_controller_test_suite_page
62+
import kotlinx.android.synthetic.main.activity_media_app_testing.media_controller_info_page
63+
import kotlinx.android.synthetic.main.activity_media_app_testing.toolbar
64+
import kotlinx.android.synthetic.main.activity_media_app_testing.view_pager
65+
import kotlinx.android.synthetic.main.config_item.view.test_name_config
66+
import kotlinx.android.synthetic.main.config_item.view.test_query_config
67+
import kotlinx.android.synthetic.main.media_controller_info.queue_item_list
68+
import kotlinx.android.synthetic.main.media_controller_info.connection_error_text
69+
import kotlinx.android.synthetic.main.media_controller_info.metadata_text
70+
import kotlinx.android.synthetic.main.media_controller_info.playback_state_text
71+
import kotlinx.android.synthetic.main.media_controller_info.queue_text
72+
import kotlinx.android.synthetic.main.media_controller_info.repeat_mode_text
73+
import kotlinx.android.synthetic.main.media_controller_info.queue_title_text
74+
import kotlinx.android.synthetic.main.media_controller_info.shuffle_mode_text
75+
import kotlinx.android.synthetic.main.media_queue_item.view.queue_id
76+
import kotlinx.android.synthetic.main.media_queue_item.view.description_title
77+
import kotlinx.android.synthetic.main.media_queue_item.view.description_subtitle
78+
import kotlinx.android.synthetic.main.media_queue_item.view.description_id
79+
import kotlinx.android.synthetic.main.media_queue_item.view.description_uri
80+
import kotlinx.android.synthetic.main.media_test_option.view.configure_test_suite_button
81+
import kotlinx.android.synthetic.main.media_test_option.view.card_text
82+
import kotlinx.android.synthetic.main.media_test_option.view.card_header
83+
import kotlinx.android.synthetic.main.media_test_option.view.card_button
84+
85+
import kotlinx.android.synthetic.main.media_test_suites.test_suite_options_list
86+
import kotlinx.android.synthetic.main.media_test_suites.test_suite_results_container
87+
import kotlinx.android.synthetic.main.media_test_suites.test_suite_num_iter
88+
import kotlinx.android.synthetic.main.media_tests.test_results_container
89+
import kotlinx.android.synthetic.main.media_tests.tests_query
90+
import kotlinx.android.synthetic.main.media_tests.test_options_list
91+
import kotlinx.android.synthetic.main.test_suite_configure_dialog.test_to_configure_list
92+
import kotlinx.android.synthetic.main.test_suite_configure_dialog.reset_results_button
93+
import kotlinx.android.synthetic.main.test_suite_configure_dialog.subtitle
94+
import kotlinx.android.synthetic.main.test_suite_configure_dialog.title
95+
import kotlinx.android.synthetic.main.test_suite_configure_dialog.done_button
96+
5897

5998
class MediaAppTestingActivity : AppCompatActivity() {
6099
private var mediaAppDetails: MediaAppDetails? = null
@@ -85,7 +124,6 @@ class MediaAppTestingActivity : AppCompatActivity() {
85124
toolbar.setNavigationOnClickListener { finish() }
86125

87126
Test.androidResources = resources
88-
89127
bottomNavigationView = findViewById(R.id.bottom_navigation_view)
90128
viewPager = view_pager
91129
testsQuery = tests_query
@@ -417,10 +455,46 @@ class MediaAppTestingActivity : AppCompatActivity() {
417455

418456

419457
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
420-
holder.cardView.card_header.text = testSuites[position].name
421-
holder.cardView.card_text.text = testSuites[position].description
422-
holder.cardView.card_button.text = "Run Suite"
423-
var suiteRunning = false
458+
val testSuite = testSuites[position]
459+
holder.cardView.card_header.text = testSuite.name
460+
holder.cardView.card_text.text = testSuite.description
461+
holder.cardView.card_button.text = resources.getText(R.string.run_suite_button)
462+
463+
val configurableTests = testSuite.getConfigurableTests()
464+
if (!configurableTests.isEmpty()) {
465+
holder.cardView.configure_test_suite_button.visibility = View.VISIBLE
466+
holder.cardView.configure_test_suite_button.setOnClickListener {
467+
val configAdapter = ConfigurationAdapter(configurableTests)
468+
val sharedPreferences = getSharedPreferences(SHARED_PREF_KEY_SUITE_CONFIG, Context.MODE_PRIVATE)
469+
Dialog(this@MediaAppTestingActivity).apply {
470+
// Init dialog
471+
requestWindowFeature(Window.FEATURE_NO_TITLE)
472+
setContentView(R.layout.test_suite_configure_dialog)
473+
title.text = testSuite.name + " Configuration"
474+
subtitle.text = testSuite.description
475+
test_to_configure_list.layoutManager = LinearLayoutManager(this@MediaAppTestingActivity)
476+
test_to_configure_list.layoutParams.height = getScreenHeightPx(this@MediaAppTestingActivity) / 2
477+
test_to_configure_list.adapter = configAdapter
478+
479+
// Reset config button clicked
480+
reset_results_button.setOnClickListener {
481+
sharedPreferences.edit().apply {
482+
for (i in configurableTests.indices) {
483+
putString(configurableTests[i].name, NO_CONFIG)
484+
configAdapter.notifyItemChanged(i)
485+
}
486+
}.apply()
487+
dismiss()
488+
}
489+
490+
// Done button pressed
491+
done_button.setOnClickListener {
492+
dismiss()
493+
}
494+
}.show()
495+
}
496+
}
497+
424498
holder.cardView.card_button.setOnClickListener {
425499
var numIter = test_suite_num_iter.text.toString().toIntOrNull()
426500
if (numIter == null) {
@@ -429,7 +503,7 @@ class MediaAppTestingActivity : AppCompatActivity() {
429503
} else if (numIter > 100 || numIter < 1) {
430504
Toast.makeText(this@MediaAppTestingActivity, getText(R.string.test_suite_error_invalid_iter), Toast.LENGTH_SHORT).show()
431505
} else if (isSuiteRunning()) {
432-
Toast.makeText(applicationContext, getText(R.string.test_suite_already_running), Toast.LENGTH_SHORT).show()
506+
Toast.makeText(this@MediaAppTestingActivity, getText(R.string.test_suite_already_running), Toast.LENGTH_SHORT).show()
433507
} else {
434508
testSuites[position].runSuite(numIter)
435509
}
@@ -448,6 +522,52 @@ class MediaAppTestingActivity : AppCompatActivity() {
448522
}
449523
}
450524

525+
// Adapter to display test suite details
526+
inner class ConfigurationAdapter(
527+
private val tests: ArrayList<TestOptionDetails>
528+
) : RecyclerView.Adapter() {
529+
inner class ViewHolder(val cardView: CardView) : RecyclerView.ViewHolder(cardView)
530+
531+
override fun onCreateViewHolder(
532+
parent: ViewGroup,
533+
viewType: Int
534+
): ConfigurationAdapter.ViewHolder {
535+
val cardView = LayoutInflater.from(parent.context)
536+
.inflate(R.layout.config_item, parent, false) as CardView
537+
return ViewHolder(cardView)
538+
}
539+
540+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
541+
val test = tests[position]
542+
holder.cardView.test_name_config.text = test.name
543+
val sharedPreferences = getSharedPreferences(SHARED_PREF_KEY_SUITE_CONFIG, Context.MODE_PRIVATE)
544+
holder.cardView.test_query_config.addTextChangedListener(object : TextWatcher {
545+
546+
override fun afterTextChanged(s: Editable) {
547+
sharedPreferences.edit().apply {
548+
putString(test.name, holder.cardView.test_query_config.text.toString())
549+
}.apply()
550+
}
551+
552+
override fun beforeTextChanged(s: CharSequence, start: Int,
553+
count: Int, after: Int) = Unit
554+
555+
override fun onTextChanged(s: CharSequence, start: Int,
556+
before: Int, count: Int) = Unit
557+
})
558+
559+
val previousConfig = sharedPreferences.getString(test.name, NO_CONFIG)
560+
holder.cardView.test_query_config.setText((previousConfig))
561+
if (previousConfig == NO_CONFIG) {
562+
holder.cardView.test_query_config.setText("")
563+
holder.cardView.test_query_config.hint = "Query"
564+
return
565+
}
566+
}
567+
568+
override fun getItemCount() = tests.size
569+
}
570+
451571
private fun setupTests() {
452572
// setupTests() should only be called after the mediaController is connected, so this
453573
// should never enter the if block
@@ -1104,6 +1224,7 @@ class MediaAppTestingActivity : AppCompatActivity() {
11041224
}
11051225

11061226
companion object {
1227+
11071228
private const val TAG = "MediaAppTestingActivity"
11081229

11091230
// Key names for external extras.
@@ -1117,6 +1238,12 @@ class MediaAppTestingActivity : AppCompatActivity() {
11171238
private const val STATE_APP_DETAILS_KEY =
11181239
"com.example.android.mediacontroller.STATE_APP_DETAILS_KEY"
11191240

1241+
// Shared pref key name for test suite config
1242+
const val SHARED_PREF_KEY_SUITE_CONFIG = "mct-test-suite-config"
1243+
1244+
// Shared pref suite no configuration setup
1245+
const val NO_CONFIG = "no-conf"
1246+
11201247
/**
11211248
* Builds an [Intent] to launch this Activity with a set of extras.
11221249
*
@@ -1132,5 +1259,14 @@ class MediaAppTestingActivity : AppCompatActivity() {
11321259
intent.putExtra(APP_DETAILS_EXTRA, appDetails)
11331260
return intent
11341261
}
1262+
1263+
// Gets the current screen height in pixels
1264+
fun getScreenHeightPx(context: Context): Int {
1265+
val displayMetrics = DisplayMetrics()
1266+
val windowManager = ContextCompat.getSystemService(context, WindowManager::class.java)
1267+
val display = windowManager.defaultDisplay
1268+
display.getMetrics(displayMetrics)
1269+
return displayMetrics.heightPixels
1270+
}
11351271
}
11361272
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
xml version="1.0" encoding="utf-8"?>
2+
17+
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:card_view="http://schemas.android.com/apk/res-auto"
19+
xmlns:tools="http://schemas.android.com/tools"
20+
android:id="@+id/card_view_config"
21+
android:layout_gravity="center"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:layout_margin="@dimen/margin_small"
25+
card_view:cardBackgroundColor="@color/cardview_light_background"
26+
card_view:cardCornerRadius="4dp">
27+
28+
<LinearLayout
29+
android:layout_height="match_parent"
30+
android:layout_width="match_parent"
31+
android:orientation="vertical"
32+
android:weightSum="5">
33+
34+
<TextView
35+
android:id="@+id/test_name_config"
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"
38+
android:textSize="@dimen/tests_subheader_text_size" />
39+
40+
<EditText
41+
android:id="@+id/test_query_config"
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:hint="@string/query_hint"
45+
android:textSize="@dimen/tests_subheader_text_size" />
46+
47+
LinearLayout>
48+
49+
androidx.cardview.widget.CardView>

0 commit comments

Comments
 (0)