Skip to content

Commit

Permalink
[screengrab] Add config to allow disabling timestamp suffix to filena…
Browse files Browse the repository at this point in the history
…me (fastlane#14781)

* Add config to allow disabling adding timestamp suffix to filename (issue fastlane#14309)

* Set default value and update tests

* use type: Boolean parameter instead of is_stirng

* add tests around use suffix value

* Fixed for androidx and a rebase

* Fixed tests from androidx rebase
  • Loading branch information
Hiroto-N authored and Josh Holtz committed Nov 12, 2019
1 parent 0e4b5f8 commit 1251a3d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions screengrab/lib/screengrab/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def self.available_options
env_name: 'SCREENGRAB_REINSTALL_APP',
description: "Enabling this option will automatically uninstall the application before running it",
default_value: false,
type: Boolean),
FastlaneCore::ConfigItem.new(key: :use_timestamp_suffix,
env_name: 'SCREENGRAB_USE_TIMESTAMP_SUFFIX',
description: "Add timestamp suffix to screenshot filename",
default_value: true,
type: Boolean)
]
end
Expand Down
1 change: 1 addition & 0 deletions screengrab/lib/screengrab/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def run_tests_for_locale(locale, device_serial, test_classes_to_use, test_packag
instrument_command = ["adb -s #{device_serial} shell am instrument --no-window-animation -w",
"-e testLocale #{locale.tr('-', '_')}",
"-e endingLocale #{@config[:ending_locale].tr('-', '_')}"]
instrument_command << "-e appendTimestamp #{@config[:use_timestamp_suffix]}"
instrument_command << "-e class #{test_classes_to_use.join(',')}" if test_classes_to_use
instrument_command << "-e package #{test_packages_to_use.join(',')}" if test_packages_to_use
instrument_command << launch_arguments.map { |item| '-e ' + item }.join(' ') if launch_arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import androidx.test.platform.app.InstrumentationRegistry;
import android.util.Log;

import java.io.BufferedOutputStream;
Expand All @@ -26,6 +26,7 @@ public class FileWritingScreenshotCallback implements ScreenshotCallback {
protected static final String EXTENSION = ".png";
private static final int FULL_QUALITY = 100;
private static final String SCREENGRAB_DIR_NAME = "screengrab";
private static final String APPEND_TIMESTAMP_CONFIG_KEY = "appendTimestamp";

private final Context appContext;

Expand Down Expand Up @@ -58,7 +59,9 @@ public void screenshotCaptured(String screenshotName, Bitmap screenshot) {
}

protected File getScreenshotFile(File screenshotDirectory, String screenshotName) {
String screenshotFileName = screenshotName + NAME_SEPARATOR + System.currentTimeMillis() + EXTENSION;
String screenshotFileName = screenshotName
+ (shouldAppendTimestamp() ? (NAME_SEPARATOR + System.currentTimeMillis()) : "")
+ EXTENSION;
return new File(screenshotDirectory, screenshotFileName);
}

Expand Down Expand Up @@ -124,4 +127,8 @@ private static void createPathTo(File dir) throws IOException {
}
Chmod.chmodPlusRWX(dir);
}

private static boolean shouldAppendTimestamp() {
return Boolean.parseBoolean(InstrumentationRegistry.getArguments().getString(APPEND_TIMESTAMP_CONFIG_KEY));
}
}
35 changes: 34 additions & 1 deletion screengrab/spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def mock_adb_response(mock_response)
config[:launch_arguments] = ["username hjanuschka", "build_type x500"]
config[:locales] = %w(en-US)
config[:ending_locale] = 'en-US'
config[:use_timestamp_suffix] = true
end
it 'sets custom launch_arguments' do
expect(mock_executor).to receive(:execute)
.with(hash_including(command: "adb -s device_serial shell am instrument --no-window-animation -w \\\n-e testLocale en_US \\\n-e endingLocale en_US \\\n-e username hjanuschka -e build_type x500 \\\n/"))
.with(hash_including(command: "adb -s device_serial shell am instrument --no-window-animation -w \\\n-e testLocale en_US \\\n-e endingLocale en_US \\\n-e appendTimestamp true \\\n-e username hjanuschka -e build_type x500 \\\n/"))
@runner.run_tests_for_locale('en-US', device_serial, test_classes_to_use, test_packages_to_use, config[:launch_arguments])
end
end
Expand Down Expand Up @@ -77,6 +78,38 @@ def mock_adb_response(mock_response)
end
end
end

context 'when using use_timestamp_suffix' do
context 'when set to false' do
before do
@runner = Screengrab::Runner.new(
mock_executor,
FastlaneCore::Configuration.create(Screengrab::Options.available_options, { use_timestamp_suffix: false }),
mock_android_environment
)
end
it 'sets appendTimestamp as false' do
expect(mock_executor).to receive(:execute)
.with(hash_including(command: "adb -s device_serial shell am instrument --no-window-animation -w \\\n-e testLocale en_US \\\n-e endingLocale en_US \\\n-e appendTimestamp false \\\n/androidx.test.runner.AndroidJUnitRunner"))
@runner.run_tests_for_locale('en-US', device_serial, test_classes_to_use, test_packages_to_use, nil)
end
end

context 'use_timestamp_suffix is not specified' do
before do
@runner = Screengrab::Runner.new(
mock_executor,
FastlaneCore::Configuration.create(Screengrab::Options.available_options, {}),
mock_android_environment
)
end
it 'should set appendTimestamp by default' do
expect(mock_executor).to receive(:execute)
.with(hash_including(command: "adb -s device_serial shell am instrument --no-window-animation -w \\\n-e testLocale en_US \\\n-e endingLocale en_US \\\n-e appendTimestamp true \\\n/androidx.test.runner.AndroidJUnitRunner"))
@runner.run_tests_for_locale('en-US', device_serial, test_classes_to_use, test_packages_to_use, nil)
end
end
end
end

describe :validate_apk do
Expand Down

0 comments on commit 1251a3d

Please sign in to comment.