Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
permission_handler
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
songyanzhi
permission_handler
Commits
b21af21a
Commit
b21af21a
authored
Mar 11, 2021
by
Jan-Derk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tests
parent
e89eaa91
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
435 additions
and
6 deletions
+435
-6
permission_handler_platform_interface/lib/permission_handler_platform_interface.dart
+1
-0
permission_handler_platform_interface/lib/src/permission_status.dart
+4
-6
permission_handler_platform_interface/lib/src/permissions.dart
+6
-0
permission_handler_platform_interface/test/src/method_channel/method_channel_mock.dart
+32
-0
permission_handler_platform_interface/test/src/method_channel/method_channel_permission_handler_test.dart
+169
-0
permission_handler_platform_interface/test/src/method_channel/utils/coded_test.dart
+36
-0
permission_handler_platform_interface/test/src/permission_handler_platform_interface_test.dart
+102
-0
permission_handler_platform_interface/test/src/permission_status_test.dart
+32
-0
permission_handler_platform_interface/test/src/permissions_test.dart
+25
-0
permission_handler_platform_interface/test/src/service_status_test.dart
+28
-0
No files found.
permission_handler_platform_interface/lib/permission_handler_platform_interface.dart
View file @
b21af21a
library
permission_handler_platform_interface
;
import
'dart:async'
;
import
'package:flutter/cupertino.dart'
;
import
'package:plugin_platform_interface/plugin_platform_interface.dart'
;
import
'src/method_channel/method_channel_permission_handler.dart'
;
...
...
permission_handler_platform_interface/lib/src/permission_status.dart
View file @
b21af21a
...
...
@@ -28,17 +28,15 @@ enum PermissionStatus {
extension
PermissionStatusValue
on
PermissionStatus
{
int
get
value
{
switch
(
this
)
{
case
PermissionStatus
.
denied
:
return
0
;
case
PermissionStatus
.
granted
:
return
0
;
case
PermissionStatus
.
denied
:
return
1
;
case
PermissionStatus
.
restricted
:
return
2
;
case
PermissionStatus
.
deni
ed
:
case
PermissionStatus
.
limit
ed
:
return
3
;
case
PermissionStatus
.
permanentlyDenied
:
return
5
;
case
PermissionStatus
.
limited
:
return
4
;
default
:
throw
UnimplementedError
();
...
...
@@ -47,8 +45,8 @@ extension PermissionStatusValue on PermissionStatus {
static
PermissionStatus
statusByValue
(
int
value
)
{
return
[
PermissionStatus
.
denied
,
PermissionStatus
.
granted
,
PermissionStatus
.
denied
,
PermissionStatus
.
restricted
,
PermissionStatus
.
limited
,
PermissionStatus
.
permanentlyDenied
,
...
...
permission_handler_platform_interface/lib/src/permissions.dart
View file @
b21af21a
...
...
@@ -5,6 +5,9 @@ part of permission_handler_platform_interface;
/// the related service.
class
PermissionWithService
extends
Permission
{
const
PermissionWithService
.
_
(
int
value
)
:
super
.
_
(
value
);
@visibleForTesting
const
PermissionWithService
.
private
(
int
value
)
:
super
.
_
(
value
);
}
/// Defines the permissions which can be checked and requested.
...
...
@@ -12,6 +15,9 @@ class Permission {
const
Permission
.
_
(
this
.
value
);
factory
Permission
.
byValue
(
int
value
)
=>
values
[
value
];
@visibleForTesting
const
Permission
.
private
(
this
.
value
);
/// Integer representation of the [Permission].
final
int
value
;
...
...
permission_handler_platform_interface/test/src/method_channel/method_channel_mock.dart
0 → 100644
View file @
b21af21a
import
'package:flutter/services.dart'
;
class
MethodChannelMock
{
final
MethodChannel
methodChannel
;
final
String
method
;
final
dynamic
result
;
final
Duration
delay
;
MethodChannelMock
({
required
String
channelName
,
required
this
.
method
,
this
.
result
,
this
.
delay
=
Duration
.
zero
,
})
:
methodChannel
=
MethodChannel
(
channelName
)
{
methodChannel
.
setMockMethodCallHandler
(
_handler
);
}
Future
_handler
(
MethodCall
methodCall
)
async
{
if
(
methodCall
.
method
!=
method
)
{
throw
MissingPluginException
(
'No implementation found for method '
'
$method
on channel
${methodChannel.name}
'
);
}
return
Future
.
delayed
(
delay
,
()
{
if
(
result
is
Exception
)
{
throw
result
;
}
return
Future
.
value
(
result
);
});
}
}
permission_handler_platform_interface/test/src/method_channel/method_channel_permission_handler_test.dart
0 → 100644
View file @
b21af21a
import
'package:flutter/cupertino.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'
;
import
'package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart'
;
import
'method_channel_mock.dart'
;
void
main
(
)
{
TestWidgetsFlutterBinding
.
ensureInitialized
();
group
(
'checkPermissionStatus: When checking for permission'
,
()
{
test
(
'Should receive granted if user wants access to the requested feature'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkPermissionStatus'
,
result:
PermissionStatus
.
granted
.
index
,
);
final
permissionStatus
=
await
MethodChannelPermissionHandler
()
.
checkPermissionStatus
(
Permission
.
calendar
);
expect
(
permissionStatus
,
PermissionStatus
.
granted
);
});
test
(
'Should receive denied if user denied access to the requested feature'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkPermissionStatus'
,
result:
PermissionStatus
.
denied
.
index
,
);
final
permissionStatus
=
await
MethodChannelPermissionHandler
()
.
checkPermissionStatus
(
Permission
.
calendar
);
expect
(
permissionStatus
,
PermissionStatus
.
denied
);
});
test
(
// ignore: lines_longer_than_80_chars
'Should receive restricted if OS denied rights for to the requested feature'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkPermissionStatus'
,
result:
PermissionStatus
.
restricted
.
index
,
);
final
permissionStatus
=
await
MethodChannelPermissionHandler
()
.
checkPermissionStatus
(
Permission
.
calendar
);
expect
(
permissionStatus
,
PermissionStatus
.
restricted
);
});
test
(
// ignore: lines_longer_than_80_chars
'Should receive limited if user has authorized this application for limited access'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkPermissionStatus'
,
result:
PermissionStatus
.
limited
.
index
,
);
final
permissionStatus
=
await
MethodChannelPermissionHandler
()
.
checkPermissionStatus
(
Permission
.
calendar
);
expect
(
permissionStatus
,
PermissionStatus
.
limited
);
});
test
(
// ignore: lines_longer_than_80_chars
'Should receive permanentlyDenied if user denied access and selected to never show a request for this permission again'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkPermissionStatus'
,
result:
PermissionStatus
.
permanentlyDenied
.
index
,
);
final
permissionStatus
=
await
MethodChannelPermissionHandler
()
.
checkPermissionStatus
(
Permission
.
calendar
);
expect
(
permissionStatus
,
PermissionStatus
.
permanentlyDenied
);
});
});
group
(
'checkServiceStatus: When checking for service'
,
()
{
// ignore: lines_longer_than_80_chars
test
(
'Should receive disabled if the service for the permission is disabled'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkServiceStatus'
,
result:
ServiceStatus
.
disabled
.
index
,
);
final
serviceStatus
=
await
MethodChannelPermissionHandler
()
.
checkServiceStatus
(
Permission
.
calendar
);
expect
(
serviceStatus
,
ServiceStatus
.
disabled
);
});
test
(
'Should receive enabled if the service for the permission is enabled'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkServiceStatus'
,
result:
ServiceStatus
.
enabled
.
index
,
);
final
serviceStatus
=
await
MethodChannelPermissionHandler
()
.
checkServiceStatus
(
Permission
.
calendar
);
expect
(
serviceStatus
,
ServiceStatus
.
enabled
);
});
test
(
// ignore: lines_longer_than_80_chars
'Should receive notApplicable if the permission does not have an associated service on the current platform'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'checkServiceStatus'
,
result:
ServiceStatus
.
notApplicable
.
index
,
);
final
serviceStatus
=
await
MethodChannelPermissionHandler
()
.
checkServiceStatus
(
Permission
.
calendar
);
expect
(
serviceStatus
,
ServiceStatus
.
notApplicable
);
});
});
group
(
'openAppSettings: When opening the App settings'
,
()
{
test
(
'Should receive true if the page can be opened'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'openAppSettings'
,
result:
true
,
);
final
hasOpenedAppSettings
=
await
MethodChannelPermissionHandler
().
openAppSettings
();
expect
(
hasOpenedAppSettings
,
true
);
});
test
(
'Should receive false if an error occurred'
,
()
async
{
MethodChannelMock
(
channelName:
'flutter.baseflow.com/permissions/methods'
,
method:
'openAppSettings'
,
result:
false
,
);
final
hasOpenedAppSettings
=
await
MethodChannelPermissionHandler
().
openAppSettings
();
expect
(
hasOpenedAppSettings
,
false
);
});
});
group
(
'requestPermissions: When requesting for permission'
,
(){
test
(
'requestPermission'
,
(){
});
});
}
permission_handler_platform_interface/test/src/method_channel/utils/coded_test.dart
0 → 100644
View file @
b21af21a
import
'package:flutter_test/flutter_test.dart'
;
import
'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'
;
import
'package:permission_handler_platform_interface/src/method_channel/utils/codec.dart'
;
void
main
(
)
{
group
(
'Codec'
,
()
{
test
(
'decodePermissionStatus should return a PermissionStatus'
,
()
{
expect
(
Codec
.
decodePermissionStatus
(
0
),
PermissionStatus
.
granted
);
});
test
(
'decodeServiceStatus should a corresponding ServiceStatus'
,
()
{
expect
(
Codec
.
decodeServiceStatus
(
0
),
ServiceStatus
.
disabled
);
});
test
(
'decodePermissionRequestResult should convert a map<int, int>'
'to map<Permission, PermissionStatus>'
,
()
{
var
value
=
<
int
,
int
>{
1
:
1
,
};
var
permissionMap
=
Codec
.
decodePermissionRequestResult
(
value
);
expect
(
permissionMap
.
keys
.
first
,
isA
<
Permission
>());
expect
(
permissionMap
.
values
.
first
,
isA
<
PermissionStatus
>());
});
test
(
'encodePermissions should return a list of integers'
,
()
{
var
permissions
=
[
Permission
.
accessMediaLocation
];
List
<
int
>
integers
=
Codec
.
encodePermissions
(
permissions
);
expect
(
integers
.
first
,
isA
<
int
>());
});
});
}
permission_handler_platform_interface/test/src/permission_handler_platform_interface_test.dart
0 → 100644
View file @
b21af21a
import
'package:flutter_test/flutter_test.dart'
;
import
'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'
;
import
'package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart'
;
import
'package:mockito/mockito.dart'
;
import
'package:plugin_platform_interface/plugin_platform_interface.dart'
;
void
main
(
)
{
TestWidgetsFlutterBinding
.
ensureInitialized
();
group
(
'
$PermissionHandlerPlatform
'
,
()
{
test
(
'
$MethodChannelPermissionHandler
is the default instance'
,
()
{
expect
(
PermissionHandlerPlatform
.
instance
,
isA
<
MethodChannelPermissionHandler
>());
});
test
(
'Cannot be implemented with `implements`'
,
()
{
expect
(()
{
PermissionHandlerPlatform
.
instance
=
ImplementsPermissionHandlerPlatform
();
},
throwsNoSuchMethodError
);
});
test
(
'Can be extended with `extend`'
,
()
{
PermissionHandlerPlatform
.
instance
=
ExtendsPermissionHandlerPlatform
();
});
test
(
'Can be mocked with `implements`'
,
()
{
final
mock
=
MockPermissionHandlerPlatform
();
PermissionHandlerPlatform
.
instance
=
mock
;
});
test
(
// ignore: lines_longer_than_80_chars
'Default implementation of checkPermissionStatus should throw unimplemented error'
,
()
{
final
permissionHandlerPlatform
=
ExtendsPermissionHandlerPlatform
();
expect
(()
{
permissionHandlerPlatform
.
checkPermissionStatus
(
Permission
.
accessMediaLocation
);
},
throwsUnimplementedError
);
});
test
(
// ignore: lines_longer_than_80_chars
'Default implementation of checkServiceStatus should throw unimplemented error'
,
()
{
final
permissionHandlerPlatform
=
ExtendsPermissionHandlerPlatform
();
expect
(()
{
permissionHandlerPlatform
.
checkServiceStatus
(
Permission
.
accessMediaLocation
);
},
throwsUnimplementedError
);
});
test
(
// ignore: lines_longer_than_80_chars
'Default implementation of openAppSettings should throw unimplemented error'
,
()
{
final
permissionHandlerPlatform
=
ExtendsPermissionHandlerPlatform
();
expect
(
permissionHandlerPlatform
.
openAppSettings
,
throwsUnimplementedError
);
});
test
(
// ignore: lines_longer_than_80_chars
'Default implementation of requestPermissions should throw unimplemented error'
,
()
{
final
permissionHandlerPlatform
=
ExtendsPermissionHandlerPlatform
();
List
<
Permission
>
permission
=
[
Permission
.
accessMediaLocation
];
expect
(()
{
permissionHandlerPlatform
.
requestPermissions
(
permission
);
},
throwsUnimplementedError
);
});
test
(
// ignore: lines_longer_than_80_chars
'Default implementation of shouldShowRequestPermissionRationale should throw unimplemented error'
,
()
{
final
permissionHandlerPlatform
=
ExtendsPermissionHandlerPlatform
();
expect
(()
{
permissionHandlerPlatform
.
shouldShowRequestPermissionRationale
(
Permission
.
accessMediaLocation
);
},
throwsUnimplementedError
);
});
});
}
class
ImplementsPermissionHandlerPlatform
implements
PermissionHandlerPlatform
{
@override
dynamic
noSuchMethod
(
Invocation
invocation
)
=>
super
.
noSuchMethod
(
invocation
);
}
class
ExtendsPermissionHandlerPlatform
extends
PermissionHandlerPlatform
{}
class
MockPermissionHandlerPlatform
extends
Mock
// ignore: prefer_mixin
with
MockPlatformInterfaceMixin
implements
PermissionHandlerPlatform
{}
permission_handler_platform_interface/test/src/permission_status_test.dart
0 → 100644
View file @
b21af21a
import
'package:flutter_test/flutter_test.dart'
;
import
'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'
;
void
main
(
)
{
test
(
'PermissionStatus should contain 5 options'
,
()
{
final
values
=
PermissionStatus
.
values
;
expect
(
values
.
length
,
5
);
});
test
(
'PermissionStatus enum should have items in correct index'
,
()
{
final
values
=
PermissionStatus
.
values
;
expect
(
values
[
0
],
PermissionStatus
.
granted
);
expect
(
values
[
1
],
PermissionStatus
.
denied
);
expect
(
values
[
2
],
PermissionStatus
.
restricted
);
expect
(
values
[
3
],
PermissionStatus
.
limited
);
expect
(
values
[
4
],
PermissionStatus
.
permanentlyDenied
);
});
test
(
'statusByValue should return right index int that corresponds with the right PermissionStatus'
,
()
{
expect
(
PermissionStatusValue
.
statusByValue
(
0
),
PermissionStatus
.
granted
);
expect
(
PermissionStatusValue
.
statusByValue
(
1
),
PermissionStatus
.
denied
);
expect
(
PermissionStatusValue
.
statusByValue
(
2
),
PermissionStatus
.
restricted
);
expect
(
PermissionStatusValue
.
statusByValue
(
3
),
PermissionStatus
.
limited
);
expect
(
PermissionStatusValue
.
statusByValue
(
4
),
PermissionStatus
.
permanentlyDenied
);
});
//TODO: Kan je de Getters testen?
}
permission_handler_platform_interface/test/src/permissions_test.dart
0 → 100644
View file @
b21af21a
import
'package:flutter_test/flutter_test.dart'
;
import
'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'
;
void
main
(
)
{
test
(
'Permission has the right amount of possible PermissionGroup values'
,
()
{
final
values
=
Permission
.
values
;
expect
(
values
.
length
,
21
);
});
test
(
'check if byValue returns corresponding PermissionGroup value'
,
()
{
final
values
=
Permission
.
values
;
for
(
var
i
=
0
;
i
<
values
.
length
;
i
++)
{
expect
(
values
[
i
],
Permission
.
byValue
(
i
));
}
});
test
(
'check if byValue returns corresponding PermissionGroup value'
,
()
{
var
permissionWithService
=
PermissionWithService
.
private
(
0
);
var
test
=
permissionWithService
.
toString
();
expect
(
test
,
'Permission.calendar'
);
});
}
permission_handler_platform_interface/test/src/service_status_test.dart
0 → 100644
View file @
b21af21a
import
'package:flutter_test/flutter_test.dart'
;
import
'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'
;
void
main
(
)
{
test
(
'ServiceStatus should contain 3 options'
,
()
{
final
values
=
ServiceStatus
.
values
;
expect
(
values
.
length
,
3
);
});
test
(
'PermissionStatus enum should have items in correct index'
,
()
{
final
values
=
ServiceStatus
.
values
;
expect
(
values
[
0
],
ServiceStatus
.
disabled
);
expect
(
values
[
1
],
ServiceStatus
.
enabled
);
expect
(
values
[
2
],
ServiceStatus
.
notApplicable
);
});
test
(
'statusByValue should return right index int that corresponds with the right PermissionStatus'
,
()
{
expect
(
ServiceStatusValue
.
statusByValue
(
0
),
ServiceStatus
.
disabled
);
expect
(
ServiceStatusValue
.
statusByValue
(
1
),
ServiceStatus
.
enabled
);
expect
(
ServiceStatusValue
.
statusByValue
(
2
),
ServiceStatus
.
notApplicable
);
});
//TODO: Kan je de Getters testen?
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment