laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 1 | # Lint as: python2, python3 |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 2 | # Copyright 2017 The Bazel Authors. All rights reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """Tests for aar_resources_extractor.""" |
| 17 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 18 | from __future__ import absolute_import |
| 19 | from __future__ import division |
| 20 | from __future__ import print_function |
| 21 | |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 22 | import io |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 23 | import os |
| 24 | import shutil |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 25 | import unittest |
| 26 | import zipfile |
| 27 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 28 | # Do not edit this line. Copybara replaces it with PY2 migration helper. |
| 29 | import six |
| 30 | |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 31 | from tools.android import aar_resources_extractor |
| 32 | |
| 33 | |
Laszlo Csomor | c60bfdf | 2017-09-27 07:38:03 -0400 | [diff] [blame] | 34 | def _HostPath(path): |
| 35 | return os.path.normpath(path) |
| 36 | |
| 37 | |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 38 | class AarResourcesExtractorTest(unittest.TestCase): |
| 39 | """Unit tests for aar_resources_extractor.py.""" |
| 40 | |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 41 | # Python 2 alias |
| 42 | if not hasattr(unittest.TestCase, "assertCountEqual"): |
| 43 | |
| 44 | def assertCountEqual(self, *args): |
| 45 | return self.assertItemsEqual(*args) |
| 46 | |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 47 | def setUp(self): |
| 48 | os.chdir(os.environ["TEST_TMPDIR"]) |
| 49 | |
| 50 | def tearDown(self): |
| 51 | shutil.rmtree("out_dir") |
| 52 | |
| 53 | def DirContents(self, d): |
Laszlo Csomor | c60bfdf | 2017-09-27 07:38:03 -0400 | [diff] [blame] | 54 | return [ |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 55 | _HostPath(six.ensure_str(path) + "/" + six.ensure_str(f)) |
| 56 | for (path, _, files) in os.walk(d) |
| 57 | for f in files |
Laszlo Csomor | c60bfdf | 2017-09-27 07:38:03 -0400 | [diff] [blame] | 58 | ] |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 59 | |
| 60 | def testNoResources(self): |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 61 | aar = zipfile.ZipFile(io.BytesIO(), "w") |
ajmichael | 56567bb | 2018-01-18 12:03:18 -0800 | [diff] [blame] | 62 | aar.writestr("res/", "") |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 63 | os.makedirs("out_dir") |
| 64 | aar_resources_extractor.ExtractResources(aar, "out_dir") |
Laszlo Csomor | c60bfdf | 2017-09-27 07:38:03 -0400 | [diff] [blame] | 65 | self.assertEqual([_HostPath("out_dir/res/values/empty.xml")], |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 66 | self.DirContents("out_dir")) |
| 67 | with open("out_dir/res/values/empty.xml", "r") as empty_xml: |
| 68 | self.assertEqual("<resources/>", empty_xml.read()) |
| 69 | |
| 70 | def testContainsResources(self): |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 71 | aar = zipfile.ZipFile(io.BytesIO(), "w") |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 72 | aar.writestr("res/values/values.xml", "some values") |
| 73 | aar.writestr("res/layouts/layout.xml", "some layout") |
ajmichael | 19044cf | 2018-01-16 14:18:01 -0800 | [diff] [blame] | 74 | aar.writestr("assets/a", "some asset") |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 75 | os.makedirs("out_dir") |
| 76 | aar_resources_extractor.ExtractResources(aar, "out_dir") |
Laszlo Csomor | c60bfdf | 2017-09-27 07:38:03 -0400 | [diff] [blame] | 77 | expected_resources = [ |
| 78 | _HostPath("out_dir/res/values/values.xml"), |
| 79 | _HostPath("out_dir/res/layouts/layout.xml") |
| 80 | ] |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 81 | self.assertCountEqual(expected_resources, self.DirContents("out_dir")) |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 82 | with open("out_dir/res/values/values.xml", "r") as values_xml: |
| 83 | self.assertEqual("some values", values_xml.read()) |
| 84 | with open("out_dir/res/layouts/layout.xml", "r") as layout_xml: |
| 85 | self.assertEqual("some layout", layout_xml.read()) |
| 86 | |
ajmichael | 19044cf | 2018-01-16 14:18:01 -0800 | [diff] [blame] | 87 | def testNoAssets(self): |
| 88 | aar = zipfile.ZipFile(io.BytesIO(), "w") |
ajmichael | 56567bb | 2018-01-18 12:03:18 -0800 | [diff] [blame] | 89 | aar.writestr("assets/", "") |
ajmichael | 19044cf | 2018-01-16 14:18:01 -0800 | [diff] [blame] | 90 | os.makedirs("out_dir") |
| 91 | aar_resources_extractor.ExtractAssets(aar, "out_dir") |
| 92 | expected_assets = [ |
| 93 | _HostPath("out_dir/assets/empty_asset_generated_by_bazel~") |
| 94 | ] |
| 95 | self.assertEqual(expected_assets, self.DirContents("out_dir")) |
| 96 | self.assertEqual( |
| 97 | os.stat("out_dir/assets/empty_asset_generated_by_bazel~").st_size, 0) |
| 98 | |
| 99 | def testContainsAssets(self): |
| 100 | aar = zipfile.ZipFile(io.BytesIO(), "w") |
| 101 | aar.writestr("res/values/values.xml", "some values") |
| 102 | aar.writestr("assets/a", "some asset") |
| 103 | aar.writestr("assets/b", "some other asset") |
| 104 | os.makedirs("out_dir") |
| 105 | aar_resources_extractor.ExtractAssets(aar, "out_dir") |
| 106 | expected_resources = [ |
| 107 | _HostPath("out_dir/assets/a"), |
| 108 | _HostPath("out_dir/assets/b") |
| 109 | ] |
| 110 | self.assertCountEqual(expected_resources, self.DirContents("out_dir")) |
| 111 | with open("out_dir/assets/a", "r") as values_xml: |
| 112 | self.assertEqual("some asset", values_xml.read()) |
| 113 | with open("out_dir/assets/b", "r") as layout_xml: |
| 114 | self.assertEqual("some other asset", layout_xml.read()) |
| 115 | |
ahumesky | 850a864 | 2020-04-30 17:02:22 -0700 | [diff] [blame] | 116 | def testDatabinding(self): |
| 117 | aar = zipfile.ZipFile(io.BytesIO(), "w") |
| 118 | |
| 119 | br_filepath = ( |
| 120 | "data-binding/com.android.databinding.library.baseAdapters--br.bin") |
| 121 | setter_store_filepath = ( |
| 122 | "data-binding/" + |
| 123 | "com.android.databinding.library.baseAdapters--setter_store.json") |
| 124 | |
| 125 | aar.writestr(br_filepath, "br data") |
| 126 | aar.writestr(setter_store_filepath, "setter store data") |
| 127 | |
| 128 | os.makedirs("out_dir/br") |
| 129 | os.makedirs("out_dir/setter_store") |
| 130 | |
| 131 | aar_resources_extractor.ExtractDatabinding(aar, "br.bin", "out_dir/br") |
| 132 | aar_resources_extractor.ExtractDatabinding(aar, "setter_store.json", |
| 133 | "out_dir/setter_store") |
| 134 | |
| 135 | with open("out_dir/br/" + br_filepath, "r") as f: |
| 136 | self.assertEqual("br data", f.read()) |
| 137 | |
| 138 | with open("out_dir/setter_store/" + setter_store_filepath, "r") as f: |
| 139 | self.assertEqual("setter store data", f.read()) |
| 140 | |
ajmichael | 69ef625 | 2017-08-29 00:41:40 +0200 | [diff] [blame] | 141 | |
| 142 | if __name__ == "__main__": |
| 143 | unittest.main() |