1e0dac50fSopenharmony_ci/* 2e0dac50fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License. 5e0dac50fSopenharmony_ci * You may obtain a copy of the License at 6e0dac50fSopenharmony_ci * 7e0dac50fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e0dac50fSopenharmony_ci * 9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and 13e0dac50fSopenharmony_ci * limitations under the License. 14e0dac50fSopenharmony_ci */ 15e0dac50fSopenharmony_ci 16e0dac50fSopenharmony_ci#include <cinttypes> 17e0dac50fSopenharmony_ci#include <cstdlib> 18e0dac50fSopenharmony_ci#include <gtest/gtest.h> 19e0dac50fSopenharmony_ci 20e0dac50fSopenharmony_ci#include "pixel_map.h" 21e0dac50fSopenharmony_ci 22e0dac50fSopenharmony_ci#include "snapshot_utils.h" 23e0dac50fSopenharmony_ci#include "common_test_utils.h" 24e0dac50fSopenharmony_ci#include "window_manager_hilog.h" 25e0dac50fSopenharmony_ci 26e0dac50fSopenharmony_ciusing namespace testing; 27e0dac50fSopenharmony_ciusing namespace testing::ext; 28e0dac50fSopenharmony_ci 29e0dac50fSopenharmony_cinamespace OHOS { 30e0dac50fSopenharmony_cinamespace Rosen { 31e0dac50fSopenharmony_cinamespace { 32e0dac50fSopenharmony_ciconstexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SnapshotDisplayTest"}; 33e0dac50fSopenharmony_ci} 34e0dac50fSopenharmony_ci 35e0dac50fSopenharmony_ciclass SnapshotDisplayTest : public testing::Test { 36e0dac50fSopenharmony_cipublic: 37e0dac50fSopenharmony_ci static void SetUpTestCase(); 38e0dac50fSopenharmony_ci static void TearDownTestCase(); 39e0dac50fSopenharmony_ci virtual void SetUp() override; 40e0dac50fSopenharmony_ci virtual void TearDown() override; 41e0dac50fSopenharmony_ci static DisplayId defaultId_; 42e0dac50fSopenharmony_ci DisplayId invalidId_ = DISPLAY_ID_INVALID; 43e0dac50fSopenharmony_ci const std::string defaultCmd_ = "/system/bin/snapshot_display"; 44e0dac50fSopenharmony_ci const int testTimeCount_ = 2; 45e0dac50fSopenharmony_ci}; 46e0dac50fSopenharmony_ci 47e0dac50fSopenharmony_ciDisplayId SnapshotDisplayTest::defaultId_ = DISPLAY_ID_INVALID; 48e0dac50fSopenharmony_ci 49e0dac50fSopenharmony_civoid SnapshotDisplayTest::SetUpTestCase() 50e0dac50fSopenharmony_ci{ 51e0dac50fSopenharmony_ci auto display = DisplayManager::GetInstance().GetDefaultDisplay(); 52e0dac50fSopenharmony_ci if (display == nullptr) { 53e0dac50fSopenharmony_ci WLOGFE("GetDefaultDisplay: failed!\n"); 54e0dac50fSopenharmony_ci return; 55e0dac50fSopenharmony_ci } 56e0dac50fSopenharmony_ci WLOGI("GetDefaultDisplay: id %" PRIu64", w %d, h %d, fps %u\n", display->GetId(), display->GetWidth(), 57e0dac50fSopenharmony_ci display->GetHeight(), display->GetRefreshRate()); 58e0dac50fSopenharmony_ci 59e0dac50fSopenharmony_ci defaultId_ = display->GetId(); 60e0dac50fSopenharmony_ci 61e0dac50fSopenharmony_ci CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0); 62e0dac50fSopenharmony_ci const char** perms = new const char *[1]; 63e0dac50fSopenharmony_ci perms[0] = "ohos.permission.CAPTURE_SCREEN"; 64e0dac50fSopenharmony_ci CommonTestUtils::SetAceessTokenPermission("DisplayManagerServiceTest", perms, 1); 65e0dac50fSopenharmony_ci} 66e0dac50fSopenharmony_ci 67e0dac50fSopenharmony_civoid SnapshotDisplayTest::TearDownTestCase() 68e0dac50fSopenharmony_ci{ 69e0dac50fSopenharmony_ci} 70e0dac50fSopenharmony_ci 71e0dac50fSopenharmony_civoid SnapshotDisplayTest::SetUp() 72e0dac50fSopenharmony_ci{ 73e0dac50fSopenharmony_ci} 74e0dac50fSopenharmony_ci 75e0dac50fSopenharmony_civoid SnapshotDisplayTest::TearDown() 76e0dac50fSopenharmony_ci{ 77e0dac50fSopenharmony_ci} 78e0dac50fSopenharmony_ci 79e0dac50fSopenharmony_cibool CheckFileExist(const std::string& fPath) 80e0dac50fSopenharmony_ci{ 81e0dac50fSopenharmony_ci if (!fPath.empty()) { 82e0dac50fSopenharmony_ci FILE* fp = fopen(fPath.c_str(), "r"); 83e0dac50fSopenharmony_ci if (fp != nullptr) { 84e0dac50fSopenharmony_ci fclose(fp); 85e0dac50fSopenharmony_ci return true; 86e0dac50fSopenharmony_ci } 87e0dac50fSopenharmony_ci } 88e0dac50fSopenharmony_ci return false; 89e0dac50fSopenharmony_ci} 90e0dac50fSopenharmony_ci 91e0dac50fSopenharmony_cibool TakeScreenshotBySpecifiedParam(std::string exec, std::string imgPath, std::string extraParam) 92e0dac50fSopenharmony_ci{ 93e0dac50fSopenharmony_ci if (CheckFileExist(imgPath)) { 94e0dac50fSopenharmony_ci remove(imgPath.c_str()); 95e0dac50fSopenharmony_ci } 96e0dac50fSopenharmony_ci const std::string cmd = exec + " -f " + imgPath + " " + extraParam; 97e0dac50fSopenharmony_ci (void)system(cmd.c_str()); 98e0dac50fSopenharmony_ci bool isExist = CheckFileExist(imgPath); 99e0dac50fSopenharmony_ci if (isExist) { 100e0dac50fSopenharmony_ci remove(imgPath.c_str()); 101e0dac50fSopenharmony_ci } 102e0dac50fSopenharmony_ci return isExist; 103e0dac50fSopenharmony_ci} 104e0dac50fSopenharmony_ci 105e0dac50fSopenharmony_cinamespace { 106e0dac50fSopenharmony_ci/** 107e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 108e0dac50fSopenharmony_ci * @tc.desc: Call screenshot default cmd and check if it saves image in default path 109e0dac50fSopenharmony_ci * @tc.type: FUNC 110e0dac50fSopenharmony_ci */ 111e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid01, Function | MediumTest | Level2) 112e0dac50fSopenharmony_ci{ 113e0dac50fSopenharmony_ci std::string imgPath[testTimeCount_]; 114e0dac50fSopenharmony_ci int i; 115e0dac50fSopenharmony_ci 116e0dac50fSopenharmony_ci for (i = 0; i < testTimeCount_; i++) { 117e0dac50fSopenharmony_ci imgPath[i] = SnapShotUtils::GenerateFileName("jpeg", i); 118e0dac50fSopenharmony_ci if (CheckFileExist(imgPath[i])) { 119e0dac50fSopenharmony_ci remove(imgPath[i].c_str()); 120e0dac50fSopenharmony_ci } 121e0dac50fSopenharmony_ci } 122e0dac50fSopenharmony_ci 123e0dac50fSopenharmony_ci (void)system(defaultCmd_.c_str()); 124e0dac50fSopenharmony_ci 125e0dac50fSopenharmony_ci for (i = 0; i < testTimeCount_; i++) { 126e0dac50fSopenharmony_ci if (CheckFileExist(imgPath[i])) { // ok 127e0dac50fSopenharmony_ci remove(imgPath[i].c_str()); 128e0dac50fSopenharmony_ci ASSERT_TRUE(true); 129e0dac50fSopenharmony_ci return; 130e0dac50fSopenharmony_ci } 131e0dac50fSopenharmony_ci } 132e0dac50fSopenharmony_ci ADD_FAILURE(); // fail, can't find snapshot file 133e0dac50fSopenharmony_ci} 134e0dac50fSopenharmony_ci 135e0dac50fSopenharmony_ci/** 136e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 137e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with default displayID and default path 138e0dac50fSopenharmony_ci * @tc.type: FUNC 139e0dac50fSopenharmony_ci */ 140e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid02, Function | MediumTest | Level2) 141e0dac50fSopenharmony_ci{ 142e0dac50fSopenharmony_ci std::string imgPath[testTimeCount_]; 143e0dac50fSopenharmony_ci int i; 144e0dac50fSopenharmony_ci 145e0dac50fSopenharmony_ci for (i = 0; i < testTimeCount_; i++) { 146e0dac50fSopenharmony_ci imgPath[i] = SnapShotUtils::GenerateFileName("jpeg", i); 147e0dac50fSopenharmony_ci if (CheckFileExist(imgPath[i])) { 148e0dac50fSopenharmony_ci remove(imgPath[i].c_str()); 149e0dac50fSopenharmony_ci } 150e0dac50fSopenharmony_ci } 151e0dac50fSopenharmony_ci 152e0dac50fSopenharmony_ci const std::string cmd = defaultCmd_ + " -i " + std::to_string(defaultId_); 153e0dac50fSopenharmony_ci (void)system(cmd.c_str()); 154e0dac50fSopenharmony_ci 155e0dac50fSopenharmony_ci for (i = 0; i < testTimeCount_; i++) { 156e0dac50fSopenharmony_ci if (CheckFileExist(imgPath[i])) { // ok 157e0dac50fSopenharmony_ci remove(imgPath[i].c_str()); 158e0dac50fSopenharmony_ci ASSERT_TRUE(true); 159e0dac50fSopenharmony_ci return; 160e0dac50fSopenharmony_ci } 161e0dac50fSopenharmony_ci } 162e0dac50fSopenharmony_ci ADD_FAILURE(); // fail, can't find snapshot file 163e0dac50fSopenharmony_ci} 164e0dac50fSopenharmony_ci 165e0dac50fSopenharmony_ci/** 166e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 167e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with default displayID and custom path 168e0dac50fSopenharmony_ci * @tc.type: FUNC 169e0dac50fSopenharmony_ci */ 170e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid03, Function | MediumTest | Level2) 171e0dac50fSopenharmony_ci{ 172e0dac50fSopenharmony_ci const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg"; 173e0dac50fSopenharmony_ci std::string extraParam = "-i " + std::to_string(defaultId_); 174e0dac50fSopenharmony_ci ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam)); 175e0dac50fSopenharmony_ci} 176e0dac50fSopenharmony_ci 177e0dac50fSopenharmony_ci/** 178e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 179e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with valid width/height 180e0dac50fSopenharmony_ci * @tc.type: FUNC 181e0dac50fSopenharmony_ci */ 182e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid04, Function | MediumTest | Level2) 183e0dac50fSopenharmony_ci{ 184e0dac50fSopenharmony_ci const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg"; 185e0dac50fSopenharmony_ci std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 100 -h 100"; 186e0dac50fSopenharmony_ci ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam)); 187e0dac50fSopenharmony_ci} 188e0dac50fSopenharmony_ci 189e0dac50fSopenharmony_ci/** 190e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 191e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with valid width 192e0dac50fSopenharmony_ci * @tc.type: FUNC 193e0dac50fSopenharmony_ci */ 194e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid05, Function | MediumTest | Level2) 195e0dac50fSopenharmony_ci{ 196e0dac50fSopenharmony_ci const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg"; 197e0dac50fSopenharmony_ci std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 100"; 198e0dac50fSopenharmony_ci ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam)); 199e0dac50fSopenharmony_ci} 200e0dac50fSopenharmony_ci 201e0dac50fSopenharmony_ci/** 202e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 203e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with valid height 204e0dac50fSopenharmony_ci * @tc.type: FUNC 205e0dac50fSopenharmony_ci */ 206e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid06, Function | MediumTest | Level2) 207e0dac50fSopenharmony_ci{ 208e0dac50fSopenharmony_ci const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg"; 209e0dac50fSopenharmony_ci std::string extraParam = "-i " + std::to_string(defaultId_) + " -h 100"; 210e0dac50fSopenharmony_ci ASSERT_EQ(true, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam)); 211e0dac50fSopenharmony_ci} 212e0dac50fSopenharmony_ci 213e0dac50fSopenharmony_ci/** 214e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 215e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with invalid width/height 216e0dac50fSopenharmony_ci * @tc.type: FUNC 217e0dac50fSopenharmony_ci */ 218e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid07, Function | MediumTest | Level2) 219e0dac50fSopenharmony_ci{ 220e0dac50fSopenharmony_ci const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg"; 221e0dac50fSopenharmony_ci std::string extraParam = "-i " + std::to_string(defaultId_) + " -w 10000 -h 10000"; 222e0dac50fSopenharmony_ci ASSERT_EQ(false, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam)); 223e0dac50fSopenharmony_ci} 224e0dac50fSopenharmony_ci 225e0dac50fSopenharmony_ci/** 226e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 227e0dac50fSopenharmony_ci * @tc.desc: Call screenshot with -m 228e0dac50fSopenharmony_ci * @tc.type: FUNC 229e0dac50fSopenharmony_ci */ 230e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid08, Function | MediumTest | Level2) 231e0dac50fSopenharmony_ci{ 232e0dac50fSopenharmony_ci const std::string imgPath = "/data/local/tmp/snapshot_display_test.jpeg"; 233e0dac50fSopenharmony_ci std::string extraParam = "-i " + std::to_string(defaultId_) + " -m"; 234e0dac50fSopenharmony_ci ASSERT_EQ(false, TakeScreenshotBySpecifiedParam(defaultCmd_, imgPath, extraParam)); 235e0dac50fSopenharmony_ci} 236e0dac50fSopenharmony_ci 237e0dac50fSopenharmony_ci/** 238e0dac50fSopenharmony_ci * @tc.name: ScreenShotCmdValid 239e0dac50fSopenharmony_ci * @tc.desc: screenshot png type 240e0dac50fSopenharmony_ci * @tc.type: FUNC 241e0dac50fSopenharmony_ci */ 242e0dac50fSopenharmony_ciHWTEST_F(SnapshotDisplayTest, ScreenShotCmdValid09, Function | MediumTest | Level2) 243e0dac50fSopenharmony_ci{ 244e0dac50fSopenharmony_ci std::string imgPath[testTimeCount_]; 245e0dac50fSopenharmony_ci int i; 246e0dac50fSopenharmony_ci 247e0dac50fSopenharmony_ci for (i = 0; i < testTimeCount_; i++) { 248e0dac50fSopenharmony_ci imgPath[i] = SnapShotUtils::GenerateFileName("png", i); 249e0dac50fSopenharmony_ci if (CheckFileExist(imgPath[i])) { 250e0dac50fSopenharmony_ci remove(imgPath[i].c_str()); 251e0dac50fSopenharmony_ci } 252e0dac50fSopenharmony_ci } 253e0dac50fSopenharmony_ci 254e0dac50fSopenharmony_ci const std::string cmd = defaultCmd_ + " -t png"; 255e0dac50fSopenharmony_ci (void)system(cmd.c_str()); 256e0dac50fSopenharmony_ci 257e0dac50fSopenharmony_ci for (i = 0; i < testTimeCount_; i++) { 258e0dac50fSopenharmony_ci if (CheckFileExist(imgPath[i])) { // ok 259e0dac50fSopenharmony_ci remove(imgPath[i].c_str()); 260e0dac50fSopenharmony_ci ASSERT_TRUE(true); 261e0dac50fSopenharmony_ci return; 262e0dac50fSopenharmony_ci } 263e0dac50fSopenharmony_ci } 264e0dac50fSopenharmony_ci ADD_FAILURE(); // fail, can't find snapshot file 265e0dac50fSopenharmony_ci} 266e0dac50fSopenharmony_ci} // namespace 267e0dac50fSopenharmony_ci} // namespace Rosen 268e0dac50fSopenharmony_ci} // namespace OHOS