16
16
package com.example.android.mediacontroller
17
17
18
18
import android.app.Activity
19
+ import android.app.Dialog
20
+ import android.content.Context
19
21
import android.content.Intent
20
22
import android.graphics.Bitmap
21
23
import android.graphics.Color
@@ -28,12 +30,17 @@ import android.support.v4.media.MediaMetadataCompat
28
30
import android.support.v4.media.session.MediaControllerCompat
29
31
import android.support.v4.media.session.MediaSessionCompat
30
32
import android.support.v4.media.session.PlaybackStateCompat
33
+ import android.text.Editable
34
+ import android.text.TextWatcher
35
+ import android.util.DisplayMetrics
31
36
import android.util.Log
32
37
import android.view.View
33
38
import android.view.ViewGroup
34
39
import android.view.MenuItem
35
40
import android.view.Menu
36
41
import android.view.LayoutInflater
42
+ import android.view.Window
43
+ import android.view.WindowManager
37
44
import android.widget.EditText
38
45
import android.widget.LinearLayout
39
46
import android.widget.TextView
@@ -48,13 +55,45 @@ import androidx.recyclerview.widget.LinearLayoutManager
48
55
import androidx.recyclerview.widget.RecyclerView
49
56
import androidx.viewpager.widget.PagerAdapter
50
57
import androidx.viewpager.widget.ViewPager
58
+
51
59
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
+
58
97
59
98
class MediaAppTestingActivity : AppCompatActivity () {
60
99
private var mediaAppDetails: MediaAppDetails ? = null
@@ -85,7 +124,6 @@ class MediaAppTestingActivity : AppCompatActivity() {
85
124
toolbar.setNavigationOnClickListener { finish() }
86
125
87
126
Test .androidResources = resources
88
-
89
127
bottomNavigationView = findViewById(R .id.bottom_navigation_view)
90
128
viewPager = view_pager
91
129
testsQuery = tests_query
@@ -417,10 +455,46 @@ class MediaAppTestingActivity : AppCompatActivity() {
417
455
418
456
419
457
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
+
424
498
holder.cardView.card_button.setOnClickListener {
425
499
var numIter = test_suite_num_iter.text.toString().toIntOrNull()
426
500
if (numIter == null ) {
@@ -429,7 +503,7 @@ class MediaAppTestingActivity : AppCompatActivity() {
429
503
} else if (numIter > 100 || numIter < 1 ) {
430
504
Toast .makeText(this @MediaAppTestingActivity, getText(R .string.test_suite_error_invalid_iter), Toast .LENGTH_SHORT ).show()
431
505
} 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()
433
507
} else {
434
508
testSuites[position].runSuite(numIter)
435
509
}
@@ -448,6 +522,52 @@ class MediaAppTestingActivity : AppCompatActivity() {
448
522
}
449
523
}
450
524
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
+
451
571
private fun setupTests () {
452
572
// setupTests() should only be called after the mediaController is connected, so this
453
573
// should never enter the if block
@@ -1104,6 +1224,7 @@ class MediaAppTestingActivity : AppCompatActivity() {
1104
1224
}
1105
1225
1106
1226
companion object {
1227
+
1107
1228
private const val TAG = " MediaAppTestingActivity"
1108
1229
1109
1230
// Key names for external extras.
@@ -1117,6 +1238,12 @@ class MediaAppTestingActivity : AppCompatActivity() {
1117
1238
private const val STATE_APP_DETAILS_KEY =
1118
1239
" com.example.android.mediacontroller.STATE_APP_DETAILS_KEY"
1119
1240
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
+
1120
1247
/* *
1121
1248
* Builds an [Intent] to launch this Activity with a set of extras.
1122
1249
*
@@ -1132,5 +1259,14 @@ class MediaAppTestingActivity : AppCompatActivity() {
1132
1259
intent.putExtra(APP_DETAILS_EXTRA , appDetails)
1133
1260
return intent
1134
1261
}
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
+ }
1135
1271
}
1136
1272
}
0 commit comments