1 #!/usr/bin/env vpython3
2 # Copyright 2020 The Chromium Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
19 "merkle": "77e876447dd2daaaab7048d646e87fe8b6d9fecef6cbfcc4af30b8fbfa50b881",
20 "path": "locales/ta.pak",
26 "merkle": "5f1932b8c9fe954f3c3fdb34ab2089d2af34e5a0cef90cad41a1cd37d92234bf",
27 "path": "lib/libEGL.so",
33 "merkle": "9822fc0dd95cdd1cc46b5c6632a928a6ad19b76ed0157397d82a2f908946fc34",
40 "merkle": "090aed4593c4f7d04a3ad80e9971c0532dd5b1d2bdf4754202cde510a88fd220",
41 "path": "locales/ru.pak",
51 class TestBinarySizes(unittest.TestCase):
56 cls.tmpdir = tempfile.mkdtemp()
59 def tearDownClass(cls):
60 shutil.rmtree(cls.tmpdir)
63 def testReadAndWritePackageBlobs(self):
64 # TODO(1309977): Disabled on Windows because Windows doesn't allow opening a
65 # NamedTemporaryFile by name.
68 with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
69 tmp_file.write(_EXAMPLE_BLOBS)
72 package_blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
74 tmp_package_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
75 tmp_package_file.close()
77 binary_sizes.WritePackageBlobsJson(tmp_package_file.name, package_blobs)
79 self.assertEqual(binary_sizes.ReadPackageBlobsJson(tmp_package_file.name),
82 os.remove(tmp_package_file.name)
84 def testReadAndWritePackageSizes(self):
85 # TODO(1309977): Disabled on Windows because Windows doesn't allow opening a
86 # NamedTemporaryFile by name.
89 with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
90 tmp_file.write(_EXAMPLE_BLOBS)
92 blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
94 sizes = binary_sizes.GetPackageSizes(blobs)
97 with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
98 binary_sizes.WritePackageSizesJson(tmp_file.name, sizes)
99 new_sizes = binary_sizes.ReadPackageSizesJson(tmp_file.name)
100 self.assertEqual(new_sizes, sizes)
101 self.assertIn('web_engine', new_sizes)
103 def testGetPackageSizesUsesBlobMerklesForCount(self):
104 # TODO(1309977): Disabled on Windows because Windows doesn't allow opening a
105 # NamedTemporaryFile by name.
108 blobs = json.loads(_EXAMPLE_BLOBS)
110 # Make a duplicate of the last blob.
111 last_blob = dict(blobs['web_engine'][-1])
112 blobs['cast_runner'] = []
113 last_blob['path'] = 'foo' # Give a non-sense name, but keep merkle.
115 # If the merkle is the same, the blob_count increases by 1.
116 # This effectively reduces the size of the blobs size by half.
117 # In both packages, despite it appearing in both and under different
119 blobs['cast_runner'].append(last_blob)
121 with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
122 tmp_file.write(json.dumps(blobs))
124 blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
126 sizes = binary_sizes.GetPackageSizes(blobs)
128 self.assertEqual(sizes['cast_runner'].compressed, last_blob['size'] / 2)
131 if __name__ == '__main__':