From 555af99d6387218677f7c07da7a74bf2d99c7c4e Mon Sep 17 00:00:00 2001 From: fanby01 Date: Thu, 24 Feb 2022 12:59:46 +0800 Subject: [PATCH] snapshot_display: set default file name with varying sufffix(using current time). example: /data/snapshot_2022-02-24_13:01:59.png Change-Id: Idefdddf9fcf82504d04d0eeab2be6ffa38ca0c31 Signed-off-by: fanby01 --- dm/test/systemtest/BUILD.gn | 7 ++- dm/test/systemtest/screenshot_cmd_test.cpp | 50 ++++++++++++++++------ snapshot/snapshot_display.cpp | 2 +- snapshot/snapshot_utils.cpp | 32 ++++++++++++++ snapshot/snapshot_utils.h | 1 + 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/dm/test/systemtest/BUILD.gn b/dm/test/systemtest/BUILD.gn index 45afe26c..1e4fc883 100644 --- a/dm/test/systemtest/BUILD.gn +++ b/dm/test/systemtest/BUILD.gn @@ -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" ] } diff --git a/dm/test/systemtest/screenshot_cmd_test.cpp b/dm/test/systemtest/screenshot_cmd_test.cpp index 8031aa09..b4822e6d 100644 --- a/dm/test/systemtest/screenshot_cmd_test.cpp +++ b/dm/test/systemtest/screenshot_cmd_test.cpp @@ -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 } /** diff --git a/snapshot/snapshot_display.cpp b/snapshot/snapshot_display.cpp index f71010fd..a9504e5b 100644 --- a/snapshot/snapshot_display.cpp +++ b/snapshot/snapshot_display.cpp @@ -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; diff --git a/snapshot/snapshot_utils.cpp b/snapshot/snapshot_utils.cpp index af4f77ee..ca5a8fd3 100644 --- a/snapshot/snapshot_utils.cpp +++ b/snapshot/snapshot_utils.cpp @@ -16,7 +16,9 @@ #include "snapshot_utils.h" #include +#include #include +#include #include #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()); diff --git a/snapshot/snapshot_utils.h b/snapshot/snapshot_utils.h index 27d3b857..722e7ca7 100644 --- a/snapshot/snapshot_utils.h +++ b/snapshot/snapshot_utils.h @@ -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 ¶m); static bool WriteToPngWithPixelMap(const std::string &fileName, Media::PixelMap &pixelMap);