!378 命令行截图功能修改默认生成文件名规则

Merge pull request !378 from YaoJian/snapshot_file_suffix
This commit is contained in:
openharmony_ci
2022-02-25 06:18:06 +00:00
committed by Gitee
5 changed files with 77 additions and 15 deletions
+6 -1
View File
@@ -75,7 +75,12 @@ ohos_systemtest("dm_screenshot_test") {
ohos_systemtest("dm_screenshot_cmd_test") {
module_out_path = module_out_path
sources = [ "screenshot_cmd_test.cpp" ]
include_dirs = [ "//foundation/windowmanager/snapshot" ]
sources = [
"//foundation/windowmanager/snapshot/snapshot_utils.cpp",
"screenshot_cmd_test.cpp",
]
deps = [ ":dm_systemtest_common" ]
}
+37 -13
View File
@@ -19,6 +19,8 @@
#include "display_test_utils.h"
#include "pixel_map.h"
#include "snapshot_utils.h"
using namespace testing;
using namespace testing::ext;
@@ -37,7 +39,7 @@ public:
static DisplayId defaultId_;
DisplayId invalidId_ = DISPLAY_ID_INVALD;
const std::string defaultCmd_ = "/system/bin/snapshot_display";
const std::string defaultImg_ = "/data/snapshot_display_1.png";
const int testTimeCount_ = 2;
};
DisplayId ScreenshotCmdTest::defaultId_ = DISPLAY_ID_INVALD;
@@ -87,15 +89,26 @@ namespace {
*/
HWTEST_F(ScreenshotCmdTest, ScreenShotCmdValid01, Function | MediumTest | Level2)
{
if (CheckFileExist(defaultImg_)) {
remove(defaultImg_.c_str());
std::string imgPath[testTimeCount_];
int i;
for (i = 0; i < testTimeCount_; i++) {
imgPath[i] = SnapShotUtils::GenerateFileName(i);
if (CheckFileExist(imgPath[i])) {
remove(imgPath[i].c_str());
}
}
(void)system(defaultCmd_.c_str());
bool isExist = CheckFileExist(defaultImg_);
if (isExist) {
remove(defaultImg_.c_str());
for (i = 0; i < testTimeCount_; i++) {
if (CheckFileExist(imgPath[i])) { // ok
remove(imgPath[i].c_str());
ASSERT_TRUE(true);
return;
}
}
ASSERT_EQ(true, isExist);
ADD_FAILURE(); // fail, can't find snapshot file
}
/**
@@ -105,16 +118,27 @@ HWTEST_F(ScreenshotCmdTest, ScreenShotCmdValid01, Function | MediumTest | Level2
*/
HWTEST_F(ScreenshotCmdTest, ScreenShotCmdValid02, Function | MediumTest | Level2)
{
if (CheckFileExist(defaultImg_)) {
remove(defaultImg_.c_str());
std::string imgPath[testTimeCount_];
int i;
for (i = 0; i < testTimeCount_; i++) {
imgPath[i] = SnapShotUtils::GenerateFileName(i);
if (CheckFileExist(imgPath[i])) {
remove(imgPath[i].c_str());
}
}
const std::string cmd = defaultCmd_ + " -i " + std::to_string(defaultId_);
(void)system(cmd.c_str());
bool isExist = CheckFileExist(defaultImg_);
if (isExist) {
remove(defaultImg_.c_str());
for (i = 0; i < testTimeCount_; i++) {
if (CheckFileExist(imgPath[i])) { // ok
remove(imgPath[i].c_str());
ASSERT_TRUE(true);
return;
}
}
ASSERT_EQ(true, isExist);
ADD_FAILURE(); // fail, can't find snapshot file
}
/**
+1 -1
View File
@@ -24,7 +24,7 @@ using namespace OHOS::Rosen;
int main(int argc, char *argv[])
{
CmdArgments cmdArgments;
cmdArgments.fileName = "/data/snapshot_display_1.png";
cmdArgments.fileName = "";
if (!SnapShotUtils::ProcessArgs(argc, argv, cmdArgments)) {
return 0;
+32
View File
@@ -16,7 +16,9 @@
#include "snapshot_utils.h"
#include <cstdio>
#include <sys/time.h>
#include <getopt.h>
#include <securec.h>
#include <png.h>
#include "wm_trace.h"
@@ -26,8 +28,11 @@ using namespace OHOS::Rosen;
namespace OHOS {
constexpr int BITMAP_DEPTH = 8;
constexpr int BPP = 4;
constexpr int MAX_TIME_STR_LEN = 40;
constexpr int YEAR_SINCE = 1900;
const char *VALID_SNAPSHOT_PATH = "/data";
const char *DEFAULT_SNAPSHOT_PREFIX = "/snapshot";
const char *VALID_SNAPSHOT_SUFFIX = ".png";
void SnapShotUtils::PrintUsage(const std::string &cmdLine)
@@ -35,6 +40,28 @@ void SnapShotUtils::PrintUsage(const std::string &cmdLine)
printf("usage: %s [-i displayId] [-f output_file] [-w width] [-h height] [-m]\n", cmdLine.c_str());
}
std::string SnapShotUtils::GenerateFileName(int offset)
{
timeval tv;
std::string fileName = VALID_SNAPSHOT_PATH;
char timeStr[MAX_TIME_STR_LEN] = { 0 };
fileName += DEFAULT_SNAPSHOT_PREFIX;
if (gettimeofday(&tv, nullptr) == 0) {
tv.tv_sec += offset; // add offset second
struct tm *tmVal = localtime(&tv.tv_sec);
if (tmVal != nullptr) {
snprintf_s(timeStr, sizeof(timeStr), sizeof(timeStr) - 1,
"_%04d-%02d-%02d_%02d-%02d-%02d",
tmVal->tm_year + YEAR_SINCE, tmVal->tm_mon + 1, tmVal->tm_mday,
tmVal->tm_hour, tmVal->tm_min, tmVal->tm_sec);
fileName += timeStr;
}
}
fileName += VALID_SNAPSHOT_SUFFIX;
return fileName;
}
bool SnapShotUtils::CheckFileNameValid(const std::string &fileName)
{
WM_SCOPED_TRACE("snapshot:CheckFileNameValid(%s)", fileName.c_str());
@@ -237,6 +264,11 @@ bool SnapShotUtils::ProcessArgs(int argc, char * const argv[], CmdArgments &cmdA
return false;
}
if (cmdArgments.fileName == "") {
cmdArgments.fileName = GenerateFileName();
printf("process: set filename to %s\n", cmdArgments.fileName.c_str());
}
// check fileName
if (!SnapShotUtils::CheckFileNameValid(cmdArgments.fileName)) {
printf("error: filename %s invalid!\n", cmdArgments.fileName.c_str());
+1
View File
@@ -47,6 +47,7 @@ public:
static void PrintUsage(const std::string &cmdLine);
static bool CheckFileNameValid(const std::string &fileName);
static std::string GenerateFileName(int offset = 0);
static bool CheckWidthAndHeightValid(const CmdArgments& cmdArgments);
static bool WriteToPng(const std::string &fileName, const WriteToPngParam &param);
static bool WriteToPngWithPixelMap(const std::string &fileName, Media::PixelMap &pixelMap);