1 /* 2 * Copyright (C) 2019 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.mediav2.cts; 18 19 import android.content.res.AssetFileDescriptor; 20 import android.media.MediaDataSource; 21 import android.media.MediaExtractor; 22 import android.os.ParcelFileDescriptor; 23 24 import androidx.test.filters.SmallTest; 25 26 import org.junit.Ignore; 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.experimental.runners.Enclosed; 30 import org.junit.rules.TestName; 31 import org.junit.runner.RunWith; 32 33 import java.io.File; 34 import java.io.FileDescriptor; 35 import java.io.FileOutputStream; 36 import java.io.IOException; 37 import java.nio.ByteBuffer; 38 39 import static org.junit.Assert.assertTrue; 40 import static org.junit.Assert.fail; 41 42 @RunWith(Enclosed.class) 43 public class ExtractorUnitTest { 44 private static final int MAX_SAMPLE_SIZE = 4 * 1024 * 1024; 45 private static final String mInpPrefix = WorkDir.getMediaDirString(); 46 private static final String mInpMedia = "ForBiggerEscapes.mp4"; 47 48 @SmallTest 49 public static class TestApi { 50 @Rule 51 public TestName testName = new TestName(); 52 53 @Test testGetTrackCountBeforeSetDataSource()54 public void testGetTrackCountBeforeSetDataSource() { 55 MediaExtractor extractor = new MediaExtractor(); 56 try { 57 assertTrue("received valid trackCount before setDataSource", 58 extractor.getTrackCount() <= 0); 59 } catch (Exception e) { 60 // expected 61 } finally { 62 extractor.release(); 63 } 64 } 65 66 @Test testGetTrackCountAfterRelease()67 public void testGetTrackCountAfterRelease() { 68 MediaExtractor extractor = new MediaExtractor(); 69 extractor.release(); 70 try { 71 extractor.getTrackCount(); 72 fail("getTrackCount succeeds after release"); 73 } catch (Exception e) { 74 // expected 75 } 76 } 77 78 @Test testSelectTrackBeforeSetDataSource()79 public void testSelectTrackBeforeSetDataSource() { 80 MediaExtractor extractor = new MediaExtractor(); 81 try { 82 extractor.selectTrack(0); 83 fail("selectTrack succeeds before setDataSource"); 84 } catch (Exception e) { 85 // expected 86 } finally { 87 extractor.release(); 88 } 89 } 90 91 @Test testSelectTrackForInvalidIndex()92 public void testSelectTrackForInvalidIndex() throws IOException { 93 MediaExtractor extractor = new MediaExtractor(); 94 extractor.setDataSource(mInpPrefix + mInpMedia); 95 try { 96 try { 97 extractor.selectTrack(-1); 98 fail("selectTrack succeeds for track index: -1"); 99 } catch (Exception e) { 100 // expected 101 } 102 try { 103 extractor.selectTrack(extractor.getTrackCount()); 104 fail("selectTrack succeeds for out of bounds track index: " + 105 extractor.getTrackCount()); 106 } catch (Exception e) { 107 // expected 108 } 109 } finally { 110 extractor.release(); 111 } 112 } 113 114 @Test testIdempotentSelectTrack()115 public void testIdempotentSelectTrack() throws IOException { 116 MediaExtractor extractor = new MediaExtractor(); 117 extractor.setDataSource(mInpPrefix + mInpMedia); 118 try { 119 extractor.selectTrack(0); 120 extractor.selectTrack(0); 121 } catch (Exception e) { 122 fail("multiple selection of same track has failed"); 123 } finally { 124 extractor.release(); 125 } 126 } 127 128 @Test testSelectTrackAfterRelease()129 public void testSelectTrackAfterRelease() { 130 MediaExtractor extractor = new MediaExtractor(); 131 extractor.release(); 132 try { 133 extractor.selectTrack(0); 134 fail("selectTrack succeeds after release"); 135 } catch (Exception e) { 136 // expected 137 } 138 } 139 140 @Test testUnselectTrackBeforeSetDataSource()141 public void testUnselectTrackBeforeSetDataSource() { 142 MediaExtractor extractor = new MediaExtractor(); 143 try { 144 extractor.unselectTrack(0); 145 fail("unselectTrack succeeds before setDataSource"); 146 } catch (Exception e) { 147 // expected 148 } finally { 149 extractor.release(); 150 } 151 } 152 153 @Test testUnselectTrackForInvalidIndex()154 public void testUnselectTrackForInvalidIndex() throws IOException { 155 MediaExtractor extractor = new MediaExtractor(); 156 extractor.setDataSource(mInpPrefix + mInpMedia); 157 try { 158 try { 159 extractor.unselectTrack(-1); 160 fail("unselectTrack succeeds for track index: -1"); 161 } catch (Exception e) { 162 // expected 163 } 164 try { 165 extractor.unselectTrack(extractor.getTrackCount()); 166 fail("unselectTrack succeeds for out of bounds track index: " + 167 extractor.getTrackCount()); 168 } catch (Exception e) { 169 // expected 170 } 171 } finally { 172 extractor.release(); 173 } 174 } 175 176 @Test testUnselectTrackForUnSelectedTrackIndex()177 public void testUnselectTrackForUnSelectedTrackIndex() throws IOException { 178 MediaExtractor extractor = new MediaExtractor(); 179 extractor.setDataSource(mInpPrefix + mInpMedia); 180 try { 181 extractor.unselectTrack(0); 182 } catch (Exception e) { 183 fail("un-selection of non-selected track has failed"); 184 } finally { 185 extractor.release(); 186 } 187 } 188 189 @Test testIdempotentUnselectTrack()190 public void testIdempotentUnselectTrack() throws IOException { 191 MediaExtractor extractor = new MediaExtractor(); 192 extractor.setDataSource(mInpPrefix + mInpMedia); 193 try { 194 extractor.selectTrack(0); 195 extractor.unselectTrack(0); 196 extractor.unselectTrack(0); 197 } catch (Exception e) { 198 fail("multiple un-selection of selected track has failed"); 199 } finally { 200 extractor.release(); 201 } 202 } 203 204 @Test testUnselectTrackAfterRelease()205 public void testUnselectTrackAfterRelease() { 206 MediaExtractor extractor = new MediaExtractor(); 207 extractor.release(); 208 try { 209 extractor.unselectTrack(0); 210 fail("unselectTrack succeeds after release"); 211 } catch (Exception e) { 212 // expected 213 } 214 } 215 216 @Test testSeekToBeforeSetDataSource()217 public void testSeekToBeforeSetDataSource() { 218 MediaExtractor extractor = new MediaExtractor(); 219 try { 220 extractor.seekTo(33000, MediaExtractor.SEEK_TO_CLOSEST_SYNC); 221 if (extractor.getSampleTime() != -1 || extractor.getSampleSize() != -1 || 222 extractor.getSampleFlags() != -1) { 223 fail("seekTo() succeeds before setting data source, returns non-negative " + 224 "sampleTime / sampleSize / sampleFlags"); 225 } 226 } catch (Exception e) { 227 // expected 228 } finally { 229 extractor.release(); 230 } 231 } 232 233 @Test testSeekToBeforeSelectTrack()234 public void testSeekToBeforeSelectTrack() throws IOException { 235 MediaExtractor extractor = new MediaExtractor(); 236 extractor.setDataSource(mInpPrefix + mInpMedia); 237 try { 238 extractor.seekTo(33000, MediaExtractor.SEEK_TO_CLOSEST_SYNC); 239 if (extractor.getSampleTime() != -1 || extractor.getSampleSize() != -1 || 240 extractor.getSampleFlags() != -1) { 241 fail("seekTo() succeeds before selectTrack, returns non-negative " + 242 "sampleTime / sampleSize / sampleFlags"); 243 } 244 } catch (Exception e) { 245 // expected 246 } finally { 247 extractor.release(); 248 } 249 } 250 251 @Test testSeekToForInvalidMode()252 public void testSeekToForInvalidMode() throws IOException { 253 MediaExtractor extractor = new MediaExtractor(); 254 extractor.setDataSource(mInpPrefix + mInpMedia); 255 extractor.selectTrack(0); 256 long pts = 33000; 257 try { 258 extractor.seekTo(pts, (int) pts); 259 fail("seekTo() succeeds for invalid mode: " + pts); 260 } catch (Exception e) { 261 // expected 262 } finally { 263 extractor.release(); 264 } 265 } 266 267 @Test testSeekToAfterRelease()268 public void testSeekToAfterRelease() throws IOException { 269 MediaExtractor extractor = new MediaExtractor(); 270 extractor.setDataSource(mInpPrefix + mInpMedia); 271 extractor.release(); 272 try { 273 extractor.seekTo(33000, MediaExtractor.SEEK_TO_CLOSEST_SYNC); 274 fail("seekTo() succeeds after release"); 275 } catch (Exception e) { 276 // expected 277 } 278 } 279 280 @Test 281 @Ignore("TODO(b/148205432)") testGetCachedDurationBeforeSetDataSource()282 public void testGetCachedDurationBeforeSetDataSource() { 283 MediaExtractor extractor = new MediaExtractor(); 284 try { 285 assertTrue("received valid cachedDuration before setDataSource", 286 extractor.getCachedDuration() == -1); 287 } catch (Exception e) { 288 // expected 289 } finally { 290 extractor.release(); 291 } 292 } 293 294 @Test testGetCachedDurationAfterRelease()295 public void testGetCachedDurationAfterRelease() { 296 MediaExtractor extractor = new MediaExtractor(); 297 extractor.release(); 298 try { 299 extractor.getCachedDuration(); 300 fail("cachedDuration succeeds after release"); 301 } catch (Exception e) { 302 // expected 303 } 304 } 305 306 @Test 307 @Ignore("TODO(b/148204634)") testHasCacheReachedEOSBeforeSetDataSource()308 public void testHasCacheReachedEOSBeforeSetDataSource() { 309 MediaExtractor extractor = new MediaExtractor(); 310 try { 311 assertTrue("unexpected value received from hasCacheReachedEndOfStream before" + 312 " setDataSource", !extractor.hasCacheReachedEndOfStream()); 313 } catch (Exception e) { 314 // expected 315 } finally { 316 extractor.release(); 317 } 318 } 319 320 @Test testHasCacheReachedEOSAfterRelease()321 public void testHasCacheReachedEOSAfterRelease() { 322 MediaExtractor extractor = new MediaExtractor(); 323 extractor.release(); 324 try { 325 extractor.hasCacheReachedEndOfStream(); 326 fail("hasCacheReachedEndOfStream succeeds after release"); 327 } catch (Exception e) { 328 // expected 329 } 330 } 331 332 @Test testGetMetricsBeforeSetDataSource()333 public void testGetMetricsBeforeSetDataSource() { 334 MediaExtractor extractor = new MediaExtractor(); 335 try { 336 assertTrue("received valid metrics before setDataSource", 337 extractor.getMetrics() == null); 338 } catch (Exception e) { 339 // expected 340 } finally { 341 extractor.release(); 342 } 343 } 344 345 @Test testGetMetricsAfterRelease()346 public void testGetMetricsAfterRelease() { 347 MediaExtractor extractor = new MediaExtractor(); 348 extractor.release(); 349 try { 350 extractor.getMetrics(); 351 fail("getMetrics() succeeds after release"); 352 } catch (Exception e) { 353 // expected 354 } 355 } 356 357 @Test testIdempotentRelease()358 public void testIdempotentRelease() { 359 MediaExtractor extractor = new MediaExtractor(); 360 try { 361 extractor.release(); 362 extractor.release(); 363 } catch (Exception e) { 364 fail(e.getMessage()); 365 } 366 } 367 368 @Test testAdvanceBeforeSetDataSource()369 public void testAdvanceBeforeSetDataSource() { 370 MediaExtractor extractor = new MediaExtractor(); 371 try { 372 assertTrue("advance succeeds before setDataSource", !extractor.advance()); 373 } catch (Exception e) { 374 // expected 375 } finally { 376 extractor.release(); 377 } 378 } 379 380 @Test testAdvanceBeforeSelectTrack()381 public void testAdvanceBeforeSelectTrack() throws IOException { 382 MediaExtractor extractor = new MediaExtractor(); 383 extractor.setDataSource(mInpPrefix + mInpMedia); 384 try { 385 assertTrue("advance succeeds without any active tracks", !extractor.advance()); 386 } catch (Exception e) { 387 // expected 388 } finally { 389 extractor.release(); 390 } 391 } 392 393 @Test testAdvanceAfterRelease()394 public void testAdvanceAfterRelease() { 395 MediaExtractor extractor = new MediaExtractor(); 396 extractor.release(); 397 try { 398 extractor.advance(); 399 fail("advance succeeds after release"); 400 } catch (Exception e) { 401 // expected 402 } 403 } 404 405 @Test testGetSampleFlagsBeforeSetDataSource()406 public void testGetSampleFlagsBeforeSetDataSource() { 407 MediaExtractor extractor = new MediaExtractor(); 408 try { 409 assertTrue("received valid sampleFlag before setDataSource", 410 extractor.getSampleFlags() == -1); 411 } catch (Exception e) { 412 // expected 413 } finally { 414 extractor.release(); 415 } 416 } 417 418 @Test testGetSampleFlagsBeforeSelectTrack()419 public void testGetSampleFlagsBeforeSelectTrack() throws IOException { 420 MediaExtractor extractor = new MediaExtractor(); 421 extractor.setDataSource(mInpPrefix + mInpMedia); 422 try { 423 assertTrue("received valid sampleFlag without any active tracks", 424 extractor.getSampleFlags() == -1); 425 } catch (Exception e) { 426 // expected 427 } finally { 428 extractor.release(); 429 } 430 } 431 432 @Test testGetSampleFlagsAfterRelease()433 public void testGetSampleFlagsAfterRelease() { 434 MediaExtractor extractor = new MediaExtractor(); 435 extractor.release(); 436 try { 437 extractor.getSampleFlags(); 438 fail("getSampleFlags succeeds after release"); 439 } catch (Exception e) { 440 // expected 441 } 442 } 443 444 @Test testGetSampleTimeBeforeSetDataSource()445 public void testGetSampleTimeBeforeSetDataSource() { 446 MediaExtractor extractor = new MediaExtractor(); 447 try { 448 assertTrue("received valid sampleTime before setDataSource", 449 extractor.getSampleTime() == -1); 450 } catch (Exception e) { 451 // expected 452 } finally { 453 extractor.release(); 454 } 455 } 456 457 @Test testGetSampleTimeBeforeSelectTrack()458 public void testGetSampleTimeBeforeSelectTrack() throws IOException { 459 MediaExtractor extractor = new MediaExtractor(); 460 extractor.setDataSource(mInpPrefix + mInpMedia); 461 try { 462 assertTrue("received valid sampleTime without any active tracks", 463 extractor.getSampleTime() == -1); 464 } catch (Exception e) { 465 // expected 466 } finally { 467 extractor.release(); 468 } 469 } 470 471 @Test testGetSampleTimeAfterRelease()472 public void testGetSampleTimeAfterRelease() { 473 MediaExtractor extractor = new MediaExtractor(); 474 extractor.release(); 475 try { 476 extractor.getSampleTime(); 477 fail("getSampleTime succeeds after release"); 478 } catch (Exception e) { 479 // expected 480 } 481 } 482 483 @Test testGetSampleSizeBeforeSetDataSource()484 public void testGetSampleSizeBeforeSetDataSource() { 485 MediaExtractor extractor = new MediaExtractor(); 486 try { 487 assertTrue("received valid sampleSize before setDataSource", 488 extractor.getSampleSize() == -1); 489 } catch (Exception e) { 490 // expected 491 } finally { 492 extractor.release(); 493 } 494 } 495 496 @Test testGetSampleSizeBeforeSelectTrack()497 public void testGetSampleSizeBeforeSelectTrack() throws IOException { 498 MediaExtractor extractor = new MediaExtractor(); 499 extractor.setDataSource(mInpPrefix + mInpMedia); 500 try { 501 assertTrue("received valid sampleSize without any active tracks", 502 extractor.getSampleSize() == -1); 503 } catch (Exception e) { 504 // expected 505 } finally { 506 extractor.release(); 507 } 508 } 509 510 @Test testGetSampleSizeAfterRelease()511 public void testGetSampleSizeAfterRelease() { 512 MediaExtractor extractor = new MediaExtractor(); 513 extractor.release(); 514 try { 515 extractor.getSampleSize(); 516 fail("getSampleSize succeeds after release"); 517 } catch (Exception e) { 518 // expected 519 } 520 } 521 522 @Test testGetSampleTrackIndexBeforeSetDataSource()523 public void testGetSampleTrackIndexBeforeSetDataSource() { 524 MediaExtractor extractor = new MediaExtractor(); 525 try { 526 assertTrue("received valid sampleTrackIndex before setDataSource", 527 extractor.getSampleTrackIndex() == -1); 528 } catch (Exception e) { 529 // expected 530 } finally { 531 extractor.release(); 532 } 533 } 534 535 @Test testGetSampleTrackIndexBeforeSelectTrack()536 public void testGetSampleTrackIndexBeforeSelectTrack() throws IOException { 537 MediaExtractor extractor = new MediaExtractor(); 538 extractor.setDataSource(mInpPrefix + mInpMedia); 539 try { 540 assertTrue("received valid sampleTrackIndex without any active tracks", 541 extractor.getSampleTrackIndex() == -1); 542 } catch (Exception e) { 543 // expected 544 } finally { 545 extractor.release(); 546 } 547 } 548 549 @Test testGetSampleTrackIndexAfterRelease()550 public void testGetSampleTrackIndexAfterRelease() { 551 MediaExtractor extractor = new MediaExtractor(); 552 extractor.release(); 553 try { 554 extractor.getSampleTrackIndex(); 555 fail("getSampleTrackIndex succeeds after release"); 556 } catch (Exception e) { 557 // expected 558 } 559 } 560 561 @Test testGetTrackFormatBeforeSetDataSource()562 public void testGetTrackFormatBeforeSetDataSource() { 563 MediaExtractor extractor = new MediaExtractor(); 564 try { 565 extractor.getTrackFormat(0); 566 fail("getTrackFormat succeeds before setDataSource"); 567 } catch (Exception e) { 568 // expected 569 } finally { 570 extractor.release(); 571 } 572 } 573 574 @Test testGetTrackFormatForInvalidIndex()575 public void testGetTrackFormatForInvalidIndex() throws IOException { 576 MediaExtractor extractor = new MediaExtractor(); 577 extractor.setDataSource(mInpPrefix + mInpMedia); 578 try { 579 try { 580 extractor.getTrackFormat(-1); 581 fail("getTrackFormat succeeds for track index: -1"); 582 } catch (Exception e) { 583 // expected 584 } 585 try { 586 extractor.getTrackFormat(extractor.getTrackCount()); 587 fail("getTrackFormat succeeds for out of bounds track index: " + 588 extractor.getTrackCount()); 589 } catch (Exception e) { 590 // expected 591 } 592 } finally { 593 extractor.release(); 594 } 595 } 596 597 @Test testGetTrackFormatAfterRelease()598 public void testGetTrackFormatAfterRelease() { 599 MediaExtractor extractor = new MediaExtractor(); 600 extractor.release(); 601 try { 602 extractor.getTrackFormat(0); 603 fail("getTrackFormat succeeds after release"); 604 } catch (Exception e) { 605 // expected 606 } 607 } 608 609 @Test testReadSampleDataBeforeSetDataSource()610 public void testReadSampleDataBeforeSetDataSource() { 611 MediaExtractor extractor = new MediaExtractor(); 612 ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE); 613 try { 614 assertTrue("readSampleData returns val >= 0 before setDataSource", 615 extractor.readSampleData(byteBuffer, 0) < 0); 616 } catch (Exception e) { 617 // expected 618 } finally { 619 extractor.release(); 620 } 621 } 622 623 @Test testReadSampleDataBeforeSelectTrack()624 public void testReadSampleDataBeforeSelectTrack() throws IOException { 625 MediaExtractor extractor = new MediaExtractor(); 626 ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE); 627 extractor.setDataSource(mInpPrefix + mInpMedia); 628 try { 629 assertTrue("readSampleData returns val >= 0 without any active tracks", 630 extractor.readSampleData(byteBuffer, 0) < 0); 631 } catch (Exception e) { 632 // expected 633 } finally { 634 extractor.release(); 635 } 636 } 637 638 @Test testIfInvalidOffsetIsRejectedByReadSampleData()639 public void testIfInvalidOffsetIsRejectedByReadSampleData() throws IOException { 640 MediaExtractor extractor = new MediaExtractor(); 641 ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE); 642 extractor.setDataSource(mInpPrefix + mInpMedia); 643 extractor.selectTrack(0); 644 try { 645 // readSampleData with negative offset 646 try { 647 extractor.readSampleData(byteBuffer, -1); 648 fail("readSampleData succeeds with negative offset"); 649 } catch (Exception e) { 650 // expected 651 } 652 653 // readSampleData with byteBuffer's capacity - offset < frame size 654 int sampleSize = (int) extractor.getSampleSize(); 655 try { 656 extractor.readSampleData(byteBuffer, MAX_SAMPLE_SIZE - sampleSize + 1); 657 fail("readSampleData succeeds when available size of byteBuffer is less than " + 658 "frame size"); 659 } catch (Exception e) { 660 // expected 661 } 662 } finally { 663 extractor.release(); 664 } 665 } 666 667 @Test testReadSampleDataAfterRelease()668 public void testReadSampleDataAfterRelease() { 669 MediaExtractor extractor = new MediaExtractor(); 670 ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE); 671 extractor.release(); 672 try { 673 extractor.readSampleData(byteBuffer, 0); 674 fail("readSampleData succeeds after release"); 675 } catch (Exception e) { 676 // expected 677 } 678 } 679 680 @Test testIfInvalidDataSourceIsRejectedBySetDataSource()681 public void testIfInvalidDataSourceIsRejectedBySetDataSource() throws IOException { 682 MediaExtractor extractor = new MediaExtractor(); 683 TestMediaDataSource dataSource = 684 TestMediaDataSource.fromString(mInpPrefix + mInpMedia, true, false); 685 try { 686 try { 687 extractor.setDataSource(dataSource); 688 fail("setDataSource succeeds with malformed media data source"); 689 } catch (Exception e) { 690 // expected 691 } 692 assertTrue(dataSource.isClosed()); 693 dataSource = TestMediaDataSource.fromString(mInpPrefix + mInpMedia, false, true); 694 695 try { 696 extractor.setDataSource(dataSource); 697 fail("setDataSource succeeds with malformed media data source"); 698 } catch (Exception e) { 699 // expected 700 } 701 } finally { 702 assertTrue(dataSource.isClosed()); 703 extractor.release(); 704 } 705 } 706 707 @Test testIfNullFDIsRejectedBySetDataSource()708 public void testIfNullFDIsRejectedBySetDataSource() { 709 MediaExtractor extractor = new MediaExtractor(); 710 try { 711 extractor.setDataSource((FileDescriptor) null); 712 fail("setDataSource succeeds with null fd"); 713 } catch (Exception e) { 714 // expected 715 } finally { 716 extractor.release(); 717 } 718 } 719 720 @Test testIfWriteOnlyAssetFDIsRejectedBySetDataSource()721 public void testIfWriteOnlyAssetFDIsRejectedBySetDataSource() throws IOException { 722 File inpFile = File.createTempFile("ExtractorTestApisetDSAFD", ".in"); 723 MediaExtractor extractor = new MediaExtractor(); 724 try (ParcelFileDescriptor parcelFD = ParcelFileDescriptor 725 .open(inpFile, ParcelFileDescriptor.MODE_WRITE_ONLY); 726 AssetFileDescriptor afd = new AssetFileDescriptor(parcelFD, 0, 727 AssetFileDescriptor.UNKNOWN_LENGTH)) { 728 extractor.setDataSource(afd); 729 fail("setDataSource succeeds write only afd"); 730 } catch (Exception e) { 731 // expected 732 } finally { 733 extractor.release(); 734 } 735 inpFile.delete(); 736 } 737 738 @Test testIfWriteOnlyFDIsRejectedBySetDataSource()739 public void testIfWriteOnlyFDIsRejectedBySetDataSource() throws IOException { 740 MediaExtractor extractor = new MediaExtractor(); 741 File inpFile = File.createTempFile("ExtractorTestApisetDSFD", ".in"); 742 try (FileOutputStream fOut = new FileOutputStream(inpFile)) { 743 extractor.setDataSource(fOut.getFD()); 744 fail("setDataSource succeeds write only fd"); 745 } catch (Exception e) { 746 // expected 747 } finally { 748 extractor.release(); 749 } 750 inpFile.delete(); 751 } 752 753 @Test testIfNullMediaDataSourceIsRejectedBySetDataSource()754 public void testIfNullMediaDataSourceIsRejectedBySetDataSource() { 755 MediaExtractor extractor = new MediaExtractor(); 756 try { 757 extractor.setDataSource((MediaDataSource) null); 758 fail("setDataSource succeeds with null data source"); 759 } catch (Exception e) { 760 // expected 761 } finally { 762 extractor.release(); 763 } 764 } 765 766 @Test testIfNullFileIsRejectedBySetDataSource()767 public void testIfNullFileIsRejectedBySetDataSource() { 768 MediaExtractor extractor = new MediaExtractor(); 769 try { 770 extractor.setDataSource((String) null); 771 fail("setDataSource succeeds with null file path"); 772 } catch (Exception e) { 773 // expected 774 } finally { 775 extractor.release(); 776 } 777 } 778 779 @Test testIfNullAssetFDIsRejectedBySetDataSource()780 public void testIfNullAssetFDIsRejectedBySetDataSource() { 781 MediaExtractor extractor = new MediaExtractor(); 782 try { 783 extractor.setDataSource((AssetFileDescriptor) null); 784 fail("setDataSource succeeds with null asset fd"); 785 } catch (Exception e) { 786 // expected 787 } finally { 788 extractor.release(); 789 } 790 } 791 } 792 793 @SmallTest 794 public static class TestApiNative { 795 @Rule 796 public TestName testName = new TestName(); 797 798 static { 799 System.loadLibrary("ctsmediav2extractor_jni"); 800 } 801 802 @Test testGetTrackCountBeforeSetDataSource()803 public void testGetTrackCountBeforeSetDataSource() { 804 assertTrue(nativeTestGetTrackCountBeforeSetDataSource()); 805 } nativeTestGetTrackCountBeforeSetDataSource()806 private native boolean nativeTestGetTrackCountBeforeSetDataSource(); 807 808 @Test testSelectTrackBeforeSetDataSource()809 public void testSelectTrackBeforeSetDataSource() { 810 assertTrue(nativeTestSelectTrackBeforeSetDataSource()); 811 } nativeTestSelectTrackBeforeSetDataSource()812 private native boolean nativeTestSelectTrackBeforeSetDataSource(); 813 814 @Test testSelectTrackForInvalidIndex()815 public void testSelectTrackForInvalidIndex() { 816 assertTrue(nativeTestSelectTrackForInvalidIndex(mInpPrefix + mInpMedia)); 817 } nativeTestSelectTrackForInvalidIndex(String srcPath)818 private native boolean nativeTestSelectTrackForInvalidIndex(String srcPath); 819 820 @Test testIdempotentSelectTrack()821 public void testIdempotentSelectTrack() { 822 assertTrue(nativeTestIdempotentSelectTrack(mInpPrefix + mInpMedia)); 823 } nativeTestIdempotentSelectTrack(String srcPath)824 private native boolean nativeTestIdempotentSelectTrack(String srcPath); 825 826 @Test testUnselectTrackBeforeSetDataSource()827 public void testUnselectTrackBeforeSetDataSource() { 828 assertTrue(nativeTestUnselectTrackBeforeSetDataSource()); 829 } nativeTestUnselectTrackBeforeSetDataSource()830 private native boolean nativeTestUnselectTrackBeforeSetDataSource(); 831 832 @Test testUnselectTrackForInvalidIndex()833 public void testUnselectTrackForInvalidIndex() { 834 assertTrue(nativeTestUnselectTrackForInvalidIndex(mInpPrefix + mInpMedia)); 835 } nativeTestUnselectTrackForInvalidIndex(String srcPath)836 private native boolean nativeTestUnselectTrackForInvalidIndex(String srcPath); 837 838 @Test testUnselectTrackForUnSelectedTrackIndex()839 public void testUnselectTrackForUnSelectedTrackIndex() { 840 assertTrue(nativeTestUnselectTrackForUnSelectedTrackIndex(mInpPrefix + mInpMedia)); 841 } nativeTestUnselectTrackForUnSelectedTrackIndex(String srcPath)842 private native boolean nativeTestUnselectTrackForUnSelectedTrackIndex(String srcPath); 843 844 @Test testIdempotentUnselectTrack()845 public void testIdempotentUnselectTrack() { 846 assertTrue(nativeTestIdempotentUnselectTrack(mInpPrefix + mInpMedia)); 847 } nativeTestIdempotentUnselectTrack(String srcPath)848 private native boolean nativeTestIdempotentUnselectTrack(String srcPath); 849 850 @Test testSeekToBeforeSetDataSource()851 public void testSeekToBeforeSetDataSource() { 852 assertTrue(nativeTestSeekToBeforeSetDataSource()); 853 } nativeTestSeekToBeforeSetDataSource()854 private native boolean nativeTestSeekToBeforeSetDataSource(); 855 856 @Test testSeekToBeforeSelectTrack()857 public void testSeekToBeforeSelectTrack() { 858 assertTrue(nativeTestSeekToBeforeSelectTrack(mInpPrefix + mInpMedia)); 859 } nativeTestSeekToBeforeSelectTrack(String srcPath)860 private native boolean nativeTestSeekToBeforeSelectTrack(String srcPath); 861 862 @Test 863 @Ignore("TODO(b/148205432)") testGetCachedDurationBeforeSetDataSource()864 public void testGetCachedDurationBeforeSetDataSource() { 865 assertTrue(nativeTestGetCachedDurationBeforeSetDataSource()); 866 } nativeTestGetCachedDurationBeforeSetDataSource()867 private native boolean nativeTestGetCachedDurationBeforeSetDataSource(); 868 869 @Test testIfGetFileFormatSucceedsBeforeSetDataSource()870 public void testIfGetFileFormatSucceedsBeforeSetDataSource() { 871 assertTrue(nativeTestIfGetFileFormatSucceedsBeforeSetDataSource()); 872 } nativeTestIfGetFileFormatSucceedsBeforeSetDataSource()873 private native boolean nativeTestIfGetFileFormatSucceedsBeforeSetDataSource(); 874 875 @Test testAdvanceBeforeSetDataSource()876 public void testAdvanceBeforeSetDataSource() { 877 assertTrue(nativeTestAdvanceBeforeSetDataSource()); 878 } nativeTestAdvanceBeforeSetDataSource()879 private native boolean nativeTestAdvanceBeforeSetDataSource(); 880 881 @Test testAdvanceBeforeSelectTrack()882 public void testAdvanceBeforeSelectTrack() { 883 assertTrue(nativeTestAdvanceBeforeSelectTrack(mInpPrefix + mInpMedia)); 884 } nativeTestAdvanceBeforeSelectTrack(String srcPath)885 private native boolean nativeTestAdvanceBeforeSelectTrack(String srcPath); 886 887 @Test testGetSampleFlagsBeforeSetDataSource()888 public void testGetSampleFlagsBeforeSetDataSource() { 889 assertTrue(nativeTestGetSampleFlagsBeforeSetDataSource()); 890 } nativeTestGetSampleFlagsBeforeSetDataSource()891 private native boolean nativeTestGetSampleFlagsBeforeSetDataSource(); 892 893 @Test testGetSampleFlagsBeforeSelectTrack()894 public void testGetSampleFlagsBeforeSelectTrack() { 895 assertTrue(nativeTestGetSampleFlagsBeforeSelectTrack(mInpPrefix + mInpMedia)); 896 } nativeTestGetSampleFlagsBeforeSelectTrack(String srcPath)897 private native boolean nativeTestGetSampleFlagsBeforeSelectTrack(String srcPath); 898 899 @Test testGetSampleTimeBeforeSetDataSource()900 public void testGetSampleTimeBeforeSetDataSource() { 901 assertTrue(nativeTestGetSampleTimeBeforeSetDataSource()); 902 } nativeTestGetSampleTimeBeforeSetDataSource()903 private native boolean nativeTestGetSampleTimeBeforeSetDataSource(); 904 905 @Test testGetSampleTimeBeforeSelectTrack()906 public void testGetSampleTimeBeforeSelectTrack() { 907 assertTrue(nativeTestGetSampleTimeBeforeSelectTrack(mInpPrefix + mInpMedia)); 908 } nativeTestGetSampleTimeBeforeSelectTrack(String srcPath)909 private native boolean nativeTestGetSampleTimeBeforeSelectTrack(String srcPath); 910 911 @Test testGetSampleSizeBeforeSetDataSource()912 public void testGetSampleSizeBeforeSetDataSource() { 913 assertTrue(nativeTestGetSampleSizeBeforeSetDataSource()); 914 } nativeTestGetSampleSizeBeforeSetDataSource()915 private native boolean nativeTestGetSampleSizeBeforeSetDataSource(); 916 917 @Test testGetSampleSizeBeforeSelectTrack()918 public void testGetSampleSizeBeforeSelectTrack() { 919 assertTrue(nativeTestGetSampleSizeBeforeSelectTrack(mInpPrefix + mInpMedia)); 920 } nativeTestGetSampleSizeBeforeSelectTrack(String srcPath)921 private native boolean nativeTestGetSampleSizeBeforeSelectTrack(String srcPath); 922 923 @Test testIfGetSampleFormatBeforeSetDataSource()924 public void testIfGetSampleFormatBeforeSetDataSource() { 925 assertTrue(nativeTestIfGetSampleFormatBeforeSetDataSource()); 926 } nativeTestIfGetSampleFormatBeforeSetDataSource()927 private native boolean nativeTestIfGetSampleFormatBeforeSetDataSource(); 928 929 @Test testIfGetSampleFormatBeforeSelectTrack()930 public void testIfGetSampleFormatBeforeSelectTrack() { 931 assertTrue(nativeTestIfGetSampleFormatBeforeSelectTrack(mInpPrefix + mInpMedia)); 932 } nativeTestIfGetSampleFormatBeforeSelectTrack(String srcPath)933 private native boolean nativeTestIfGetSampleFormatBeforeSelectTrack(String srcPath); 934 935 @Test testGetSampleTrackIndexBeforeSetDataSource()936 public void testGetSampleTrackIndexBeforeSetDataSource() { 937 assertTrue(nativeTestGetSampleTrackIndexBeforeSetDataSource()); 938 } nativeTestGetSampleTrackIndexBeforeSetDataSource()939 private native boolean nativeTestGetSampleTrackIndexBeforeSetDataSource(); 940 941 @Test testGetSampleTrackIndexBeforeSelectTrack()942 public void testGetSampleTrackIndexBeforeSelectTrack() { 943 assertTrue( 944 nativeTestGetSampleTrackIndexBeforeSelectTrack(mInpPrefix + mInpMedia)); 945 } nativeTestGetSampleTrackIndexBeforeSelectTrack(String srcPath)946 private native boolean nativeTestGetSampleTrackIndexBeforeSelectTrack(String srcPath); 947 948 @Test testGetTrackFormatBeforeSetDataSource()949 public void testGetTrackFormatBeforeSetDataSource() { 950 assertTrue(nativeTestGetTrackFormatBeforeSetDataSource()); 951 } nativeTestGetTrackFormatBeforeSetDataSource()952 private native boolean nativeTestGetTrackFormatBeforeSetDataSource(); 953 954 @Test testGetTrackFormatForInvalidIndex()955 public void testGetTrackFormatForInvalidIndex() { 956 assertTrue(nativeTestGetTrackFormatForInvalidIndex(mInpPrefix + mInpMedia)); 957 } nativeTestGetTrackFormatForInvalidIndex(String srcPath)958 private native boolean nativeTestGetTrackFormatForInvalidIndex(String srcPath); 959 960 @Test testReadSampleDataBeforeSetDataSource()961 public void testReadSampleDataBeforeSetDataSource() { 962 assertTrue(nativeTestReadSampleDataBeforeSetDataSource()); 963 } nativeTestReadSampleDataBeforeSetDataSource()964 private native boolean nativeTestReadSampleDataBeforeSetDataSource(); 965 966 @Test testReadSampleDataBeforeSelectTrack()967 public void testReadSampleDataBeforeSelectTrack() { 968 assertTrue(nativeTestReadSampleDataBeforeSelectTrack(mInpPrefix + mInpMedia)); 969 } nativeTestReadSampleDataBeforeSelectTrack(String srcPath)970 private native boolean nativeTestReadSampleDataBeforeSelectTrack(String srcPath); 971 972 @Test testIfNullLocationIsRejectedBySetDataSource()973 public void testIfNullLocationIsRejectedBySetDataSource() { 974 assertTrue(nativeTestIfNullLocationIsRejectedBySetDataSource()); 975 } nativeTestIfNullLocationIsRejectedBySetDataSource()976 private native boolean nativeTestIfNullLocationIsRejectedBySetDataSource(); 977 } 978 } 979