• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.providers.tv;
18 
19 import static com.android.providers.tv.Utils.PreviewProgram;
20 import static com.android.providers.tv.Utils.Program;
21 import static com.android.providers.tv.Utils.RecordedProgram;
22 import static com.android.providers.tv.Utils.WatchNextProgram;
23 import static com.android.providers.tv.Utils.WatchedProgram;
24 
25 import android.content.Intent;
26 import android.content.pm.ProviderInfo;
27 import android.media.tv.TvContract;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.os.SystemClock;
31 import android.provider.Settings;
32 import android.test.AndroidTestCase;
33 import android.test.mock.MockContentProvider;
34 import android.test.mock.MockContentResolver;
35 
36 import com.google.android.collect.Sets;
37 
38 import java.util.concurrent.Executor;
39 
40 public class PackageRemovedReceiverTest extends AndroidTestCase {
41     private static final String FAKE_PACKAGE_NAME_1 = "package.removed.receiver.Test1";
42     private static final String FAKE_PACKAGE_NAME_2 = "package.removed.receiver.Test2";
43 
44     private MockContentResolver mResolver;
45     private TvProviderForTesting mProvider;
46     private PackageRemovedReceiver mReceiver;
47 
48     @Override
setUp()49     protected void setUp() throws Exception {
50         super.setUp();
51 
52         mResolver = new MockContentResolver();
53         mResolver.addProvider(Settings.AUTHORITY, new MockContentProvider() {
54             @Override
55             public Bundle call(String method, String request, Bundle args) {
56                 return new Bundle();
57             }
58         });
59 
60         mProvider = new TvProviderForTesting();
61         mResolver.addProvider(TvContract.AUTHORITY, mProvider);
62 
63         setContext(new MockTvProviderContext(mResolver, getContext()));
64 
65         final ProviderInfo info = new ProviderInfo();
66         info.authority = TvContract.AUTHORITY;
67         mProvider.attachInfoForTesting(getContext(), info);
68 
69         mReceiver = new PackageRemovedReceiver(
70                 new Executor() {
71                     public void execute(Runnable command) {
72                         command.run();
73                     }
74                 });
75         Utils.clearTvProvider(mResolver);
76     }
77 
78     @Override
tearDown()79     protected void tearDown() throws Exception {
80         Utils.clearTvProvider(mResolver);
81         mProvider.shutdown();
82         super.tearDown();
83     }
84 
testPackageRemoved()85     public void testPackageRemoved() {
86         Program programInPackage1 = new Program(FAKE_PACKAGE_NAME_1);
87         Program programInPackage2 = new Program(FAKE_PACKAGE_NAME_2);
88         PreviewProgram previewProgramInPackage1 = new PreviewProgram(FAKE_PACKAGE_NAME_1);
89         PreviewProgram previewProgramInPackage2 = new PreviewProgram(FAKE_PACKAGE_NAME_2);
90         WatchedProgram watchedProgramInPackage1 = new WatchedProgram(FAKE_PACKAGE_NAME_1);
91         WatchedProgram watchedProgramInPackage2 = new WatchedProgram(FAKE_PACKAGE_NAME_2);
92         WatchNextProgram watchNextProgramInPackage1 = new WatchNextProgram(FAKE_PACKAGE_NAME_1);
93         WatchNextProgram watchNextProgramInPackage2 = new WatchNextProgram(FAKE_PACKAGE_NAME_2);
94 
95         mProvider.callingPackage = FAKE_PACKAGE_NAME_1;
96         long channelInPackage1Id = Utils.insertChannel(mResolver);
97         Utils.insertPrograms(mResolver, channelInPackage1Id, programInPackage1);
98         Utils.insertPreviewPrograms(mResolver, channelInPackage1Id, previewProgramInPackage1);
99         RecordedProgram recordedProgramInPackage1 =
100                 new RecordedProgram(FAKE_PACKAGE_NAME_1, channelInPackage1Id);
101         Utils.insertRecordedPrograms(mResolver, channelInPackage1Id, recordedProgramInPackage1);
102         Utils.insertWatchedPrograms(mResolver, FAKE_PACKAGE_NAME_1, channelInPackage1Id,
103                 watchedProgramInPackage1);
104         Utils.insertWatchNextPrograms(mResolver, FAKE_PACKAGE_NAME_1, watchNextProgramInPackage1);
105 
106         mProvider.callingPackage = FAKE_PACKAGE_NAME_2;
107         long channelInPackage2Id = Utils.insertChannel(mResolver);
108         Utils.insertPrograms(mResolver, channelInPackage2Id, programInPackage2);
109         Utils.insertPreviewPrograms(mResolver, channelInPackage2Id, previewProgramInPackage2);
110         RecordedProgram recordedProgramInPackage2 =
111                 new RecordedProgram(FAKE_PACKAGE_NAME_2, channelInPackage2Id);
112         Utils.insertRecordedPrograms(mResolver, channelInPackage2Id, recordedProgramInPackage2);
113         Utils.insertWatchedPrograms(mResolver, FAKE_PACKAGE_NAME_2, channelInPackage2Id,
114                 watchedProgramInPackage2);
115         Utils.insertWatchNextPrograms(mResolver, FAKE_PACKAGE_NAME_2, watchNextProgramInPackage2);
116 
117         assertEquals(
118                 Sets.newHashSet(programInPackage1, programInPackage2),
119                 Utils.queryPrograms(mResolver));
120         assertEquals(
121                 Sets.newHashSet(previewProgramInPackage1, previewProgramInPackage2),
122                 Utils.queryPreviewPrograms(mResolver));
123         assertEquals(
124                 Sets.newHashSet(recordedProgramInPackage1, recordedProgramInPackage2),
125                 Utils.queryRecordedPrograms(mResolver));
126         assertEquals(
127                 Sets.newHashSet(watchedProgramInPackage1, watchedProgramInPackage2),
128                 Utils.queryWatchedPrograms(mResolver));
129         assertEquals(
130                 Sets.newHashSet(watchNextProgramInPackage1, watchNextProgramInPackage2),
131                 Utils.queryWatchNextPrograms(mResolver));
132         assertEquals(2, Utils.getChannelCount(mResolver));
133 
134         mReceiver.setPendingResult(Utils.createFakePendingResultForTests());
135         mReceiver.onReceive(getContext(), new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED,
136                 Uri.parse("package:" + FAKE_PACKAGE_NAME_1)));
137 
138         assertEquals("Program should be removed if its package is removed.",
139                 Sets.newHashSet(programInPackage2), Utils.queryPrograms(mResolver));
140         assertEquals("PreviewProgram should be removed if its package is removed.",
141                 Sets.newHashSet(previewProgramInPackage2), Utils.queryPreviewPrograms(mResolver));
142         assertEquals("RecordedProgram should be removed if its package is removed.",
143                 Sets.newHashSet(recordedProgramInPackage2),
144                 Utils.queryRecordedPrograms(mResolver));
145         assertEquals("WatchedProgram should be removed if its package is removed.",
146                 Sets.newHashSet(watchedProgramInPackage2), Utils.queryWatchedPrograms(mResolver));
147         assertEquals("WatchNextProgram should be removed if its package is removed.",
148                 Sets.newHashSet(watchNextProgramInPackage2),
149                 Utils.queryWatchNextPrograms(mResolver));
150         assertEquals("Channel should be removed if its package is removed.",
151                 1, Utils.getChannelCount(mResolver));
152 
153         mReceiver.setPendingResult(Utils.createFakePendingResultForTests());
154         mReceiver.onReceive(getContext(), new Intent(Intent.ACTION_PACKAGE_FULLY_REMOVED,
155                 Uri.parse("package:" + FAKE_PACKAGE_NAME_2)));
156 
157         assertTrue("Program should be removed if its package is removed.",
158                 Utils.queryPrograms(mResolver).isEmpty());
159         assertTrue("PreviewProgram should be removed if its package is removed.",
160                 Utils.queryPreviewPrograms(mResolver).isEmpty());
161         assertTrue("RecordedProgram should be removed if its package is removed.",
162                 Utils.queryRecordedPrograms(mResolver).isEmpty());
163         assertTrue("WatchedProgram should be removed if its package is removed.",
164                 Utils.queryWatchedPrograms(mResolver).isEmpty());
165         assertTrue("WatchedProgram should be removed if its package is removed.",
166                 Utils.queryWatchNextPrograms(mResolver).isEmpty());
167         assertEquals("Channel should be removed if its package is removed.",
168                 0, Utils.getChannelCount(mResolver));
169     }
170 }
171