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