Skip to content
This repository was archived by the owner on Sep 15, 2021. It is now read-only.

Commit be147c2

Browse files
matthieuxyzgortok
authored andcommitted
New wrapper for webIntent plugin (#1306)
* New wrapper for webIntent plugin * Added webIntent in the README
1 parent 43fff1a commit be147c2

File tree

5 files changed

+479
-0
lines changed

5 files changed

+479
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ $ bower install ngCordova
103103
- [Touchid](https://github.com/leecrossley/cordova-plugin-touchid)
104104
- [Vibration](https://github.com/apache/cordova-plugin-vibration) *
105105
- [Video Capture Plus](https://github.com/EddyVerbruggen/VideoCapturePlus-PhoneGap-Plugin) *
106+
- [Web Intent](https://github.com/Initsogar/cordova-webintent)
106107
- [Zip](https://github.com/MobileChromeApps/cordova-plugin-zip)
107108

108109
`* official Apache Cordova Plugin`

src/mocks/webIntent.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
ngCordovaMocks.factory('$webIntent', ['$q', '$rootScope', function($q, $rootScope) {
2+
var scope = $rootScope.$new();
3+
var has = true;
4+
var url = "http://url.mock/ressource";
5+
var throwsError = false;
6+
7+
return{
8+
has: has,
9+
url: url,
10+
throwsError: throwsError,
11+
12+
startActivity: function (params) {
13+
var q = $q.defer();
14+
if(this.throwsError) {
15+
q.reject(new Error());
16+
} else {
17+
q.resolve();
18+
}
19+
return q.promise;
20+
},
21+
hasExtra: function (params) {
22+
var q = $q.defer();
23+
if(this.throwsError) {
24+
q.reject(new Error());
25+
} else {
26+
q.resolve(has);
27+
}
28+
return q.promise;
29+
},
30+
getUri: function () {
31+
var q = $q.defer();
32+
if(this.throwsError) {
33+
q.reject(new Error());
34+
} else {
35+
q.resolve(url);
36+
}
37+
return q.promise;
38+
},
39+
getExtra: function (params) {
40+
var q = $q.defer();
41+
if(this.throwsError) {
42+
q.reject(new Error());
43+
} else {
44+
q.resolve(url);
45+
}
46+
return q.promise;
47+
},
48+
onNewIntent: function (callback) {
49+
scope.$on('newIntent', function() {
50+
callback(url);
51+
});
52+
},
53+
newIntent: function() {
54+
scope.$broadcast('newIntent')
55+
},
56+
sendBroadcast: function (params) {
57+
var q = $q.defer();
58+
if(this.throwsError) {
59+
q.reject(new Error());
60+
} else {
61+
q.resolve();
62+
}
63+
return q.promise;
64+
},
65+
actionSend: function() {return "actionSend"},
66+
actionView: function() {return "actionView"},
67+
actionCall: function() {return "actionCall"},
68+
actionSendTo: function() {return "actionSendTo"},
69+
extraText: function() {return "extraText"},
70+
extraSubject: function() {return "extraSubject"},
71+
extraStream: function() {return "extraStream"},
72+
extraEmail: function() {return "extraEmail"}
73+
}
74+
75+
}]);

src/plugins/webIntent.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// install : cordova plugin add https://github.com/Initsogar/cordova-webintent.git
2+
// link : https://github.com/Initsogar/cordova-webintent
3+
4+
angular.module('ngCordova.plugins.webIntent', [])
5+
6+
.factory('$webIntent', ['$q', '$window', function ($q, $window) {
7+
8+
return {
9+
startActivity: function (params) {
10+
var q = $q.defer();
11+
12+
if (!$window.cordova) {
13+
q.reject('Not supported without cordova.js');
14+
} else {
15+
$window.plugins.webintent.startActivity(params, function (result) {
16+
q.resolve(result);
17+
}, function (err) {
18+
q.reject(err);
19+
});
20+
}
21+
22+
return q.promise;
23+
},
24+
hasExtrafunction(params) {
25+
var q = $q.defer();
26+
27+
if (!$window.cordova) {
28+
q.reject('Not supported without cordova.js');
29+
} else {
30+
$window.plugins.webintent.hasExtra(params, function (result) {
31+
q.resolve(result);
32+
}, function (err) {
33+
q.reject(err);
34+
});
35+
}
36+
37+
return q.promise;
38+
},
39+
getUrifunction() {
40+
var q = $q.defer();
41+
42+
if (!$window.cordova) {
43+
q.reject('Not supported without cordova.js');
44+
} else {
45+
$window.plugins.webintent.getUri(function (result) {
46+
q.resolve(result);
47+
}, function (err) {
48+
q.reject(err);
49+
});
50+
}
51+
52+
return q.promise;
53+
},
54+
getExtrafunction(params) {
55+
var q = $q.defer();
56+
57+
if (!$window.cordova) {
58+
q.reject('Not supported without cordova.js');
59+
} else {
60+
$window.plugins.webintent.getExtra(params, function (result) {
61+
q.resolve(result);
62+
}, function (err) {
63+
q.reject(err);
64+
});
65+
}
66+
67+
return q.promise;
68+
},
69+
onNewIntentfunction(callback) {
70+
if ($window.plugins) {
71+
$window.plugins.webintent.onNewIntent(callback);
72+
}
73+
},
74+
sendBroadcastfunction(params) {
75+
var q = $q.defer();
76+
77+
if (!$window.cordova) {
78+
q.reject('Not supported without cordova.js');
79+
} else {
80+
$window.plugins.webintent.sendBroadcast(params, function (result) {
81+
q.resolve(result);
82+
}, function (err) {
83+
q.reject(err);
84+
});
85+
}
86+
87+
return q.promise;
88+
},
89+
90+
actionSend: function() {
91+
if ($window.plugins) {
92+
return $window.plugins.webintent.ACTION_SEND;
93+
}
94+
},
95+
actionView: function() {
96+
if ($window.plugins) {
97+
return $window.plugins.webintent.ACTION_VIEW;
98+
}
99+
},
100+
actionCall: function() {
101+
if ($window.plugins) {
102+
return $window.plugins.webintent.ACTION_CALL;
103+
}
104+
},
105+
actionSendTo: function() {
106+
if ($window.plugins) {
107+
return $window.plugins.webintent.ACTION_SENDTO;
108+
}
109+
},
110+
extraText: function() {
111+
if ($window.plugins) {
112+
return $window.plugins.webintent.EXTRA_TEXT;
113+
}
114+
},
115+
extraSubject: function() {
116+
if ($window.plugins) {
117+
return $window.plugins.webintent.EXTRA_SUBJECT;
118+
}
119+
},
120+
extraStream: function() {
121+
if ($window.plugins) {
122+
return $window.plugins.webintent.EXTRA_STREAM;
123+
}
124+
},
125+
extraEmail: function() {
126+
if ($window.plugins) {
127+
return $window.plugins.webintent.EXTRA_EMAIL;
128+
}
129+
}
130+
131+
132+
};
133+
}] );

test/mocks/webIntent.spec.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
describe('ngCordovaMocks', function() {
2+
3+
beforeEach(function() {
4+
module('ngCordovaMocks');
5+
});
6+
7+
describe('webIntent', function () {
8+
var $webIntent = null;
9+
var $rootScope = null;
10+
var spy = {};
11+
12+
//beforeEach(module('ngCordova.plugins.webIntent'));
13+
14+
beforeEach(inject(function (_$webIntent_, _$rootScope_) {
15+
$webIntent = _$webIntent_;
16+
$rootScope = _$rootScope_;
17+
18+
spy.success = jasmine.createSpy('success');
19+
spy.fail = function() {};//jasmine.createSpy('fail');
20+
21+
spyOn(spy, 'fail').and.callThrough();
22+
}));
23+
24+
it('should return actionSend on actionSend', function() {
25+
expect($webIntent.actionSend()).toBe("actionSend");
26+
});
27+
28+
it('should return actionView on actionView', function() {
29+
expect($webIntent.actionView()).toBe("actionView");
30+
});
31+
32+
it('should return extraText on extraText', function() {
33+
expect($webIntent.extraText()).toBe("extraText");
34+
});
35+
36+
it('should return extraSubject on extraSubject', function() {
37+
expect($webIntent.extraSubject()).toBe("extraSubject");
38+
});
39+
40+
it('should return extraStream on extraStream', function() {
41+
expect($webIntent.extraStream()).toBe("extraStream");
42+
});
43+
44+
it('should return extraEmail on extraEmail', function() {
45+
expect($webIntent.extraEmail()).toBe("extraEmail");
46+
});
47+
48+
it('should return actionCall on actionCall', function() {
49+
expect($webIntent.actionCall()).toBe("actionCall");
50+
});
51+
52+
it('should return actionSendTo on actionSendTo', function() {
53+
expect($webIntent.actionSendTo()).toBe("actionSendTo");
54+
});
55+
56+
describe('startActivity', function() {
57+
it('should call the success callback', function(done) {
58+
$webIntent.startActivity("myFile")
59+
.then(spy.success).catch(spy.fail).then(function() {
60+
expect(spy.success).toHaveBeenCalled();
61+
expect(spy.fail).not.toHaveBeenCalled();
62+
}).finally(done);
63+
64+
$rootScope.$apply();
65+
});
66+
67+
it('should call the fail callback', function(done) {
68+
$webIntent.throwsError = true;
69+
$webIntent.startActivity("myFile")
70+
.then(spy.success).catch(spy.fail).then(function() {
71+
expect(spy.fail).toHaveBeenCalled();
72+
expect(spy.success).not.toHaveBeenCalled();
73+
}).finally(done);
74+
$rootScope.$apply();
75+
});
76+
});
77+
78+
describe('hasExtra', function() {
79+
it('should call the success callback', function(done) {
80+
$webIntent.hasExtra("extraText")
81+
.then(spy.success).catch(spy.fail).then(function() {
82+
expect(spy.success).toHaveBeenCalledWith($webIntent.has);
83+
expect(spy.fail).not.toHaveBeenCalled();
84+
}).finally(done);
85+
86+
$rootScope.$apply();
87+
});
88+
89+
it('should call the fail callback', function(done) {
90+
$webIntent.throwsError = true;
91+
$webIntent.hasExtra("extraText")
92+
.then(spy.success).catch(spy.fail).then(function() {
93+
expect(spy.fail).toHaveBeenCalled();
94+
expect(spy.success).not.toHaveBeenCalled();
95+
}).finally(done);
96+
$rootScope.$apply();
97+
});
98+
});
99+
100+
describe('getExtra', function() {
101+
it('should call the success callback', function(done) {
102+
$webIntent.getExtra("extraText")
103+
.then(spy.success).catch(spy.fail).then(function() {
104+
expect(spy.success).toHaveBeenCalledWith($webIntent.url);
105+
expect(spy.fail).not.toHaveBeenCalled();
106+
}).finally(done);
107+
108+
$rootScope.$apply();
109+
});
110+
111+
it('should call the fail callback', function(done) {
112+
$webIntent.throwsError = true;
113+
$webIntent.getExtra("extraText")
114+
.then(spy.success).catch(spy.fail).then(function() {
115+
expect(spy.fail).toHaveBeenCalled();
116+
expect(spy.success).not.toHaveBeenCalled();
117+
}).finally(done);
118+
$rootScope.$apply();
119+
});
120+
});
121+
122+
describe('getUri', function() {
123+
it('should call the success callback', function(done) {
124+
$webIntent.getUri()
125+
.then(spy.success).catch(spy.fail).then(function() {
126+
expect(spy.success).toHaveBeenCalledWith($webIntent.url);
127+
expect(spy.fail).not.toHaveBeenCalled();
128+
}).finally(done);
129+
130+
$rootScope.$apply();
131+
});
132+
133+
it('should call the fail callback', function(done) {
134+
$webIntent.throwsError = true;
135+
$webIntent.getUri()
136+
.then(spy.success).catch(spy.fail).then(function() {
137+
expect(spy.fail).toHaveBeenCalled();
138+
expect(spy.success).not.toHaveBeenCalled();
139+
}).finally(done);
140+
$rootScope.$apply();
141+
});
142+
});
143+
144+
describe('onNewIntent', function() {
145+
it('should call the success callback', function() {
146+
$webIntent.onNewIntent(spy.success);
147+
$webIntent.newIntent();
148+
149+
expect(spy.success).toHaveBeenCalled();
150+
});
151+
});
152+
153+
describe('sendBroadcast', function() {
154+
it('should call the success callback', function(done) {
155+
$webIntent.sendBroadcast()
156+
.then(spy.success).catch(spy.fail).then(function() {
157+
expect(spy.success).toHaveBeenCalled();
158+
expect(spy.fail).not.toHaveBeenCalled();
159+
}).finally(done);
160+
161+
$rootScope.$apply();
162+
});
163+
164+
it('should call the fail callback', function(done) {
165+
$webIntent.throwsError = true;
166+
$webIntent.sendBroadcast()
167+
.then(spy.success).catch(spy.fail).then(function() {
168+
expect(spy.fail).toHaveBeenCalled();
169+
expect(spy.success).not.toHaveBeenCalled();
170+
}).finally(done);
171+
$rootScope.$apply();
172+
});
173+
});
174+
175+
176+
177+
});
178+
});

0 commit comments

Comments
 (0)