Add TvProvider permission tests

Bug: 18010743
Change-Id: I883a1d7b99f0d17b1330736966eede2ea69debf9
diff --git a/tests/tests/permission/src/android/permission/cts/TvPermissionTest.java b/tests/tests/permission/src/android/permission/cts/TvPermissionTest.java
new file mode 100644
index 0000000..0c648c5
--- /dev/null
+++ b/tests/tests/permission/src/android/permission/cts/TvPermissionTest.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.permission.cts;
+
+import android.content.ContentValues;
+import android.content.pm.PackageManager;
+import android.media.tv.TvContract;
+import android.net.Uri;
+import android.test.AndroidTestCase;
+
+/**
+ * Tests for TV API related permissions.
+ */
+public class TvPermissionTest extends AndroidTestCase {
+    private static final String DUMMY_INPUT_ID = "dummy";
+
+    private boolean mHasTvInputFramework;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mHasTvInputFramework = getContext().getPackageManager()
+                .hasSystemFeature(PackageManager.FEATURE_LIVE_TV);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    private void verifyQuery(Uri uri, final String[] projection,
+            String tableName) throws Exception {
+        try {
+            getContext().getContentResolver().query(uri, projection, null, null, null);
+            fail("Accessing " + tableName + " table should require READ_EPG_DATA permission.");
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    public void verifyInsert(Uri uri, String tableName) throws Exception {
+        try {
+            ContentValues values = new ContentValues();
+            getContext().getContentResolver().insert(uri, values);
+            fail("Accessing " + tableName + " table should require WRITE_EPG_DATA permission.");
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    public void verifyUpdate(Uri uri, String tableName) throws Exception {
+        try {
+            ContentValues values = new ContentValues();
+            getContext().getContentResolver().update(uri, values, null, null);
+            fail("Accessing " + tableName + " table should require WRITE_EPG_DATA permission.");
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    public void verifyDelete(Uri uri, String tableName) throws Exception {
+        try {
+            getContext().getContentResolver().delete(uri, null, null);
+            fail("Accessing " + tableName + " table should require WRITE_EPG_DATA permission.");
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    public void testQueryChannels() throws Exception {
+        if (!mHasTvInputFramework) return;
+        final String[] projection = { TvContract.Channels._ID };
+        verifyQuery(TvContract.Channels.CONTENT_URI, projection, "channels");
+    }
+
+    public void testInsertChannels() throws Exception {
+        if (!mHasTvInputFramework) return;
+        verifyInsert(TvContract.Channels.CONTENT_URI, "channels");
+    }
+
+    public void testUpdateChannels() throws Exception {
+        if (!mHasTvInputFramework) return;
+        verifyUpdate(TvContract.Channels.CONTENT_URI, "channels");
+    }
+
+    public void testDeleteChannels() throws Exception {
+        if (!mHasTvInputFramework) return;
+        verifyDelete(TvContract.Channels.CONTENT_URI, "channels");
+    }
+
+    public void testQueryPrograms() throws Exception {
+        if (!mHasTvInputFramework) return;
+        final String[] projection = { TvContract.Programs._ID };
+        verifyQuery(TvContract.Programs.CONTENT_URI, projection, "programs");
+    }
+
+    public void testInsertPrograms() throws Exception {
+        if (!mHasTvInputFramework) return;
+        verifyInsert(TvContract.Programs.CONTENT_URI, "programs");
+    }
+
+    public void testUpdatePrograms() throws Exception {
+        if (!mHasTvInputFramework) return;
+        verifyUpdate(TvContract.Programs.CONTENT_URI, "programs");
+    }
+
+    public void testDeletePrograms() throws Exception {
+        if (!mHasTvInputFramework) return;
+        verifyDelete(TvContract.Programs.CONTENT_URI, "programs");
+    }
+}
diff --git a/tests/tests/tv/src/android/media/tv/cts/TvContractTest.java b/tests/tests/tv/src/android/media/tv/cts/TvContractTest.java
index 651a199a..857e489 100644
--- a/tests/tests/tv/src/android/media/tv/cts/TvContractTest.java
+++ b/tests/tests/tv/src/android/media/tv/cts/TvContractTest.java
@@ -395,4 +395,111 @@
         verifyOverlap(programEndMillis + hour, programEndMillis + hour * 2, 0,
                 channelId, channelUri);
     }
+
+    private void verifyQueryWithSortOrder(Uri uri, final String[] projection,
+            String sortOrder) throws Exception {
+        try {
+            getContext().getContentResolver().query(uri, projection, null, null, sortOrder);
+            fail("Setting sortOrder should fail without ACCESS_ALL_EPG_DATA permission for " + uri);
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    private void verifyQueryWithSelection(Uri uri, final String[] projection,
+            String selection) throws Exception {
+        try {
+            getContext().getContentResolver().query(uri, projection, selection, null, null);
+            fail("Setting selection should fail without ACCESS_ALL_EPG_DATA permission for " + uri);
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    private void verifyUpdateWithSelection(Uri uri, String selection) throws Exception {
+        try {
+            ContentValues values = new ContentValues();
+            getContext().getContentResolver().update(uri, values, selection, null);
+            fail("Setting selection should fail without ACCESS_ALL_EPG_DATA permission for " + uri);
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    private void verifyDeleteWithSelection(Uri uri, String selection) throws Exception {
+        try {
+            getContext().getContentResolver().delete(uri, selection, null);
+            fail("Setting selection should fail without ACCESS_ALL_EPG_DATA permission for " + uri);
+        } catch (SecurityException e) {
+            // Expected exception
+        }
+    }
+
+    public void testAllEpgPermissionBlocksSortOrderOnQuery_Channels() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        final String[] projection = { TvContract.Channels._ID };
+        verifyQueryWithSortOrder(TvContract.Channels.CONTENT_URI, projection,
+                TvContract.Channels._ID + " ASC");
+    }
+
+    public void testAllEpgPermissionBlocksSelectionOnQuery_Channels() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        final String[] projection = { TvContract.Channels._ID };
+        verifyQueryWithSelection(TvContract.Channels.CONTENT_URI, projection,
+                TvContract.Channels._ID + ">0");
+    }
+
+    public void testAllEpgPermissionBlocksSelectionOnUpdate_Channels() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        verifyUpdateWithSelection(TvContract.Channels.CONTENT_URI,
+                TvContract.Channels._ID + ">0");
+    }
+
+    public void testAllEpgPermissionBlocksSelectionOnDelete_Channels() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        verifyDeleteWithSelection(TvContract.Channels.CONTENT_URI,
+                TvContract.Channels._ID + ">0");
+    }
+
+    public void testAllEpgPermissionBlocksSortOrderOnQuery_Programs() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        final String[] projection = { TvContract.Programs._ID };
+        verifyQueryWithSortOrder(TvContract.Programs.CONTENT_URI, projection,
+                TvContract.Programs._ID + " ASC");
+    }
+
+    public void testAllEpgPermissionBlocksSelectionOnQuery_Programs() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        final String[] projection = { TvContract.Channels._ID };
+        verifyQueryWithSelection(TvContract.Programs.CONTENT_URI, projection,
+                TvContract.Programs._ID + ">0");
+    }
+
+    public void testAllEpgPermissionBlocksSelectionOnUpdate_Programs() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        verifyUpdateWithSelection(TvContract.Programs.CONTENT_URI,
+                TvContract.Programs._ID + ">0");
+    }
+
+    public void testAllEpgPermissionBlocksSelectionOnDelete_Programs() throws Exception {
+        if (!Utils.hasTvInputFramework(getContext())) {
+            return;
+        }
+        verifyDeleteWithSelection(TvContract.Programs.CONTENT_URI,
+                TvContract.Programs._ID + ">0");
+    }
 }