1 /* 2 * Copyright (C) 2020 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 android.content.cts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertEquals; 22 23 import android.app.PendingIntent; 24 import android.content.ClipData; 25 import android.content.ClipDescription; 26 import android.content.Intent; 27 import android.content.IntentSender; 28 import android.net.Uri; 29 import android.os.PersistableBundle; 30 import android.platform.test.annotations.AppModeSdkSandbox; 31 import android.platform.test.annotations.DisabledOnRavenwood; 32 import android.platform.test.ravenwood.RavenwoodRule; 33 import android.util.Log; 34 35 import androidx.test.InstrumentationRegistry; 36 import androidx.test.filters.SmallTest; 37 import androidx.test.runner.AndroidJUnit4; 38 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 @SmallTest 44 @RunWith(AndroidJUnit4.class) 45 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 46 public class ClipDataTest { 47 private static final String LOG_TAG = "ClipDataTest"; 48 49 @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule(); 50 51 @Test 52 @DisabledOnRavenwood(blockedBy = {PendingIntent.class}) testBuilder()53 public void testBuilder() { 54 final Uri uri = Uri.fromParts("scheme", "ssp", "fragment"); 55 final String htmlText = "<blink>htmlText</blink>"; 56 final String text = "text"; 57 final Intent intent = new Intent(Intent.ACTION_MAIN); 58 final PendingIntent pi = PendingIntent.getActivity(InstrumentationRegistry.getContext(), 0, 59 intent, PendingIntent.FLAG_IMMUTABLE); 60 final IntentSender sender = pi.getIntentSender(); 61 final ClipData.Item clip = new ClipData.Item.Builder() 62 .setHtmlText(htmlText) 63 .setIntentSender(sender) 64 .setText(text) 65 .setUri(uri) 66 .setIntent(intent) 67 .build(); 68 assertEquals(clip.getHtmlText(), htmlText); 69 assertEquals(clip.getIntentSender(), sender); 70 assertEquals(clip.getText(), text); 71 assertEquals(clip.getUri(), uri); 72 assertEquals(clip.getIntent(), intent); 73 pi.cancel(); 74 } 75 76 @Test testToString_text()77 public void testToString_text() { 78 ClipData clip = ClipData.newPlainText(null, "secret-text"); 79 String clipStr = clip.toString(); 80 Log.i(LOG_TAG, clipStr); 81 assertThat(clipStr).doesNotContain("secret"); 82 } 83 84 @Test testToString_html()85 public void testToString_html() { 86 ClipData clip = ClipData.newHtmlText(null, "secret-text", "secret-html"); 87 String clipStr = clip.toString(); 88 Log.i(LOG_TAG, clipStr); 89 assertThat(clipStr).doesNotContain("secret"); 90 } 91 92 @Test testToString_uri()93 public void testToString_uri() { 94 ClipData clip = ClipData.newRawUri(null, Uri.parse("content://secret")); 95 String clipStr = clip.toString(); 96 Log.i(LOG_TAG, clipStr); 97 assertThat(clipStr).doesNotContain("secret"); 98 } 99 100 @Test 101 @DisabledOnRavenwood(blockedBy = android.util.TimeUtils.class) testToString_metadata()102 public void testToString_metadata() { 103 ClipDescription description = new ClipDescription("secret-label", 104 new String[]{"text/plain"}); 105 PersistableBundle extras = new PersistableBundle(); 106 extras.putString("secret-key", "secret-value"); 107 description.setExtras(extras); 108 description.setTimestamp(42); 109 ClipData clip = new ClipData(description, new ClipData.Item("secret-text")); 110 String clipStr = clip.toString(); 111 Log.i(LOG_TAG, clipStr); 112 assertThat(clipStr).doesNotContain("secret"); 113 } 114 115 @Test testToString_multipleItems()116 public void testToString_multipleItems() { 117 ClipData clip = ClipData.newPlainText(null, "secret-one"); 118 clip.addItem(new ClipData.Item("secret-two")); 119 clip.addItem(new ClipData.Item("secret-three")); 120 String clipStr = clip.toString(); 121 Log.i(LOG_TAG, clipStr); 122 assertThat(clipStr).doesNotContain("secret"); 123 } 124 125 @Test testToString_complexItem()126 public void testToString_complexItem() { 127 ClipData.Item item = new ClipData.Item( 128 "secret-text", 129 "secret-html", 130 new Intent(), 131 Uri.parse("content://secret")); 132 String[] mimeTypes = { 133 ClipDescription.MIMETYPE_TEXT_PLAIN, 134 ClipDescription.MIMETYPE_TEXT_HTML, 135 ClipDescription.MIMETYPE_TEXT_INTENT, 136 ClipDescription.MIMETYPE_TEXT_URILIST 137 }; 138 ClipData clip = new ClipData("secret-label", mimeTypes, item); 139 String clipStr = clip.toString(); 140 Log.i(LOG_TAG, clipStr); 141 assertThat(clipStr).doesNotContain("secret"); 142 } 143 144 @Test testCoerceToText_text()145 public void testCoerceToText_text() { 146 final ClipData.Item item = new ClipData.Item( 147 "text" 148 ); 149 assertThat(item.coerceToText(InstrumentationRegistry.getTargetContext())) 150 .isEqualTo("text"); 151 } 152 153 @Test testCoerceToText_uri()154 public void testCoerceToText_uri() { 155 final ClipData.Item item = new ClipData.Item( 156 new Intent("action") 157 ); 158 assertThat(item.coerceToText(InstrumentationRegistry.getTargetContext())) 159 .isEqualTo("intent:#Intent;action=action;end"); 160 } 161 162 @Test testCoerceToText_intent()163 public void testCoerceToText_intent() { 164 final ClipData.Item item = new ClipData.Item( 165 Uri.parse("example://authority/") 166 ); 167 assertThat(item.coerceToText(InstrumentationRegistry.getTargetContext())) 168 .isEqualTo("example://authority/"); 169 } 170 171 @Test testCoerceToText_multiple()172 public void testCoerceToText_multiple() { 173 final ClipData.Item item = new ClipData.Item( 174 "text", 175 new Intent("action"), 176 Uri.parse("example://authority/") 177 ); 178 assertThat(item.coerceToText(InstrumentationRegistry.getTargetContext())) 179 .isEqualTo("text"); 180 } 181 } 182