Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 android.content.cts;
     18 
     19 import android.content.ContentUris;
     20 import android.net.Uri;
     21 import android.net.Uri.Builder;
     22 import android.test.AndroidTestCase;
     23 import dalvik.annotation.TestTargets;
     24 import dalvik.annotation.TestLevel;
     25 import dalvik.annotation.TestTargetNew;
     26 import dalvik.annotation.TestTargetClass;
     27 import dalvik.annotation.ToBeFixed;
     28 
     29 @TestTargetClass(ContentUris.class)
     30 public class ContentUrisTest extends AndroidTestCase {
     31     private static final String AUTHORITY = "ctstest";
     32     private static final String PATH1 = "testPath1";
     33     private static final String PATH2 = "testPath2";
     34 
     35     private static final int CODE1 = 1;
     36     private static final int CODE2 = 2;
     37 
     38     private Uri uri1 = Uri.parse("content://" + AUTHORITY + "/" + PATH1);
     39     private Uri uri2 = Uri.parse("content://" + AUTHORITY + "/" + PATH2);
     40 
     41     @TestTargetNew(
     42         level = TestLevel.COMPLETE,
     43         notes = "Test constructor(s) of ContentUris.",
     44         method = "ContentUris",
     45         args = {}
     46     )
     47     public void testConstructor() {
     48         new ContentUris();
     49     }
     50 
     51     @TestTargetNew(
     52         level = TestLevel.COMPLETE,
     53         notes = "Test parseId(Uri contentUri).",
     54         method = "parseId",
     55         args = {android.net.Uri.class}
     56     )
     57     public void testParseId() {
     58         Uri result = ContentUris.withAppendedId(uri1, CODE1);
     59         assertEquals(CODE1, ContentUris.parseId(result));
     60 
     61         result = ContentUris.withAppendedId(uri2, CODE2);
     62         assertEquals(CODE2, ContentUris.parseId(result));
     63 
     64         // no code in Uri
     65         assertEquals(-1, ContentUris.parseId(Uri.parse("")));
     66     }
     67 
     68     @TestTargetNew(
     69         level = TestLevel.COMPLETE,
     70         notes = "Test parseId(Uri contentUri).",
     71         method = "parseId",
     72         args = {android.net.Uri.class}
     73     )
     74     @ToBeFixed(bug = "", explanation =
     75         "There should not be a NullPointerException thrown out," +
     76         "and should be an UnsupportedOperationException thrown out.")
     77     public void testParseIdFailure() {
     78         try {
     79             ContentUris.parseId(uri1);
     80             fail("There should be a NumberFormatException thrown out.");
     81         } catch (NumberFormatException e) {
     82             //expected, test success.
     83         }
     84 
     85         Uri uri = Uri.fromParts("abc", "123", null);
     86         ContentUris.parseId(uri);
     87 
     88         try {
     89             ContentUris.parseId(null);
     90             fail("There should be a NullPointerException thrown out.");
     91         } catch (NullPointerException e) {
     92             // expected, test success.
     93         }
     94     }
     95 
     96     @TestTargetNew(
     97         level = TestLevel.COMPLETE,
     98         notes = "Test withAppendedId(Uri contentUri, long id).",
     99         method = "withAppendedId",
    100         args = {android.net.Uri.class, long.class}
    101     )
    102     public void testWithAppendedId() {
    103         String expected = "content://" + AUTHORITY + "/" + PATH1 + "/" + CODE1;
    104         Uri actually;
    105 
    106         assertNotNull(actually = ContentUris.withAppendedId(uri1, CODE1));
    107         assertEquals(expected, actually.toString());
    108 
    109         expected = "content://" + AUTHORITY + "/" + PATH2 + "/" + CODE2;
    110         assertNotNull(actually = ContentUris.withAppendedId(uri2, CODE2));
    111         assertEquals(expected, actually.toString());
    112     }
    113 
    114     @TestTargetNew(
    115         level = TestLevel.COMPLETE,
    116         notes = "Test withAppendedId(Uri contentUri, long id).",
    117         method = "withAppendedId",
    118         args = {android.net.Uri.class, long.class}
    119     )
    120     @ToBeFixed(bug = "1417734", explanation = "Unexpected NullPointerException")
    121     public void testWithAppendedIdFailure() {
    122         try {
    123             ContentUris.withAppendedId(null, -1);
    124             fail("There should be a NullPointerException thrown out.");
    125         } catch (NullPointerException e) {
    126             // expected, test success.
    127         }
    128     }
    129 
    130     @TestTargetNew(
    131         level = TestLevel.COMPLETE,
    132         notes = "Test appendId(Builder builder, long id).",
    133         method = "appendId",
    134         args = {android.net.Uri.Builder.class, long.class}
    135     )
    136     public void testAppendId() {
    137         String expected = "content://" + AUTHORITY + "/" + PATH1 + "/" + CODE1;
    138         Builder actually;
    139         Builder b = uri1.buildUpon();
    140 
    141         assertNotNull(actually = ContentUris.appendId(b, CODE1));
    142         assertEquals(expected, actually.toString());
    143 
    144         expected = "content://" + AUTHORITY + "/" + PATH2 + "/" + CODE2;
    145         b = uri2.buildUpon();
    146 
    147         assertNotNull(actually = ContentUris.appendId(b, CODE2));
    148         assertEquals(expected, actually.toString());
    149     }
    150 
    151     @TestTargetNew(
    152         level = TestLevel.COMPLETE,
    153         notes = "Test appendId(Builder builder, long id).",
    154         method = "appendId",
    155         args = {android.net.Uri.Builder.class, long.class}
    156     )
    157     @ToBeFixed(bug = "1417734", explanation = "Unexpected NullPointerException")
    158     public void testAppendIdFailure() {
    159         try {
    160             ContentUris.appendId(null, -1);
    161             fail("There should be a NullPointerException thrown out.");
    162         } catch (NullPointerException e) {
    163             // expected, test success.
    164         }
    165     }
    166 }
    167