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
8bb64397
authored
Jun 23, 2017
by
Oliver Woodman
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Fix ContentDataSource and enhance tests to validate read data
parent
2da22e9e
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
179 additions
and
117 deletions
library/core/src/androidTest/AndroidManifest.xml
library/core/src/androidTest/assets/binary/1024_incrementing_bytes.mp3
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/AndroidDataSourceConstants.java
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/AssetDataSourceTest.java
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestDataProvider.java
library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java
library/core/src/androidTest/AndroidManifest.xml
View file @
8bb64397
...
@@ -25,8 +25,8 @@
...
@@ -25,8 +25,8 @@
tools:ignore=
"MissingApplicationIcon,HardcodedDebugMode"
>
tools:ignore=
"MissingApplicationIcon,HardcodedDebugMode"
>
<uses-library
android:name=
"android.test.runner"
/>
<uses-library
android:name=
"android.test.runner"
/>
<provider
<provider
android:authorities=
"
exoplayer
"
android:authorities=
"
com.google.android.exoplayer2.core.test
"
android:name=
"com.google.android.exoplayer2.upstream.
TestData
Provider"
/>
android:name=
"com.google.android.exoplayer2.upstream.
ContentDataSourceTest$TestContent
Provider"
/>
</application>
</application>
<instrumentation
<instrumentation
...
...
library/core/src/androidTest/assets/binary/1024_incrementing_bytes.mp3
0 → 100644
View file @
8bb64397
No preview for this file type
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/AndroidDataSourceConstants.java
deleted
100644 → 0
View file @
2da22e9e
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
import
android.content.ContentResolver
;
import
android.net.Uri
;
final
class
AndroidDataSourceConstants
{
static
final
long
SAMPLE_MP4_BYTES
=
101597
;
static
final
String
SAMPLE_MP4_PATH
=
"/mp4/sample.mp4"
;
static
final
String
TEST_DATA_PROVIDER_AUTHORITY
=
"exoplayer"
;
static
final
Uri
NULL_DESCRIPTOR_URI
=
new
Uri
.
Builder
()
.
scheme
(
ContentResolver
.
SCHEME_CONTENT
)
.
authority
(
TEST_DATA_PROVIDER_AUTHORITY
)
.
build
();
private
AndroidDataSourceConstants
()
{}
}
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/AssetDataSourceTest.java
View file @
8bb64397
/*
* Copyright (C) 2017 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
com
.
google
.
android
.
exoplayer2
.
upstream
;
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
import
android.content.Context
;
import
android.content.Context
;
import
android.net.Uri
;
import
android.net.Uri
;
import
android.test.InstrumentationTestCase
;
import
android.test.InstrumentationTestCase
;
import
android.test.MoreAsserts
;
import
com.google.android.exoplayer2.testutil.TestUtil
;
import
static
com
.
google
.
android
.
exoplayer2
.
upstream
.
AndroidDataSourceConstants
.
SAMPLE_MP4_BYTES
;
/**
import
static
com
.
google
.
android
.
exoplayer2
.
upstream
.
AndroidDataSourceConstants
.
SAMPLE_MP4_PATH
;
* Unit tests for {@link AssetDataSource}.
*/
public
final
class
AssetDataSourceTest
extends
InstrumentationTestCase
{
public
class
AssetDataSourceTest
extends
InstrumentationTestCase
{
private
static
final
String
DATA_PATH
=
"binary/1024_incrementing_bytes.mp3"
;
private
static
final
long
DATA_LENGTH
=
1024
;
public
void
test
AssetDataSource
()
throws
Exception
{
public
void
test
ReadFileUri
()
throws
Exception
{
final
Context
context
=
getInstrumentation
().
getContext
();
Context
context
=
getInstrumentation
().
getContext
();
AssetDataSource
dataSource
=
new
AssetDataSource
(
context
);
AssetDataSource
dataSource
=
new
AssetDataSource
(
context
);
Uri
assetUri
=
Uri
.
parse
(
"file:///android_asset
"
+
SAMPLE_MP4
_PATH
);
Uri
assetUri
=
Uri
.
parse
(
"file:///android_asset
/"
+
DATA
_PATH
);
DataSpec
dataSpec
=
new
DataSpec
(
assetUri
);
DataSpec
dataSpec
=
new
DataSpec
(
assetUri
);
long
sourceLengthBytes
=
dataSource
.
open
(
dataSpec
);
try
{
long
length
=
dataSource
.
open
(
dataSpec
);
assertEquals
(
DATA_LENGTH
,
length
);
byte
[]
readData
=
TestUtil
.
readToEnd
(
dataSource
);
MoreAsserts
.
assertEquals
(
TestUtil
.
getByteArray
(
getInstrumentation
(),
DATA_PATH
),
readData
);
}
finally
{
dataSource
.
close
();
}
}
assertEquals
(
SAMPLE_MP4_BYTES
,
sourceLengthBytes
);
public
void
testReadAssetUri
()
throws
Exception
{
Context
context
=
getInstrumentation
().
getContext
();
AssetDataSource
dataSource
=
new
AssetDataSource
(
context
);
Uri
assetUri
=
Uri
.
parse
(
"asset:///"
+
DATA_PATH
);
DataSpec
dataSpec
=
new
DataSpec
(
assetUri
);
try
{
long
length
=
dataSource
.
open
(
dataSpec
);
assertEquals
(
DATA_LENGTH
,
length
);
byte
[]
readData
=
TestUtil
.
readToEnd
(
dataSource
);
MoreAsserts
.
assertEquals
(
TestUtil
.
getByteArray
(
getInstrumentation
(),
DATA_PATH
),
readData
);
}
finally
{
dataSource
.
close
();
}
}
}
}
}
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java
View file @
8bb64397
/*
* Copyright (C) 2017 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
com
.
google
.
android
.
exoplayer2
.
upstream
;
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
import
android.content.ContentProvider
;
import
android.content.ContentResolver
;
import
android.content.ContentResolver
;
import
android.content.Context
;
import
android.content.ContentValues
;
import
android.content.res.AssetFileDescriptor
;
import
android.database.Cursor
;
import
android.net.Uri
;
import
android.net.Uri
;
import
android.support.annotation.NonNull
;
import
android.test.InstrumentationTestCase
;
import
android.test.InstrumentationTestCase
;
import
android.test.MoreAsserts
;
import
com.google.android.exoplayer2.testutil.TestUtil
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
static
com
.
google
.
android
.
exoplayer2
.
upstream
.
AndroidDataSourceConstants
.
NULL_DESCRIPTOR_URI
;
/**
import
static
com
.
google
.
android
.
exoplayer2
.
upstream
.
AndroidDataSourceConstants
.
SAMPLE_MP4_BYTES
;
* Unit tests for {@link ContentDataSource}.
import
static
com
.
google
.
android
.
exoplayer2
.
upstream
.
AndroidDataSourceConstants
.
SAMPLE_MP4_PATH
;
*/
import
static
com
.
google
.
android
.
exoplayer2
.
upstream
.
AndroidDataSourceConstants
.
TEST_DATA_PROVIDER_AUTHORITY
;
public
final
class
ContentDataSourceTest
extends
InstrumentationTestCase
{
public
class
ContentDataSourceTest
extends
InstrumentationTestCase
{
private
static
final
String
DATA_PATH
=
"binary/1024_incrementing_bytes.mp3"
;
private
static
final
long
DATA_LENGTH
=
1024
;
public
void
testValidContentDataSource
()
throws
Exception
{
public
void
testReadValidUri
()
throws
Exception
{
Context
context
=
getInstrumentation
().
getContext
();
ContentDataSource
dataSource
=
new
ContentDataSource
(
getInstrumentation
().
getContext
());
ContentDataSource
dataSource
=
new
ContentDataSource
(
context
);
Uri
contentUri
=
new
Uri
.
Builder
()
Uri
contentUri
=
new
Uri
.
Builder
()
.
scheme
(
ContentResolver
.
SCHEME_CONTENT
)
.
scheme
(
ContentResolver
.
SCHEME_CONTENT
)
.
authority
(
TEST_DATA_PROVIDER_
AUTHORITY
)
.
authority
(
TestContentProvider
.
AUTHORITY
)
.
path
(
SAMPLE_MP4
_PATH
).
build
();
.
path
(
DATA
_PATH
).
build
();
DataSpec
dataSpec
=
new
DataSpec
(
contentUri
);
DataSpec
dataSpec
=
new
DataSpec
(
contentUri
);
long
sourceLengthBytes
=
dataSource
.
open
(
dataSpec
);
try
{
long
length
=
dataSource
.
open
(
dataSpec
);
assertEquals
(
SAMPLE_MP4_BYTES
,
sourceLengthBytes
);
assertEquals
(
DATA_LENGTH
,
length
);
byte
[]
readData
=
TestUtil
.
readToEnd
(
dataSource
);
MoreAsserts
.
assertEquals
(
TestUtil
.
getByteArray
(
getInstrumentation
(),
DATA_PATH
),
readData
);
}
finally
{
dataSource
.
close
();
}
}
}
public
void
testNullContentDataSource
()
throws
Exception
{
public
void
testReadInvalidUri
()
throws
Exception
{
Context
context
=
getInstrumentation
().
getContext
();
ContentDataSource
dataSource
=
new
ContentDataSource
(
getInstrumentation
().
getContext
());
ContentDataSource
dataSource
=
new
ContentDataSource
(
context
);
Uri
contentUri
=
new
Uri
.
Builder
()
DataSpec
dataSpec
=
new
DataSpec
(
NULL_DESCRIPTOR_URI
);
.
scheme
(
ContentResolver
.
SCHEME_CONTENT
)
.
authority
(
TestContentProvider
.
AUTHORITY
)
.
build
();
DataSpec
dataSpec
=
new
DataSpec
(
contentUri
);
try
{
try
{
dataSource
.
open
(
dataSpec
);
dataSource
.
open
(
dataSpec
);
fail
(
"Expected exception not thrown."
);
fail
();
}
catch
(
ContentDataSource
.
ContentDataSourceException
e
)
{
}
catch
(
ContentDataSource
.
ContentDataSourceException
e
)
{
// Expected.
// Expected.
}
finally
{
dataSource
.
close
();
}
}
}
}
public
static
final
class
TestContentProvider
extends
ContentProvider
{
private
static
final
String
AUTHORITY
=
"com.google.android.exoplayer2.core.test"
;
@Override
public
boolean
onCreate
()
{
return
true
;
}
@Override
public
Cursor
query
(
@NonNull
Uri
uri
,
String
[]
projection
,
String
selection
,
String
[]
selectionArgs
,
String
sortOrder
)
{
throw
new
UnsupportedOperationException
();
}
@Override
public
AssetFileDescriptor
openAssetFile
(
@NonNull
Uri
uri
,
@NonNull
String
mode
)
throws
FileNotFoundException
{
if
(
uri
.
getPath
()
==
null
)
{
return
null
;
}
try
{
return
getContext
().
getAssets
().
openFd
(
uri
.
getPath
().
replaceFirst
(
"/"
,
""
));
}
catch
(
IOException
e
)
{
FileNotFoundException
exception
=
new
FileNotFoundException
(
e
.
getMessage
());
exception
.
initCause
(
e
);
throw
exception
;
}
}
@Override
public
String
getType
(
@NonNull
Uri
uri
)
{
throw
new
UnsupportedOperationException
();
}
@Override
public
Uri
insert
(
@NonNull
Uri
uri
,
ContentValues
values
)
{
throw
new
UnsupportedOperationException
();
}
@Override
public
int
delete
(
@NonNull
Uri
uri
,
String
selection
,
String
[]
selectionArgs
)
{
throw
new
UnsupportedOperationException
();
}
@Override
public
int
update
(
@NonNull
Uri
uri
,
ContentValues
values
,
String
selection
,
String
[]
selectionArgs
)
{
throw
new
UnsupportedOperationException
();
}
}
}
}
library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestDataProvider.java
deleted
100644 → 0
View file @
2da22e9e
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
import
android.content.ContentProvider
;
import
android.content.ContentValues
;
import
android.content.Context
;
import
android.content.res.AssetFileDescriptor
;
import
android.database.Cursor
;
import
android.net.Uri
;
import
android.support.annotation.NonNull
;
import
android.support.annotation.Nullable
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
static
junit
.
framework
.
Assert
.
assertNotNull
;
public
class
TestDataProvider
extends
ContentProvider
{
@Override
public
boolean
onCreate
()
{
return
true
;
}
@Nullable
@Override
public
Cursor
query
(
@NonNull
final
Uri
uri
,
@Nullable
final
String
[]
projection
,
@Nullable
final
String
selection
,
@Nullable
final
String
[]
selectionArgs
,
@Nullable
final
String
sortOrder
)
{
throw
new
UnsupportedOperationException
(
"Not implemented"
);
}
@Nullable
@Override
public
AssetFileDescriptor
openAssetFile
(
@NonNull
final
Uri
uri
,
@NonNull
final
String
mode
)
throws
FileNotFoundException
{
if
(
uri
.
equals
(
AndroidDataSourceConstants
.
NULL_DESCRIPTOR_URI
))
{
return
null
;
}
try
{
Context
context
=
getContext
();
assertNotNull
(
context
);
return
context
.
getAssets
().
openFd
(
uri
.
getPath
().
replaceFirst
(
"/"
,
""
));
}
catch
(
IOException
e
)
{
FileNotFoundException
exception
=
new
FileNotFoundException
(
e
.
getMessage
());
exception
.
initCause
(
e
);
throw
exception
;
}
}
@Nullable
@Override
public
String
getType
(
@NonNull
final
Uri
uri
)
{
throw
new
UnsupportedOperationException
(
"Not implemented"
);
}
@Nullable
@Override
public
Uri
insert
(
@NonNull
final
Uri
uri
,
@Nullable
final
ContentValues
values
)
{
throw
new
UnsupportedOperationException
(
"Not implemented"
);
}
@Override
public
int
delete
(
@NonNull
final
Uri
uri
,
@Nullable
final
String
selection
,
@Nullable
final
String
[]
selectionArgs
)
{
throw
new
UnsupportedOperationException
(
"Not implemented"
);
}
@Override
public
int
update
(
@NonNull
final
Uri
uri
,
@Nullable
final
ContentValues
values
,
@Nullable
final
String
selection
,
@Nullable
final
String
[]
selectionArgs
)
{
throw
new
UnsupportedOperationException
(
"Not implemented"
);
}
}
library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java
View file @
8bb64397
...
@@ -76,8 +76,9 @@ public final class ContentDataSource implements DataSource {
...
@@ -76,8 +76,9 @@ public final class ContentDataSource implements DataSource {
throw
new
FileNotFoundException
(
"Could not open file descriptor for: "
+
uri
);
throw
new
FileNotFoundException
(
"Could not open file descriptor for: "
+
uri
);
}
}
inputStream
=
new
FileInputStream
(
assetFileDescriptor
.
getFileDescriptor
());
inputStream
=
new
FileInputStream
(
assetFileDescriptor
.
getFileDescriptor
());
long
skipped
=
inputStream
.
skip
(
dataSpec
.
position
);
long
assertStartOffset
=
assetFileDescriptor
.
getStartOffset
();
if
(
skipped
<
dataSpec
.
position
)
{
long
skipped
=
inputStream
.
skip
(
assertStartOffset
+
dataSpec
.
position
)
-
assertStartOffset
;
if
(
skipped
!=
dataSpec
.
position
)
{
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
// skip beyond the end of the data.
// skip beyond the end of the data.
throw
new
EOFException
();
throw
new
EOFException
();
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java
View file @
8bb64397
...
@@ -22,6 +22,7 @@ import com.google.android.exoplayer2.extractor.Extractor;
...
@@ -22,6 +22,7 @@ import com.google.android.exoplayer2.extractor.Extractor;
import
com.google.android.exoplayer2.extractor.PositionHolder
;
import
com.google.android.exoplayer2.extractor.PositionHolder
;
import
com.google.android.exoplayer2.extractor.SeekMap
;
import
com.google.android.exoplayer2.extractor.SeekMap
;
import
com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException
;
import
com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException
;
import
com.google.android.exoplayer2.upstream.DataSource
;
import
com.google.android.exoplayer2.util.Assertions
;
import
com.google.android.exoplayer2.util.Assertions
;
import
com.google.android.exoplayer2.util.Util
;
import
com.google.android.exoplayer2.util.Util
;
import
java.io.IOException
;
import
java.io.IOException
;
...
@@ -64,6 +65,22 @@ public class TestUtil {
...
@@ -64,6 +65,22 @@ public class TestUtil {
}
}
}
}
public
static
byte
[]
readToEnd
(
DataSource
dataSource
)
throws
IOException
{
byte
[]
data
=
new
byte
[
1024
];
int
position
=
0
;
int
bytesRead
=
0
;
while
(
bytesRead
!=
C
.
RESULT_END_OF_INPUT
)
{
if
(
position
==
data
.
length
)
{
data
=
Arrays
.
copyOf
(
data
,
data
.
length
*
2
);
}
bytesRead
=
dataSource
.
read
(
data
,
position
,
data
.
length
-
position
);
if
(
bytesRead
!=
C
.
RESULT_END_OF_INPUT
)
{
position
+=
bytesRead
;
}
}
return
Arrays
.
copyOf
(
data
,
position
);
}
public
static
FakeExtractorOutput
consumeTestData
(
Extractor
extractor
,
FakeExtractorInput
input
,
public
static
FakeExtractorOutput
consumeTestData
(
Extractor
extractor
,
FakeExtractorInput
input
,
long
timeUs
)
throws
IOException
,
InterruptedException
{
long
timeUs
)
throws
IOException
,
InterruptedException
{
return
consumeTestData
(
extractor
,
input
,
timeUs
,
false
);
return
consumeTestData
(
extractor
,
input
,
timeUs
,
false
);
...
...
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