Compare commits

..

14 Commits
2.1.1 ... 2.2.0

Author SHA1 Message Date
Steve Gill
d124e03cb9 Updated version and RELEASENOTES.md for release 2.2.0 2016-04-15 13:08:26 -07:00
Omar Mefire
4202fff7ac CB-10873
- Avoid crash due to usage of uninitialized variable when writing geolocation data to image destination
    - Properly handle 'CameraUsesGeolocation' option by properly setting geolocation data in EXIF header in all cases

 This closes #205
2016-04-13 15:49:51 -07:00
Omar Mefire
def399fe51 CB-10873 Avoid crash due to usage of uninitialized variable when writing geolocation data to image destination 2016-04-13 15:49:50 -07:00
Alexander Sorokin
82c9f4524a CB-11073 Appium tests stability improvements 2016-04-13 12:42:51 +03:00
Simon MacDonald
a9c18710f2 Replace PermissionHelper.java with cordova-plugin-compat 2016-04-05 12:17:18 -04:00
Raghav Katyal
624ddd5ced Making focus handler work only for windows 10 phone 2016-03-23 18:33:09 -07:00
daserge
fb871d40e2 CB-10865 Run ios native tests on Travis
Updated cordova-ios tests dependency version to latest published
Changed the tests to reflect the current scaling behavior
Fixed a typo in manual test img.onloadend -> img.onload
2016-03-21 12:54:07 +03:00
riknoll
0cd962466d CB-10120 android: Fixing use of constants and PermissionHelper
This closes #179
2016-03-14 17:56:52 -07:00
ochakov
c12206ebc8 CB-10120 android: Fix missing CAMERA permission for Android M
According to the PR conversation, when android.permission.CAMERA
is not set in the package, there is no need to ask for the
camera permission. Also, checking now camera and storage
permissions separately, so if only one of them is missing, the
other one will be requested and not both.

Rebased by MatthewBooth and riknoll

This closes #142, closes #174
2016-03-14 17:03:26 -07:00
Richard Knoll
826aca3524 CB-10756: Adding sterner warnings about DATA_URL
This closes #193
2016-03-14 10:39:20 -07:00
Julio César
76c129c95e CB-10460 getRealPath return null in some cases
Now there is only a function to get the real path on API 11 and above.
2016-03-11 20:43:08 +01:00
Carlos Santana
fac7a53383 clean RELEASENOTES for bold font 2016-03-10 13:49:49 -05:00
Alexander Sorokin
1348d2e138 Appium tests: tweaking some timeouts and default screenshot path 2016-03-10 13:27:34 +03:00
Carlos Santana
c5f5a46e3e CB-10820 Incremented plugin version. 2016-03-09 22:34:17 -05:00
21 changed files with 475 additions and 870 deletions

View File

@@ -1,4 +1,8 @@
language: node_js
language: objective-c
sudo: false
node_js:
- "4.2"
env:
- TEST_DIR=.
- TEST_DIR=./tests/ios
script: cd $TEST_DIR && npm install && npm test

View File

@@ -73,20 +73,20 @@ Documentation consists of template and API docs produced from the plugin JS code
* [camera](#module_camera)
* [.getPicture(successCallback, errorCallback, options)](#module_camera.getPicture)
* [.cleanup()](#module_camera.cleanup)
* [.onError](#module_camera.onError) : <code>function</code>
* [.onSuccess](#module_camera.onSuccess) : <code>function</code>
* [.CameraOptions](#module_camera.CameraOptions) : <code>Object</code>
* [.getPicture(successCallback, errorCallback, options)](#module_camera.getPicture)
* [.cleanup()](#module_camera.cleanup)
* [.onError](#module_camera.onError) : <code>function</code>
* [.onSuccess](#module_camera.onSuccess) : <code>function</code>
* [.CameraOptions](#module_camera.CameraOptions) : <code>Object</code>
* [Camera](#module_Camera)
* [.DestinationType](#module_Camera.DestinationType) : <code>enum</code>
* [.EncodingType](#module_Camera.EncodingType) : <code>enum</code>
* [.MediaType](#module_Camera.MediaType) : <code>enum</code>
* [.PictureSourceType](#module_Camera.PictureSourceType) : <code>enum</code>
* [.PopoverArrowDirection](#module_Camera.PopoverArrowDirection) : <code>enum</code>
* [.Direction](#module_Camera.Direction) : <code>enum</code>
* [.DestinationType](#module_Camera.DestinationType) : <code>enum</code>
* [.EncodingType](#module_Camera.EncodingType) : <code>enum</code>
* [.MediaType](#module_Camera.MediaType) : <code>enum</code>
* [.PictureSourceType](#module_Camera.PictureSourceType) : <code>enum</code>
* [.PopoverArrowDirection](#module_Camera.PopoverArrowDirection) : <code>enum</code>
* [.Direction](#module_Camera.Direction) : <code>enum</code>
* [CameraPopoverHandle](#module_CameraPopoverHandle)
* [CameraPopoverOptions](#module_CameraPopoverOptions)
@@ -249,7 +249,7 @@ Optional parameters to customize the camera settings.
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| DATA_URL | <code>number</code> | <code>0</code> | Return base64 encoded string |
| DATA_URL | <code>number</code> | <code>0</code> | Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI or NATIVE_URI if possible |
| FILE_URI | <code>number</code> | <code>1</code> | Return file uri (content://media/external/images/media/2 for Android) |
| NATIVE_URI | <code>number</code> | <code>2</code> | Return native uri (eg. asset-library://... for iOS) |
@@ -314,13 +314,7 @@ Matches iOS UIPopoverArrowDirection constants to specify arrow location on popov
<a name="module_CameraPopoverOptions"></a>
## CameraPopoverOptions
iOS-only parameters that specify the anchor element location and arrow
direction of the popover when selecting images from an iPad's library
or album.
Note that the size of the popover may change to adjust to the
direction of the arrow and orientation of the screen. Make sure to
account for orientation changes when specifying the anchor element
location.
iOS-only parameters that specify the anchor element location and arrow
direction of the popover when selecting images from an iPad's library
or album.
Note that the size of the popover may change to adjust to the
@@ -363,21 +357,6 @@ window.onorientationchange = function() {
}
```
---
Take a photo and retrieve it as a Base64-encoded image:
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
## `camera.getPicture` Errata
@@ -392,6 +371,27 @@ Take a photo and retrieve the image's file location:
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
function onFail(message) {
alert('Failed because: ' + message);
}
Take a photo and retrieve it as a Base64-encoded image:
/**
* Warning: Using DATA_URL is not recommended! The DATA_URL destination
* type is very memory intensive, even with a low quality setting. Using it
* can result in out of memory errors and application crashes. Use FILE_URI
* or NATIVE_URI instead.
*/
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {

View File

@@ -20,31 +20,42 @@
-->
# Release Notes
### 2.2.0 (Apr 15, 2016)
* CB-10873 Avoid crash due to usage of uninitialized variable when writing geolocation data to image destination. Properly handle 'CameraUsesGeolocation' option by properly setting geolocation data in EXIF header in all cases
* CB-11073 Appium tests stability improvements
* Replace `PermissionHelper.java` with `cordova-plugin-compat`
* Making focus handler work only for **windows 10** phone
* CB-10865 Run **ios** native tests on **Travis**
* CB-10120 Fixing use of constants and `PermissionHelper`
* CB-10120 Fix missing CAMERA permission for **Android M**
* CB-10756 Adding sterner warnings about `DATA_URL`
* CB-10460 `getRealPath` return null in some cases
### 2.1.1 (Mar 09, 2016)
* [CB-10825](https://issues.apache.org/jira/browse/CB-10825) android: Always request READ permission for gallery source
* [CB-10825](https://issues.apache.org/jira/browse/CB-10825) **Android** should request READ permission for gallery source
* added apache license header to appium files
* [CB-10720](https://issues.apache.org/jira/browse/CB-10720) Fixed spelling, capitalization, and other small issues.
* [CB-10414](https://issues.apache.org/jira/browse/CB-10414) Adding focus handler to resume video when user comes back on leaving the app while preview was running
* Appium tests: adjust swipe distance on ** Android **
* Appium tests: adjust swipe distance on **Android**
* [CB-10750](https://issues.apache.org/jira/browse/CB-10750) Appium tests: fail fast if session is irrecoverable
* Adding missing semi colon
* Adding focus handler to make sure filepicker gets launched when app is active on ** Windows **
* Adding focus handler to make sure filepicker gets launched when app is active on **Windows**
* [CB-10128](https://issues.apache.org/jira/browse/CB-10128) **iOS** Fixed how checks access authorization to camera & library. This closes #146
* [CB-10636](https://issues.apache.org/jira/browse/CB-10636) Add JSHint for plugins
* [CB-10639](https://issues.apache.org/jira/browse/CB-10639) Appium tests: Added some timeouts, Taking a screenshot on failure, Retry taking a picture up to 3 times, Try to restart the Appium session if it's lost
* [CB-10552](https://issues.apache.org/jira/browse/CB-10552) Replacing images in README.md.
* Added a lot of more cases to get the real path on ** Android **
* [CB-10625](https://issues.apache.org/jira/browse/CB-10625) ** Android ** getPicture fails when getting a photo from the Photo Library - Google Photos
* [CB-10619](https://issues.apache.org/jira/browse/CB-10619) Appium tests: Properly switch to webview on ** Android **
* Added a lot of more cases to get the real path on **Android**
* [CB-10625](https://issues.apache.org/jira/browse/CB-10625) **Android** getPicture fails when getting a photo from the Photo Library - Google Photos
* [CB-10619](https://issues.apache.org/jira/browse/CB-10619) Appium tests: Properly switch to webview on **Android**
* [CB-10397](https://issues.apache.org/jira/browse/CB-10397) Added Appium tests
* [CB-10576](https://issues.apache.org/jira/browse/CB-10576) MobileSpec can't get results for **Windows**-Store 8.1 Builds
* chore: edit package.json license to match SPDX id
* [CB-10539](https://issues.apache.org/jira/browse/CB-10539) Commenting out the verySmallQvga maxResolution option on ** Windows **
* [CB-10541](https://issues.apache.org/jira/browse/CB-10541) Changing default maxResoltion to be highestAvailable for CameraCaptureUI on ** Windows **
* [CB-10113](https://issues.apache.org/jira/browse/CB-10113) ** Browser ** - Layer camera UI on top of all!
* [CB-10502](https://issues.apache.org/jira/browse/CB-10502) ** Browser ** - Fix camera plugin exception in Chrome when click capture.
* [CB-10539](https://issues.apache.org/jira/browse/CB-10539) Commenting out the verySmallQvga maxResolution option on **Windows**
* [CB-10541](https://issues.apache.org/jira/browse/CB-10541) Changing default maxResoltion to be highestAvailable for CameraCaptureUI on **Windows**
* [CB-10113](https://issues.apache.org/jira/browse/CB-10113) **Browse** - Layer camera UI on top of all!
* [CB-10502](https://issues.apache.org/jira/browse/CB-10502) **Browser** - Fix camera plugin exception in Chrome when click capture.
* Adding comments
* Camera tapping fix on ** Windows **
* Camera tapping fix on **Windows**
### 2.1.0 (Jan 15, 2016)
* added `.ratignore`

View File

@@ -1,4 +1,5 @@
/*jshint node: true, jasmine: true */
/* global navigator, Q */
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -27,18 +28,18 @@
'use strict';
var wdHelper = require('../helpers/wdHelper');
var wdHelper = global.WD_HELPER;
var screenshotHelper = global.SCREENSHOT_HELPER;
var wd = wdHelper.getWD();
var cameraConstants = require('../../www/CameraConstants');
var cameraHelper = require('../helpers/cameraHelper');
var screenshotHelper = require('../helpers/screenshotHelper');
var STARTING_MESSAGE = 'Ready for action!';
var RETRY_COUNT = 3; // how many times to retry taking a picture before failing
var MINUTE = 60 * 1000;
var BACK_BUTTON = 4;
var DEFAULT_SCREEN_WIDTH = 360;
var DEFAULT_SCREEN_HEIGHT = 567;
var DEFAULT_WEBVIEW_CONTEXT = 'WEBVIEW';
var PROMISE_PREFIX = 'appium_camera_promise_';
describe('Camera tests Android.', function () {
var driver;
@@ -46,33 +47,29 @@ describe('Camera tests Android.', function () {
var webviewContext = DEFAULT_WEBVIEW_CONTEXT;
// this indicates that the device library has the test picture:
var isTestPictureSaved = false;
// this indicates that there was a critical error and we should try to recover:
var errorFlag = false;
// this indicates that we couldn't restore Appium session and should fail fast:
var stopFlag = false;
// we need to know the screen width and height to properly click on an image in the gallery:
var screenWidth = DEFAULT_SCREEN_WIDTH;
var screenHeight = DEFAULT_SCREEN_HEIGHT;
// promise count to use in promise ID
var promiseCount = 0;
function win() {
expect(true).toBe(true);
function getNextPromiseId() {
promiseCount += 1;
return getCurrentPromiseId();
}
function fail(error) {
screenshotHelper.saveScreenshot(driver);
if (error && error.message) {
console.log('An error occured: ' + error.message);
expect(true).toFailWithMessage(error.message);
throw error.message;
}
if (error) {
console.log('Failed expectation: ' + error);
expect(true).toFailWithMessage(error);
throw error;
}
// no message provided :(
expect(true).toBe(false);
throw 'An error without description occured';
function getCurrentPromiseId() {
return PROMISE_PREFIX + promiseCount;
}
function saveScreenshotAndFail(error) {
fail(error);
return screenshotHelper
.saveScreenshot(driver)
.quit()
.then(function () {
return getDriver();
});
}
// generates test specs by combining all the specified options
@@ -95,152 +92,110 @@ describe('Camera tests Android.', function () {
return cameraHelper.generateSpecs(sourceTypes, destinationTypes, encodingTypes, allowEditOptions);
}
function getPicture(options, skipUiInteractions, retry) {
// invokes Camera.getPicture() with the specified options
// and goes through all UI interactions unless 'skipUiInteractions' is true
function getPicture(options, skipUiInteractions) {
var promiseId = getNextPromiseId();
if (!options) {
options = {};
}
if (typeof retry === 'undefined') {
retry = 1;
}
var command = "navigator.camera.getPicture(function (result) { document.getElementById('info').innerHTML = result.slice(0, 100); }, " +
"function (err) { document.getElementById('info').innerHTML = 'ERROR: ' + err; }," + JSON.stringify(options) + ");";
return driver
.context(webviewContext)
.execute(command)
.sleep(7000)
.execute(cameraHelper.getPicture, [options, promiseId])
.context('NATIVE_APP')
.sleep(5000)
.then(function () {
if (skipUiInteractions) {
return;
}
// selecting a picture from gallery
if (options.hasOwnProperty('sourceType') &&
(options.sourceType === cameraConstants.PictureSourceType.PHOTOLIBRARY ||
options.sourceType === cameraConstants.PictureSourceType.SAVEDPHOTOALBUM)) {
var touchTile = new wd.TouchAction(),
swipeRight = new wd.TouchAction();
touchTile.press({x: Math.round(screenWidth / 4), y: Math.round(screenHeight / 5)}).release();
swipeRight.press({x: 10, y: Math.round(screenHeight * 0.8)})
var tapTile = new wd.TouchAction();
var swipeRight = new wd.TouchAction();
tapTile.tap({x: Math.round(screenWidth / 4), y: Math.round(screenHeight / 5)});
swipeRight.press({x: 10, y: 100})
.wait(300)
.moveTo({x: Math.round(screenWidth / 2), y: Math.round(screenHeight / 2)})
.moveTo({x: Math.round(screenWidth / 2), y: 100})
.release();
return driver
.performTouchAction(swipeRight)
.sleep(3000)
.elementByXPath('//*[@text="Gallery"]')
.elementByXPath('//android.widget.TextView[@text="Gallery"]')
.fail(function () {
return driver
.performTouchAction(swipeRight)
.elementByXPath('//android.widget.TextView[@text="Gallery"]');
})
.then(function (element) {
return element.click().sleep(5000);
return element
.click()
// we need to sleep here to give a sidebar some time to close
// if we don't sleep here, sometimes we would click on a sidebar
// in the next step
.sleep(3000);
}, function () {
// if the gallery is already opened, we'd just go on:
// the gallery is already opened, just go on:
return driver;
})
.performTouchAction(touchTile);
.performTouchAction(tapTile);
}
// taking a picture from camera
return driver
.elementByXPath('//android.widget.ImageView[contains(@resource-id,\'shutter\')]')
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'shutter\')]', MINUTE)
.click()
.sleep(3000)
.elementByXPath('//android.widget.ImageView[contains(@resource-id,\'done\')]')
.click()
.sleep(10000);
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'done\')]', MINUTE)
.click();
})
.then(function () {
if (skipUiInteractions) {
return;
}
if (options.hasOwnProperty('allowEdit') && options.allowEdit === true) {
if (options.allowEdit) {
return driver
.elementByXPath('//*[contains(@resource-id,\'save\')]')
.waitForElementByXPath('//*[contains(@resource-id,\'save\')]', MINUTE)
.click();
}
})
.then(function () {
if (!skipUiInteractions) {
return driver.sleep(10000);
}
})
.fail(function (error) {
if (retry < RETRY_COUNT) {
console.log('Failed to get a picture. Let\'s try it again... ');
return getPicture(options, skipUiInteractions, ++retry);
} else {
console.log('Tried ' + RETRY_COUNT + ' times but couldn\'t get the picture. Failing...');
fail(error);
}
});
}
function enterTest() {
return driver
// trying to determine where we are
.context(webviewContext)
.fail(function (error) {
fail(error);
})
.elementById('info')
.then(function () {
return driver; //we're already on the test screen
}, function () {
return driver
.elementById('middle')
.then(function () {
return driver
// we're on autotests page, we should go to start page
.execute('window.location = "../index.html"')
.sleep(5000)
.fail(function () {
errorFlag = true;
throw 'Couldn\'t find start page.';
});
}, function () {
return; // no-op
})
// unknown starting page: no 'info' div
// adding it manually
.execute('var info = document.createElement("div"); ' +
'info.id = "info"; ' +
'document.body.appendChild(info);');
})
.sleep(5000);
.fail(fail);
}
// checks if the picture was successfully taken
// if shouldLoad is falsy, ensures that the error callback was called
function checkPicture(shouldLoad) {
return driver
.context(webviewContext)
.elementById('info')
.getAttribute('innerHTML')
.then(function (html) {
if (html.indexOf(STARTING_MESSAGE) >= 0) {
expect(true).toFailWithMessage('No callback was fired');
} else if (shouldLoad) {
expect(html.length).toBeGreaterThan(0);
if (html.indexOf('ERROR') >= 0) {
fail(html);
.setAsyncScriptTimeout(MINUTE)
.executeAsync(cameraHelper.checkPicture, [getCurrentPromiseId()])
.then(function (result) {
if (shouldLoad) {
expect(result.length).toBeGreaterThan(0);
if (result.indexOf('ERROR') >= 0) {
return fail(result);
}
} else {
if (html.indexOf('ERROR') === -1) {
fail('Unexpected success callback with result: ' + html);
if (result.indexOf('ERROR') === -1) {
return fail('Unexpected success callback with result: ' + result);
}
expect(html.indexOf('ERROR')).toBe(0);
expect(result.indexOf('ERROR')).toBe(0);
}
});
}
function runCombinedSpec(spec) {
return enterTest()
return driver
.then(function () {
return getPicture(spec.options);
})
.then(function () {
return checkPicture(true);
})
.then(win, fail);
.fail(saveScreenshotAndFail);
}
// deletes the latest image from the gallery
function deleteImage() {
var holdTile = new wd.TouchAction();
holdTile.press({x: Math.round(screenWidth / 3), y: Math.round(screenHeight / 5)}).wait(1000).release();
holdTile.press({x: Math.round(screenWidth / 4), y: Math.round(screenHeight / 5)}).wait(1000).release();
return driver
.performTouchAction(holdTile)
.elementByXPath('//android.widget.TextView[@text="Delete"]')
@@ -251,141 +206,48 @@ describe('Camera tests Android.', function () {
.click();
}, function () {
// couldn't find Delete menu item. Possibly there is no image.
return;
return driver;
});
}
function getDriver() {
driver = wdHelper.getDriver('Android');
return driver;
return wdHelper.getWebviewContext(driver)
.then(function(context) {
webviewContext = context;
return driver.context(webviewContext);
})
.then(function () {
return wdHelper.waitForDeviceReady(driver);
})
.then(function () {
return wdHelper.injectLibraries(driver);
});
}
function checkStopFlag() {
if (stopFlag) {
fail('Something went wrong: the stopFlag is on. Please see the log for more details.');
}
return stopFlag;
}
beforeEach(function () {
jasmine.addMatchers({
toFailWithMessage : function () {
return {
compare: function (actual, msg) {
console.log('Failing with message: ' + msg);
var result = {
pass: false,
message: msg
};
// status 6 means that we've lost the session
// status 7 means that Appium couldn't find an element
// both these statuses mean that the test has failed but
// we should try to recreate the session for the following tests
if (msg.indexOf('Error response status: 6') >= 0 ||
msg.indexOf('Error response status: 7') >= 0) {
errorFlag = true;
}
return result;
}
};
}
});
});
it('camera.ui.util configuring driver and starting a session', function (done) {
stopFlag = true; // just in case of timeout
getDriver().then(function () {
stopFlag = false;
}, function (error) {
fail(error);
})
.finally(done);
getDriver()
.fail(fail)
.done(done);
}, 5 * MINUTE);
it('camera.ui.util determine webview context name', function (done) {
var i = 0;
return driver
.contexts(function (err, contexts) {
if (err) {
console.log(err);
}
for (i = 0; i < contexts.length; i++) {
if (contexts[i].indexOf('mobilespec') >= 0) {
webviewContext = contexts[i];
}
}
done();
});
}, MINUTE);
it('camera.ui.util determine screen dimensions', function (done) {
return enterTest()
.execute('document.getElementById(\'info\').innerHTML = window.innerWidth;')
.sleep(5000)
.elementById('info')
.getAttribute('innerHTML')
.then(function (html) {
if (html !== STARTING_MESSAGE) {
screenWidth = Number(html);
}
return driver
.context(webviewContext)
.execute(function () {
return {
'width': window.innerWidth,
'height': window.innerHeight
};
}, [])
.then(function (size) {
screenWidth = Number(size.width);
screenHeight = Number(size.height);
})
.execute('document.getElementById(\'info\').innerHTML = \'' + STARTING_MESSAGE + '\';')
.execute('document.getElementById(\'info\').innerHTML = window.innerHeight;')
.sleep(5000)
.elementById('info')
.getAttribute('innerHTML')
.then(function (html) {
if (html !== STARTING_MESSAGE) {
screenHeight = Number(html);
}
done();
});
.done(done);
}, MINUTE);
describe('Specs.', function () {
beforeEach(function (done) {
// prepare the app for the test
if (!stopFlag) {
return driver
.context(webviewContext)
.then(function () {
return driver; // no-op
}, function (error) {
expect(true).toFailWithMessage(error);
})
.execute('document.getElementById("info").innerHTML = "' + STARTING_MESSAGE + '";')
.finally(done);
}
done();
}, 3 * MINUTE);
afterEach(function (done) {
if (!errorFlag || stopFlag) {
// either there's no error or we've failed irrecoverably
// nothing to worry about!
done();
return;
}
// recreate the session if there was a critical error in a previous spec
stopFlag = true; // we're going to set this to false if we're able to restore the session
return driver
.quit()
.then(function () {
return getDriver()
.then(function () {
errorFlag = false;
stopFlag = false;
}, function (error) {
fail(error);
stopFlag = true;
});
}, function (error) {
fail(error);
stopFlag = true;
})
.finally(done);
}, 3 * MINUTE);
// getPicture() with saveToPhotoLibrary = true
it('camera.ui.spec.1 Saving the picture to photo library', function (done) {
var options = {
@@ -394,8 +256,7 @@ describe('Camera tests Android.', function () {
sourceType: cameraConstants.PictureSourceType.CAMERA,
saveToPhotoAlbum: true
};
enterTest()
.context(webviewContext)
driver
.then(function () {
return getPicture(options);
})
@@ -403,40 +264,26 @@ describe('Camera tests Android.', function () {
isTestPictureSaved = true;
return checkPicture(true);
})
.then(win, fail)
.finally(done);
.fail(saveScreenshotAndFail)
.done(done);
}, 3 * MINUTE);
// getPicture() with mediaType: VIDEO, sourceType: PHOTOLIBRARY
it('camera.ui.spec.2 Selecting only videos', function (done) {
if (checkStopFlag()) {
done();
return;
}
var options = { sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
mediaType: cameraConstants.MediaType.VIDEO };
enterTest()
driver
.then(function () {
return getPicture(options, true);
})
.sleep(5000)
.context(webviewContext)
.elementById('info')
.getAttribute('innerHTML')
.then(function (html) {
if (html.indexOf('ERROR') >= 0) {
throw html;
}
})
.context('NATIVE_APP')
.sleep(5000)
.then(function () {
// try to find "Gallery" menu item
// if there's none, the gallery should be already opened
return driver
.elementByXPath('//*[@text="Gallery"]')
.elementByXPath('//android.widget.TextView[@text="Gallery"]')
.then(function (element) {
return element.click().sleep(2000);
return element.click();
}, function () {
return driver;
});
@@ -450,143 +297,115 @@ describe('Camera tests Android.', function () {
throw 'Couldn\'t find "Choose video" element.';
});
})
.then(win, fail)
.deviceKeyEvent(4)
.sleep(2000)
.deviceKeyEvent(4)
.sleep(2000)
.elementById('action_bar_title')
.then(function () {
// success means we're still in native app
.deviceKeyEvent(BACK_BUTTON)
.elementByXPath('//android.widget.TextView[@text="Gallery"]')
.deviceKeyEvent(BACK_BUTTON)
.finally(function () {
return driver
.deviceKeyEvent(4)
.sleep(2000);
}, function () {
// error means we're already in webview
return driver;
.elementById('action_bar_title')
.then(function () {
// success means we're still in native app
return driver
.deviceKeyEvent(BACK_BUTTON);
}, function () {
// error means we're already in webview
return driver;
});
})
.finally(done);
.fail(saveScreenshotAndFail)
.done(done);
}, 3 * MINUTE);
// getPicture(), then dismiss
// wait for the error callback to bee called
// wait for the error callback to be called
it('camera.ui.spec.3 Dismissing the camera', function (done) {
if (checkStopFlag()) {
done();
return;
}
var options = { quality: 50,
allowEdit: true,
sourceType: cameraConstants.PictureSourceType.CAMERA,
destinationType: cameraConstants.DestinationType.FILE_URI };
enterTest()
.context(webviewContext)
driver
.then(function () {
return getPicture(options, true);
})
.sleep(5000)
.context("NATIVE_APP")
.elementByXPath('//android.widget.ImageView[contains(@resource-id,\'cancel\')]')
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'cancel\')]', MINUTE / 2)
.click()
.context(webviewContext)
.then(function () {
return driver
.elementByXPath('//*[contains(text(),"Camera cancelled")]')
.then(function () {
return checkPicture(false);
}, function () {
throw 'Couldn\'t find "Camera cancelled" message.';
});
return checkPicture(false);
})
.then(win, fail)
.finally(done);
.fail(saveScreenshotAndFail)
.done(done);
}, 3 * MINUTE);
// getPicture(), then take picture but dismiss the edit
// wait for the error cllback to be called
// wait for the error callback to be called
it('camera.ui.spec.4 Dismissing the edit', function (done) {
if (checkStopFlag()) {
done();
return;
}
var options = { quality: 50,
allowEdit: true,
sourceType: cameraConstants.PictureSourceType.CAMERA,
destinationType: cameraConstants.DestinationType.FILE_URI };
enterTest()
.context(webviewContext)
driver
.then(function () {
return getPicture(options, true);
})
.sleep(5000)
.context('NATIVE_APP')
.elementByXPath('//android.widget.ImageView[contains(@resource-id,\'shutter\')]')
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'shutter\')]', MINUTE / 2)
.click()
.elementByXPath('//android.widget.ImageView[contains(@resource-id,\'done\')]')
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'done\')]', MINUTE / 2)
.click()
.elementByXPath('//*[contains(@resource-id,\'discard\')]')
.waitForElementByXPath('//*[contains(@resource-id,\'discard\')]', MINUTE / 2)
.click()
.sleep(5000)
.context(webviewContext)
.then(function () {
return driver
.elementByXPath('//*[contains(text(),"Camera cancelled")]')
.then(function () {
return checkPicture(false);
}, function () {
throw 'Couldn\'t find "Camera cancelled" message.';
});
return checkPicture(false);
})
.then(win, fail)
.finally(done);
.fail(saveScreenshotAndFail)
.done(done);
}, 3 * MINUTE);
// combine various options for getPicture()
generateSpecs().forEach(function (spec) {
it('camera.ui.spec.5.' + spec.id + ' Combining options', function (done) {
if (checkStopFlag()) {
done();
return;
}
runCombinedSpec(spec).then(done);
runCombinedSpec(spec)
.done(done);
}, 3 * MINUTE);
});
it('camera.ui.util Delete test image from device library', function (done) {
if (checkStopFlag()) {
if (!isTestPictureSaved) {
// couldn't save test picture earlier, so nothing to delete here
done();
return;
}
if (isTestPictureSaved) {
// delete exactly one last picture
// this should be the picture we've taken in the first spec
return driver
.context('NATIVE_APP')
.deviceKeyEvent(3)
.sleep(5000)
.elementByName('Apps')
.click()
.elementByXPath('//android.widget.TextView[@text="Gallery"]')
.click()
.elementByXPath('//android.widget.TextView[contains(@text,"Pictures")]')
.then(function (element) {
return element
.click()
.sleep(3000)
.then(deleteImage)
.then(function () { done(); }, function () { done(); });
}, function () {
done();
});
}
// couldn't save test picture earlier, so nothing to delete here
done();
// delete exactly one latest picture
// this should be the picture we've taken in the first spec
return driver
.context('NATIVE_APP')
.deviceKeyEvent(BACK_BUTTON)
.sleep(1000)
.deviceKeyEvent(BACK_BUTTON)
.sleep(1000)
.deviceKeyEvent(BACK_BUTTON)
.elementById('Apps')
.click()
.elementByXPath('//android.widget.TextView[@text="Gallery"]')
.click()
.elementByXPath('//android.widget.TextView[contains(@text,"Pictures")]')
.click()
.then(deleteImage)
.deviceKeyEvent(BACK_BUTTON)
.sleep(1000)
.deviceKeyEvent(BACK_BUTTON)
.sleep(1000)
.deviceKeyEvent(BACK_BUTTON)
.fail(fail)
.finally(done);
}, 3 * MINUTE);
});
it('camera.ui.util Destroy the session', function (done) {
return driver.quit(done);
}, 10000);
driver
.quit()
.done(done);
}, MINUTE);
});

View File

@@ -1,4 +1,5 @@
/*jshint node: true */
/* global Q */
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -84,3 +85,21 @@ module.exports.generateSpecs = function (sourceTypes, destinationTypes, encoding
}
return specs;
};
module.exports.getPicture = function (opts, pid) {
navigator._appiumPromises[pid] = Q.defer();
navigator.camera.getPicture(function (result) {
navigator._appiumPromises[pid].resolve(result);
}, function (err) {
navigator._appiumPromises[pid].reject(err);
}, opts);
};
module.exports.checkPicture = function (pid, cb) {
navigator._appiumPromises[pid].promise
.then(function (result) {
cb(result);
}, function (err) {
cb('ERROR: ' + err);
});
};

View File

@@ -1,58 +0,0 @@
/* jshint node: true */
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
'use strict';
var path = require('path');
var screenshotPath = global.SCREENSHOT_PATH || path.join(__dirname, '../../appium_screenshots/');
function generateScreenshotName() {
var date = new Date();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
month = (month < 10 ? "0" : "") + month;
day = (day < 10 ? "0" : "") + day;
hour = (hour < 10 ? "0" : "") + hour;
min = (min < 10 ? "0" : "") + min;
sec = (sec < 10 ? "0" : "") + sec;
return date.getFullYear() + '-' + month + '-' + day + '_' + hour + '.' + min + '.' + sec + '.png';
}
module.exports.saveScreenshot = function (driver) {
var oldContext;
return driver
.currentContext()
.then(function (cc) {
oldContext = cc;
})
.context('NATIVE_APP')
.saveScreenshot(screenshotPath + generateScreenshotName())
.then(function () {
return driver.context(oldContext);
});
};

View File

@@ -1,68 +0,0 @@
/* jshint node: true */
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
'use strict';
var wd = global.WD || require('wd');
var driver;
module.exports.getDriver = function (platform, callback) {
var serverConfig = {
host: 'localhost',
port: 4723
},
driverConfig = {
browserName: '',
'appium-version': '1.5',
platformName: platform,
platformVersion: global.PLATFORM_VERSION || '',
deviceName: global.DEVICE_NAME || '',
app: global.PACKAGE_PATH,
autoAcceptAlerts: true,
};
if (process.env.CHROMEDRIVER_EXECUTABLE) {
driverConfig.chromedriverExecutable = process.env.CHROMEDRIVER_EXECUTABLE;
}
driver = wd.promiseChainRemote(serverConfig);
module.exports.configureLogging(driver);
return driver.init(driverConfig).setImplicitWaitTimeout(10000)
.sleep(20000) // wait for the app to load
.then(callback);
};
module.exports.getWD = function () {
return wd;
};
module.exports.configureLogging = function (driver) {
driver.on('status', function (info) {
console.log(info);
});
driver.on('command', function (meth, path, data) {
console.log(' > ' + meth, path, data || '');
});
driver.on('http', function (meth, path, data) {
console.log(' > ' + meth, path, data || '');
});
};

View File

@@ -1,4 +1,5 @@
/*jshint node: true, jasmine: true */
/* global navigator, Q */
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -27,41 +28,40 @@
'use strict';
var wdHelper = require('../helpers/wdHelper');
var wdHelper = global.WD_HELPER;
var screenshotHelper = global.SCREENSHOT_HELPER;
var wd = wdHelper.getWD();
var isDevice = global.DEVICE;
var cameraConstants = require('../../www/CameraConstants');
var cameraHelper = require('../helpers/cameraHelper');
var screenshotHelper = require('../helpers/screenshotHelper');
var MINUTE = 60 * 1000;
var DEFAULT_WEBVIEW_CONTEXT = 'WEBVIEW_1';
var PROMISE_PREFIX = 'appium_camera_promise_';
describe('Camera tests iOS.', function () {
var driver;
var webviewContext = DEFAULT_WEBVIEW_CONTEXT;
var startingMessage = 'Ready for action!';
// promise count to use in promise ID
var promiseCount = 0;
function win() {
expect(true).toBe(true);
function getNextPromiseId() {
promiseCount += 1;
return getCurrentPromiseId();
}
function fail(error) {
screenshotHelper.saveScreenshot(driver);
if (error && error.message) {
console.log('An error occured: ' + error.message);
expect(true).toFailWithMessage(error.message);
throw error.message;
}
if (error) {
console.log('Failed expectation: ' + error);
expect(true).toFailWithMessage(error);
throw error;
}
// no message provided :(
expect(true).toBe(false);
throw 'An error without description occured';
function getCurrentPromiseId() {
return PROMISE_PREFIX + promiseCount;
}
function saveScreenshotAndFail(error) {
fail(error);
return screenshotHelper
.saveScreenshot(driver)
.quit()
.then(function () {
return getDriver();
});
}
// generates test specs by combining all the specified options
@@ -84,17 +84,34 @@ describe('Camera tests iOS.', function () {
return cameraHelper.generateSpecs(sourceTypes, destinationTypes, encodingTypes, allowEditOptions);
}
function usePicture() {
return driver
.elementByXPath('//*[@label="Use"]')
.click()
.fail(function () {
return driver
// For some reason "Choose" element is not clickable by standard Appium methods
// So getting its position and tapping there using TouchAction
.elementByXPath('//UIAButton[@label="Choose"]')
.getLocation()
.then(function (loc) {
var tapChoose = new wd.TouchAction();
tapChoose.tap(loc);
return driver
.performTouchAction(tapChoose);
});
});
}
function getPicture(options, cancelCamera, skipUiInteractions) {
var promiseId = getNextPromiseId();
if (!options) {
options = {};
}
var command = "navigator.camera.getPicture(function (result) { document.getElementById('info').innerHTML = 'Success: ' + result.slice(0, 100); }, " +
"function (err) { document.getElementById('info').innerHTML = 'ERROR: ' + err; }," + JSON.stringify(options) + ");";
return driver
.sleep(2000)
.context(webviewContext)
.execute(command)
.sleep(5000)
.execute(cameraHelper.getPicture, [options, promiseId])
.context('NATIVE_APP')
.then(function () {
if (skipUiInteractions) {
@@ -102,187 +119,146 @@ describe('Camera tests iOS.', function () {
}
if (options.hasOwnProperty('sourceType') && options.sourceType === cameraConstants.PictureSourceType.PHOTOLIBRARY) {
return driver
.elementByName('Camera Roll')
.waitForElementByXPath('//*[@label="Camera Roll"]', MINUTE / 2)
.click()
.elementByXPath('//UIACollectionCell')
.click()
.then(function () {
if (options.hasOwnProperty('allowEdit') && options.allowEdit === true) {
return driver
.elementByName('Use')
.click();
if (!options.allowEdit) {
return driver;
}
return driver;
return usePicture();
});
}
if (options.hasOwnProperty('sourceType') && options.sourceType === cameraConstants.PictureSourceType.SAVEDPHOTOALBUM) {
return driver
.elementByXPath('//UIACollectionCell')
.waitForElementByXPath('//UIACollectionCell', MINUTE / 2)
.click()
.then(function () {
if (options.hasOwnProperty('allowEdit') && options.allowEdit === true) {
return driver
.elementByName('Use')
.click();
if (!options.allowEdit) {
return driver;
}
return driver;
return usePicture();
});
}
if (cancelCamera) {
return driver
.elementByName('Cancel')
.waitForElementByXPath('//*[@label="Cancel"]', MINUTE / 2)
.click();
}
return driver
.elementByName('PhotoCapture')
.waitForElementByXPath('//*[@label="Take Picture"]', MINUTE / 2)
.click()
.elementByName('Use Photo')
.elementByXPath('//*[@label="Use Photo"]')
.click();
})
.sleep(3000);
}
function enterTest() {
return driver
.contexts(function (err, contexts) {
if (err) {
fail(err);
} else {
// if WEBVIEW context is available, use it
// if not, use NATIVE_APP
webviewContext = contexts[contexts.length - 1];
}
})
.then(function () {
return driver
.context(webviewContext);
})
.fail(fail)
.elementById('info')
.fail(function () {
// unknown starting page: no 'info' div
// adding it manually
return driver
.execute('var info = document.createElement("div"); ' +
'info.id = "info"' +
'document.body.appendChild(info);')
.fail(fail);
})
.execute('document.getElementById("info").innerHTML = "' + startingMessage + '";')
.fail(fail);
}
// checks if the picture was successfully taken
// if shouldLoad is falsy, ensures that the error callback was called
function checkPicture(shouldLoad) {
return driver
.contexts(function (err, contexts) {
// if WEBVIEW context is available, use it
// if not, use NATIVE_APP
webviewContext = contexts[contexts.length - 1];
})
.context(webviewContext)
.elementById('info')
.getAttribute('innerHTML')
.then(function (html) {
if (html.indexOf(startingMessage) >= 0) {
expect(true).toFailWithMessage('No callback was fired');
} else if (shouldLoad) {
expect(html.length).toBeGreaterThan(0);
if (html.indexOf('ERROR') >= 0) {
expect(true).toFailWithMessage(html);
.setAsyncScriptTimeout(MINUTE)
.executeAsync(cameraHelper.checkPicture, [getCurrentPromiseId()])
.then(function (result) {
if (shouldLoad) {
expect(result.length).toBeGreaterThan(0);
if (result.indexOf('ERROR') >= 0) {
return fail(result);
}
} else {
if (html.indexOf('ERROR') === -1) {
expect(true).toFailWithMessage('Unexpected success callback with result: ' + html);
if (result.indexOf('ERROR') === -1) {
return fail('Unexpected success callback with result: ' + result);
}
expect(html.indexOf('ERROR')).toBe(0);
expect(result.indexOf('ERROR')).toBe(0);
}
})
.context('NATIVE_APP');
});
}
function runCombinedSpec(spec) {
return enterTest()
return driver
.then(function () {
return getPicture(spec.options);
})
.then(function () {
return checkPicture(true);
})
.then(win, fail);
.fail(saveScreenshotAndFail);
}
beforeEach(function () {
jasmine.addMatchers({
toFailWithMessage : function () {
return {
compare: function (actual, msg) {
console.log('Failing with message: ' + msg);
var result = {
pass: false,
message: msg
};
return result;
}
};
}
});
});
function getDriver() {
driver = wdHelper.getDriver('iOS');
return wdHelper.getWebviewContext(driver)
.then(function(context) {
webviewContext = context;
return driver.context(webviewContext);
})
.then(function () {
return wdHelper.waitForDeviceReady(driver);
})
.then(function () {
return wdHelper.injectLibraries(driver);
});
}
it('camera.ui.util Configuring driver and starting a session', function (done) {
driver = wdHelper.getDriver('iOS', done);
}, 3 * MINUTE);
it('camera.ui.util configure driver and start a session', function (done) {
getDriver()
.fail(fail)
.finally(done);
}, 5 * MINUTE);
describe('Specs.', function () {
// getPicture() with mediaType: VIDEO, sourceType: PHOTOLIBRARY
it('camera.ui.spec.1 Selecting only videos', function (done) {
var options = { sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
mediaType: cameraConstants.MediaType.VIDEO };
enterTest()
.then(function () { return getPicture(options, false, true); }) // skip ui unteractions
.sleep(5000)
.elementByName('Videos')
.then(win, fail)
.elementByName('Cancel')
driver
// skip ui unteractions
.then(function () { return getPicture(options, false, true); })
.waitForElementByXPath('//*[contains(@label,"Videos")]', MINUTE / 2)
.elementByXPath('//*[@label="Cancel"]')
.click()
.finally(done);
.fail(saveScreenshotAndFail)
.done(done);
}, 3 * MINUTE);
// getPicture(), then dismiss
// wait for the error callback to bee called
// wait for the error callback to be called
it('camera.ui.spec.2 Dismissing the camera', function (done) {
// camera is not available on iOS simulator
// camera is not available on the iOS simulator
if (!isDevice) {
pending();
}
var options = { sourceType: cameraConstants.PictureSourceType.CAMERA };
enterTest()
driver
.then(function () {
return getPicture(options, true);
})
.then(function () {
return checkPicture(false);
})
.elementByXPath('//UIAStaticText[contains(@label,"no image selected")]')
.then(function () {
return checkPicture(false);
}, fail)
.finally(done);
.fail(saveScreenshotAndFail)
.done(done);
}, 3 * MINUTE);
// combine various options for getPicture()
generateSpecs().forEach(function (spec) {
it('camera.ui.spec.3.' + spec.id + ' Combining options', function (done) {
// camera is not available on iOS simulator
if (!isDevice) {
if (!isDevice && spec.options.sourceType === cameraConstants.PictureSourceType.CAMERA) {
pending();
}
runCombinedSpec(spec).then(done);
runCombinedSpec(spec).done(done);
}, 3 * MINUTE);
});
});
it('camera.ui.util.4 Destroy the session', function (done) {
driver.quit(done);
}, 10000);
driver
.quit()
.done(done);
}, MINUTE);
});

View File

@@ -34,21 +34,6 @@ the system's image library.
#### Example <a name="camera-getPicture-examples"></a>
Take a photo and retrieve it as a Base64-encoded image:
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
Take a photo and retrieve the image's file location:
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
@@ -63,6 +48,27 @@ Take a photo and retrieve the image's file location:
alert('Failed because: ' + message);
}
Take a photo and retrieve it as a Base64-encoded image:
/**
* Warning: Using DATA_URL is not recommended! The DATA_URL destination
* type is very memory intensive, even with a low quality setting. Using it
* can result in out of memory errors and application crashes. Use FILE_URI
* or NATIVE_URI instead.
*/
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
#### Preferences (iOS)
- __CameraUsesGeolocation__ (boolean, defaults to false). For capturing JPEGs, set to true to get geolocation data in the EXIF header. This will trigger a request for geolocation permissions if set to true.

View File

@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-camera",
"version": "2.1.1",
"version": "2.2.0",
"description": "Cordova Camera Plugin",
"cordova": {
"id": "cordova-plugin-camera",

View File

@@ -22,7 +22,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="cordova-plugin-camera"
version="2.1.1">
version="2.2.0">
<name>Camera</name>
<description>Cordova Camera Plugin</description>
<license>Apache 2.0</license>
@@ -30,6 +30,8 @@
<repo>https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git</repo>
<issue>https://issues.apache.org/jira/browse/CB/component/12320645</issue>
<dependency id="cordova-plugin-compat" version="^1.0.0" />
<js-module src="www/CameraConstants.js" name="Camera">
<clobbers target="Camera" />
</js-module>
@@ -71,7 +73,6 @@
<source-file src="src/android/CameraLauncher.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/ExifHelper.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/PermissionHelper.java" target-dir="src/org/apache/cordova/camera" />
<js-module src="www/CameraPopoverHandle.js" name="CameraPopoverHandle">
<clobbers target="CameraPopoverHandle" />

View File

@@ -34,6 +34,7 @@ import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaResourceApi;
import org.apache.cordova.LOG;
import org.apache.cordova.PermissionHelper;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
@@ -58,6 +59,9 @@ import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionInfo;
/**
* This class launches the camera view, allows the user to take a picture, closes the camera view,
* and returns the captured image. When the camera view is closed, the screen displayed before
@@ -105,7 +109,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private boolean orientationCorrected; // Has the picture's orientation been corrected
private boolean allowEdit; // Should we allow the user to crop the image.
protected final static String[] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE };
protected final static String[] permissions = { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE };
public CallbackContext callbackContext;
private int numPics;
@@ -114,6 +118,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private Uri scanMe; // Uri of image to be added to content store
private Uri croppedUri;
/**
* Executes the request and returns PluginResult.
*
@@ -229,14 +234,43 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
* @param returnType Set the type of image to return.
*/
public void callTakePicture(int returnType, int encodingType) {
if (PermissionHelper.hasPermission(this, permissions[0])) {
boolean saveAlbumPermission = PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
boolean takePicturePermission = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA);
// CB-10120: The CAMERA permission does not need to be requested unless it is declared
// in AndroidManifest.xml. This plugin does not declare it, but others may and so we must
// check the package info to determine if the permission is present.
if (!takePicturePermission) {
takePicturePermission = true;
try {
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
if (permissionsInPackage != null) {
for (String permission : permissionsInPackage) {
if (permission.equals(Manifest.permission.CAMERA)) {
takePicturePermission = false;
break;
}
}
}
} catch (NameNotFoundException e) {
// We are requesting the info for our package, so this should
// never be caught
}
}
if (takePicturePermission && saveAlbumPermission) {
takePicture(returnType, encodingType);
} else {
} else if (saveAlbumPermission && !takePicturePermission) {
PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.CAMERA);
} else if (!saveAlbumPermission && takePicturePermission) {
PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.READ_EXTERNAL_STORAGE);
} else {
PermissionHelper.requestPermissions(this, TAKE_PIC_SEC, permissions);
}
}
public void takePicture(int returnType, int encodingType)
{
// Save the number of images currently on disk for later

View File

@@ -55,13 +55,9 @@ public class FileHelper {
if (Build.VERSION.SDK_INT < 11)
realPath = FileHelper.getRealPathFromURI_BelowAPI11(cordova.getActivity(), uri);
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
realPath = FileHelper.getRealPathFromURI_API11to18(cordova.getActivity(), uri);
// SDK > 19 (Android 4.4)
// SDK >= 11
else
realPath = FileHelper.getRealPathFromURI_API19(cordova.getActivity(), uri);
realPath = FileHelper.getRealPathFromURI_API11_And_Above(cordova.getActivity(), uri);
return realPath;
}
@@ -79,10 +75,11 @@ public class FileHelper {
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(final Context context, final Uri uri) {
public static String getRealPathFromURI_API11_And_Above(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if ( DocumentsContract.isDocumentUri(context, uri)) {
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
@@ -145,26 +142,6 @@ public class FileHelper {
return null;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
try {
CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
} catch (Exception e) {
result = null;
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
@@ -296,6 +273,8 @@ public class FileHelper {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch (Exception e) {
return null;
} finally {
if (cursor != null)
cursor.close();

View File

@@ -1,138 +0,0 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 org.apache.cordova.camera;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import android.content.pm.PackageManager;
/**
* This class provides reflective methods for permission requesting and checking so that plugins
* written for cordova-android 5.0.0+ can still compile with earlier cordova-android versions.
*/
public class PermissionHelper {
private static final String LOG_TAG = "CordovaPermissionHelper";
/**
* Requests a "dangerous" permission for the application at runtime. This is a helper method
* alternative to cordovaInterface.requestPermission() that does not require the project to be
* built with cordova-android 5.0.0+
*
* @param plugin The plugin the permission is being requested for
* @param requestCode A requestCode to be passed to the plugin's onRequestPermissionResult()
* along with the result of the permission request
* @param permission The permission to be requested
*/
public static void requestPermission(CordovaPlugin plugin, int requestCode, String permission) {
PermissionHelper.requestPermissions(plugin, requestCode, new String[] {permission});
}
/**
* Requests "dangerous" permissions for the application at runtime. This is a helper method
* alternative to cordovaInterface.requestPermissions() that does not require the project to be
* built with cordova-android 5.0.0+
*
* @param plugin The plugin the permissions are being requested for
* @param requestCode A requestCode to be passed to the plugin's onRequestPermissionResult()
* along with the result of the permissions request
* @param permissions The permissions to be requested
*/
public static void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
try {
Method requestPermission = CordovaInterface.class.getDeclaredMethod(
"requestPermissions", CordovaPlugin.class, int.class, String[].class);
// If there is no exception, then this is cordova-android 5.0.0+
requestPermission.invoke(plugin.cordova, plugin, requestCode, permissions);
} catch (NoSuchMethodException noSuchMethodException) {
// cordova-android version is less than 5.0.0, so permission is implicitly granted
LOG.d(LOG_TAG, "No need to request permissions " + Arrays.toString(permissions));
// Notify the plugin that all were granted by using more reflection
deliverPermissionResult(plugin, requestCode, permissions);
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(LOG_TAG, "IllegalAccessException when requesting permissions " + Arrays.toString(permissions), illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method does not throw any exceptions, so this should never be caught
LOG.e(LOG_TAG, "invocationTargetException when requesting permissions " + Arrays.toString(permissions), invocationTargetException);
}
}
/**
* Checks at runtime to see if the application has been granted a permission. This is a helper
* method alternative to cordovaInterface.hasPermission() that does not require the project to
* be built with cordova-android 5.0.0+
*
* @param plugin The plugin the permission is being checked against
* @param permission The permission to be checked
*
* @return True if the permission has already been granted and false otherwise
*/
public static boolean hasPermission(CordovaPlugin plugin, String permission) {
try {
Method hasPermission = CordovaInterface.class.getDeclaredMethod("hasPermission", String.class);
// If there is no exception, then this is cordova-android 5.0.0+
return (Boolean) hasPermission.invoke(plugin.cordova, permission);
} catch (NoSuchMethodException noSuchMethodException) {
// cordova-android version is less than 5.0.0, so permission is implicitly granted
LOG.d(LOG_TAG, "No need to check for permission " + permission);
return true;
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(LOG_TAG, "IllegalAccessException when checking permission " + permission, illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method does not throw any exceptions, so this should never be caught
LOG.e(LOG_TAG, "invocationTargetException when checking permission " + permission, invocationTargetException);
}
return false;
}
private static void deliverPermissionResult(CordovaPlugin plugin, int requestCode, String[] permissions) {
// Generate the request results
int[] requestResults = new int[permissions.length];
Arrays.fill(requestResults, PackageManager.PERMISSION_GRANTED);
try {
Method onRequestPermissionResult = CordovaPlugin.class.getDeclaredMethod(
"onRequestPermissionResult", int.class, String[].class, int[].class);
onRequestPermissionResult.invoke(plugin, requestCode, permissions, requestResults);
} catch (NoSuchMethodException noSuchMethodException) {
// Should never be caught since the plugin must be written for cordova-android 5.0.0+ if it
// made it to this point
LOG.e(LOG_TAG, "NoSuchMethodException when delivering permissions results", noSuchMethodException);
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(LOG_TAG, "IllegalAccessException when delivering permissions results", illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method may throw a JSONException. We are just duplicating cordova-android's
// exception handling behavior here; all it does is log the exception in CordovaActivity,
// print the stacktrace, and ignore it
LOG.e(LOG_TAG, "InvocationTargetException when delivering permissions results", invocationTargetException);
}
}
}

View File

@@ -358,24 +358,24 @@ static NSString* toBase64(NSData* data) {
// use image unedited as requested , don't resize
data = UIImageJPEGRepresentation(image, 1.0);
} else {
if (options.usesGeolocation) {
NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
if (controllerMetadata) {
self.data = data;
self.metadata = [[NSMutableDictionary alloc] init];
NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
if (EXIFDictionary) {
[self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
}
if (IsAtLeastiOSVersion(@"8.0")) {
[[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
}
[[self locationManager] startUpdatingLocation];
data = UIImageJPEGRepresentation(image, [options.quality floatValue] / 100.0f);
}
if (options.usesGeolocation) {
NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
if (controllerMetadata) {
self.data = data;
self.metadata = [[NSMutableDictionary alloc] init];
NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
if (EXIFDictionary) {
[self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
}
} else {
data = UIImageJPEGRepresentation(image, [options.quality floatValue] / 100.0f);
if (IsAtLeastiOSVersion(@"8.0")) {
[[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
}
[[self locationManager] startUpdatingLocation];
}
}
}

View File

@@ -745,8 +745,9 @@ function takePictureFromCameraWindows(successCallback, errorCallback, args) {
cameraCaptureUI.photoSettings.maxResolution = maxRes;
var cameraPicture;
var savePhotoOnFocus = function() {
// define focus handler for windows phone 10.0
var savePhotoOnFocus = function () {
window.removeEventListener("focus", savePhotoOnFocus);
// call only when the app is in focus again
savePhoto(cameraPicture, {
@@ -758,16 +759,31 @@ function takePictureFromCameraWindows(successCallback, errorCallback, args) {
}, successCallback, errorCallback);
};
// add and delete focus eventHandler to capture the focus back from cameraUI to app
window.addEventListener("focus", savePhotoOnFocus);
cameraCaptureUI.captureFileAsync(WMCapture.CameraCaptureUIMode.photo).done(function(picture) {
// if windows phone 10, add and delete focus eventHandler to capture the focus back from cameraUI to app
if (navigator.appVersion.indexOf('Windows Phone 10.0') >= 0) {
window.addEventListener("focus", savePhotoOnFocus);
}
cameraCaptureUI.captureFileAsync(WMCapture.CameraCaptureUIMode.photo).done(function (picture) {
if (!picture) {
errorCallback("User didn't capture a photo.");
// Remove the focus handler if present
window.removeEventListener("focus", savePhotoOnFocus);
return;
}
cameraPicture = picture;
}, function() {
// If not windows 10, call savePhoto() now. If windows 10, wait for the app to be in focus again
if (navigator.appVersion.indexOf('Windows Phone 10.0') < 0) {
savePhoto(cameraPicture, {
destinationType: destinationType,
targetHeight: targetHeight,
targetWidth: targetWidth,
encodingType: encodingType,
saveToPhotoAlbum: saveToPhotoAlbum
}, successCallback, errorCallback);
}
}, function () {
errorCallback("Fail to capture a photo.");
window.removeEventListener("focus", savePhotoOnFocus);
});

View File

@@ -21,9 +21,6 @@
#import <XCTest/XCTest.h>
#import "CDVCamera.h"
#import "UIImage+CropScaleOrientation.h"
#import <Cordova/NSArray+Comparisons.h>
#import <Cordova/NSData+Base64.h>
#import <Cordova/NSDictionary+Extensions.h>
#import <MobileCoreServices/UTCoreTypes.h>
@@ -291,12 +288,14 @@
// test 640x480
targetSize = CGSizeMake(640, 480);
targetSize = CGSizeMake(480, 640);
targetImage = [sourceImagePortrait imageByScalingNotCroppingForSize:targetSize];
XCTAssertEqual(targetImage.size.width, targetSize.width);
XCTAssertEqual(targetImage.size.height, targetSize.height);
targetSize = CGSizeMake(640, 480);
targetImage = [sourceImageLandscape imageByScalingNotCroppingForSize:targetSize];
XCTAssertEqual(targetImage.size.width, targetSize.width);
XCTAssertEqual(targetImage.size.height, targetSize.height);
@@ -304,24 +303,28 @@
// test 800x600
targetSize = CGSizeMake(800, 600);
targetSize = CGSizeMake(600, 800);
targetImage = [sourceImagePortrait imageByScalingNotCroppingForSize:targetSize];
XCTAssertEqual(targetImage.size.width, targetSize.width);
XCTAssertEqual(targetImage.size.height, targetSize.height);
targetSize = CGSizeMake(800, 600);
targetImage = [sourceImageLandscape imageByScalingNotCroppingForSize:targetSize];
XCTAssertEqual(targetImage.size.width, targetSize.width);
XCTAssertEqual(targetImage.size.height, targetSize.height);
// test 1024x768
targetSize = CGSizeMake(1024, 768);
targetSize = CGSizeMake(768, 1024);
targetImage = [sourceImagePortrait imageByScalingNotCroppingForSize:targetSize];
XCTAssertEqual(targetImage.size.width, targetSize.width);
XCTAssertEqual(targetImage.size.height, targetSize.height);
targetSize = CGSizeMake(1024, 768);
targetImage = [sourceImageLandscape imageByScalingNotCroppingForSize:targetSize];
XCTAssertEqual(targetImage.size.width, targetSize.width);
XCTAssertEqual(targetImage.size.height, targetSize.height);

View File

@@ -5,9 +5,9 @@
"author": "Apache Software Foundation",
"license": "Apache Version 2.0",
"dependencies": {
"cordova-ios": "^3.7.0"
"cordova-ios": "*"
},
"scripts": {
"test": "xcodebuild -scheme CordovaLib && xcodebuild test -scheme CDVCameraLibTests -destination 'platform=iOS Simulator,name=iPhone 5'"
}
}
}
}

View File

@@ -22,7 +22,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="cordova-plugin-camera-tests"
version="2.1.1">
version="2.2.0">
<name>Cordova Camera Plugin Tests</name>
<license>Apache 2.0</license>

View File

@@ -125,7 +125,8 @@ exports.defineManualTests = function (contentEl, createActionButton) {
var img = document.getElementById('camera_image');
var startTime = new Date();
img.src = url;
img.onloadend = function () {
img.onload = function () {
log('Img size: ' + img.naturalWidth + 'x' + img.naturalHeight);
log('Image tag load time: ' + (new Date() - startTime));
if (callback) {
callback();

View File

@@ -27,7 +27,7 @@ module.exports = {
* @enum {number}
*/
DestinationType:{
/** Return base64 encoded string */
/** Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI or NATIVE_URI if possible */
DATA_URL: 0,
/** Return file uri (content://media/external/images/media/2 for Android) */
FILE_URI: 1,