Adrian Roos | 2c5cacf | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import unittest |
| 18 | |
| 19 | import apilint |
| 20 | |
| 21 | def cls(pkg, name): |
| 22 | return apilint.Class(apilint.Package(999, "package %s {" % pkg, None), 999, |
| 23 | "public final class %s {" % name, None) |
| 24 | |
| 25 | _ri = apilint._retry_iterator |
| 26 | |
| 27 | c1 = cls("android.app", "ActivityManager") |
| 28 | c2 = cls("android.app", "Notification") |
| 29 | c3 = cls("android.app", "Notification.Action") |
| 30 | c4 = cls("android.graphics", "Bitmap") |
| 31 | |
| 32 | class UtilTests(unittest.TestCase): |
| 33 | def test_retry_iterator(self): |
| 34 | it = apilint._retry_iterator([1, 2, 3, 4]) |
| 35 | self.assertEqual(it.next(), 1) |
| 36 | self.assertEqual(it.next(), 2) |
| 37 | self.assertEqual(it.next(), 3) |
| 38 | it.send("retry") |
| 39 | self.assertEqual(it.next(), 3) |
| 40 | self.assertEqual(it.next(), 4) |
| 41 | with self.assertRaises(StopIteration): |
| 42 | it.next() |
| 43 | |
| 44 | def test_retry_iterator_one(self): |
| 45 | it = apilint._retry_iterator([1]) |
| 46 | self.assertEqual(it.next(), 1) |
| 47 | it.send("retry") |
| 48 | self.assertEqual(it.next(), 1) |
| 49 | with self.assertRaises(StopIteration): |
| 50 | it.next() |
| 51 | |
| 52 | def test_retry_iterator_one(self): |
| 53 | it = apilint._retry_iterator([1]) |
| 54 | self.assertEqual(it.next(), 1) |
| 55 | it.send("retry") |
| 56 | self.assertEqual(it.next(), 1) |
| 57 | with self.assertRaises(StopIteration): |
| 58 | it.next() |
| 59 | |
| 60 | def test_skip_to_matching_class_found(self): |
| 61 | it = _ri([c1, c2, c3, c4]) |
Adrian Roos | 61e3730 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 62 | self.assertEquals(apilint._skip_to_matching_class(it, c3), |
Adrian Roos | 2c5cacf | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 63 | c3) |
| 64 | self.assertEqual(it.next(), c4) |
| 65 | |
| 66 | def test_skip_to_matching_class_not_found(self): |
| 67 | it = _ri([c1, c2, c3, c4]) |
Adrian Roos | 61e3730 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 68 | self.assertEquals(apilint._skip_to_matching_class(it, cls("android.content", "ContentProvider")), |
Adrian Roos | 2c5cacf | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 69 | None) |
| 70 | self.assertEqual(it.next(), c4) |
| 71 | |
Adrian Roos | 61e3730 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 72 | def test_yield_until_matching_class_found(self): |
| 73 | it = _ri([c1, c2, c3, c4]) |
| 74 | self.assertEquals(list(apilint._yield_until_matching_class(it, c3)), |
| 75 | [c1, c2]) |
| 76 | self.assertEqual(it.next(), c4) |
| 77 | |
| 78 | def test_yield_until_matching_class_not_found(self): |
| 79 | it = _ri([c1, c2, c3, c4]) |
| 80 | self.assertEquals(list(apilint._yield_until_matching_class(it, cls("android.content", "ContentProvider"))), |
| 81 | [c1, c2, c3]) |
| 82 | self.assertEqual(it.next(), c4) |
| 83 | |
| 84 | def test_yield_until_matching_class_None(self): |
| 85 | it = _ri([c1, c2, c3, c4]) |
| 86 | self.assertEquals(list(apilint._yield_until_matching_class(it, None)), |
| 87 | [c1, c2, c3, c4]) |
| 88 | |
| 89 | |
| 90 | faulty_current_txt = """ |
| 91 | package android.app { |
| 92 | public final class Activity { |
| 93 | } |
| 94 | |
| 95 | public final class WallpaperColors implements android.os.Parcelable { |
| 96 | ctor public WallpaperColors(android.os.Parcel); |
| 97 | method public int describeContents(); |
| 98 | method public void writeToParcel(android.os.Parcel, int); |
| 99 | field public static final android.os.Parcelable.Creator<android.app.WallpaperColors> CREATOR; |
| 100 | } |
| 101 | } |
| 102 | """.split('\n') |
| 103 | |
| 104 | ok_current_txt = """ |
| 105 | package android.app { |
| 106 | public final class Activity { |
| 107 | } |
| 108 | |
| 109 | public final class WallpaperColors implements android.os.Parcelable { |
| 110 | ctor public WallpaperColors(); |
| 111 | method public int describeContents(); |
| 112 | method public void writeToParcel(android.os.Parcel, int); |
| 113 | field public static final android.os.Parcelable.Creator<android.app.WallpaperColors> CREATOR; |
| 114 | } |
| 115 | } |
| 116 | """.split('\n') |
| 117 | |
| 118 | system_current_txt = """ |
| 119 | package android.app { |
| 120 | public final class WallpaperColors implements android.os.Parcelable { |
| 121 | method public int getSomething(); |
| 122 | } |
| 123 | } |
| 124 | """.split('\n') |
| 125 | |
| 126 | |
| 127 | |
| 128 | class BaseFileTests(unittest.TestCase): |
| 129 | def test_base_file_avoids_errors(self): |
| 130 | failures, _ = apilint.examine_stream(system_current_txt, ok_current_txt) |
| 131 | self.assertEquals(failures, {}) |
| 132 | |
| 133 | def test_class_with_base_finds_same_errors(self): |
| 134 | failures_with_classes_with_base, _ = apilint.examine_stream("", faulty_current_txt, |
| 135 | in_classes_with_base=[cls("android.app", "WallpaperColors")]) |
| 136 | failures_with_system_txt, _ = apilint.examine_stream(system_current_txt, faulty_current_txt) |
| 137 | |
| 138 | self.assertEquals(failures_with_classes_with_base.keys(), failures_with_system_txt.keys()) |
| 139 | |
| 140 | def test_classes_with_base_is_emited(self): |
| 141 | classes_with_base = [] |
| 142 | _, _ = apilint.examine_stream(system_current_txt, faulty_current_txt, |
| 143 | out_classes_with_base=classes_with_base) |
| 144 | self.assertEquals(map(lambda x: x.fullname, classes_with_base), ["android.app.WallpaperColors"]) |
| 145 | |
Adrian Roos | 373df11 | 2019-01-21 15:43:15 +0100 | [diff] [blame] | 146 | class ParseV2Stream(unittest.TestCase): |
| 147 | def test_field_kinds(self): |
| 148 | api = apilint._parse_stream(""" |
| 149 | // Signature format: 2.0 |
| 150 | package android { |
| 151 | public enum SomeEnum { |
| 152 | enum_constant public static final android.SomeEnum ENUM_CONST; |
| 153 | field public static final int FIELD_CONST; |
| 154 | property public final int someProperty; |
| 155 | ctor public SomeEnum(); |
| 156 | method public Object? getObject(); |
| 157 | } |
| 158 | } |
| 159 | """.strip().split('\n')) |
| 160 | |
| 161 | self.assertEquals(api['android.SomeEnum'].fields[0].split[0], 'enum_constant') |
| 162 | self.assertEquals(api['android.SomeEnum'].fields[1].split[0], 'field') |
| 163 | self.assertEquals(api['android.SomeEnum'].fields[2].split[0], 'property') |
| 164 | self.assertEquals(api['android.SomeEnum'].ctors[0].split[0], 'ctor') |
| 165 | self.assertEquals(api['android.SomeEnum'].methods[0].split[0], 'method') |
| 166 | |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 167 | class V2TokenizerTests(unittest.TestCase): |
| 168 | def _test(self, raw, expected): |
| 169 | self.assertEquals(apilint.V2Tokenizer(raw).tokenize(), expected) |
| 170 | |
| 171 | def test_simple(self): |
| 172 | self._test(" method public some.Type someName(some.Argument arg, int arg);", |
| 173 | ['method', 'public', 'some.Type', 'someName', '(', 'some.Argument', |
| 174 | 'arg', ',', 'int', 'arg', ')', ';']) |
| 175 | self._test("class Some.Class extends SomeOther {", |
| 176 | ['class', 'Some.Class', 'extends', 'SomeOther', '{']) |
| 177 | |
Adrian Roos | 7884d6b | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 178 | def test_varargs(self): |
| 179 | self._test("name(String...)", |
| 180 | ['name', '(', 'String', '...', ')']) |
| 181 | |
| 182 | def test_kotlin(self): |
| 183 | self._test("String? name(String!...)", |
| 184 | ['String', '?', 'name', '(', 'String', '!', '...', ')']) |
| 185 | |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 186 | def test_annotation(self): |
| 187 | self._test("method @Nullable public void name();", |
| 188 | ['method', '@', 'Nullable', 'public', 'void', 'name', '(', ')', ';']) |
| 189 | |
| 190 | def test_annotation_args(self): |
| 191 | self._test("@Some(val=1, other=2) class Class {", |
| 192 | ['@', 'Some', '(', 'val', '=', '1', ',', 'other', '=', '2', ')', |
| 193 | 'class', 'Class', '{']) |
| 194 | def test_comment(self): |
| 195 | self._test("some //comment", ['some']) |
| 196 | |
| 197 | def test_strings(self): |
| 198 | self._test(r'"" "foo" "\"" "\\"', ['""', '"foo"', r'"\""', r'"\\"']) |
| 199 | |
| 200 | def test_at_interface(self): |
| 201 | self._test("public @interface Annotation {", |
| 202 | ['public', '@interface', 'Annotation', '{']) |
| 203 | |
| 204 | def test_array_type(self): |
| 205 | self._test("int[][]", ['int', '[]', '[]']) |
| 206 | |
| 207 | def test_generics(self): |
| 208 | self._test("<>foobar<A extends Object>", |
| 209 | ['<', '>', 'foobar', '<', 'A', 'extends', 'Object', '>']) |
| 210 | |
| 211 | class V2ParserTests(unittest.TestCase): |
| 212 | def _cls(self, raw): |
| 213 | pkg = apilint.Package(999, "package pkg {", None) |
| 214 | return apilint.Class(pkg, 1, raw, '', sig_format=2) |
| 215 | |
| 216 | def _method(self, raw, cls=None): |
| 217 | if not cls: |
| 218 | cls = self._cls("class Class {") |
| 219 | return apilint.Method(cls, 1, raw, '', sig_format=2) |
| 220 | |
| 221 | def _field(self, raw): |
| 222 | cls = self._cls("class Class {") |
| 223 | return apilint.Field(cls, 1, raw, '', sig_format=2) |
| 224 | |
| 225 | def test_class(self): |
| 226 | cls = self._cls("@Deprecated @IntRange(from=1, to=2) public static abstract class Some.Name extends Super<Class> implements Interface<Class> {") |
| 227 | self.assertTrue('deprecated' in cls.split) |
| 228 | self.assertTrue('static' in cls.split) |
| 229 | self.assertTrue('abstract' in cls.split) |
| 230 | self.assertTrue('class' in cls.split) |
| 231 | self.assertEquals('Super', cls.extends) |
| 232 | self.assertEquals('Interface', cls.implements) |
| 233 | self.assertEquals('pkg.Some.Name', cls.fullname) |
| 234 | |
Adrian Roos | 373df11 | 2019-01-21 15:43:15 +0100 | [diff] [blame] | 235 | def test_enum(self): |
| 236 | cls = self._cls("public enum Some.Name {") |
| 237 | self._field("enum_constant public static final android.ValueType COLOR;") |
| 238 | |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 239 | def test_interface(self): |
| 240 | cls = self._cls("@Deprecated @IntRange(from=1, to=2) public interface Some.Name extends Interface<Class> {") |
| 241 | self.assertTrue('deprecated' in cls.split) |
| 242 | self.assertTrue('interface' in cls.split) |
| 243 | self.assertEquals('Interface', cls.extends) |
| 244 | self.assertEquals('Interface', cls.implements) |
| 245 | self.assertEquals('pkg.Some.Name', cls.fullname) |
| 246 | |
| 247 | def test_at_interface(self): |
| 248 | cls = self._cls("@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.FIELD, java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.CONSTRUCTOR, java.lang.annotation.ElementType.LOCAL_VARIABLE}) @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS) public @interface SuppressLint {") |
| 249 | self.assertTrue('@interface' in cls.split) |
| 250 | self.assertEquals('pkg.SuppressLint', cls.fullname) |
| 251 | |
| 252 | def test_parse_method(self): |
Adrian Roos | 403c8e3 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 253 | m = self._method("method @Deprecated public static native <T> Class<T>[][] name(" |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 254 | + "Class<T[]>[][], Class<T[][][]>[][]...) throws Exception, T;") |
| 255 | self.assertTrue('static' in m.split) |
| 256 | self.assertTrue('public' in m.split) |
| 257 | self.assertTrue('method' in m.split) |
Adrian Roos | 403c8e3 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 258 | self.assertTrue('native' in m.split) |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 259 | self.assertTrue('deprecated' in m.split) |
| 260 | self.assertEquals('java.lang.Class[][]', m.typ) |
| 261 | self.assertEquals('name', m.name) |
| 262 | self.assertEquals(['java.lang.Class[][]', 'java.lang.Class[][]...'], m.args) |
| 263 | self.assertEquals(['java.lang.Exception', 'T'], m.throws) |
| 264 | |
| 265 | def test_ctor(self): |
| 266 | m = self._method("ctor @Deprecated <T> ClassName();") |
| 267 | self.assertTrue('ctor' in m.split) |
| 268 | self.assertTrue('deprecated' in m.split) |
| 269 | self.assertEquals('ctor', m.typ) |
| 270 | self.assertEquals('ClassName', m.name) |
| 271 | |
| 272 | def test_parse_annotation_method(self): |
| 273 | cls = self._cls("@interface Annotation {") |
| 274 | self._method('method abstract String category() default "";', cls=cls) |
| 275 | self._method('method abstract boolean deepExport() default false;', cls=cls) |
| 276 | self._method('method abstract ViewDebug.FlagToString[] flagMapping() default {};', cls=cls) |
Adrian Roos | 403c8e3 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 277 | self._method('method abstract ViewDebug.FlagToString[] flagMapping() default (double)java.lang.Float.NEGATIVE_INFINITY;', cls=cls) |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 278 | |
| 279 | def test_parse_string_field(self): |
| 280 | f = self._field('field @Deprecated public final String SOME_NAME = "value";') |
| 281 | self.assertTrue('field' in f.split) |
| 282 | self.assertTrue('deprecated' in f.split) |
| 283 | self.assertTrue('final' in f.split) |
| 284 | self.assertEquals('java.lang.String', f.typ) |
| 285 | self.assertEquals('SOME_NAME', f.name) |
| 286 | self.assertEquals('value', f.value) |
| 287 | |
| 288 | def test_parse_field(self): |
| 289 | f = self._field('field public Object SOME_NAME;') |
| 290 | self.assertTrue('field' in f.split) |
| 291 | self.assertEquals('java.lang.Object', f.typ) |
| 292 | self.assertEquals('SOME_NAME', f.name) |
| 293 | self.assertEquals(None, f.value) |
| 294 | |
| 295 | def test_parse_int_field(self): |
| 296 | f = self._field('field public int NAME = 123;') |
| 297 | self.assertTrue('field' in f.split) |
| 298 | self.assertEquals('int', f.typ) |
| 299 | self.assertEquals('NAME', f.name) |
| 300 | self.assertEquals('123', f.value) |
| 301 | |
| 302 | def test_parse_quotient_field(self): |
| 303 | f = self._field('field public int NAME = (0.0/0.0);') |
| 304 | self.assertTrue('field' in f.split) |
| 305 | self.assertEquals('int', f.typ) |
| 306 | self.assertEquals('NAME', f.name) |
| 307 | self.assertEquals('( 0.0 / 0.0 )', f.value) |
| 308 | |
Adrian Roos | 7884d6b | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 309 | def test_kotlin_types(self): |
| 310 | self._field('field public List<Integer[]?[]!>?[]![]? NAME;') |
| 311 | self._method("method <T?> Class<T!>?[]![][]? name(Type!, Type argname," |
| 312 | + "Class<T?>[][]?[]!...!) throws Exception, T;") |
| 313 | self._method("method <T> T name(T a = 1, T b = A(1), Lambda f = { false }, N? n = null, " |
| 314 | + """double c = (1/0), float d = 1.0f, String s = "heyo", char c = 'a');""") |
| 315 | |
Adrian Roos | 403c8e3 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 316 | def test_kotlin_operator(self): |
| 317 | self._method('method public operator void unaryPlus(androidx.navigation.NavDestination);') |
| 318 | self._method('method public static operator androidx.navigation.NavDestination get(androidx.navigation.NavGraph, @IdRes int id);') |
| 319 | self._method('method public static operator <T> T get(androidx.navigation.NavigatorProvider, kotlin.reflect.KClass<T> clazz);') |
| 320 | |
| 321 | def test_kotlin_property(self): |
| 322 | self._field('property public VM value;') |
| 323 | self._field('property public final String? action;') |
| 324 | |
| 325 | def test_kotlin_varargs(self): |
| 326 | self._method('method public void error(int p = "42", Integer int2 = "null", int p1 = "42", vararg String args);') |
| 327 | |
| 328 | def test_kotlin_default_values(self): |
| 329 | self._method('method public void foo(String! = null, String! = "Hello World", int = 42);') |
| 330 | self._method('method void method(String, String firstArg = "hello", int secondArg = "42", String thirdArg = "world");') |
| 331 | self._method('method void method(String, String firstArg = "hello", int secondArg = "42");') |
| 332 | self._method('method void method(String, String firstArg = "hello");') |
| 333 | self._method('method void edit(android.Type, boolean commit = false, Function1<? super Editor,kotlin.Unit> action);') |
| 334 | self._method('method <K, V> LruCache<K,V> lruCache(int maxSize, Function2<? super K,? super V,java.lang.Integer> sizeOf = { _, _ -> 1 }, Function1<? extends V> create = { (V)null }, Function4<kotlin.Unit> onEntryRemoved = { _, _, _, _ -> });') |
| 335 | self._method('method android.Bitmap? drawToBitmap(android.View, android.Config config = android.graphics.Bitmap.Config.ARGB_8888);') |
| 336 | self._method('method void emptyLambda(Function0<kotlin.Unit> sizeOf = {});') |
| 337 | self._method('method void method1(int p = 42, Integer? int2 = null, int p1 = 42, String str = "hello world", java.lang.String... args);') |
| 338 | self._method('method void method2(int p, int int2 = (2 * int) * some.other.pkg.Constants.Misc.SIZE);') |
| 339 | self._method('method void method3(String str, int p, int int2 = double(int) + str.length);') |
| 340 | self._method('method void print(test.pkg.Foo foo = test.pkg.Foo());') |
| 341 | |
| 342 | def test_type_use_annotation(self): |
| 343 | self._method('method public static int codePointAt(char @NonNull [], int);') |
| 344 | self._method('method @NonNull public java.util.Set<java.util.Map.@NonNull Entry<K,V>> entrySet();') |
| 345 | |
| 346 | m = self._method('method @NonNull public java.lang.annotation.@NonNull Annotation @NonNull [] getAnnotations();') |
| 347 | self.assertEquals('java.lang.annotation.Annotation[]', m.typ) |
| 348 | |
| 349 | m = self._method('method @NonNull public abstract java.lang.annotation.@NonNull Annotation @NonNull [] @NonNull [] getParameterAnnotations();') |
| 350 | self.assertEquals('java.lang.annotation.Annotation[][]', m.typ) |
| 351 | |
| 352 | m = self._method('method @NonNull public @NonNull String @NonNull [] split(@NonNull String, int);') |
| 353 | self.assertEquals('java.lang.String[]', m.typ) |
| 354 | |
Adrian Roos | 2c5cacf | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 355 | if __name__ == "__main__": |
Adrian Roos | d170961 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 356 | unittest.main() |