• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.textclassifier.notification;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.ClipData;
22 import android.content.ClipboardManager;
23 import android.content.Intent;
24 import androidx.test.core.app.ApplicationProvider;
25 import androidx.test.ext.junit.runners.AndroidJUnit4;
26 import androidx.test.filters.SmallTest;
27 import androidx.test.platform.app.InstrumentationRegistry;
28 import androidx.test.rule.ActivityTestRule;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public final class CopyCodeActivityTest {
36 
37   private static final String CODE_TO_COPY = "code";
38   private static final Intent EMPTY_INTENT =
39       new Intent(Intent.ACTION_VIEW).putExtra(Intent.EXTRA_TEXT, "");
40   private static final Intent CODE_INTENT =
41       new Intent(Intent.ACTION_VIEW).putExtra(Intent.EXTRA_TEXT, CODE_TO_COPY);
42 
43   @Rule
44   public ActivityTestRule<CopyCodeActivity> activityRule =
45       new ActivityTestRule<>(
46           CopyCodeActivity.class, /* initialTouchMode= */ false, /* launchActivity= */ false);
47 
48   @Test
onCreate_emptyCode()49   public void onCreate_emptyCode() throws Exception {
50     ClipboardManager clipboardManager =
51         ApplicationProvider.getApplicationContext().getSystemService(ClipboardManager.class);
52     // Use shell's permissions to ensure we can access the clipboard
53     InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity();
54     clipboardManager.clearPrimaryClip();
55 
56     activityRule.launchActivity(EMPTY_INTENT);
57 
58     try {
59       assertThat(clipboardManager.hasPrimaryClip()).isFalse();
60     } finally {
61       InstrumentationRegistry.getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
62     }
63   }
64 
65   @Test
onCreate_codeCopied()66   public void onCreate_codeCopied() throws Exception {
67     ClipboardManager clipboardManager =
68         ApplicationProvider.getApplicationContext().getSystemService(ClipboardManager.class);
69     // Use shell's permissions to ensure we can access the clipboard
70     InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity();
71     clipboardManager.clearPrimaryClip();
72 
73     activityRule.launchActivity(CODE_INTENT);
74 
75     ClipData clipFromClipboard;
76     try {
77       assertThat(clipboardManager.hasPrimaryClip()).isTrue();
78       clipFromClipboard = clipboardManager.getPrimaryClip();
79     } finally {
80       clipboardManager.clearPrimaryClip();
81       InstrumentationRegistry.getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
82     }
83 
84     assertThat(clipFromClipboard).isNotNull();
85     assertThat(clipFromClipboard.getItemCount()).isEqualTo(1);
86     assertThat(clipFromClipboard.getItemAt(0).getText().toString()).isEqualTo(CODE_TO_COPY);
87   }
88 }
89