1 // Copyright (C) 2025 The Android Open Source Project
2 //
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 #include <binder/ProcessState.h>
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <gui/SurfaceComposerClient.h>
19
20 #include "android/gui/CaptureArgs.h"
21 #include "gmock/gmock.h"
22 #include "gui/ScreenCaptureResults.h"
23 #include "screencap_utils.h"
24 #include "ui/DisplayId.h"
25
26 using ::android::DisplayId;
27 using ::android::OK;
28 using ::android::PhysicalDisplayId;
29 using ::android::ProcessState;
30 using ::android::SurfaceComposerClient;
31 using ::android::gui::CaptureArgs;
32 using ::android::gui::ScreenCaptureResults;
33 using ::testing::AllOf;
34 using ::testing::HasSubstr;
35
36 class ScreenCapTest : public ::testing::Test {
37 protected:
SetUpTestSuite()38 static void SetUpTestSuite() {
39 // These lines are copied from screencap.cpp. They are necessary to call binder.
40 ProcessState::self()->setThreadPoolMaxThreadCount(0);
41 ProcessState::self()->startThreadPool();
42 }
43 };
44
TEST_F(ScreenCapTest,Capture_InvalidDisplayNumber)45 TEST_F(ScreenCapTest, Capture_InvalidDisplayNumber) {
46 DisplayId display;
47 display.value = -1;
48
49 CaptureArgs args;
50 auto result = ::android::screencap::capture(display, args);
51 EXPECT_FALSE(result.ok());
52 EXPECT_THAT(result.error().message(),
53 AllOf(HasSubstr("Display Id"), HasSubstr("is not valid.")));
54 }
55
TEST_F(ScreenCapTest,Capture_SuccessWithPhysicalDisplay)56 TEST_F(ScreenCapTest, Capture_SuccessWithPhysicalDisplay) {
57 const std::vector<PhysicalDisplayId> physicalDisplays =
58 SurfaceComposerClient::getPhysicalDisplayIds();
59
60 ASSERT_FALSE(physicalDisplays.empty());
61 DisplayId display;
62 display.value = physicalDisplays.front().value;
63
64 CaptureArgs args;
65 auto result = ::android::screencap::capture(display, args);
66 EXPECT_TRUE(result.ok());
67 // TODO consider verifying actual captured image.
68 }