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