CB-11757 (ios) Error out if trying to stop playback while in a wrong state 113/head
authorAlexander Sorokin <alexander.sorokin@akvelon.com>
Fri, 26 Aug 2016 09:48:50 +0000 (12:48 +0300)
committerAlexander Sorokin <alexander.sorokin@akvelon.com>
Mon, 29 Aug 2016 07:49:44 +0000 (10:49 +0300)
src/ios/CDVSound.h
src/ios/CDVSound.m
tests/tests.js

index 2f1d0e0..3f123b0 100644 (file)
@@ -22,6 +22,7 @@
 #import <Cordova/CDVPlugin.h>
 
 enum CDVMediaError {
+    MEDIA_ERR_NONE_ACTIVE = 0,
     MEDIA_ERR_ABORTED = 1,
     MEDIA_ERR_NETWORK = 2,
     MEDIA_ERR_DECODE = 3,
index 98de46f..09d3587 100644 (file)
                }];
             jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_STOPPED];
         } else {
-            // cannot seek, do nothing
+            // cannot seek, wrong state
+            CDVMediaError errcode = MEDIA_ERR_NONE_ACTIVE;
+            NSString* errMsg = @"Cannot service stop request until the avPlayer is in 'AVPlayerStatusReadyToPlay' state.";
+            jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:errcode message:errMsg]];
         }
     }
     // ignore if no media playing
index d627221..fa283d9 100644 (file)
@@ -403,6 +403,7 @@ exports.defineAutoTests = function () {
                                 expect(position).toBeLessThan(10);
                                 media1.stop();
                                 media1.release();
+                                context.done = true;
                                 done();
                             }, failed.bind(null, done, 'media1.getCurrentPosition - Error getting media current position'),context);
                         }, 4000);
@@ -436,6 +437,7 @@ exports.defineAutoTests = function () {
                         expect(true).toBe(true);
                         media1.stop();
                         media1.release();
+                        context.done = true;
                         done();
                     }
                 };
@@ -444,27 +446,73 @@ exports.defineAutoTests = function () {
             media1.play();
         }, ACTUAL_PLAYBACK_TEST_TIMEOUT);
 
-        it("media.spec.26 should not crash or throw when setting the volume right after creating the media", function () {
+        it("media.spec.26 should not crash or throw when setting the volume right after creating the media", function (done) {
             //bb10 dialog pops up, preventing tests from running
             if (cordova.platformId === 'blackberry10') {
                 pending();
             }
 
-            var context = this;
             var mediaFile = WEB_MP3_FILE;
             var media = null;
 
-            var successCallback = function () { };
-            var statusChange = function () { };
-
             expect(function () {
-                media = new Media(mediaFile, successCallback, failed.bind(null, successCallback, 'Error creating Media object. Media file: ' + mediaFile, context), statusChange);
+                media = new Media(mediaFile);
                 media.setVolume('0.5');
             }).not.toThrow();
-            if (media) {
-                media.release();
+
+            // if there is no exception or crash in 3 seconds, the spec is completed
+            setTimeout(function () {
+                if (media) {
+                    media.release();
+                    done();
+                }
+            }, 3000);
+        });
+
+        it("media.spec.27 should call success or error when trying to stop a media that is in starting state", function (done) {
+            //bb10 dialog pops up, preventing tests from running
+            if (!isAudioSupported || cordova.platformId === 'blackberry10') {
+                pending();
             }
+
+            var mediaFile = WEB_MP3_FILE;
+            var media = null;
+            var context = this;
+            var beenStarting = false;
+            var safeDone = function () {
+                if (!context.done) {
+                    media.release();
+                    context.done = true;
+                    done();
+                }
+            };
+
+            var errorCallback = jasmine.createSpy('errorCallback').and.callFake(function (e) {
+                expect(beenStarting).toBe(true);
+                safeDone();
+            });
+            var successCallback = function () {
+                expect(true).toBe(true);
+                safeDone();
+            };
+            var statusChange = function (s) {
+                if ((s == Media.MEDIA_STARTING) && !context.done) {
+                    beenStarting = true;
+                    media.stop();
+                } else if (s == Media.MEDIA_RUNNING) {
+                    // Some plugin implementations may skip "Starting" state
+                    // so we'll also try to call stop in "Running" state,
+                    // but in this case we should check that the "Starting" state wasn't really reached,
+                    // otherwise it would mean that the previous media.stop() call has been ignored
+                    expect(beenStarting).toBe(false);
+                    media.stop();
+                }
+            };
+
+            media = new Media(mediaFile, successCallback, errorCallback, statusChange);
+            media.play();
         });
+
     });
 };