blob: ece69a99f5792a2918cb756f921dbac3dc2f2733 [file] [log] [blame]
Adrian Roos2c5cacf2018-12-19 17:10:22 +01001#!/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
17import unittest
18
19import apilint
20
21def 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
27c1 = cls("android.app", "ActivityManager")
28c2 = cls("android.app", "Notification")
29c3 = cls("android.app", "Notification.Action")
30c4 = cls("android.graphics", "Bitmap")
31
32class 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 Roos61e37302018-12-19 17:11:21 +010062 self.assertEquals(apilint._skip_to_matching_class(it, c3),
Adrian Roos2c5cacf2018-12-19 17:10:22 +010063 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 Roos61e37302018-12-19 17:11:21 +010068 self.assertEquals(apilint._skip_to_matching_class(it, cls("android.content", "ContentProvider")),
Adrian Roos2c5cacf2018-12-19 17:10:22 +010069 None)
70 self.assertEqual(it.next(), c4)
71
Adrian Roos61e37302018-12-19 17:11:21 +010072 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
90faulty_current_txt = """
91package 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
104ok_current_txt = """
105package 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
118system_current_txt = """
119package android.app {
120 public final class WallpaperColors implements android.os.Parcelable {
121 method public int getSomething();
122 }
123}
124""".split('\n')
125
126
127
128class 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 Roos2c5cacf2018-12-19 17:10:22 +0100146if __name__ == "__main__":
147 unittest.main()