Skip to content

Commit b27d95c

Browse files
committed
test: Reproduce the existing test cases and new test cases for aapt2
1 parent 71cb76d commit b27d95c

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

lib/android_apk.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
require_relative "android_apk/apksigner"
1010
require_relative "android_apk/app_icon"
1111
require_relative "android_apk/app_signature"
12-
require_relative "android_apk/configuration"
1312
require_relative "android_apk/error"
1413
require_relative "android_apk/resource_finder"
1514
require_relative "android_apk/signature_digest"
1615
require_relative "android_apk/signature_lineage_reader"
1716
require_relative "android_apk/signature_verifier"
1817
require_relative "android_apk/xmltree"
19-
require_relative "android_apk/aapt2/resource_finder"
18+
require_relative "android_apk/aapt2/dump_resources"
2019

2120
class AndroidApk
2221
FALLBACK_DPI = 65_534

lib/android_apk/aapt2/dump_resources.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
class AndroidApk
44
module Aapt2
55
class DumpResources
6+
def self.dump_resource_values(apk_filepath:)
7+
stdout, _, status = Open3.capture3("aapt2", "dump", "resources", apk_filepath)
8+
# we just need only drawables/mipmaps, and they are utf-8(ascii) friendly.
9+
stdout if status.success?
10+
end
11+
612
# @param apk_filepath [String] a path to apk_filepath
713
def initialize(apk_filepath:)
814
@apk_filepath = apk_filepath
9-
@dump_results = dump_resource_values(apk_filepath: apk_filepath)&.scrub&.split("\n")
15+
@dump_results = self.class.dump_resource_values(apk_filepath: apk_filepath)&.scrub&.split("\n")
1016
end
1117

1218
# @param default_icon_path [String] the path to the default icon in the apk
@@ -65,7 +71,7 @@ def collect_in_section(lines:, pivot_index:, &block)
6571

6672
break if dpi.nil? || path.nil? # unexpected.
6773

68-
dpi = dpi.empty? ? ::AndroidApk::DEFAULT_RESOURCE_CONFIG : dpi # reassign
74+
dpi = ::AndroidApk::DEFAULT_RESOURCE_CONFIG if dpi.empty? # reassign
6975

7076
block.call(dpi, path)
7177

@@ -78,12 +84,6 @@ def collect_in_section(lines:, pivot_index:, &block)
7884
end
7985
end
8086
end
81-
82-
private def dump_resource_values(apk_filepath:)
83-
stdout, _, status = Open3.capture3("aapt2", "dump", "resources", apk_filepath)
84-
# we just need only drawables/mipmaps, and they are utf-8(ascii) friendly.
85-
stdout if status.success?
86-
end
8787
end
8888
end
8989
end

spec/android_apk/resource_finder_spec.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@
3232
# emulate
3333
context "sample.apk includes non UTF-8" do
3434
let(:apk_filepath) { File.join(FIXTURE_DIR, "other", "sample.apk") }
35-
let(:aapt_output) do
36-
stdout = AndroidApk::Aapt::ResourceFinder.dump_resource_values(apk_filepath: apk_filepath)
37-
(+"#{stdout}\xFF").force_encoding("UTF-8")
38-
end
3935
let(:aapt2_output) do
40-
stdout = AndroidApk::Aapt2::ResourceFinder.dump_resource_values(apk_filepath: apk_filepath)
36+
stdout = AndroidApk::Aapt2::DumpResources.dump_resource_values(apk_filepath: apk_filepath)
4137
(+"#{stdout}\xFF").force_encoding("UTF-8")
4238
end
4339

4440
before do
45-
allow(AndroidApk::Aapt::ResourceFinder).to receive(:dump_resource_values).and_return(aapt_output) # inject
46-
allow(AndroidApk::Aapt2::ResourceFinder).to receive(:dump_resource_values).and_return(aapt2_output) # inject
41+
allow(AndroidApk::Aapt2::DumpResources).to receive(:dump_resource_values).and_return(aapt2_output) # inject
4742
end
4843

49-
it { expect { aapt_output.split('\n') }.to raise_error(ArgumentError, "invalid byte sequence in UTF-8") }
5044
it { expect { aapt2_output.split('\n') }.to raise_error(ArgumentError, "invalid byte sequence in UTF-8") }
5145

5246
it do

0 commit comments

Comments
 (0)