1 /* 2 * Copyright (C) 2024 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 package com.android.tv.settings.connectivity; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.net.Uri; 23 24 import org.junit.Before; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.mockito.MockitoAnnotations; 28 import org.robolectric.RobolectricTestRunner; 29 import org.robolectric.RuntimeEnvironment; 30 31 /** 32 * Tests for {@link NetworkChangeContentProvider}. 33 */ 34 @RunWith(RobolectricTestRunner.class) 35 public class NetworkChangeContentProviderTest { 36 37 private NetworkChangeContentProvider mContentProvider; 38 private Context mContext; 39 40 @Before setUp()41 public void setUp() { 42 MockitoAnnotations.initMocks(this); 43 mContext = RuntimeEnvironment.application; 44 mContentProvider = new NetworkChangeContentProvider(); 45 mContentProvider.attachInfo(mContext, null); 46 } 47 48 @Test onCreate_defaultState_returnsTrue()49 public void onCreate_defaultState_returnsTrue() { 50 assertThat(mContentProvider.onCreate()).isTrue(); 51 } 52 53 @Test query_currentNetworkCount_returnsInt()54 public void query_currentNetworkCount_returnsInt() { 55 mContext.getSharedPreferences(NetworkChangeDetectionConfigs.SHARED_PREFERENCES_NAME, 56 Context.MODE_PRIVATE).edit().putInt(NetworkChangeDetectionConfigs.PREFERENCE_KEY, 57 1).commit(); 58 Uri contentUri = Uri.parse( 59 "content://" + NetworkChangeDetectionConfigs.AUTHORITY + "/" 60 + NetworkChangeDetectionConfigs.PREFERENCE_KEY); 61 Cursor cursor = 62 mContentProvider.query( 63 contentUri, 64 /* projection= */ null, 65 /* selection= */ null, 66 /* selectionArgs= */ null, 67 /* sortOrder= */ null); 68 69 assertThat(cursor).isNotNull(); 70 assertThat(cursor.getCount()).isEqualTo(1); 71 assertThat(cursor.getColumnCount()).isEqualTo(1); 72 assertThat(cursor.moveToFirst()).isTrue(); 73 } 74 75 @Test(expected = IllegalArgumentException.class) query_incorrectContentUri_returnsNull()76 public void query_incorrectContentUri_returnsNull() throws IllegalArgumentException { 77 String wrongKey = "wrong_test_key"; 78 Uri contentUri = Uri.parse( 79 "content://" + NetworkChangeDetectionConfigs.AUTHORITY + "/" + wrongKey); 80 81 Cursor cursor = 82 mContentProvider.query( 83 contentUri, 84 /* projection= */ null, 85 /* selection= */ null, 86 /* selectionArgs= */ null, 87 /* sortOrder= */ null); 88 89 assertThat(cursor).isNull(); 90 } 91 } 92 93