Home | History | Annotate | Download | only in bots
      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 
     39     public InspectorBot(UiDevice device, Context context, int timeout) {
     40         super(device, context, timeout);
     41     }
     42 
     43     public void assertTitle(String expected) throws Exception {
     44         UiSelector textView
     45                 = new UiSelector().resourceId("com.android.documentsui:id/inspector_file_title");
     46         String text = mDevice.findObject(textView).getText();
     47         assertEquals(expected, text);
     48     }
     49 
     50     public void assertRowPresent(String key, Activity activity)
     51             throws Exception {
     52         assertTrue(isRowPresent(key, activity));
     53     }
     54 
     55     public void assertRowEquals(String key, String value, Activity activity)
     56             throws Exception {
     57         assertTrue(isRowEquals(key, value, activity));
     58     }
     59 
     60     private static Map<String, String> getTableRows(TableView table)
     61             throws Exception {
     62         Map<String, String> rows = new HashMap<>();
     63         int children = table.getChildCount();
     64         for (int i = 0; i < children; i++) {
     65             View view = table.getChildAt(i);
     66             if (view instanceof KeyValueRow) {
     67                 LinearLayout row = (LinearLayout) table.getChildAt(i);
     68                 TextView key = (TextView) row.getChildAt(0);
     69                 TextView value = (TextView) row.getChildAt(1);
     70                 rows.put(
     71                         String.valueOf(key.getText()),
     72                         String.valueOf(value.getText()));
     73             }
     74         }
     75         return rows;
     76     }
     77 
     78     private boolean isRowPresent(String key, Activity activity)
     79             throws Exception {
     80         DetailsView details = (DetailsView) activity.findViewById(R.id.inspector_details_view);
     81         Map<String, String> rows = getTableRows(details);
     82         return rows.containsKey(key);
     83     }
     84 
     85     private boolean isRowEquals(String key, String value, Activity activity)
     86             throws Exception {
     87         DetailsView details = (DetailsView) activity.findViewById(R.id.inspector_details_view);
     88         Map<String, String> rows = getTableRows(details);
     89         return rows.containsKey(key) && value.equals(rows.get(key));
     90     }
     91 }
     92