1 /* 2 * Copyright (C) 2023 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.server.contentprotection; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.fail; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.verifyZeroInteractions; 24 25 import android.annotation.NonNull; 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.content.pm.ParceledListSlice; 29 import android.os.UserHandle; 30 import android.service.contentcapture.IContentProtectionService; 31 import android.view.contentcapture.ContentCaptureEvent; 32 33 import androidx.test.core.app.ApplicationProvider; 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 import androidx.test.filters.SmallTest; 36 37 import com.android.internal.infra.AndroidFuture; 38 import com.android.internal.infra.ServiceConnector; 39 40 import com.google.common.collect.ImmutableList; 41 42 import org.junit.Before; 43 import org.junit.Rule; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.junit.MockitoJUnit; 48 import org.mockito.junit.MockitoRule; 49 50 /** 51 * Test for {@link RemoteContentProtectionService}. 52 * 53 * <p>Run with: {@code atest 54 * FrameworksServicesTests:com.android.server.contentprotection.RemoteContentProtectionServiceTest} 55 */ 56 @RunWith(AndroidJUnit4.class) 57 @SmallTest 58 public class RemoteContentProtectionServiceTest { 59 60 private final Context mContext = ApplicationProvider.getApplicationContext(); 61 62 @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 63 64 @Mock private IContentProtectionService mMockContentProtectionService; 65 66 private RemoteContentProtectionService mRemoteContentProtectionService; 67 68 private int mConnectCallCount = 0; 69 70 @Before setup()71 public void setup() { 72 ComponentName componentName = new ComponentName(mContext.getPackageName(), "TestClass"); 73 mRemoteContentProtectionService = 74 new TestRemoteContentProtectionService(mContext, componentName); 75 } 76 77 @Test doesNotAutoConnect()78 public void doesNotAutoConnect() { 79 assertThat(mConnectCallCount).isEqualTo(0); 80 verifyZeroInteractions(mMockContentProtectionService); 81 } 82 83 @Test getAutoDisconnectTimeoutMs()84 public void getAutoDisconnectTimeoutMs() { 85 long actual = mRemoteContentProtectionService.getAutoDisconnectTimeoutMs(); 86 87 assertThat(actual).isEqualTo(3000L); 88 } 89 90 @Test onLoginDetected()91 public void onLoginDetected() throws Exception { 92 ContentCaptureEvent event = 93 new ContentCaptureEvent(/* sessionId= */ 1111, /* type= */ 2222); 94 ParceledListSlice<ContentCaptureEvent> events = 95 new ParceledListSlice<>(ImmutableList.of(event)); 96 97 mRemoteContentProtectionService.onLoginDetected(events); 98 99 verify(mMockContentProtectionService).onLoginDetected(events); 100 } 101 102 private final class TestRemoteContentProtectionService extends RemoteContentProtectionService { 103 TestRemoteContentProtectionService(Context context, ComponentName componentName)104 TestRemoteContentProtectionService(Context context, ComponentName componentName) { 105 super(context, componentName, UserHandle.myUserId(), /* bindAllowInstant= */ false); 106 } 107 108 @Override // from ServiceConnector connect()109 public synchronized AndroidFuture<IContentProtectionService> connect() { 110 mConnectCallCount++; 111 return AndroidFuture.completedFuture(mMockContentProtectionService); 112 } 113 114 @Override // from ServiceConnector run(@onNull ServiceConnector.VoidJob<IContentProtectionService> job)115 public boolean run(@NonNull ServiceConnector.VoidJob<IContentProtectionService> job) { 116 try { 117 job.run(mMockContentProtectionService); 118 } catch (Exception ex) { 119 fail("Unexpected exception: " + ex); 120 } 121 return true; 122 } 123 } 124 } 125