• 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.traceur.uitest;
18 
19 import static org.junit.Assert.assertNotNull;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.RemoteException;
24 import android.support.test.filters.MediumTest;
25 import android.support.test.uiautomator.By;
26 import android.support.test.uiautomator.UiDevice;
27 import android.support.test.uiautomator.UiObject2;
28 import android.support.test.uiautomator.Until;
29 import android.support.test.InstrumentationRegistry;
30 import android.support.test.runner.AndroidJUnit4;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class TraceurAppTests {
39 
40     private static final String TRACEUR_PACKAGE = "com.android.traceur";
41     private static final int TIMEOUT = 2000;   // milliseconds
42 
43     private UiDevice mDevice;
44 
45     @Before
setUp()46     public void setUp() throws Exception {
47         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
48 
49         try {
50             mDevice.setOrientationNatural();
51         } catch (RemoteException e) {
52             throw new RuntimeException("Failed to freeze device orientation.", e);
53         }
54 
55         Context context = InstrumentationRegistry.getContext();
56         Intent intent = context.getPackageManager().getLaunchIntentForPackage(TRACEUR_PACKAGE);
57         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
58         context.startActivity(intent);
59 
60        // Wait for the app to appear
61         mDevice.wait(Until.hasObject(By.pkg(TRACEUR_PACKAGE).depth(0)), TIMEOUT);
62     }
63 
64     @After
tearDown()65     public void tearDown() throws Exception {
66         // Finish Traceur activity.
67         mDevice.pressBack();
68         mDevice.pressHome();
69     }
70 
71     @Test
72     @MediumTest
testElementsOnMainScreen()73     public void testElementsOnMainScreen() throws Exception {
74         assertNotNull("Record trace switch not found.",
75                 mDevice.wait(Until.findObject(By.text("Record trace")),
76                 TIMEOUT));
77         assertNotNull("Applications element not found.",
78                 mDevice.wait(Until.findObject(By.text("Trace debuggable applications")),
79                 TIMEOUT));
80         assertNotNull("Categories element not found.",
81                 mDevice.wait(Until.findObject(By.text("Categories")),
82                 TIMEOUT));
83         assertNotNull("Restore default categories element not found.",
84                 mDevice.wait(Until.findObject(By.text("Restore default categories")),
85                 TIMEOUT));
86         assertNotNull("Buffer size element not found.",
87                 mDevice.wait(Until.findObject(By.text("Buffer size")),
88                 TIMEOUT));
89         assertNotNull("Clear saved traces element not found.",
90                 mDevice.wait(Until.findObject(By.text("Clear saved traces")),
91                 TIMEOUT));
92         assertNotNull("Show Quick Settings tile switch not found.",
93                 mDevice.wait(Until.findObject(By.text("Show Quick Settings tile")),
94                 TIMEOUT));
95     }
96 
97     /*
98      * In this test:
99      * Take a trace by toggling 'Record trace' and then tap 'Save and share trace'.
100      * Tap the notification once the trace is saved, and verify the share dialog appears.
101      */
102     @Test
103     @MediumTest
testSuccessfulTracing()104     public void testSuccessfulTracing() throws Exception {
105         mDevice.wait(Until.findObject(By.text("Record trace")), TIMEOUT);
106 
107         mDevice.findObject(By.text("Record trace")).click();
108         mDevice.findObject(By.text("Record trace")).click();
109 
110         mDevice.openNotification();
111         mDevice.wait(Until.hasObject(By.text("Tap to share your trace")), TIMEOUT);
112         mDevice.findObject(By.text("Tap to share your trace")).click();
113 
114         mDevice.wait(Until.hasObject(By.text("Share with")), TIMEOUT);
115     }
116 }
117