1 /* 2 * Copyright (C) 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 com.android.systemui.screenshot 18 19 import android.content.ComponentName 20 import android.content.Context 21 import android.content.Intent 22 import android.net.Uri 23 import androidx.test.filters.SmallTest 24 import com.android.systemui.R 25 import com.android.systemui.SysuiTestCase 26 import com.android.systemui.util.mockito.eq 27 import com.android.systemui.util.mockito.mock 28 import com.google.common.truth.Truth.assertThat 29 import org.junit.Test 30 import org.mockito.Mockito.`when` as whenever 31 32 @SmallTest 33 class ActionIntentCreatorTest : SysuiTestCase() { 34 35 @Test testCreateShareIntentnull36 fun testCreateShareIntent() { 37 val uri = Uri.parse("content://fake") 38 39 val output = ActionIntentCreator.createShareIntent(uri) 40 41 assertThat(output.action).isEqualTo(Intent.ACTION_CHOOSER) 42 assertFlagsSet( 43 Intent.FLAG_ACTIVITY_NEW_TASK or 44 Intent.FLAG_ACTIVITY_CLEAR_TASK or 45 Intent.FLAG_GRANT_READ_URI_PERMISSION, 46 output.flags 47 ) 48 49 val wrappedIntent = output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java) 50 assertThat(wrappedIntent?.action).isEqualTo(Intent.ACTION_SEND) 51 assertThat(wrappedIntent?.data).isEqualTo(uri) 52 assertThat(wrappedIntent?.type).isEqualTo("image/png") 53 assertThat(wrappedIntent?.getStringExtra(Intent.EXTRA_SUBJECT)).isNull() 54 assertThat(wrappedIntent?.getStringExtra(Intent.EXTRA_TEXT)).isNull() 55 assertThat(wrappedIntent?.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)) 56 .isEqualTo(uri) 57 } 58 59 @Test testCreateShareIntentWithSubjectnull60 fun testCreateShareIntentWithSubject() { 61 val uri = Uri.parse("content://fake") 62 val subject = "Example subject" 63 64 val output = ActionIntentCreator.createShareIntentWithSubject(uri, subject) 65 66 assertThat(output.action).isEqualTo(Intent.ACTION_CHOOSER) 67 assertFlagsSet( 68 Intent.FLAG_ACTIVITY_NEW_TASK or 69 Intent.FLAG_ACTIVITY_CLEAR_TASK or 70 Intent.FLAG_GRANT_READ_URI_PERMISSION, 71 output.flags 72 ) 73 74 val wrappedIntent = output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java) 75 assertThat(wrappedIntent?.action).isEqualTo(Intent.ACTION_SEND) 76 assertThat(wrappedIntent?.data).isEqualTo(uri) 77 assertThat(wrappedIntent?.type).isEqualTo("image/png") 78 assertThat(wrappedIntent?.getStringExtra(Intent.EXTRA_SUBJECT)).isEqualTo(subject) 79 assertThat(wrappedIntent?.getStringExtra(Intent.EXTRA_TEXT)).isNull() 80 assertThat(wrappedIntent?.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)) 81 .isEqualTo(uri) 82 } 83 84 @Test testCreateShareIntentWithExtraTextnull85 fun testCreateShareIntentWithExtraText() { 86 val uri = Uri.parse("content://fake") 87 val extraText = "Extra text" 88 89 val output = ActionIntentCreator.createShareIntentWithExtraText(uri, extraText) 90 91 assertThat(output.action).isEqualTo(Intent.ACTION_CHOOSER) 92 assertFlagsSet( 93 Intent.FLAG_ACTIVITY_NEW_TASK or 94 Intent.FLAG_ACTIVITY_CLEAR_TASK or 95 Intent.FLAG_GRANT_READ_URI_PERMISSION, 96 output.flags 97 ) 98 99 val wrappedIntent = output.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java) 100 assertThat(wrappedIntent?.action).isEqualTo(Intent.ACTION_SEND) 101 assertThat(wrappedIntent?.data).isEqualTo(uri) 102 assertThat(wrappedIntent?.type).isEqualTo("image/png") 103 assertThat(wrappedIntent?.getStringExtra(Intent.EXTRA_SUBJECT)).isNull() 104 assertThat(wrappedIntent?.getStringExtra(Intent.EXTRA_TEXT)).isEqualTo(extraText) 105 assertThat(wrappedIntent?.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)) 106 .isEqualTo(uri) 107 } 108 109 @Test testCreateEditIntentnull110 fun testCreateEditIntent() { 111 val uri = Uri.parse("content://fake") 112 val context = mock<Context>() 113 114 val output = ActionIntentCreator.createEditIntent(uri, context) 115 116 assertThat(output.action).isEqualTo(Intent.ACTION_EDIT) 117 assertThat(output.data).isEqualTo(uri) 118 assertThat(output.type).isEqualTo("image/png") 119 assertThat(output.component).isNull() 120 val expectedFlags = 121 Intent.FLAG_GRANT_READ_URI_PERMISSION or 122 Intent.FLAG_GRANT_WRITE_URI_PERMISSION or 123 Intent.FLAG_ACTIVITY_NEW_TASK or 124 Intent.FLAG_ACTIVITY_CLEAR_TASK 125 assertFlagsSet(expectedFlags, output.flags) 126 } 127 128 @Test testCreateEditIntent_withEditornull129 fun testCreateEditIntent_withEditor() { 130 val uri = Uri.parse("content://fake") 131 val context = mock<Context>() 132 var component = ComponentName("com.android.foo", "com.android.foo.Something") 133 134 whenever(context.getString(eq(R.string.config_screenshotEditor))) 135 .thenReturn(component.flattenToString()) 136 137 val output = ActionIntentCreator.createEditIntent(uri, context) 138 139 assertThat(output.component).isEqualTo(component) 140 } 141 assertFlagsSetnull142 private fun assertFlagsSet(expected: Int, observed: Int) { 143 assertThat(observed and expected).isEqualTo(expected) 144 } 145 } 146