1bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov/*
2bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * Copyright (C) 2016 The Android Open Source Project
3bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov *
4bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * you may not use this file except in compliance with the License.
6bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * You may obtain a copy of the License at
7bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov *
8bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov *
10bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * See the License for the specific language governing permissions and
14bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov * limitations under the License.
15bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov */
16bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov
17bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovpackage android.support.percent;
18bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov
19bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovimport android.app.Activity;
201e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikovimport android.app.Instrumentation;
21bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovimport android.support.test.InstrumentationRegistry;
221e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikovimport android.support.test.rule.ActivityTestRule;
23bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovimport android.support.test.runner.AndroidJUnit4;
24bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovimport android.test.ActivityInstrumentationTestCase2;
25ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikovimport junit.framework.Assert;
26bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovimport org.junit.Before;
271e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikovimport org.junit.Rule;
28bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikovimport org.junit.runner.RunWith;
29bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov
301e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikovimport static junit.framework.Assert.assertEquals;
311e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikov
32bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov@RunWith(AndroidJUnit4.class)
331e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikovpublic abstract class BaseInstrumentationTestCase<A extends Activity> {
341e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikov    @Rule
351e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikov    public final ActivityTestRule<A> mActivityTestRule;
36bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov
37bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov    protected BaseInstrumentationTestCase(Class<A> activityClass) {
381e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikov        mActivityTestRule = new ActivityTestRule<A>(activityClass);
39bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov    }
40bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov
41ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov    protected static void assertFuzzyEquals(String description, float expected, float actual) {
42ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov        // On devices with certain screen densities we may run into situations where multiplying
43ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov        // container width / height by a certain fraction ends up in a number that is almost but
44ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov        // not exactly a round float number. For example, we can do float math to compute 15%
45ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov        // of 1440 pixels and get 216.00002 due to inexactness of float math. This is why our
46ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov        // tolerance is slightly bigger than 1 pixel in the comparison below.
47ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov        assertEquals(description, expected, actual, 1.1f);
48ff6cfa41a3c76a979795b32985f8e1f7157cb33cKirill Grouchnikov    }
49bcbbc065a6ff7a0cd2ebb0b29cc9298a6fce77e5Kirill Grouchnikov}
50