RemoteViewsTest.java revision 8376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64c
17da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos/*
27da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * Copyright (C) 2016 The Android Open Source Project
37da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos *
47da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License");
57da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * you may not use this file except in compliance with the License.
67da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * You may obtain a copy of the License at
77da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos *
87da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos *      http://www.apache.org/licenses/LICENSE-2.0
97da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos *
107da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * Unless required by applicable law or agreed to in writing, software
117da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS,
127da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * See the License for the specific language governing permissions and
147da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * limitations under the License
157da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos */
167da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
178376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovpackage android.widget;
187da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
197da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.content.Context;
207da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.graphics.Bitmap;
217da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.graphics.drawable.BitmapDrawable;
227da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.graphics.drawable.Drawable;
237da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.support.test.InstrumentationRegistry;
247da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.support.test.filters.SmallTest;
257da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.support.test.runner.AndroidJUnit4;
268376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovimport android.view.View;
277da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
287da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport com.android.frameworks.coretests.R;
297da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
308376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovimport org.junit.Before;
318376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovimport org.junit.Rule;
328376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovimport org.junit.Test;
338376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovimport org.junit.rules.ExpectedException;
348376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikovimport org.junit.runner.RunWith;
358376f9eaa0bd41a9c7ce7ada4cb7c2cda28ef64cKirill Grouchnikov
367da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport static org.junit.Assert.assertEquals;
377da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport static org.junit.Assert.assertSame;
387da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
397da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos/**
407da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * Tests for RemoteViews.
417da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos */
427da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos@RunWith(AndroidJUnit4.class)
437da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos@SmallTest
447da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roospublic class RemoteViewsTest {
457da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
467da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Rule
477da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public final ExpectedException exception = ExpectedException.none();
487da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
497da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    private Context mContext;
507da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    private String mPackage;
517da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    private LinearLayout mContainer;
527da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
537da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Before
547da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void setup() {
557da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        mContext = InstrumentationRegistry.getContext();
564d10db7fdd1dc86684f31ec582872072fb02c350Adrian Roos        mPackage = mContext.getPackageName();
577da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        mContainer = new LinearLayout(mContext);
587da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
597da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
607da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Test
617da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void clone_doesNotCopyBitmap() {
627da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
637da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
647da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
657da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        original.setImageViewBitmap(R.id.image, bitmap);
667da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews clone = original.clone();
677da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        View inflated = clone.apply(mContext, mContainer);
687da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
697da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        Drawable drawable = ((ImageView) inflated.findViewById(R.id.image)).getDrawable();
707da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        assertSame(bitmap, ((BitmapDrawable)drawable).getBitmap());
717da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
727da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
737da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Test
747da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void clone_originalCanStillBeApplied() {
757da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
767da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
777da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews clone = original.clone();
787da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
797da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        clone.apply(mContext, mContainer);
807da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
817da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
827da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Test
837da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void clone_clones() {
847da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
857da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
867da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews clone = original.clone();
877da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        original.setTextViewText(R.id.text, "test");
887da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        View inflated = clone.apply(mContext, mContainer);
897da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
907da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        TextView textView = (TextView) inflated.findViewById(R.id.text);
917da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        assertEquals("", textView.getText());
927da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
937da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
947da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Test
957da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void clone_child_fails() {
967da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
977da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews child = new RemoteViews(mPackage, R.layout.remote_views_test);
987da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
997da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        original.addView(R.id.layout, child);
1007da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1017da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        exception.expect(IllegalStateException.class);
1027da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews clone = child.clone();
1037da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
1047da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1057da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Test
1067da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void clone_repeatedly() {
1077da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
1087da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1097da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        original.clone();
1107da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        original.clone();
1117da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1127da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        original.apply(mContext, mContainer);
1137da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
1147da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1157da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    @Test
1167da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    public void clone_chained() {
1177da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
1187da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1197da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        RemoteViews clone = original.clone().clone();
1207da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1217da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos        clone.apply(mContext, mContainer);
1227da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos    }
1237da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos
1247da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos}
125