1/*
2 * Copyright (C) 2016 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.view;
18
19import org.junit.Before;
20import org.junit.Rule;
21import org.junit.Test;
22import org.junit.rules.ExpectedException;
23import org.junit.runner.RunWith;
24
25import android.content.Context;
26import android.graphics.Bitmap;
27import android.graphics.drawable.BitmapDrawable;
28import android.graphics.drawable.Drawable;
29import android.support.test.InstrumentationRegistry;
30import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32import android.widget.ImageView;
33import android.widget.LinearLayout;
34import android.widget.RemoteViews;
35import android.widget.TextView;
36
37import com.android.frameworks.coretests.R;
38
39import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertSame;
41
42/**
43 * Tests for RemoteViews.
44 */
45@RunWith(AndroidJUnit4.class)
46@SmallTest
47public class RemoteViewsTest {
48
49    @Rule
50    public final ExpectedException exception = ExpectedException.none();
51
52    private Context mContext;
53    private String mPackage;
54    private LinearLayout mContainer;
55
56    @Before
57    public void setup() {
58        mContext = InstrumentationRegistry.getContext();
59        mPackage = mPackage;
60        mContainer = new LinearLayout(mContext);
61    }
62
63    @Test
64    public void clone_doesNotCopyBitmap() {
65        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
66        Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
67
68        original.setImageViewBitmap(R.id.image, bitmap);
69        RemoteViews clone = original.clone();
70        View inflated = clone.apply(mContext, mContainer);
71
72        Drawable drawable = ((ImageView) inflated.findViewById(R.id.image)).getDrawable();
73        assertSame(bitmap, ((BitmapDrawable)drawable).getBitmap());
74    }
75
76    @Test
77    public void clone_originalCanStillBeApplied() {
78        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
79
80        RemoteViews clone = original.clone();
81
82        clone.apply(mContext, mContainer);
83    }
84
85    @Test
86    public void clone_clones() {
87        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
88
89        RemoteViews clone = original.clone();
90        original.setTextViewText(R.id.text, "test");
91        View inflated = clone.apply(mContext, mContainer);
92
93        TextView textView = (TextView) inflated.findViewById(R.id.text);
94        assertEquals("", textView.getText());
95    }
96
97    @Test
98    public void clone_child_fails() {
99        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
100        RemoteViews child = new RemoteViews(mPackage, R.layout.remote_views_test);
101
102        original.addView(R.id.layout, child);
103
104        exception.expect(IllegalStateException.class);
105        RemoteViews clone = child.clone();
106    }
107
108    @Test
109    public void clone_repeatedly() {
110        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
111
112        original.clone();
113        original.clone();
114
115        original.apply(mContext, mContainer);
116    }
117
118    @Test
119    public void clone_chained() {
120        RemoteViews original = new RemoteViews(mPackage, R.layout.remote_views_test);
121
122        RemoteViews clone = original.clone().clone();
123
124        clone.apply(mContext, mContainer);
125    }
126
127}
128