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 
17 package com.android.tv.common;
18 
19 import android.content.ComponentCallbacks2;
20 import android.media.tv.TvContentRating;
21 import android.test.AndroidTestCase;
22 import android.test.MoreAsserts;
23 import android.test.suitebuilder.annotation.SmallTest;
24 
25 import com.android.tv.testing.TvContentRatingConstants;
26 import com.android.tv.util.Utils;
27 
28 /**
29  * Test for {@link android.media.tv.TvContentRating} tests in {@link Utils}.
30  */
31 @SmallTest
32 public class TvContentRatingCacheTest extends AndroidTestCase {
33 
34     /**
35      * US_TV_MA and US_TV_Y7 in order
36      */
37     public static final String MA_AND_Y7 = TvContentRatingConstants.STRING_US_TV_MA + ","
38             + TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV;
39 
40     /**
41      * US_TV_MA and US_TV_Y7 not in order
42      */
43     public static final String Y7_AND_MA = TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV + ","
44             + TvContentRatingConstants.STRING_US_TV_MA;
45     TvContentRatingCache mCache = TvContentRatingCache.getInstance();
46 
47     @Override
setUp()48     protected void setUp() throws Exception {
49         super.setUp();
50         mCache.performTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
51     }
52 
53     @Override
tearDown()54     protected void tearDown() throws Exception {
55         mCache.performTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
56         super.tearDown();
57     }
58 
testGetRatings_US_TV_MA()59     public void testGetRatings_US_TV_MA() {
60         TvContentRating[] result = mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA);
61         MoreAsserts.assertEquals(asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_MA), result);
62     }
63 
testGetRatings_US_TV_MA_same()64     public void testGetRatings_US_TV_MA_same() {
65         TvContentRating[] first = mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA);
66         TvContentRating[] second = mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA);
67         assertSame(first, second);
68     }
69 
testGetRatings_US_TV_MA_diffAfterClear()70     public void testGetRatings_US_TV_MA_diffAfterClear() {
71         TvContentRating[] first = mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA);
72         mCache.performTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
73         TvContentRating[] second = mCache.getRatings(TvContentRatingConstants.STRING_US_TV_MA);
74         assertNotSame(first, second);
75     }
76 
testGetRatings_TWO_orderDoesNotMatter()77     public void testGetRatings_TWO_orderDoesNotMatter() {
78         TvContentRating[] first = mCache.getRatings(MA_AND_Y7);
79         TvContentRating[] second = mCache.getRatings(Y7_AND_MA);
80         assertSame(first, second);
81     }
82 
testContentRatingsToString_null()83     public void testContentRatingsToString_null() {
84         String result = TvContentRatingCache.contentRatingsToString(null);
85         assertEquals("ratings string", null, result);
86     }
87 
testContentRatingsToString_none()88     public void testContentRatingsToString_none() {
89         String result = TvContentRatingCache.contentRatingsToString(asArray());
90         assertEquals("ratings string", null, result);
91     }
92 
testContentRatingsToString_one()93     public void testContentRatingsToString_one() {
94         String result = TvContentRatingCache
95                 .contentRatingsToString(asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_MA));
96         assertEquals("ratings string", TvContentRatingConstants.STRING_US_TV_MA, result);
97     }
98 
testContentRatingsToString_twoInOrder()99     public void testContentRatingsToString_twoInOrder() {
100         String result = TvContentRatingCache.contentRatingsToString(
101                 asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_MA,
102                         TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV));
103         assertEquals("ratings string", MA_AND_Y7, result);
104     }
105 
testContentRatingsToString_twoNotInOrder()106     public void testContentRatingsToString_twoNotInOrder() {
107         String result = TvContentRatingCache.contentRatingsToString(asArray(
108                 TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV,
109                 TvContentRatingConstants.CONTENT_RATING_US_TV_MA));
110         assertEquals("ratings string", MA_AND_Y7, result);
111     }
112 
testContentRatingsToString_double()113     public void testContentRatingsToString_double() {
114         String result = TvContentRatingCache.contentRatingsToString(asArray(
115                 TvContentRatingConstants.CONTENT_RATING_US_TV_MA,
116                 TvContentRatingConstants.CONTENT_RATING_US_TV_MA));
117         assertEquals("ratings string", TvContentRatingConstants.STRING_US_TV_MA, result);
118     }
119 
testStringToContentRatings_null()120     public void testStringToContentRatings_null() {
121         assertNull(TvContentRatingCache.stringToContentRatings(null));
122     }
123 
testStringToContentRatings_none()124     public void testStringToContentRatings_none() {
125         assertNull(TvContentRatingCache.stringToContentRatings(""));
126     }
127 
testStringToContentRatings_bad()128     public void testStringToContentRatings_bad() {
129         assertNull(TvContentRatingCache.stringToContentRatings("bad"));
130     }
131 
testStringToContentRatings_oneGoodOneBad()132     public void testStringToContentRatings_oneGoodOneBad() {
133         TvContentRating[] results = TvContentRatingCache
134                 .stringToContentRatings(TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV + ",bad");
135         MoreAsserts.assertEquals("ratings",
136                 asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV), results);
137     }
138 
testStringToContentRatings_one()139     public void testStringToContentRatings_one() {
140         TvContentRating[] results = TvContentRatingCache
141                 .stringToContentRatings(TvContentRatingConstants.STRING_US_TV_Y7_US_TV_FV);
142         MoreAsserts.assertEquals("ratings",
143                 asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV), results);
144     }
145 
testStringToContentRatings_twoNotInOrder()146     public void testStringToContentRatings_twoNotInOrder() {
147         TvContentRating[] results = TvContentRatingCache.stringToContentRatings(Y7_AND_MA);
148         MoreAsserts.assertEquals("ratings",
149                 asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_MA,
150                         TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV), results);
151     }
152 
testStringToContentRatings_twoInOrder()153     public void testStringToContentRatings_twoInOrder() {
154         TvContentRating[] results = TvContentRatingCache.stringToContentRatings(MA_AND_Y7);
155         MoreAsserts.assertEquals("ratings",
156                 asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_MA,
157                         TvContentRatingConstants.CONTENT_RATING_US_TV_Y7_US_TV_FV), results);
158     }
159 
testStringToContentRatings_double()160     public void testStringToContentRatings_double() {
161         TvContentRating[] results = TvContentRatingCache.stringToContentRatings(
162                 TvContentRatingConstants.STRING_US_TV_MA + ","
163                         + TvContentRatingConstants.STRING_US_TV_MA);
164         MoreAsserts
165                 .assertEquals("ratings", asArray(TvContentRatingConstants.CONTENT_RATING_US_TV_MA),
166                         results);
167     }
168 
asArray(TvContentRating... ratings)169     private static TvContentRating[] asArray(TvContentRating... ratings) {
170         return ratings;
171     }
172 }
173