Skip to content

Commit 071f985

Browse files
committed
Android tests: add cases with/out Universal APK
1 parent 5a40636 commit 071f985

File tree

4 files changed

+183
-37
lines changed

4 files changed

+183
-37
lines changed

spec/android_send_app_size_metrics_spec.rb

Lines changed: 138 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,54 @@
33
describe Fastlane::Actions::AndroidSendAppSizeMetricsAction do
44
let(:test_data_dir) { File.join(File.dirname(__FILE__), 'test-data', 'app_size_metrics') }
55

6-
def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_action_args)
6+
def test_app_size_action(fake_aab_size:, fake_apks:, fake_universal_apk_sizes:, expected_payload:, **other_action_args)
77
in_tmp_dir do |tmp_dir|
88
# Arrange
99
output_file = File.join(tmp_dir, 'output-payload')
10-
aab_path = File.join(tmp_dir, 'fake.aab')
11-
File.write(aab_path, '-fake-aab-file-')
12-
allow(File).to receive(:size).with(aab_path).and_return(fake_aab_size)
10+
aab_path = nil
11+
unless fake_aab_size.nil?
12+
aab_path = File.join(tmp_dir, 'fake.aab')
13+
File.write(aab_path, '-fake-aab-file-')
14+
allow(File).to receive(:size).with(aab_path).and_return(fake_aab_size)
15+
end
16+
universal_apk_path = nil
17+
unless fake_universal_apk_sizes.empty?
18+
universal_apk_path = File.join(tmp_dir, 'fake.apk')
19+
File.write(universal_apk_path, '-fake-universal-apk-file-')
20+
allow(File).to receive(:size).with(universal_apk_path).and_return(fake_universal_apk_sizes[0])
21+
end
1322

1423
if other_action_args[:include_split_sizes] != false
1524
# Arrange: fake that apkanalyzer exists
1625
apkanalyzer_bin = File.join('__ANDROID_SDK_ROOT__FOR_TESTS__', 'cmdline-tools', 'latest', 'bin', 'apkanalyzer')
1726
allow(described_class).to receive(:find_apkanalyzer_binary).and_return(apkanalyzer_bin)
1827
allow(File).to receive(:executable?).with(apkanalyzer_bin).and_return(true)
1928

20-
# Arrange: fake that bundletool exists and mock its call to create fake apks with corresponding apkanalyzer calls mocks
21-
allow(Fastlane::Action).to receive(:sh).with('command', '-v', 'bundletool', anything)
22-
allow(Fastlane::Action).to receive(:sh).with('bundletool', 'build-apks', '--bundle', aab_path, '--output-format', 'DIRECTORY', '--output', anything) do |*args|
23-
bundletool_tmpdir = args.last
24-
FileUtils.mkdir(File.join(bundletool_tmpdir, 'splits'))
25-
fake_apks.each do |apk_name, sizes|
26-
apk_path = File.join(bundletool_tmpdir, 'splits', apk_name.to_s)
27-
File.write(apk_path, "Fake APK file (#{sizes})")
28-
allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'file-size', apk_path, anything).and_return(sizes[0].to_s)
29-
allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'download-size', apk_path, anything).and_return(sizes[1].to_s)
29+
unless fake_apks.empty?
30+
# Arrange: fake that `bundletool` exists and mock its call to create fake apks with corresponding apkanalyzer calls mocks
31+
allow(Fastlane::Action).to receive(:sh).with('command', '-v', 'bundletool', anything)
32+
allow(Fastlane::Action).to receive(:sh).with('bundletool', 'build-apks', '--bundle', aab_path, '--output-format', 'DIRECTORY', '--output', anything) do |*args|
33+
bundletool_tmpdir = args.last
34+
FileUtils.mkdir(File.join(bundletool_tmpdir, 'splits'))
35+
fake_apks.each do |apk_name, sizes|
36+
apk_path = File.join(bundletool_tmpdir, 'splits', apk_name.to_s)
37+
File.write(apk_path, "Fake APK file (#{sizes})")
38+
allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'file-size', apk_path, anything).and_return(sizes[0].to_s)
39+
allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'download-size', apk_path, anything).and_return(sizes[1].to_s)
40+
end
3041
end
3142
end
43+
unless fake_universal_apk_sizes.empty?
44+
allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'file-size', universal_apk_path, anything).and_return(fake_universal_apk_sizes[1].to_s)
45+
allow(Fastlane::Action).to receive(:sh).with(apkanalyzer_bin, 'apk', 'download-size', universal_apk_path, anything).and_return(fake_universal_apk_sizes[2].to_s)
46+
end
3247
end
3348

3449
# Act
3550
code = run_described_fastlane_action(
3651
api_url: File.join('file://localhost/', output_file),
3752
aab_path: aab_path,
53+
universal_apk_path: universal_apk_path,
3854
**other_action_args
3955
)
4056

@@ -51,6 +67,17 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
5167
end
5268

5369
context 'when `include_split_sizes` is turned off' do
70+
let(:common_action_args) do
71+
{
72+
app_name: 'my-app',
73+
app_version_name: '10.2-rc-3',
74+
product_flavor: 'Vanilla',
75+
build_type: 'Release',
76+
source: 'unit-test',
77+
include_split_sizes: false
78+
}
79+
end
80+
5481
context 'when only providing an `aab_path`' do
5582
it 'generates the expected payload compressed by default' do
5683
expected = {
@@ -70,13 +97,9 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
7097
test_app_size_action(
7198
fake_aab_size: 123_456,
7299
fake_apks: {},
100+
fake_universal_apk_sizes: [],
73101
expected_payload: expected,
74-
app_name: 'my-app',
75-
app_version_name: '10.2-rc-3',
76-
product_flavor: 'Vanilla',
77-
build_type: 'Release',
78-
source: 'unit-test',
79-
include_split_sizes: false
102+
**common_action_args
80103
)
81104
end
82105

@@ -92,37 +115,90 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
92115
],
93116
metrics: [
94117
{ name: 'AAB File Size', value: 123_456 },
118+
{ name: 'Universal APK File Size', value: 56_789 },
95119
]
96120
}
97121

98122
test_app_size_action(
99123
fake_aab_size: 123_456,
100124
fake_apks: {},
125+
fake_universal_apk_sizes: [56_789],
101126
expected_payload: expected,
102-
app_name: 'my-app',
103-
app_version_name: '10.2-rc-3',
104-
product_flavor: 'Vanilla',
105-
build_type: 'Release',
106-
source: 'unit-test',
107-
include_split_sizes: false,
127+
**common_action_args,
108128
use_gzip_content_encoding: false
109129
)
110130
end
111131
end
112132

113133
context 'when only providing an `universal_apk_path`' do
114-
it 'generates the expected payload containing the apk file size'
134+
it 'generates the expected payload containing the apk file size' do
135+
expected = {
136+
meta: [
137+
{ name: 'Platform', value: 'Android' },
138+
{ name: 'App Name', value: 'my-app' },
139+
{ name: 'App Version', value: '10.2-rc-3' },
140+
{ name: 'Product Flavor', value: 'Vanilla' },
141+
{ name: 'Build Type', value: 'Release' },
142+
{ name: 'Source', value: 'unit-test' },
143+
],
144+
metrics: [
145+
{ name: 'Universal APK File Size', value: 567_654_321 },
146+
]
147+
}
148+
149+
test_app_size_action(
150+
fake_aab_size: nil,
151+
fake_apks: {},
152+
fake_universal_apk_sizes: [567_654_321],
153+
expected_payload: expected,
154+
**common_action_args
155+
)
156+
end
115157
end
116158

117159
context 'when providing both an `aab_path` and an `universal_apk_path`' do
118-
it 'generates the expected payload containing the aab and universal apk file size'
160+
it 'generates the expected payload containing the aab and universal apk file size' do
161+
expected = {
162+
meta: [
163+
{ name: 'Platform', value: 'Android' },
164+
{ name: 'App Name', value: 'my-app' },
165+
{ name: 'App Version', value: '10.2-rc-3' },
166+
{ name: 'Product Flavor', value: 'Vanilla' },
167+
{ name: 'Build Type', value: 'Release' },
168+
{ name: 'Source', value: 'unit-test' },
169+
],
170+
metrics: [
171+
{ name: 'AAB File Size', value: 123_456 },
172+
{ name: 'Universal APK File Size', value: 567_654_321 },
173+
]
174+
}
175+
176+
test_app_size_action(
177+
fake_aab_size: 123_456,
178+
fake_apks: {},
179+
fake_universal_apk_sizes: [567_654_321],
180+
expected_payload: expected,
181+
**common_action_args
182+
)
183+
end
119184
end
120185
end
121186

122187
context 'when keeping the default value of `include_split_sizes` turned on' do
188+
let(:common_action_args) do
189+
{
190+
app_name: 'wordpress',
191+
app_version_name: '19.8-rc-3',
192+
app_version_code: 1214,
193+
product_flavor: 'Vanilla',
194+
build_type: 'Release',
195+
source: 'unit-test'
196+
}
197+
end
198+
123199
context 'when only providing an `aab_path`' do
124200
it 'generates the expected payload containing the aab file size and optimized split sizes' do
125-
expected_fixture = File.join(test_data_dir, 'android-metrics-payload.json')
201+
expected_fixture = File.join(test_data_dir, 'android-metrics-payload-aab.json')
126202
expected = JSON.parse(File.read(expected_fixture))
127203

128204
test_app_size_action(
@@ -135,23 +211,48 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
135211
'base-armeabi_v7a.apk': [150_070, 50_070],
136212
'base-armeabi_v7a_2.apk': [150_072, 50_072]
137213
},
214+
fake_universal_apk_sizes: [],
138215
expected_payload: expected,
139-
app_name: 'wordpress',
140-
app_version_name: '19.8-rc-3',
141-
app_version_code: 1214,
142-
product_flavor: 'Vanilla',
143-
build_type: 'Release',
144-
source: 'unit-test'
216+
**common_action_args
145217
)
146218
end
147219
end
148220

149221
context 'when only providing an `universal_apk_path`' do
150-
it 'generates the expected payload containing the apk file size and optimized file and download sizes'
222+
it 'generates the expected payload containing the apk file size and optimized file and download sizes' do
223+
expected_fixture = File.join(test_data_dir, 'android-metrics-payload-apk.json')
224+
expected = JSON.parse(File.read(expected_fixture))
225+
226+
test_app_size_action(
227+
fake_aab_size: nil,
228+
fake_apks: {},
229+
fake_universal_apk_sizes: [567_654_321, 555_000, 533_000],
230+
expected_payload: expected,
231+
**common_action_args
232+
)
233+
end
151234
end
152235

153236
context 'when providing both an `aab_path` and an `universal_apk_path`' do
154-
it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits'
237+
it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits' do
238+
expected_fixture = File.join(test_data_dir, 'android-metrics-payload-aab+apk.json')
239+
expected = JSON.parse(File.read(expected_fixture))
240+
241+
test_app_size_action(
242+
fake_aab_size: 987_654_321,
243+
fake_apks: {
244+
'base-arm64_v8a.apk': [164_080, 64_080],
245+
'base-arm64_v8a_2.apk': [164_082, 64_082],
246+
'base-armeabi.apk': [150_000, 50_000],
247+
'base-armeabi_2.apk': [150_002, 50_002],
248+
'base-armeabi_v7a.apk': [150_070, 50_070],
249+
'base-armeabi_v7a_2.apk': [150_072, 50_072]
250+
},
251+
fake_universal_apk_sizes: [567_654_321, 555_000, 533_000],
252+
expected_payload: expected,
253+
**common_action_args
254+
)
255+
end
155256
end
156257
end
157258
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"meta": [
3+
{ "name": "Platform", "value": "Android" },
4+
{ "name": "App Name", "value": "wordpress" },
5+
{ "name": "App Version", "value": "19.8-rc-3" },
6+
{ "name": "Version Code", "value": 1214 },
7+
{ "name": "Product Flavor", "value": "Vanilla" },
8+
{ "name": "Build Type", "value": "Release" },
9+
{ "name": "Source", "value": "unit-test" }
10+
],
11+
"metrics": [
12+
{ "name": "AAB File Size", "value": 987654321 },
13+
{ "name": "Universal APK File Size", "value": 567654321 },
14+
{ "name": "Optimized APK File Size", "value": 164082, "meta": [{ "name": "split", "value": "base-arm64_v8a_2" }] },
15+
{ "name": "Download Size", "value": 64082, "meta": [{ "name": "split", "value": "base-arm64_v8a_2" }] },
16+
{ "name": "Optimized APK File Size", "value": 150072, "meta": [{ "name": "split", "value": "base-armeabi_v7a_2" }] },
17+
{ "name": "Download Size", "value": 50072, "meta": [{ "name": "split", "value": "base-armeabi_v7a_2" }] },
18+
{ "name": "Optimized APK File Size", "value": 150002, "meta": [{ "name": "split", "value": "base-armeabi_2" }] },
19+
{ "name": "Download Size", "value": 50002, "meta": [{ "name": "split", "value": "base-armeabi_2" }] },
20+
{ "name": "Optimized APK File Size", "value": 164080, "meta": [{ "name": "split", "value": "base-arm64_v8a" }] },
21+
{ "name": "Download Size", "value": 64080, "meta": [{ "name": "split", "value": "base-arm64_v8a" }] },
22+
{ "name": "Optimized APK File Size", "value": 150000, "meta": [{ "name": "split", "value": "base-armeabi" }] },
23+
{ "name": "Download Size", "value": 50000, "meta": [{ "name": "split", "value": "base-armeabi" }] },
24+
{ "name": "Optimized APK File Size", "value": 150070, "meta": [{ "name": "split", "value": "base-armeabi_v7a" }] },
25+
{ "name": "Download Size", "value": 50070, "meta": [{ "name": "split", "value": "base-armeabi_v7a" }] },
26+
{ "name": "Optimized APK File Size", "value": 555000, "meta": [{ "name": "split", "value": "Universal" }] },
27+
{ "name": "Download Size", "value": 533000, "meta": [{ "name": "split", "value": "Universal" }] }
28+
]
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"meta": [
3+
{ "name": "Platform", "value": "Android" },
4+
{ "name": "App Name", "value": "wordpress" },
5+
{ "name": "App Version", "value": "19.8-rc-3" },
6+
{ "name": "Version Code", "value": 1214 },
7+
{ "name": "Product Flavor", "value": "Vanilla" },
8+
{ "name": "Build Type", "value": "Release" },
9+
{ "name": "Source", "value": "unit-test" }
10+
],
11+
"metrics": [
12+
{ "name": "Universal APK File Size", "value": 567654321 },
13+
{ "name": "Optimized APK File Size", "value": 555000, "meta": [{ "name": "split", "value": "Universal" }] },
14+
{ "name": "Download Size", "value": 533000, "meta": [{ "name": "split", "value": "Universal" }] }
15+
]
16+
}

0 commit comments

Comments
 (0)