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 com.android.documentsui.base; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.os.Process; 26 import android.os.UserHandle; 27 import android.test.AndroidTestCase; 28 29 import androidx.test.core.app.ApplicationProvider; 30 import androidx.test.filters.SmallTest; 31 32 import org.junit.Test; 33 34 import java.io.ByteArrayInputStream; 35 import java.io.ByteArrayOutputStream; 36 import java.io.DataInputStream; 37 import java.io.DataOutputStream; 38 import java.io.IOException; 39 import java.net.ProtocolException; 40 41 @SmallTest 42 public class UserIdTest extends AndroidTestCase { 43 44 private Context mContext = ApplicationProvider.getApplicationContext(); 45 46 @Test testEquals()47 public void testEquals() { 48 assertEquals(UserId.CURRENT_USER, UserId.CURRENT_USER); 49 assertEquals(UserId.of(UserHandle.of(10)), UserId.of(UserHandle.of(10))); 50 } 51 52 @Test testEquals_handlesNulls()53 public void testEquals_handlesNulls() { 54 assertFalse(UserId.CURRENT_USER.equals(null)); 55 } 56 57 @Test testNotEquals_differentUserHandle()58 public void testNotEquals_differentUserHandle() { 59 assertFalse(UserId.of(UserHandle.of(0)).equals(UserId.of(UserHandle.of(10)))); 60 } 61 62 @Test testHashCode_sameUserHandle()63 public void testHashCode_sameUserHandle() { 64 assertEquals(UserId.of(UserHandle.of(0)).hashCode(), 65 UserId.of(UserHandle.of(0)).hashCode()); 66 } 67 68 @Test testHashCode_differentUserHandle()69 public void testHashCode_differentUserHandle() { 70 assertThat(UserId.of(UserHandle.of(0)).hashCode()).isNotEqualTo( 71 UserId.of(UserHandle.of(10)).hashCode()); 72 } 73 74 @Test testAsContext_sameUserReturnProvidedContext()75 public void testAsContext_sameUserReturnProvidedContext() { 76 UserId userId = UserId.of(Process.myUserHandle()); 77 assertSame(mContext, userId.asContext(mContext)); 78 } 79 80 @Test testAsContext_unspecifiedUserReturnProvidedContext()81 public void testAsContext_unspecifiedUserReturnProvidedContext() { 82 assertSame(mContext, UserId.UNSPECIFIED_USER.asContext(mContext)); 83 } 84 85 @Test testAsContext_differentUserShouldCreatePackageContextAsUser()86 public void testAsContext_differentUserShouldCreatePackageContextAsUser() 87 throws Exception { 88 Context mockContext = mock(Context.class); 89 Context expectedContext = mock(Context.class); 90 UserHandle differentUserHandle = UserHandle.of(UserHandle.myUserId() + 1); 91 when(mockContext.createPackageContextAsUser("android", 0, differentUserHandle)).thenReturn( 92 expectedContext); 93 94 assertThat(UserId.of(differentUserHandle).asContext(mockContext)).isSameAs( 95 expectedContext); 96 } 97 98 @Test testOf_UserIdUserHandleIdentifier()99 public void testOf_UserIdUserHandleIdentifier() { 100 UserHandle userHandle = UserHandle.of(10); 101 assertEquals(UserId.of(userHandle), UserId.of(userHandle.getIdentifier())); 102 } 103 104 @Test testOf_UserIdRecreatingShouldEqual()105 public void testOf_UserIdRecreatingShouldEqual() { 106 UserId userId = UserId.of(3); 107 assertEquals(userId, UserId.of(userId.getIdentifier())); 108 } 109 110 @Test testRead_success()111 public void testRead_success() throws IOException { 112 ByteArrayOutputStream output = new ByteArrayOutputStream(); 113 DataOutputStream o = new DataOutputStream(output); 114 115 o.writeInt(1); // version 116 o.writeInt(10); // user id 117 118 UserId userId = UserId.read( 119 new DataInputStream(new ByteArrayInputStream(output.toByteArray()))); 120 121 assertThat(userId).isEqualTo(UserId.of(UserHandle.of(10))); 122 } 123 124 @Test testRead_failWithInvalidVersion()125 public void testRead_failWithInvalidVersion() throws IOException { 126 ByteArrayOutputStream output = new ByteArrayOutputStream(); 127 DataOutputStream o = new DataOutputStream(output); 128 129 o.writeInt(2); // version 130 o.writeInt(10); // user id 131 132 try { 133 UserId.read(new DataInputStream(new ByteArrayInputStream(output.toByteArray()))); 134 fail(); 135 } catch (ProtocolException expected) { 136 } finally { 137 o.close(); 138 output.close(); 139 } 140 } 141 142 @Test testWrite_writeDataOutputStream()143 public void testWrite_writeDataOutputStream() throws IOException { 144 ByteArrayOutputStream output = new ByteArrayOutputStream(); 145 DataOutputStream o = new DataOutputStream(output); 146 147 UserId.write(o, UserId.of(UserHandle.of(12))); 148 149 DataInputStream in = new DataInputStream(new ByteArrayInputStream(output.toByteArray())); 150 151 assertEquals(in.readInt(), 1); // version 152 assertEquals(in.readInt(), 12); // user id 153 assertEquals(in.read(), -1); // end of stream 154 155 output.close(); 156 o.close(); 157 in.close(); 158 } 159 } 160