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