Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / android / javatests / src / org / chromium / chrome / test / util / TranslateUtil.java
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.
4
5 package org.chromium.chrome.test.util;
6
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;
12
13 import org.chromium.chrome.R;
14 import org.chromium.chrome.browser.infobar.InfoBar;
15 import org.chromium.content.browser.test.util.TestTouchUtils;
16
17
18 /**
19  * Utility functions for dealing with Translate InfoBars.
20  */
21 public class TranslateUtil {
22     /**
23      * Finds the first clickable span inside a TextView and clicks it.
24      *
25      * @return True if the panel is opened.
26      */
27     public static boolean openLanguagePanel(ActivityInstrumentationTestCase2<?> test,
28             InfoBar infoBar) {
29         View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
30         if (view == null) {
31             return false;
32         }
33
34         TextView text = (TextView) view.findViewById(R.id.infobar_message);
35
36         SpannableString spannable = (SpannableString) text.getText();
37         ClickableSpan[] clickable =
38             spannable.getSpans(0, spannable.length() - 1, ClickableSpan.class);
39         if (clickable.length <= 0) {
40             return false;
41         }
42
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
46         // is more reliable.
47         int x = spannable.getSpanStart(clickable[0]) + 1;
48         float nChars = text.getLayout().getLineVisibleEnd(0);
49
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;
54
55         TestTouchUtils.singleClickView(test.getInstrumentation(), text, (int) xPos, (int) yPos);
56
57         return verifyInfoBarText(infoBar,
58             test.getActivity().getString(R.string.translate_infobar_change_languages));
59     }
60
61     public static boolean verifyInfoBarText(InfoBar infoBar, String text) {
62         View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
63         if (view == null) {
64             return false;
65         }
66         String infoBarText = findInfoBarText(view);
67         return text.equals(infoBarText);
68     }
69
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;
73     }
74 }