Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit c8fcad4

Browse files
[various] Adopt plugin_platform_interface (#6332)
1 parent 0f0ef20 commit c8fcad4

File tree

8 files changed

+120
-46
lines changed

8 files changed

+120
-46
lines changed

packages/google_sign_in/google_sign_in_platform_interface/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.3.0
2+
3+
* Adopts `plugin_platform_interface`. As a result, `isMock` is deprecated in
4+
favor of the now-standard `MockPlatformInterfaceMixin`.
5+
16
## 2.2.0
27

38
* Adds support for the `serverClientId` parameter.

packages/google_sign_in/google_sign_in_platform_interface/lib/google_sign_in_platform_interface.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:flutter/foundation.dart' show visibleForTesting;
8+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
89

910
import 'src/method_channel_google_sign_in.dart';
1011
import 'src/types.dart';
@@ -20,13 +21,19 @@ export 'src/types.dart';
2021
/// ensures that the subclass will get the default implementation, while
2122
/// platform implementations that `implements` this interface will be broken by
2223
/// newly added [GoogleSignInPlatform] methods.
23-
abstract class GoogleSignInPlatform {
24+
abstract class GoogleSignInPlatform extends PlatformInterface {
25+
/// Constructs a GoogleSignInPlatform.
26+
GoogleSignInPlatform() : super(token: _token);
27+
28+
static final Object _token = Object();
29+
2430
/// Only mock implementations should set this to `true`.
2531
///
2632
/// Mockito mocks implement this class with `implements` which is forbidden
2733
/// (see class docs). This property provides a backdoor for mocks to skip the
2834
/// verification that the class isn't implemented with `implements`.
2935
@visibleForTesting
36+
@Deprecated('Use MockPlatformInterfaceMixin instead')
3037
bool get isMock => false;
3138

3239
/// The default instance of [GoogleSignInPlatform] to use.
@@ -44,25 +51,11 @@ abstract class GoogleSignInPlatform {
4451
// https://github.com/flutter/flutter/issues/43368
4552
static set instance(GoogleSignInPlatform instance) {
4653
if (!instance.isMock) {
47-
try {
48-
instance._verifyProvidesDefaultImplementations();
49-
} on NoSuchMethodError catch (_) {
50-
throw AssertionError(
51-
'Platform interfaces must not be implemented with `implements`');
52-
}
54+
PlatformInterface.verify(instance, _token);
5355
}
5456
_instance = instance;
5557
}
5658

57-
/// This method ensures that [GoogleSignInPlatform] isn't implemented with `implements`.
58-
///
59-
/// See class docs for more details on why using `implements` to implement
60-
/// [GoogleSignInPlatform] is forbidden.
61-
///
62-
/// This private method is called by the [instance] setter, which should fail
63-
/// if the provided instance is a class implemented with `implements`.
64-
void _verifyProvidesDefaultImplementations() {}
65-
6659
/// Initializes the plugin. Deprecated: call [initWithParams] instead.
6760
///
6861
/// The [hostedDomain] argument specifies a hosted domain restriction. By

packages/google_sign_in/google_sign_in_platform_interface/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.2.0
7+
version: 2.3.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"
@@ -13,6 +13,7 @@ environment:
1313
dependencies:
1414
flutter:
1515
sdk: flutter
16+
plugin_platform_interface: ^2.1.0
1617
quiver: ^3.0.0
1718

1819
dev_dependencies:

packages/google_sign_in/google_sign_in_platform_interface/test/google_sign_in_platform_interface_test.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:flutter_test/flutter_test.dart';
66
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
77
import 'package:mockito/mockito.dart';
8+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
89

910
void main() {
1011
// Store the initial instance before any tests change it.
@@ -33,7 +34,11 @@ void main() {
3334
});
3435

3536
test('Can be mocked with `implements`', () {
36-
GoogleSignInPlatform.instance = ImplementsWithIsMock();
37+
GoogleSignInPlatform.instance = ModernMockImplementation();
38+
});
39+
40+
test('still supports legacy isMock', () {
41+
GoogleSignInPlatform.instance = LegacyIsMockImplementation();
3742
});
3843
});
3944

@@ -76,11 +81,18 @@ void main() {
7681
});
7782
}
7883

79-
class ImplementsWithIsMock extends Mock implements GoogleSignInPlatform {
84+
class LegacyIsMockImplementation extends Mock implements GoogleSignInPlatform {
8085
@override
8186
bool get isMock => true;
8287
}
8388

89+
class ModernMockImplementation extends Mock
90+
with MockPlatformInterfaceMixin
91+
implements GoogleSignInPlatform {
92+
@override
93+
bool get isMock => false;
94+
}
95+
8496
class ImplementsGoogleSignInPlatform extends Mock
8597
implements GoogleSignInPlatform {}
8698

packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 2.1.0
22

3-
* Fixes newly enabled analyzer options.
3+
* Adopts `plugin_platform_interface`. As a result, `isMock` is deprecated in
4+
favor of the now-standard `MockPlatformInterfaceMixin`.
45

56
## 2.0.0
67

packages/shared_preferences/shared_preferences_platform_interface/lib/shared_preferences_platform_interface.dart

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:flutter/foundation.dart';
8+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
89

910
import 'method_channel_shared_preferences.dart';
1011

@@ -15,24 +16,24 @@ import 'method_channel_shared_preferences.dart';
1516
/// (using `extends`) ensures that the subclass will get the default implementation, while
1617
/// platform implementations that `implements` this interface will be broken by newly added
1718
/// [SharedPreferencesStorePlatform] methods.
18-
abstract class SharedPreferencesStorePlatform {
19+
abstract class SharedPreferencesStorePlatform extends PlatformInterface {
20+
/// Constructs a SharedPreferencesStorePlatform.
21+
SharedPreferencesStorePlatform() : super(token: _token);
22+
23+
static final Object _token = Object();
24+
1925
/// The default instance of [SharedPreferencesStorePlatform] to use.
2026
///
2127
/// Defaults to [MethodChannelSharedPreferencesStore].
2228
static SharedPreferencesStorePlatform get instance => _instance;
2329

2430
/// Platform-specific plugins should set this with their own platform-specific
2531
/// class that extends [SharedPreferencesStorePlatform] when they register themselves.
26-
static set instance(SharedPreferencesStorePlatform value) {
27-
if (!value.isMock) {
28-
try {
29-
value._verifyProvidesDefaultImplementations();
30-
} on NoSuchMethodError catch (_) {
31-
throw AssertionError(
32-
'Platform interfaces must not be implemented with `implements`');
33-
}
32+
static set instance(SharedPreferencesStorePlatform instance) {
33+
if (!instance.isMock) {
34+
PlatformInterface.verify(instance, _token);
3435
}
35-
_instance = value;
36+
_instance = instance;
3637
}
3738

3839
static SharedPreferencesStorePlatform _instance =
@@ -44,6 +45,7 @@ abstract class SharedPreferencesStorePlatform {
4445
/// other than mocks (see class docs). This property provides a backdoor for mockito mocks to
4546
/// skip the verification that the class isn't implemented with `implements`.
4647
@visibleForTesting
48+
@Deprecated('Use MockPlatformInterfaceMixin instead')
4749
bool get isMock => false;
4850

4951
/// Removes the value associated with the [key].
@@ -65,14 +67,6 @@ abstract class SharedPreferencesStorePlatform {
6567

6668
/// Returns all key/value pairs persisted in this store.
6769
Future<Map<String, Object>> getAll();
68-
69-
// This method makes sure that SharedPreferencesStorePlatform isn't implemented with `implements`.
70-
//
71-
// See class doc for more details on why implementing this class is forbidden.
72-
//
73-
// This private method is called by the instance setter, which fails if the class is
74-
// implemented with `implements`.
75-
void _verifyProvidesDefaultImplementations() {}
7670
}
7771

7872
/// Stores data in memory.

packages/shared_preferences/shared_preferences_platform_interface/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: shared_preferences_platform_interface
22
description: A common platform interface for the shared_preferences plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/shared_preferences/shared_preferences_platform_interface
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
5-
version: 2.0.0
5+
version: 2.1.0
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"
@@ -11,6 +11,7 @@ environment:
1111
dependencies:
1212
flutter:
1313
sdk: flutter
14+
plugin_platform_interface: ^2.1.0
1415

1516
dev_dependencies:
1617
flutter_test:

packages/shared_preferences/shared_preferences_platform_interface/test/shared_preferences_platform_interface_test.dart

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,38 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
67
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
78

89
void main() {
910
TestWidgetsFlutterBinding.ensureInitialized();
1011

1112
group(SharedPreferencesStorePlatform, () {
1213
test('disallows implementing interface', () {
13-
expect(
14-
() {
15-
SharedPreferencesStorePlatform.instance = IllegalImplementation();
16-
},
17-
throwsAssertionError,
18-
);
14+
expect(() {
15+
SharedPreferencesStorePlatform.instance = IllegalImplementation();
16+
},
17+
// In versions of `package:plugin_platform_interface` prior to fixing
18+
// https://github.com/flutter/flutter/issues/109339, an attempt to
19+
// implement a platform interface using `implements` would sometimes
20+
// throw a `NoSuchMethodError` and other times throw an
21+
// `AssertionError`. After the issue is fixed, an `AssertionError` will
22+
// always be thrown. For the purpose of this test, we don't really care
23+
// what exception is thrown, so just allow any exception.
24+
throwsA(anything));
25+
});
26+
27+
test('supports MockPlatformInterfaceMixin', () {
28+
SharedPreferencesStorePlatform.instance = ModernMockImplementation();
29+
});
30+
31+
test('still supports legacy isMock', () {
32+
SharedPreferencesStorePlatform.instance = LegacyIsMockImplementation();
1933
});
2034
});
2135
}
2236

37+
/// An implementation using `implements` that isn't a mock, which isn't allowed.
2338
class IllegalImplementation implements SharedPreferencesStorePlatform {
2439
// Intentionally declare self as not a mock to trigger the
2540
// compliance check.
@@ -46,3 +61,55 @@ class IllegalImplementation implements SharedPreferencesStorePlatform {
4661
throw UnimplementedError();
4762
}
4863
}
64+
65+
class LegacyIsMockImplementation implements SharedPreferencesStorePlatform {
66+
@override
67+
bool get isMock => true;
68+
69+
@override
70+
Future<bool> clear() {
71+
throw UnimplementedError();
72+
}
73+
74+
@override
75+
Future<Map<String, Object>> getAll() {
76+
throw UnimplementedError();
77+
}
78+
79+
@override
80+
Future<bool> remove(String key) {
81+
throw UnimplementedError();
82+
}
83+
84+
@override
85+
Future<bool> setValue(String valueType, String key, Object value) {
86+
throw UnimplementedError();
87+
}
88+
}
89+
90+
class ModernMockImplementation
91+
with MockPlatformInterfaceMixin
92+
implements SharedPreferencesStorePlatform {
93+
@override
94+
bool get isMock => false;
95+
96+
@override
97+
Future<bool> clear() {
98+
throw UnimplementedError();
99+
}
100+
101+
@override
102+
Future<Map<String, Object>> getAll() {
103+
throw UnimplementedError();
104+
}
105+
106+
@override
107+
Future<bool> remove(String key) {
108+
throw UnimplementedError();
109+
}
110+
111+
@override
112+
Future<bool> setValue(String valueType, String key, Object value) {
113+
throw UnimplementedError();
114+
}
115+
}

0 commit comments

Comments
 (0)