- add sources.
[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.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;
15
16 import org.chromium.chrome.R;
17
18 import org.chromium.chrome.browser.infobar.InfoBar;
19 import org.chromium.content.browser.test.util.TestTouchUtils;
20
21
22 /**
23  * Utility functions for dealing with Translate InfoBars.
24  */
25 public class TranslateUtil {
26     /**
27      * Finds the first clickable span inside a TextView and clicks it.
28      *
29      * @return True if the panel is opened.
30      */
31     public static boolean openLanguagePanel(ActivityInstrumentationTestCase2<?> test,
32             InfoBar infoBar) {
33         View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
34         if (view == null) {
35             return false;
36         }
37
38         TextView text = (TextView) view.findViewById(R.id.infobar_message);
39
40         SpannableString spannable = (SpannableString) text.getText();
41         ClickableSpan[] clickable =
42             spannable.getSpans(0, spannable.length() -1, ClickableSpan.class);
43         if (clickable.length <= 0) {
44             return false;
45         }
46
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
50         // is more reliable.
51         int x = spannable.getSpanStart(clickable[0]) + 1;
52         float nChars = text.getLayout().getLineVisibleEnd(0);
53
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;
58
59         TestTouchUtils.singleClickView(test.getInstrumentation(), text, (int) xPos, (int) yPos);
60
61         return verifyInfoBarText(infoBar,
62             test.getActivity().getString(R.string.translate_infobar_change_languages));
63     }
64
65     public static boolean verifyInfoBarText(InfoBar infoBar, String text) {
66         View view = infoBar.getContentWrapper().findViewById(R.id.infobar_message);
67         if (view == null) {
68             return false;
69         }
70         String infoBarText = findInfoBarText(view);
71         return text.equals(infoBarText);
72     }
73
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;
77     }
78 }