1 /* 2 * Copyright (C) 2017 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 package com.android.documentsui.bots; 17 18 import static junit.framework.Assert.assertEquals; 19 import static junit.framework.Assert.assertTrue; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.view.View; 24 import android.widget.LinearLayout; 25 import android.widget.TextView; 26 27 import androidx.test.uiautomator.UiDevice; 28 import androidx.test.uiautomator.UiSelector; 29 30 import com.android.documentsui.R; 31 import com.android.documentsui.inspector.DetailsView; 32 import com.android.documentsui.inspector.KeyValueRow; 33 import com.android.documentsui.inspector.TableView; 34 35 import java.util.HashMap; 36 import java.util.Map; 37 38 public class InspectorBot extends Bots.BaseBot { 39 InspectorBot(UiDevice device, Context context, int timeout)40 public InspectorBot(UiDevice device, Context context, int timeout) { 41 super(device, context, timeout); 42 } 43 assertTitle(String expected)44 public void assertTitle(String expected) throws Exception { 45 UiSelector detailView = 46 new UiSelector().resourceId(mTargetPackage + ":id/inspector_details_view"); 47 UiSelector textView = 48 detailView.resourceId(mTargetPackage + ":id/inspector_header_title"); 49 String text = mDevice.findObject(textView).getText(); 50 assertEquals(expected, text); 51 } 52 assertRowPresent(String key, Activity activity)53 public void assertRowPresent(String key, Activity activity) 54 throws Exception { 55 assertTrue(isRowPresent(key, activity)); 56 } 57 assertRowEquals(String key, String value, Activity activity)58 public void assertRowEquals(String key, String value, Activity activity) 59 throws Exception { 60 assertTrue(isRowEquals(key, value, activity)); 61 } 62 getTableRows(TableView table)63 private static Map<String, String> getTableRows(TableView table) 64 throws Exception { 65 Map<String, String> rows = new HashMap<>(); 66 int children = table.getChildCount(); 67 for (int i = 0; i < children; i++) { 68 View view = table.getChildAt(i); 69 if (view instanceof KeyValueRow) { 70 LinearLayout row = (LinearLayout) table.getChildAt(i); 71 TextView key = (TextView) row.getChildAt(0); 72 TextView value = (TextView) row.getChildAt(1); 73 rows.put( 74 String.valueOf(key.getText()), 75 String.valueOf(value.getText())); 76 } 77 } 78 return rows; 79 } 80 isRowPresent(String key, Activity activity)81 private boolean isRowPresent(String key, Activity activity) 82 throws Exception { 83 DetailsView details = (DetailsView) activity.findViewById(R.id.inspector_details_view); 84 Map<String, String> rows = getTableRows(details); 85 return rows.containsKey(key); 86 } 87 isRowEquals(String key, String value, Activity activity)88 private boolean isRowEquals(String key, String value, Activity activity) 89 throws Exception { 90 DetailsView details = (DetailsView) activity.findViewById(R.id.inspector_details_view); 91 Map<String, String> rows = getTableRows(details); 92 return rows.containsKey(key) && value.equals(rows.get(key)); 93 } 94 } 95