Skip to content

Commit 34865c2

Browse files
committed
Android tests: add cases with/out Universal APK
1 parent 1592f71 commit 34865c2

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,37 +3,53 @@
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
fake_apkanalyzer_path = './fake_apkanalyzer_bin'
1726
allow(described_class).to receive(:apkanalyzer_binary!).and_return(fake_apkanalyzer_path)
1827

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

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

@@ -50,6 +66,17 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
5066
end
5167

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

@@ -91,37 +114,90 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
91114
],
92115
metrics: [
93116
{ name: 'AAB File Size', value: 123_456 },
117+
{ name: 'Universal APK File Size', value: 56_789 },
94118
]
95119
}
96120

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

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

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

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

127203
test_app_size_action(
@@ -134,23 +210,48 @@ def test_app_size_action(fake_aab_size:, fake_apks:, expected_payload:, **other_
134210
'base-armeabi_v7a.apk': [150_070, 50_070],
135211
'base-armeabi_v7a_2.apk': [150_072, 50_072]
136212
},
213+
fake_universal_apk_sizes: [],
137214
expected_payload: expected,
138-
app_name: 'wordpress',
139-
app_version_name: '19.8-rc-3',
140-
app_version_code: 1214,
141-
product_flavor: 'Vanilla',
142-
build_type: 'Release',
143-
source: 'unit-test'
215+
**common_action_args
144216
)
145217
end
146218
end
147219

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

152235
context 'when providing both an `aab_path` and an `universal_apk_path`' do
153-
it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits'
236+
it 'generates the expected payload containing the aab and universal apk file size and optimized file and download sizes for all splits' do
237+
expected_fixture = File.join(test_data_dir, 'android-metrics-payload-aab+apk.json')
238+
expected = JSON.parse(File.read(expected_fixture))
239+
240+
test_app_size_action(
241+
fake_aab_size: 987_654_321,
242+
fake_apks: {
243+
'base-arm64_v8a.apk': [164_080, 64_080],
244+
'base-arm64_v8a_2.apk': [164_082, 64_082],
245+
'base-armeabi.apk': [150_000, 50_000],
246+
'base-armeabi_2.apk': [150_002, 50_002],
247+
'base-armeabi_v7a.apk': [150_070, 50_070],
248+
'base-armeabi_v7a_2.apk': [150_072, 50_072]
249+
},
250+
fake_universal_apk_sizes: [567_654_321, 555_000, 533_000],
251+
expected_payload: expected,
252+
**common_action_args
253+
)
254+
end
154255
end
155256
end
156257
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)