1 /*
2  * Copyright (C) 2018 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 
17 package com.android.tv.util;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.pm.ProviderInfo;
22 import android.media.tv.TvContract;
23 import android.os.Bundle;
24 
25 import com.android.tv.testing.constants.ConfigConstants;
26 import com.android.tv.testing.fakes.FakeTvProvider;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.Robolectric;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.RuntimeEnvironment;
34 import org.robolectric.annotation.Config;
35 import org.robolectric.shadows.ShadowContentResolver;
36 
37 import java.util.Set;
38 
39 /** Tests for {@link TvProviderUtils}. */
40 @RunWith(RobolectricTestRunner.class)
41 @Config(
42         sdk = ConfigConstants.SDK,
43         shadows = {ShadowContentResolver.class})
44 public class TvProviderUtilsTest {
45 
46     @Before
setUp()47     public void setUp() {
48         ProviderInfo info = new ProviderInfo();
49         info.authority = TvContract.AUTHORITY;
50         FakeTvProvider provider =
51                 Robolectric.buildContentProvider(FakeTvProviderForTesting.class).create(info).get();
52         provider.onCreate();
53         ShadowContentResolver.registerProviderInternal(TvContract.AUTHORITY, provider);
54     }
55 
56     @Test
testAddExtraColumnsToProjection()57     public void testAddExtraColumnsToProjection() {
58         String[] inputStrings = {"column_1", "column_2", "column_3"};
59         assertThat(
60                         TvProviderUtils.addExtraColumnsToProjection(
61                                 inputStrings, TvProviderUtils.EXTRA_PROGRAM_COLUMN_STATE))
62                 .asList()
63                 .containsExactly(
64                         "column_1",
65                         "column_2",
66                         "column_3",
67                         TvProviderUtils.EXTRA_PROGRAM_COLUMN_STATE)
68                 .inOrder();
69     }
70 
71     @Test
testAddExtraColumnsToProjection_extraColumnExists()72     public void testAddExtraColumnsToProjection_extraColumnExists() {
73         String[] inputStrings = {
74             "column_1",
75             "column_2",
76             TvProviderUtils.EXTRA_PROGRAM_COLUMN_SERIES_ID,
77             TvProviderUtils.EXTRA_PROGRAM_COLUMN_STATE,
78             "column_3"
79         };
80         assertThat(
81                         TvProviderUtils.addExtraColumnsToProjection(
82                                 inputStrings, TvProviderUtils.EXTRA_PROGRAM_COLUMN_STATE))
83                 .asList()
84                 .containsExactly(
85                         "column_1",
86                         "column_2",
87                         TvProviderUtils.EXTRA_PROGRAM_COLUMN_SERIES_ID,
88                         TvProviderUtils.EXTRA_PROGRAM_COLUMN_STATE,
89                         "column_3")
90                 .inOrder();
91     }
92 
93     @Test
testGetExistingColumns_noException()94     public void testGetExistingColumns_noException() {
95         FakeTvProviderForTesting.mThrowException = false;
96         FakeTvProviderForTesting.mBundleResult = new Bundle();
97         FakeTvProviderForTesting.mBundleResult.putStringArray(
98                 TvContract.EXTRA_EXISTING_COLUMN_NAMES, new String[] {"column 1", "column 2"});
99         Set<String> columns =
100                 TvProviderUtils.getExistingColumns(
101                         RuntimeEnvironment.application, TvContract.Programs.CONTENT_URI);
102         assertThat(columns).containsExactly("column 1", "column 2");
103     }
104 
105     @Test
testGetExistingColumns_throwsException()106     public void testGetExistingColumns_throwsException() {
107         FakeTvProviderForTesting.mThrowException = true;
108         FakeTvProviderForTesting.mBundleResult = new Bundle();
109         // should be no exception here
110         Set<String> columns =
111                 TvProviderUtils.getExistingColumns(
112                         RuntimeEnvironment.application, TvContract.Programs.CONTENT_URI);
113         assertThat(columns).isEmpty();
114     }
115 
116     private static class FakeTvProviderForTesting extends FakeTvProvider {
117         private static Bundle mBundleResult;
118         private static boolean mThrowException;
119 
120         @Override
call(String method, String arg, Bundle extras)121         public Bundle call(String method, String arg, Bundle extras) {
122             if (mThrowException) {
123                 throw new IllegalStateException();
124             }
125             return mBundleResult;
126         }
127     }
128 }
129