1 /* 2 * Copyright (C) 2016 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.view.inputmethod.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 22 import android.content.ClipDescription; 23 import android.net.Uri; 24 import android.os.Parcel; 25 import android.view.inputmethod.InputContentInfo; 26 27 import androidx.test.filters.SmallTest; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.security.InvalidParameterException; 34 35 @SmallTest 36 @RunWith(AndroidJUnit4.class) 37 public class InputContentInfoTest { 38 @Test 39 public void testInputContentInfo() { 40 InputContentInfo info = new InputContentInfo( 41 Uri.parse("content://com.example/path"), 42 new ClipDescription("sample content", new String[]{"image/png"}), 43 Uri.parse("https://example.com")); 44 45 assertEquals(Uri.parse("content://com.example/path"), info.getContentUri()); 46 assertEquals(1, info.getDescription().getMimeTypeCount()); 47 assertEquals("image/png", info.getDescription().getMimeType(0)); 48 assertEquals("sample content", info.getDescription().getLabel()); 49 assertEquals(Uri.parse("https://example.com"), info.getLinkUri()); 50 assertEquals(0, info.describeContents()); 51 52 Parcel p = Parcel.obtain(); 53 info.writeToParcel(p, 0); 54 p.setDataPosition(0); 55 InputContentInfo targetInfo = InputContentInfo.CREATOR.createFromParcel(p); 56 p.recycle(); 57 58 assertEquals(info.getContentUri(), targetInfo.getContentUri()); 59 assertEquals(info.getDescription().getMimeTypeCount(), 60 targetInfo.getDescription().getMimeTypeCount()); 61 assertEquals(info.getDescription().getMimeType(0), 62 targetInfo.getDescription().getMimeType(0)); 63 assertEquals(info.getDescription().getLabel(), targetInfo.getDescription().getLabel()); 64 assertEquals(info.getLinkUri(), targetInfo.getLinkUri()); 65 assertEquals(info.describeContents(), targetInfo.describeContents()); 66 } 67 68 @Test 69 public void testOptionalConstructorParam() { 70 InputContentInfo info = new InputContentInfo( 71 Uri.parse("content://com.example/path"), 72 new ClipDescription("sample content", new String[]{"image/png"})); 73 74 assertEquals(Uri.parse("content://com.example/path"), info.getContentUri()); 75 assertEquals(1, info.getDescription().getMimeTypeCount()); 76 assertEquals("image/png", info.getDescription().getMimeType(0)); 77 assertEquals("sample content", info.getDescription().getLabel()); 78 assertNull(info.getLinkUri()); 79 assertEquals(0, info.describeContents()); 80 } 81 82 @Test(expected = NullPointerException.class) 83 public void testContentUriNullContentUri() { 84 new InputContentInfo( 85 null, new ClipDescription("sample content", new String[]{"image/png"}), 86 Uri.parse("https://example.com")); 87 } 88 89 @Test(expected = InvalidParameterException.class) 90 public void testContentUriInvalidContentUri() { 91 new InputContentInfo( 92 Uri.parse("https://example.com"), 93 new ClipDescription("sample content", new String[]{"image/png"}), 94 Uri.parse("https://example.com")); 95 } 96 97 @Test(expected = NullPointerException.class) 98 public void testMimeTypeNulLDescription() { 99 new InputContentInfo( 100 Uri.parse("content://com.example/path"), null, 101 Uri.parse("https://example.com")); 102 } 103 104 @Test 105 public void testLinkUri() { 106 // Test that we accept null link Uri 107 new InputContentInfo( 108 Uri.parse("content://com.example/path"), 109 new ClipDescription("sample content", new String[]{"image/png"}), 110 null); 111 112 // Test that we accept http link Uri 113 new InputContentInfo( 114 Uri.parse("content://com.example/path"), 115 new ClipDescription("sample content", new String[]{"image/png"}), 116 Uri.parse("http://example.com/path")); 117 118 // Test that we accept https link Uri 119 new InputContentInfo( 120 Uri.parse("content://com.example/path"), 121 new ClipDescription("sample content", new String[]{"image/png"}), 122 Uri.parse("https://example.com/path")); 123 } 124 125 @Test(expected = InvalidParameterException.class) 126 public void testLinkUriFtpLinkUri() { 127 // InputContentInfo must accept http and https link Uri only 128 new InputContentInfo( 129 Uri.parse("content://com.example/path"), 130 new ClipDescription("sample content", new String[]{"image/png"}), 131 Uri.parse("ftp://example.com/path")); 132 } 133 134 @Test(expected = InvalidParameterException.class) 135 public void testLinkUriContentLinkUri() { 136 // InputContentInfo must accept http and https link Uri only 137 new InputContentInfo( 138 Uri.parse("content://com.example/path"), 139 new ClipDescription("sample content", new String[]{"image/png"}), 140 Uri.parse("content://com.example/path")); 141 } 142 143 @Test 144 public void testRequestAndReleasePermission() { 145 InputContentInfo info = new InputContentInfo( 146 Uri.parse("content://com.example/path"), 147 new ClipDescription("sample content", new String[]{"image/png"}), 148 Uri.parse("https://example.com")); 149 150 // Here we only assert that {request, release}Permission() do not crash, because ensuring 151 // the entire functionality of these methods requires end-to-end IME test environment, which 152 // we do not have yet in CTS. 153 // Note it is actually intentional that calling these methods here has no effect. Those 154 // methods would have effect only after the object is passed from the IME process to the 155 // application process. 156 // TODO: Create an end-to-end CTS test for this functionality. 157 info.requestPermission(); 158 info.releasePermission(); 159 info.requestPermission(); 160 info.releasePermission(); 161 } 162 163 } 164