blob: 4bb196ab71add7fcd83f4d2c93e6f0c4f5d99938 [file] [log] [blame]
laszlocsomord93a1462019-11-04 09:14:39 -08001# Lint as: python2, python3
ajmichael69ef6252017-08-29 00:41:40 +02002# 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
laszlocsomord93a1462019-11-04 09:14:39 -080018from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
Googler246ee562017-12-19 11:04:21 -080022import io
ajmichael69ef6252017-08-29 00:41:40 +020023import os
24import shutil
ajmichael69ef6252017-08-29 00:41:40 +020025import unittest
26import zipfile
27
laszlocsomord93a1462019-11-04 09:14:39 -080028# Do not edit this line. Copybara replaces it with PY2 migration helper.
29import six
30
ajmichael69ef6252017-08-29 00:41:40 +020031from tools.android import aar_resources_extractor
32
33
Laszlo Csomorc60bfdf2017-09-27 07:38:03 -040034def _HostPath(path):
35 return os.path.normpath(path)
36
37
ajmichael69ef6252017-08-29 00:41:40 +020038class AarResourcesExtractorTest(unittest.TestCase):
39 """Unit tests for aar_resources_extractor.py."""
40
Googler246ee562017-12-19 11:04:21 -080041 # Python 2 alias
42 if not hasattr(unittest.TestCase, "assertCountEqual"):
43
44 def assertCountEqual(self, *args):
45 return self.assertItemsEqual(*args)
46
ajmichael69ef6252017-08-29 00:41:40 +020047 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 Csomorc60bfdf2017-09-27 07:38:03 -040054 return [
laszlocsomord93a1462019-11-04 09:14:39 -080055 _HostPath(six.ensure_str(path) + "/" + six.ensure_str(f))
56 for (path, _, files) in os.walk(d)
57 for f in files
Laszlo Csomorc60bfdf2017-09-27 07:38:03 -040058 ]
ajmichael69ef6252017-08-29 00:41:40 +020059
60 def testNoResources(self):
Googler246ee562017-12-19 11:04:21 -080061 aar = zipfile.ZipFile(io.BytesIO(), "w")
ajmichael56567bb2018-01-18 12:03:18 -080062 aar.writestr("res/", "")
ajmichael69ef6252017-08-29 00:41:40 +020063 os.makedirs("out_dir")
64 aar_resources_extractor.ExtractResources(aar, "out_dir")
Laszlo Csomorc60bfdf2017-09-27 07:38:03 -040065 self.assertEqual([_HostPath("out_dir/res/values/empty.xml")],
ajmichael69ef6252017-08-29 00:41:40 +020066 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):
Googler246ee562017-12-19 11:04:21 -080071 aar = zipfile.ZipFile(io.BytesIO(), "w")
ajmichael69ef6252017-08-29 00:41:40 +020072 aar.writestr("res/values/values.xml", "some values")
73 aar.writestr("res/layouts/layout.xml", "some layout")
ajmichael19044cf2018-01-16 14:18:01 -080074 aar.writestr("assets/a", "some asset")
ajmichael69ef6252017-08-29 00:41:40 +020075 os.makedirs("out_dir")
76 aar_resources_extractor.ExtractResources(aar, "out_dir")
Laszlo Csomorc60bfdf2017-09-27 07:38:03 -040077 expected_resources = [
78 _HostPath("out_dir/res/values/values.xml"),
79 _HostPath("out_dir/res/layouts/layout.xml")
80 ]
Googler246ee562017-12-19 11:04:21 -080081 self.assertCountEqual(expected_resources, self.DirContents("out_dir"))
ajmichael69ef6252017-08-29 00:41:40 +020082 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
ajmichael19044cf2018-01-16 14:18:01 -080087 def testNoAssets(self):
88 aar = zipfile.ZipFile(io.BytesIO(), "w")
ajmichael56567bb2018-01-18 12:03:18 -080089 aar.writestr("assets/", "")
ajmichael19044cf2018-01-16 14:18:01 -080090 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
ahumesky850a8642020-04-30 17:02:22 -0700116 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
ajmichael69ef6252017-08-29 00:41:40 +0200141
142if __name__ == "__main__":
143 unittest.main()