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.test.ActivityInstrumentationTestCase2;
8 import android.text.SpannableString;
9 import android.text.style.ClickableSpan;
10 import android.view.View;
11 import android.widget.TextView;
13 import org.chromium.chrome.R;
14 import org.chromium.chrome.browser.infobar.InfoBar;
15 import org.chromium.content.browser.test.util.TestTouchUtils;
19 * Utility functions for dealing with Translate InfoBars.
21 public class TranslateUtil {
23 * Finds the first clickable span inside a TextView and clicks it.
25 * @return True if the panel is opened.
27 public static boolean openLanguagePanel(ActivityInstrumentationTestCase2<?> test,
29 View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
34 TextView text = (TextView) view.findViewById(R.id.infobar_message);
36 SpannableString spannable = (SpannableString) text.getText();
37 ClickableSpan[] clickable =
38 spannable.getSpans(0, spannable.length() - 1, ClickableSpan.class);
39 if (clickable.length <= 0) {
43 // Find the approximate coordinates of the first link of the first line of text so we can
44 // click there. Clicking on any character of the link will work so instead of focusing on
45 // the beginning of the link we add one more character so that finding a valid coordinate
47 int x = spannable.getSpanStart(clickable[0]) + 1;
48 float nChars = text.getLayout().getLineVisibleEnd(0);
50 // Not all characters have the same width but this is a good approximation.
51 float sizePerChar = text.getLayout().getLineWidth(0) / nChars;
52 float xPos = text.getPaddingLeft() + (sizePerChar * x);
53 float yPos = text.getHeight() / (float) 2;
55 TestTouchUtils.singleClickView(test.getInstrumentation(), text, (int) xPos, (int) yPos);
57 return verifyInfoBarText(infoBar,
58 test.getActivity().getString(R.string.translate_infobar_change_languages));
61 public static boolean verifyInfoBarText(InfoBar infoBar, String text) {
62 View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
66 String infoBarText = findInfoBarText(view);
67 return text.equals(infoBarText);
70 private static String findInfoBarText(View view) {
71 TextView text = (TextView) view.findViewById(R.id.infobar_message);
72 return text != null ? text.getText().toString() : null;