1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.test.util;
7 import android.content.Context;
8 import android.test.ActivityInstrumentationTestCase2;
9 import android.test.TouchUtils;
10 import android.text.SpannableString;
11 import android.text.style.ClickableSpan;
12 import android.view.View;
13 import android.view.ViewGroup;
14 import android.widget.TextView;
16 import org.chromium.chrome.R;
18 import org.chromium.chrome.browser.infobar.InfoBar;
19 import org.chromium.content.browser.test.util.TestTouchUtils;
23 * Utility functions for dealing with Translate InfoBars.
25 public class TranslateUtil {
27 * Finds the first clickable span inside a TextView and clicks it.
29 * @return True if the panel is opened.
31 public static boolean openLanguagePanel(ActivityInstrumentationTestCase2<?> test,
33 View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
38 TextView text = (TextView) view.findViewById(R.id.infobar_message);
40 SpannableString spannable = (SpannableString) text.getText();
41 ClickableSpan[] clickable =
42 spannable.getSpans(0, spannable.length() -1, ClickableSpan.class);
43 if (clickable.length <= 0) {
47 // Find the approximate coordinates of the first link of the first line of text so we can
48 // click there. Clicking on any character of the link will work so instead of focusing on
49 // the beginning of the link we add one more character so that finding a valid coordinate
51 int x = spannable.getSpanStart(clickable[0]) + 1;
52 float nChars = text.getLayout().getLineVisibleEnd(0);
54 // Not all characters have the same width but this is a good approximation.
55 float sizePerChar = text.getLayout().getLineWidth(0) / nChars;
56 float xPos = text.getPaddingLeft() + (sizePerChar * x);
57 float yPos = text.getHeight() / (float) 2;
59 TestTouchUtils.singleClickView(test.getInstrumentation(), text, (int) xPos, (int) yPos);
61 return verifyInfoBarText(infoBar,
62 test.getActivity().getString(R.string.translate_infobar_change_languages));
65 public static boolean verifyInfoBarText(InfoBar infoBar, String text) {
66 View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
70 String infoBarText = findInfoBarText(view);
71 return text.equals(infoBarText);
74 private static String findInfoBarText(View view) {
75 TextView text = (TextView) view.findViewById(R.id.infobar_message);
76 return text != null ? text.getText().toString() : null;