Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2d494861
authored
Apr 28, 2020
by
olly
Committed by
Oliver Woodman
May 01, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Cleanup and document TestDownloadManagerListener
PiperOrigin-RevId: 308843488
parent
4580b524
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
123 deletions
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java
View file @
2d494861
...
@@ -100,7 +100,7 @@ public class DownloadManagerTest {
...
@@ -100,7 +100,7 @@ public class DownloadManagerTest {
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
assertDownloaderCount
(
ID1
,
1
);
assertDownloaderCount
(
ID1
,
1
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
assertThat
(
downloadManager
.
getCurrentDownloads
()).
isEmpty
();
assertThat
(
downloadManager
.
getCurrentDownloads
()).
isEmpty
();
}
}
...
@@ -117,7 +117,7 @@ public class DownloadManagerTest {
...
@@ -117,7 +117,7 @@ public class DownloadManagerTest {
assertRemoved
(
ID1
);
assertRemoved
(
ID1
);
assertDownloaderCount
(
ID1
,
2
);
assertDownloaderCount
(
ID1
,
2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
assertThat
(
downloadManager
.
getCurrentDownloads
()).
isEmpty
();
assertThat
(
downloadManager
.
getCurrentDownloads
()).
isEmpty
();
}
}
...
@@ -134,7 +134,7 @@ public class DownloadManagerTest {
...
@@ -134,7 +134,7 @@ public class DownloadManagerTest {
downloader
.
assertStartCount
(
MIN_RETRY_COUNT
+
1
);
downloader
.
assertStartCount
(
MIN_RETRY_COUNT
+
1
);
assertFailed
(
ID1
);
assertFailed
(
ID1
);
downloadManagerListener
.
blockUntil
TasksComplet
e
();
downloadManagerListener
.
blockUntil
Idl
e
();
assertCurrentDownloadCount
(
0
);
assertCurrentDownloadCount
(
0
);
}
}
...
@@ -153,7 +153,7 @@ public class DownloadManagerTest {
...
@@ -153,7 +153,7 @@ public class DownloadManagerTest {
downloader
.
assertStartCount
(
MIN_RETRY_COUNT
+
1
);
downloader
.
assertStartCount
(
MIN_RETRY_COUNT
+
1
);
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
downloadManagerListener
.
blockUntil
TasksComplet
e
();
downloadManagerListener
.
blockUntil
Idl
e
();
assertCurrentDownloadCount
(
0
);
assertCurrentDownloadCount
(
0
);
}
}
...
@@ -174,7 +174,7 @@ public class DownloadManagerTest {
...
@@ -174,7 +174,7 @@ public class DownloadManagerTest {
downloader
.
assertStartCount
(
tooManyRetries
+
1
);
downloader
.
assertStartCount
(
tooManyRetries
+
1
);
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
downloadManagerListener
.
blockUntil
TasksComplet
e
();
downloadManagerListener
.
blockUntil
Idl
e
();
}
}
@Test
@Test
...
@@ -192,7 +192,7 @@ public class DownloadManagerTest {
...
@@ -192,7 +192,7 @@ public class DownloadManagerTest {
downloader2
.
unblock
();
downloader2
.
unblock
();
downloader2
.
assertCompleted
();
downloader2
.
assertCompleted
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -212,7 +212,7 @@ public class DownloadManagerTest {
...
@@ -212,7 +212,7 @@ public class DownloadManagerTest {
downloader2
.
unblock
();
downloader2
.
unblock
();
downloader2
.
assertCompleted
();
downloader2
.
assertCompleted
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -230,7 +230,7 @@ public class DownloadManagerTest {
...
@@ -230,7 +230,7 @@ public class DownloadManagerTest {
assertRemoved
(
ID1
);
assertRemoved
(
ID1
);
assertDownloaderCount
(
ID1
,
2
);
assertDownloaderCount
(
ID1
,
2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -238,13 +238,13 @@ public class DownloadManagerTest {
...
@@ -238,13 +238,13 @@ public class DownloadManagerTest {
// Finish one download and keep one running.
// Finish one download and keep one running.
postDownloadRequest
(
ID1
);
postDownloadRequest
(
ID1
);
getDownloader
(
ID1
,
0
).
unblock
();
getDownloader
(
ID1
,
0
).
unblock
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
postDownloadRequest
(
ID2
);
postDownloadRequest
(
ID2
);
postRemoveAllRequest
();
postRemoveAllRequest
();
getDownloader
(
ID1
,
1
).
unblock
();
getDownloader
(
ID1
,
1
).
unblock
();
getDownloader
(
ID2
,
1
).
unblock
();
getDownloader
(
ID2
,
1
).
unblock
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
assertRemoved
(
ID1
);
assertRemoved
(
ID1
);
assertRemoved
(
ID2
);
assertRemoved
(
ID2
);
...
@@ -270,7 +270,7 @@ public class DownloadManagerTest {
...
@@ -270,7 +270,7 @@ public class DownloadManagerTest {
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
assertDownloaderCount
(
ID1
,
2
);
assertDownloaderCount
(
ID1
,
2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -287,7 +287,7 @@ public class DownloadManagerTest {
...
@@ -287,7 +287,7 @@ public class DownloadManagerTest {
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
assertCompleted
(
ID2
);
assertCompleted
(
ID2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -307,7 +307,7 @@ public class DownloadManagerTest {
...
@@ -307,7 +307,7 @@ public class DownloadManagerTest {
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
assertCompleted
(
ID2
);
assertCompleted
(
ID2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -327,7 +327,7 @@ public class DownloadManagerTest {
...
@@ -327,7 +327,7 @@ public class DownloadManagerTest {
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
assertRemoved
(
ID2
);
assertRemoved
(
ID2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -351,7 +351,7 @@ public class DownloadManagerTest {
...
@@ -351,7 +351,7 @@ public class DownloadManagerTest {
assertCompleted
(
ID1
);
assertCompleted
(
ID1
);
assertCompleted
(
ID2
);
assertCompleted
(
ID2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -412,7 +412,7 @@ public class DownloadManagerTest {
...
@@ -412,7 +412,7 @@ public class DownloadManagerTest {
downloader5
.
assertStarted
();
downloader5
.
assertStarted
();
downloader5
.
unblock
();
downloader5
.
unblock
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -431,7 +431,7 @@ public class DownloadManagerTest {
...
@@ -431,7 +431,7 @@ public class DownloadManagerTest {
downloader
.
assertStarted
();
downloader
.
assertStarted
();
downloader
.
unblock
();
downloader
.
unblock
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -450,7 +450,7 @@ public class DownloadManagerTest {
...
@@ -450,7 +450,7 @@ public class DownloadManagerTest {
downloader
.
unblock
();
downloader
.
unblock
();
assertRemoving
(
ID1
);
assertRemoving
(
ID1
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -476,7 +476,7 @@ public class DownloadManagerTest {
...
@@ -476,7 +476,7 @@ public class DownloadManagerTest {
downloader2
.
assertStarted
();
downloader2
.
assertStarted
();
downloader2
.
unblock
();
downloader2
.
unblock
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
}
}
@Test
@Test
...
@@ -563,8 +563,7 @@ public class DownloadManagerTest {
...
@@ -563,8 +563,7 @@ public class DownloadManagerTest {
downloadManager
.
setMinRetryCount
(
MIN_RETRY_COUNT
);
downloadManager
.
setMinRetryCount
(
MIN_RETRY_COUNT
);
downloadManager
.
setRequirements
(
new
Requirements
(
0
));
downloadManager
.
setRequirements
(
new
Requirements
(
0
));
downloadManager
.
resumeDownloads
();
downloadManager
.
resumeDownloads
();
downloadManagerListener
=
downloadManagerListener
=
new
TestDownloadManagerListener
(
downloadManager
);
new
TestDownloadManagerListener
(
downloadManager
,
dummyMainThread
);
});
});
downloadManagerListener
.
blockUntilInitialized
();
downloadManagerListener
.
blockUntilInitialized
();
}
catch
(
Throwable
throwable
)
{
}
catch
(
Throwable
throwable
)
{
...
@@ -632,31 +631,31 @@ public class DownloadManagerTest {
...
@@ -632,31 +631,31 @@ public class DownloadManagerTest {
}
}
private
void
assertDownloading
(
String
id
)
{
private
void
assertDownloading
(
String
id
)
{
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_DOWNLOADING
,
TIMEOUT_MS
);
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_DOWNLOADING
);
}
}
private
void
assertCompleted
(
String
id
)
{
private
void
assertCompleted
(
String
id
)
{
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_COMPLETED
,
TIMEOUT_MS
);
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_COMPLETED
);
}
}
private
void
assertRemoving
(
String
id
)
{
private
void
assertRemoving
(
String
id
)
{
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_REMOVING
,
TIMEOUT_MS
);
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_REMOVING
);
}
}
private
void
assertFailed
(
String
id
)
{
private
void
assertFailed
(
String
id
)
{
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_FAILED
,
TIMEOUT_MS
);
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_FAILED
);
}
}
private
void
assertQueued
(
String
id
)
{
private
void
assertQueued
(
String
id
)
{
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_QUEUED
,
TIMEOUT_MS
);
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_QUEUED
);
}
}
private
void
assertStopped
(
String
id
)
{
private
void
assertStopped
(
String
id
)
{
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_STOPPED
,
TIMEOUT_MS
);
downloadManagerListener
.
assertState
(
id
,
Download
.
STATE_STOPPED
);
}
}
private
void
assertRemoved
(
String
id
)
{
private
void
assertRemoved
(
String
id
)
{
downloadManagerListener
.
assertRemoved
(
id
,
TIMEOUT_MS
);
downloadManagerListener
.
assertRemoved
(
id
);
}
}
private
void
assertCurrentDownloadCount
(
int
expectedCount
)
{
private
void
assertCurrentDownloadCount
(
int
expectedCount
)
{
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java
View file @
2d494861
...
@@ -147,14 +147,14 @@ public class DownloadManagerDashTest {
...
@@ -147,14 +147,14 @@ public class DownloadManagerDashTest {
dummyMainThread
.
runOnMainThread
(
this
::
createDownloadManager
);
dummyMainThread
.
runOnMainThread
(
this
::
createDownloadManager
);
// Block on the test thread.
// Block on the test thread.
blockUntilTasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
assertCachedData
(
cache
,
fakeDataSet
);
assertCachedData
(
cache
,
fakeDataSet
);
}
}
@Test
@Test
public
void
handleDownloadRequest_downloadSuccess
()
throws
Throwable
{
public
void
handleDownloadRequest_downloadSuccess
()
throws
Throwable
{
handleDownloadRequest
(
fakeStreamKey1
,
fakeStreamKey2
);
handleDownloadRequest
(
fakeStreamKey1
,
fakeStreamKey2
);
blockUntilTasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
assertCachedData
(
cache
,
new
RequestSet
(
fakeDataSet
).
useBoundedDataSpecFor
(
"audio_init_data"
));
assertCachedData
(
cache
,
new
RequestSet
(
fakeDataSet
).
useBoundedDataSpecFor
(
"audio_init_data"
));
}
}
...
@@ -162,7 +162,7 @@ public class DownloadManagerDashTest {
...
@@ -162,7 +162,7 @@ public class DownloadManagerDashTest {
public
void
handleDownloadRequest_withRequest_downloadSuccess
()
throws
Throwable
{
public
void
handleDownloadRequest_withRequest_downloadSuccess
()
throws
Throwable
{
handleDownloadRequest
(
fakeStreamKey1
);
handleDownloadRequest
(
fakeStreamKey1
);
handleDownloadRequest
(
fakeStreamKey2
);
handleDownloadRequest
(
fakeStreamKey2
);
blockUntilTasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
assertCachedData
(
cache
,
new
RequestSet
(
fakeDataSet
).
useBoundedDataSpecFor
(
"audio_init_data"
));
assertCachedData
(
cache
,
new
RequestSet
(
fakeDataSet
).
useBoundedDataSpecFor
(
"audio_init_data"
));
}
}
...
@@ -173,23 +173,17 @@ public class DownloadManagerDashTest {
...
@@ -173,23 +173,17 @@ public class DownloadManagerDashTest {
.
appendReadAction
(()
->
handleDownloadRequest
(
fakeStreamKey2
))
.
appendReadAction
(()
->
handleDownloadRequest
(
fakeStreamKey2
))
.
appendReadData
(
TestUtil
.
buildTestData
(
5
))
.
appendReadData
(
TestUtil
.
buildTestData
(
5
))
.
endData
();
.
endData
();
handleDownloadRequest
(
fakeStreamKey1
);
handleDownloadRequest
(
fakeStreamKey1
);
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCachedData
(
cache
,
new
RequestSet
(
fakeDataSet
).
useBoundedDataSpecFor
(
"audio_init_data"
));
assertCachedData
(
cache
,
new
RequestSet
(
fakeDataSet
).
useBoundedDataSpecFor
(
"audio_init_data"
));
}
}
@Test
@Test
public
void
handleRemoveAction_blockUntilTaskCompleted_noDownloadedData
()
throws
Throwable
{
public
void
handleRemoveAction_blockUntilTaskCompleted_noDownloadedData
()
throws
Throwable
{
handleDownloadRequest
(
fakeStreamKey1
);
handleDownloadRequest
(
fakeStreamKey1
);
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
blockUntilTasksCompleteAndThrowAnyDownloadError
();
handleRemoveAction
();
handleRemoveAction
();
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCacheEmpty
(
cache
);
assertCacheEmpty
(
cache
);
}
}
...
@@ -197,9 +191,7 @@ public class DownloadManagerDashTest {
...
@@ -197,9 +191,7 @@ public class DownloadManagerDashTest {
public
void
handleRemoveAction_beforeDownloadFinish_noDownloadedData
()
throws
Throwable
{
public
void
handleRemoveAction_beforeDownloadFinish_noDownloadedData
()
throws
Throwable
{
handleDownloadRequest
(
fakeStreamKey1
);
handleDownloadRequest
(
fakeStreamKey1
);
handleRemoveAction
();
handleRemoveAction
();
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCacheEmpty
(
cache
);
assertCacheEmpty
(
cache
);
}
}
...
@@ -211,23 +203,15 @@ public class DownloadManagerDashTest {
...
@@ -211,23 +203,15 @@ public class DownloadManagerDashTest {
.
appendReadAction
(
downloadInProgressLatch:
:
countDown
)
.
appendReadAction
(
downloadInProgressLatch:
:
countDown
)
.
appendReadData
(
TestUtil
.
buildTestData
(
5
))
.
appendReadData
(
TestUtil
.
buildTestData
(
5
))
.
endData
();
.
endData
();
handleDownloadRequest
(
fakeStreamKey1
);
handleDownloadRequest
(
fakeStreamKey1
);
assertThat
(
downloadInProgressLatch
.
await
(
ASSERT_TRUE_TIMEOUT_MS
,
TimeUnit
.
MILLISECONDS
))
assertThat
(
downloadInProgressLatch
.
await
(
ASSERT_TRUE_TIMEOUT_MS
,
TimeUnit
.
MILLISECONDS
))
.
isTrue
();
.
isTrue
();
handleRemoveAction
();
handleRemoveAction
();
downloadManagerListener
.
blockUntilIdleAndThrowAnyFailure
();
blockUntilTasksCompleteAndThrowAnyDownloadError
();
assertCacheEmpty
(
cache
);
assertCacheEmpty
(
cache
);
}
}
private
void
blockUntilTasksCompleteAndThrowAnyDownloadError
()
throws
Throwable
{
downloadManagerListener
.
blockUntilTasksCompleteAndThrowAnyDownloadError
();
}
private
void
handleDownloadRequest
(
StreamKey
...
keys
)
{
private
void
handleDownloadRequest
(
StreamKey
...
keys
)
{
DownloadRequest
request
=
getDownloadRequest
(
keys
);
DownloadRequest
request
=
getDownloadRequest
(
keys
);
runOnMainThread
(()
->
downloadManager
.
addDownload
(
request
));
runOnMainThread
(()
->
downloadManager
.
addDownload
(
request
));
...
@@ -262,9 +246,7 @@ public class DownloadManagerDashTest {
...
@@ -262,9 +246,7 @@ public class DownloadManagerDashTest {
new
DownloadManager
(
new
DownloadManager
(
ApplicationProvider
.
getApplicationContext
(),
downloadIndex
,
downloaderFactory
);
ApplicationProvider
.
getApplicationContext
(),
downloadIndex
,
downloaderFactory
);
downloadManager
.
setRequirements
(
new
Requirements
(
0
));
downloadManager
.
setRequirements
(
new
Requirements
(
0
));
downloadManagerListener
=
new
TestDownloadManagerListener
(
downloadManager
);
downloadManagerListener
=
new
TestDownloadManagerListener
(
downloadManager
,
dummyMainThread
);
downloadManager
.
resumeDownloads
();
downloadManager
.
resumeDownloads
();
});
});
}
}
...
...
library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java
View file @
2d494861
...
@@ -121,8 +121,7 @@ public class DownloadServiceDashTest {
...
@@ -121,8 +121,7 @@ public class DownloadServiceDashTest {
final
DownloadManager
dashDownloadManager
=
final
DownloadManager
dashDownloadManager
=
new
DownloadManager
(
new
DownloadManager
(
ApplicationProvider
.
getApplicationContext
(),
downloadIndex
,
downloaderFactory
);
ApplicationProvider
.
getApplicationContext
(),
downloadIndex
,
downloaderFactory
);
downloadManagerListener
=
downloadManagerListener
=
new
TestDownloadManagerListener
(
dashDownloadManager
);
new
TestDownloadManagerListener
(
dashDownloadManager
,
dummyMainThread
);
dashDownloadManager
.
resumeDownloads
();
dashDownloadManager
.
resumeDownloads
();
dashDownloadService
=
dashDownloadService
=
...
@@ -160,7 +159,7 @@ public class DownloadServiceDashTest {
...
@@ -160,7 +159,7 @@ public class DownloadServiceDashTest {
downloadKeys
(
fakeStreamKey1
);
downloadKeys
(
fakeStreamKey1
);
downloadKeys
(
fakeStreamKey2
);
downloadKeys
(
fakeStreamKey2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
assertCachedData
(
cache
,
fakeDataSet
);
assertCachedData
(
cache
,
fakeDataSet
);
}
}
...
@@ -170,11 +169,11 @@ public class DownloadServiceDashTest {
...
@@ -170,11 +169,11 @@ public class DownloadServiceDashTest {
public
void
removeAction
()
throws
Throwable
{
public
void
removeAction
()
throws
Throwable
{
downloadKeys
(
fakeStreamKey1
,
fakeStreamKey2
);
downloadKeys
(
fakeStreamKey1
,
fakeStreamKey2
);
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
removeAll
();
removeAll
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
assertCacheEmpty
(
cache
);
assertCacheEmpty
(
cache
);
}
}
...
@@ -187,7 +186,7 @@ public class DownloadServiceDashTest {
...
@@ -187,7 +186,7 @@ public class DownloadServiceDashTest {
removeAll
();
removeAll
();
downloadManagerListener
.
blockUntil
TasksCompleteAndThrowAnyDownloadError
();
downloadManagerListener
.
blockUntil
IdleAndThrowAnyFailure
();
assertCacheEmpty
(
cache
);
assertCacheEmpty
(
cache
);
}
}
...
@@ -221,5 +220,4 @@ public class DownloadServiceDashTest {
...
@@ -221,5 +220,4 @@ public class DownloadServiceDashTest {
dashDownloadService
.
onStartCommand
(
startIntent
,
0
,
0
);
dashDownloadService
.
onStartCommand
(
startIntent
,
0
,
0
);
});
});
}
}
}
}
testutils/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java
View file @
2d494861
...
@@ -18,53 +18,85 @@ package com.google.android.exoplayer2.testutil;
...
@@ -18,53 +18,85 @@ package com.google.android.exoplayer2.testutil;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
junit
.
Assert
.
fail
;
import
android.os.Handler
;
import
androidx.annotation.Nullable
;
import
androidx.annotation.Nullable
;
import
com.google.android.exoplayer2.offline.Download
;
import
com.google.android.exoplayer2.offline.Download
;
import
com.google.android.exoplayer2.offline.Download.State
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.util.ConditionVariable
;
import
com.google.android.exoplayer2.util.ConditionVariable
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.concurrent.
Array
BlockingQueue
;
import
java.util.concurrent.
Linked
BlockingQueue
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/** A {@link DownloadManager.Listener} for testing. */
/**
* Allows tests to block for, and assert properties of, calls from a {@link DownloadManager} to its
* {@link DownloadManager.Listener}.
*/
public
final
class
TestDownloadManagerListener
implements
DownloadManager
.
Listener
{
public
final
class
TestDownloadManagerListener
implements
DownloadManager
.
Listener
{
private
static
final
int
TIMEOUT_MS
=
10_000
;
private
static
final
int
TIMEOUT_MS
=
10_000
;
private
static
final
int
STATE_REMOVED
=
-
1
;
private
static
final
int
STATE_REMOVED
=
-
1
;
private
final
DownloadManager
downloadManager
;
private
final
DownloadManager
downloadManager
;
private
final
DummyMainThread
dummyMainThread
;
private
final
HashMap
<
String
,
LinkedBlockingQueue
<
Integer
>>
downloadStates
;
private
final
HashMap
<
String
,
ArrayBlockingQueue
<
Integer
>>
downloadStates
;
private
final
ConditionVariable
initializedCondition
;
private
final
ConditionVariable
initializedCondition
;
private
final
ConditionVariable
idleCondition
;
private
final
ConditionVariable
idleCondition
;
@Download
.
FailureReason
private
int
failureReason
;
@Download
.
FailureReason
private
int
failureReason
;
public
TestDownloadManagerListener
(
public
TestDownloadManagerListener
(
DownloadManager
downloadManager
)
{
DownloadManager
downloadManager
,
DummyMainThread
dummyMainThread
)
{
this
.
downloadManager
=
downloadManager
;
this
.
downloadManager
=
downloadManager
;
this
.
dummyMainThread
=
dummyMainThread
;
downloadStates
=
new
HashMap
<>();
downloadStates
=
new
HashMap
<>();
initializedCondition
=
TestUtil
.
createRobolectricConditionVariable
();
initializedCondition
=
TestUtil
.
createRobolectricConditionVariable
();
idleCondition
=
TestUtil
.
createRobolectricConditionVariable
();
idleCondition
=
TestUtil
.
createRobolectricConditionVariable
();
downloadManager
.
addListener
(
this
);
downloadManager
.
addListener
(
this
);
}
}
@Nullable
/** Blocks until the manager is initialized. */
public
Integer
pollStateChange
(
String
taskId
,
long
timeoutMs
)
throws
InterruptedException
{
public
void
blockUntilInitialized
(
)
throws
InterruptedException
{
return
getStateQueue
(
taskId
).
poll
(
timeoutMs
,
TimeUnit
.
MILLISECONDS
);
assertThat
(
initializedCondition
.
block
(
TIMEOUT_MS
)).
isTrue
(
);
}
}
@Override
/** Blocks until the manager is idle. */
public
void
onInitialized
(
DownloadManager
downloadManager
)
{
public
void
blockUntilIdle
()
throws
InterruptedException
{
initializedCondition
.
open
();
idleCondition
.
close
();
// If the manager is already idle the condition will be opened by the code immediately below.
// Else it will be opened by onIdle().
ConditionVariable
checkedOnMainThread
=
TestUtil
.
createRobolectricConditionVariable
();
new
Handler
(
downloadManager
.
getApplicationLooper
())
.
post
(
()
->
{
if
(
downloadManager
.
isIdle
())
{
idleCondition
.
open
();
checkedOnMainThread
.
open
();
}
});
checkedOnMainThread
.
block
(
TIMEOUT_MS
);
assertThat
(
idleCondition
.
block
(
TIMEOUT_MS
)).
isTrue
();
}
}
public
void
blockUntilInitialized
()
throws
InterruptedException
{
/** Blocks until the manager is idle and throws if any of the downloads failed. */
if
(!
downloadManager
.
isInitialized
())
{
public
void
blockUntilIdleAndThrowAnyFailure
()
throws
Exception
{
assertThat
(
initializedCondition
.
block
(
TIMEOUT_MS
)).
isTrue
();
blockUntilIdle
();
if
(
failureReason
!=
Download
.
FAILURE_REASON_NONE
)
{
throw
new
Exception
(
"Failure reason: "
+
failureReason
);
}
}
/** Asserts that the specified download transitions to the specified state. */
public
void
assertState
(
String
id
,
@Download
.
State
int
state
)
{
assertStateInternal
(
id
,
state
);
}
}
/** Asserts that the specified download is removed. */
public
void
assertRemoved
(
String
id
)
{
assertStateInternal
(
id
,
STATE_REMOVED
);
}
// DownloadManager.Listener implementation.
@Override
public
void
onInitialized
(
DownloadManager
downloadManager
)
{
initializedCondition
.
open
();
}
}
@Override
@Override
...
@@ -81,57 +113,17 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
...
@@ -81,57 +113,17 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
}
}
@Override
@Override
public
synchronized
void
onIdle
(
DownloadManager
downloadManager
)
{
public
void
onIdle
(
DownloadManager
downloadManager
)
{
idleCondition
.
open
();
idleCondition
.
open
();
}
}
/**
// Internal logic.
* Blocks until all remove and download tasks are complete and throws an exception if there was an
* error.
*/
public
void
blockUntilTasksCompleteAndThrowAnyDownloadError
()
throws
Throwable
{
blockUntilTasksComplete
();
if
(
failureReason
!=
Download
.
FAILURE_REASON_NONE
)
{
throw
new
Exception
(
"Failure reason: "
+
failureReason
);
}
}
/** Blocks until all remove and download tasks are complete. Task errors are ignored. */
private
void
assertStateInternal
(
String
id
,
int
expectedState
)
{
public
void
blockUntilTasksComplete
()
throws
InterruptedException
{
idleCondition
.
close
();
dummyMainThread
.
runOnMainThread
(
()
->
{
if
(
downloadManager
.
isIdle
())
{
idleCondition
.
open
();
}
});
assertThat
(
idleCondition
.
block
(
TIMEOUT_MS
)).
isTrue
();
}
private
ArrayBlockingQueue
<
Integer
>
getStateQueue
(
String
taskId
)
{
synchronized
(
downloadStates
)
{
@Nullable
ArrayBlockingQueue
<
Integer
>
stateQueue
=
downloadStates
.
get
(
taskId
);
if
(
stateQueue
==
null
)
{
stateQueue
=
new
ArrayBlockingQueue
<>(
10
);
downloadStates
.
put
(
taskId
,
stateQueue
);
}
return
stateQueue
;
}
}
public
void
assertRemoved
(
String
taskId
,
int
timeoutMs
)
{
assertStateInternal
(
taskId
,
STATE_REMOVED
,
timeoutMs
);
}
public
void
assertState
(
String
taskId
,
@State
int
expectedState
,
int
timeoutMs
)
{
assertStateInternal
(
taskId
,
expectedState
,
timeoutMs
);
}
private
void
assertStateInternal
(
String
taskId
,
int
expectedState
,
int
timeoutMs
)
{
while
(
true
)
{
while
(
true
)
{
@Nullable
Integer
state
=
null
;
@Nullable
Integer
state
=
null
;
try
{
try
{
state
=
pollStateChange
(
taskId
,
timeoutMs
);
state
=
getStateQueue
(
id
).
poll
(
TIMEOUT_MS
,
TimeUnit
.
MILLISECONDS
);
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
fail
(
"Interrupted: "
+
e
.
getMessage
());
fail
(
"Interrupted: "
+
e
.
getMessage
());
}
}
...
@@ -144,4 +136,15 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
...
@@ -144,4 +136,15 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
}
}
}
}
}
}
private
LinkedBlockingQueue
<
Integer
>
getStateQueue
(
String
id
)
{
synchronized
(
downloadStates
)
{
@Nullable
LinkedBlockingQueue
<
Integer
>
stateQueue
=
downloadStates
.
get
(
id
);
if
(
stateQueue
==
null
)
{
stateQueue
=
new
LinkedBlockingQueue
<>();
downloadStates
.
put
(
id
,
stateQueue
);
}
return
stateQueue
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment