ConsistencyTest.java revision f78a300c82748e29a3890c8f17a13726aacf33be
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v7.graphics;
18
19import android.graphics.Bitmap;
20import android.graphics.BitmapFactory;
21import android.test.InstrumentationTestCase;
22
23/**
24 * @hide
25 */
26public class ConsistencyTest extends InstrumentationTestCase {
27
28    private static final int NUMBER_TRIALS = 10;
29
30    public void testConsistency() {
31        Bitmap icon = BitmapFactory.decodeResource(getInstrumentation().getContext().getResources(),
32                android.R.drawable.sym_def_app_icon);
33
34        testConsistencyForBitmap(icon);
35    }
36
37    private void testConsistencyForBitmap(Bitmap bitmap) {
38        Palette lastPalette = null;
39
40        for (int i = 0; i < NUMBER_TRIALS; i++) {
41            Palette newPalette = Palette.from(bitmap).generate();
42            if (lastPalette != null) {
43                assetPalettesEqual(lastPalette, newPalette);
44            }
45            lastPalette = newPalette;
46        }
47    }
48
49    private static void assetPalettesEqual(Palette p1, Palette p2) {
50        assertEquals(p1.getVibrantSwatch(), p2.getVibrantSwatch());
51        assertEquals(p1.getLightVibrantSwatch(), p2.getLightVibrantSwatch());
52        assertEquals(p1.getDarkVibrantSwatch(), p2.getDarkVibrantSwatch());
53        assertEquals(p1.getMutedSwatch(), p2.getMutedSwatch());
54        assertEquals(p1.getLightMutedSwatch(), p2.getLightMutedSwatch());
55        assertEquals(p1.getDarkMutedSwatch(), p2.getDarkMutedSwatch());
56    }
57}
58