Kelvin Kwan | d8c64f5 | 2020-01-23 23:50:39 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 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 |
| 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 |
| 53 | public void testEquals_handlesNulls() { |
| 54 | assertFalse(UserId.CURRENT_USER.equals(null)); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testNotEquals_differentUserHandle() { |
| 59 | assertFalse(UserId.of(UserHandle.of(0)).equals(UserId.of(UserHandle.of(10)))); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testHashCode_sameUserHandle() { |
| 64 | assertEquals(UserId.of(UserHandle.of(0)).hashCode(), |
| 65 | UserId.of(UserHandle.of(0)).hashCode()); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 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 |
| 75 | public void testAsContext_sameUserReturnProvidedContext() { |
| 76 | UserId userId = UserId.of(Process.myUserHandle()); |
| 77 | assertSame(mContext, userId.asContext(mContext)); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testAsContext_unspecifiedUserReturnProvidedContext() { |
| 82 | assertSame(mContext, UserId.UNSPECIFIED_USER.asContext(mContext)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 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 |
| 99 | public void testRead_success() throws IOException { |
| 100 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 101 | DataOutputStream o = new DataOutputStream(output); |
| 102 | |
| 103 | o.writeInt(1); // version |
| 104 | o.writeInt(10); // user id |
| 105 | |
| 106 | UserId userId = UserId.read( |
| 107 | new DataInputStream(new ByteArrayInputStream(output.toByteArray()))); |
| 108 | |
| 109 | assertThat(userId).isEqualTo(UserId.of(UserHandle.of(10))); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testRead_failWithInvalidVersion() throws IOException { |
| 114 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 115 | DataOutputStream o = new DataOutputStream(output); |
| 116 | |
| 117 | o.writeInt(2); // version |
| 118 | o.writeInt(10); // user id |
| 119 | |
| 120 | try { |
| 121 | UserId.read(new DataInputStream(new ByteArrayInputStream(output.toByteArray()))); |
| 122 | fail(); |
| 123 | } catch (ProtocolException expected) { |
| 124 | } finally { |
| 125 | o.close(); |
| 126 | output.close(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testWrite_writeDataOutputStream() throws IOException { |
| 132 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 133 | DataOutputStream o = new DataOutputStream(output); |
| 134 | |
| 135 | UserId.write(o, UserId.of(UserHandle.of(12))); |
| 136 | |
| 137 | DataInputStream in = new DataInputStream(new ByteArrayInputStream(output.toByteArray())); |
| 138 | |
| 139 | assertEquals(in.readInt(), 1); // version |
| 140 | assertEquals(in.readInt(), 12); // user id |
| 141 | assertEquals(in.read(), -1); // end of stream |
| 142 | |
| 143 | output.close(); |
| 144 | o.close(); |
| 145 | in.close(); |
| 146 | } |
| 147 | } |