summaryrefslogtreecommitdiff
path: root/tests/tests/provider/src/android/provider/cts/media/MediaStore_Audio_PlaylistsTest.java
blob: 32224c4aea36dbdf72423c90a52f0c0dcf3a014e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2009 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.provider.cts.media;

import static android.provider.cts.media.MediaStoreTest.TAG;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Playlists;
import android.provider.MediaStore.MediaColumns;
import android.provider.cts.ProviderTestUtils;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SdkSuppress;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@SdkSuppress(minSdkVersion = Build.VERSION_CODES.R)
@RunWith(Parameterized.class)
public class MediaStore_Audio_PlaylistsTest {
    private Context mContext;
    private ContentResolver mContentResolver;

    @Parameter(0)
    public String mVolumeName;

    @Parameters
    public static Iterable<? extends Object> data() {
        return ProviderTestUtils.getSharedVolumeNames();
    }

    @Before
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();
        mContentResolver = mContext.getContentResolver();

        Log.d(TAG, "Using volume " + mVolumeName);
    }

    @Test
    public void testGetContentUri() {
        Cursor c = null;
        assertNotNull(c = mContentResolver.query(
                Playlists.getContentUri(mVolumeName), null, null,
                null, null));
        c.close();
    }

    @Test
    public void testStoreAudioPlaylistsExternal() throws Exception {
        ContentValues values = new ContentValues();
        values.put(Playlists.NAME, "My favourites " + System.nanoTime());
        long dateAdded = System.currentTimeMillis() / 1000;
        long dateModified = System.currentTimeMillis() / 1000;
        values.put(Playlists.DATE_MODIFIED, dateModified);
        // insert
        Uri uri = mContentResolver.insert(Playlists.getContentUri(mVolumeName), values);
        assertNotNull(uri);

        try {
            // query
            Cursor c = mContentResolver.query(uri, null, null, null, null);
            assertEquals(1, c.getCount());
            c.moveToFirst();
            assertEquals(values.getAsString(Playlists.NAME),
                    c.getString(c.getColumnIndex(Playlists.NAME)));

            long realDateAdded = c.getLong(c.getColumnIndex(Playlists.DATE_ADDED));
            long realDateModified = c.getLong(c.getColumnIndex(Playlists.DATE_MODIFIED));
            assertTrue(realDateAdded >= dateAdded);
            // Sometimes the realDateModified is less than dateModified by exactly one second.
            // We've never seen any real issues with that and now the Playlists are deprecated.
            // Just changing the test to remove flakiness.
            assertTrue(Math.abs(dateModified - realDateModified) <= 1);
            assertTrue(c.getLong(c.getColumnIndex(Playlists._ID)) > 0);
            c.close();
        } finally {
            assertEquals(1, mContentResolver.delete(uri, null, null));
        }
    }

    /**
     * Verify that creating playlists using only {@link Playlists#NAME} defined
     * will flow into the {@link MediaColumns#DISPLAY_NAME}, both during initial
     * insert and subsequent updates.
     */
    @Test
    public void testName() throws Exception {
        final String name1 = "Playlist " + System.nanoTime();
        final String name2 = "Playlist " + System.nanoTime();
        assertNotEquals(name1, name2);

        final ContentValues values = new ContentValues();
        values.clear();
        values.put(Playlists.NAME, name1);
        final Uri playlist = mContentResolver
                .insert(MediaStore.Audio.Playlists.getContentUri(mVolumeName), values);
        MediaStore.waitForIdle(mContentResolver);
        try (Cursor c = mContentResolver.query(playlist,
                new String[] { Playlists.NAME, MediaColumns.DISPLAY_NAME }, null, null)) {
            assertTrue(c.moveToFirst());
            assertTrue(c.getString(0).startsWith(name1));
            assertTrue(c.getString(1).startsWith(name1));
        }

        values.clear();
        values.put(Playlists.NAME, name2);
        mContentResolver.update(playlist, values, null);
        MediaStore.waitForIdle(mContentResolver);
        try (Cursor c = mContentResolver.query(playlist,
                new String[] { Playlists.NAME, MediaColumns.DISPLAY_NAME }, null, null)) {
            assertTrue(c.moveToFirst());
            assertTrue(c.getString(0).startsWith(name2));
            assertTrue(c.getString(1).startsWith(name2));
        }
    }
}