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 177da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roospackage android.view; 187da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 197da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport org.junit.Before; 207da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport org.junit.Rule; 217da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport org.junit.Test; 227da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport org.junit.rules.ExpectedException; 237da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport org.junit.runner.RunWith; 247da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 257da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.content.Context; 267da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.graphics.Bitmap; 277da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.graphics.drawable.BitmapDrawable; 287da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.graphics.drawable.Drawable; 297da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.support.test.InstrumentationRegistry; 307da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.support.test.filters.SmallTest; 317da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.support.test.runner.AndroidJUnit4; 327da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.widget.ImageView; 337da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.widget.LinearLayout; 347da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.widget.RemoteViews; 357da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport android.widget.TextView; 367da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 377da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport com.android.frameworks.coretests.R; 387da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 397da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport static org.junit.Assert.assertEquals; 407da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roosimport static org.junit.Assert.assertSame; 417da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 427da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos/** 437da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos * Tests for RemoteViews. 447da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos */ 457da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos@RunWith(AndroidJUnit4.class) 467da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos@SmallTest 477da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roospublic class RemoteViewsTest { 487da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 497da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Rule 507da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public final ExpectedException exception = ExpectedException.none(); 517da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 527da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos private Context mContext; 537da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos private String mPackage; 547da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos private LinearLayout mContainer; 557da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 567da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Before 577da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void setup() { 587da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos mContext = InstrumentationRegistry.getContext(); 597da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos mPackage = mPackage; 607da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos mContainer = new LinearLayout(mContext); 617da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 627da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 637da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Test 647da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void clone_doesNotCopyBitmap() { 657da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test); 667da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); 677da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 687da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos original.setImageViewBitmap(R.id.image, bitmap); 697da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews clone = original.clone(); 707da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos View inflated = clone.apply(mContext, mContainer); 717da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 727da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos Drawable drawable = ((ImageView) inflated.findViewById(R.id.image)).getDrawable(); 737da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos assertSame(bitmap, ((BitmapDrawable)drawable).getBitmap()); 747da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 757da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 767da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Test 777da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void clone_originalCanStillBeApplied() { 787da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test); 797da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 807da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews clone = original.clone(); 817da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 827da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos clone.apply(mContext, mContainer); 837da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 847da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 857da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Test 867da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void clone_clones() { 877da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test); 887da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 897da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews clone = original.clone(); 907da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos original.setTextViewText(R.id.text, "test"); 917da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos View inflated = clone.apply(mContext, mContainer); 927da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 937da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos TextView textView = (TextView) inflated.findViewById(R.id.text); 947da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos assertEquals("", textView.getText()); 957da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 967da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 977da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Test 987da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void clone_child_fails() { 997da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test); 1007da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews child = new RemoteViews(mPackage, R.layout.remote_views_test); 1017da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1027da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos original.addView(R.id.layout, child); 1037da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1047da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos exception.expect(IllegalStateException.class); 1057da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews clone = child.clone(); 1067da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 1077da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1087da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Test 1097da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void clone_repeatedly() { 1107da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test); 1117da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1127da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos original.clone(); 1137da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos original.clone(); 1147da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1157da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos original.apply(mContext, mContainer); 1167da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 1177da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1187da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos @Test 1197da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos public void clone_chained() { 1207da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test); 1217da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1227da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos RemoteViews clone = original.clone().clone(); 1237da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1247da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos clone.apply(mContext, mContainer); 1257da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos } 1267da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos 1277da889d81d2ff4c1704dc0a64389947fb30de5d8Adrian Roos} 128