1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.content.res.AssetFileDescriptor;
20import android.content.res.Configuration;
21import android.test.AndroidTestCase;
22import android.text.TextUtils;
23import android.util.DisplayMetrics;
24import android.view.inputmethod.EditorInfo;
25
26import com.android.inputmethod.keyboard.KeyboardId;
27
28import java.io.File;
29import java.io.InputStream;
30import java.util.Locale;
31
32public class SuggestTestsBase extends AndroidTestCase {
33    protected File mTestPackageFile;
34
35    @Override
36    protected void setUp() throws Exception {
37        super.setUp();
38        mTestPackageFile = new File(getTestContext().getApplicationInfo().sourceDir);
39    }
40
41    protected KeyboardId createKeyboardId(Locale locale, int orientation) {
42        final DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
43        final int width;
44        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
45            width = Math.max(dm.widthPixels, dm.heightPixels);
46        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
47            width = Math.min(dm.widthPixels, dm.heightPixels);
48        } else {
49            fail("Orientation should be ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT: "
50                    + "orientation=" + orientation);
51            return null;
52        }
53        return new KeyboardId(locale.toString() + " keyboard",
54                com.android.inputmethod.latin.R.xml.kbd_qwerty, locale, orientation, width,
55                KeyboardId.MODE_TEXT, new EditorInfo(), false, KeyboardId.F2KEY_MODE_NONE,
56                false, false, false);
57    }
58
59    protected InputStream openTestRawResource(int resIdInTest) {
60        return getTestContext().getResources().openRawResource(resIdInTest);
61    }
62
63    protected AssetFileDescriptor openTestRawResourceFd(int resIdInTest) {
64        return getTestContext().getResources().openRawResourceFd(resIdInTest);
65    }
66
67    private static String format(String message, Object expected, Object actual) {
68        return message + " expected:<" + expected + "> but was:<" + actual + ">";
69    }
70
71    protected static void suggested(CharSequence expected, CharSequence actual) {
72        if (!TextUtils.equals(expected, actual))
73            fail(format("assertEquals", expected, actual));
74    }
75
76    protected static void suggested(String message, CharSequence expected, CharSequence actual) {
77        if (!TextUtils.equals(expected, actual))
78            fail(format(message, expected, actual));
79    }
80
81    protected static void notSuggested(CharSequence expected, CharSequence actual) {
82        if (TextUtils.equals(expected, actual))
83            fail(format("assertNotEquals", expected, actual));
84    }
85
86    protected static void notSuggested(String message, CharSequence expected, CharSequence actual) {
87        if (TextUtils.equals(expected, actual))
88            fail(format(message, expected, actual));
89    }
90
91    protected static void isInSuggestions(String message, int position) {
92        assertTrue(message, position >= 0);
93    }
94
95    protected static void isNotInSuggestions(String message, int position) {
96        assertTrue(message, position < 0);
97    }
98}
99