1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "ResultProcessorTest"
18 #include <log/log.h>
19
20 #include <gtest/gtest.h>
21 #include <memory>
22
23 #include "basic_result_processor.h"
24
25 namespace android {
26 namespace google_camera_hal {
27
28 static constexpr native_handle kDummyNativeHandle = {};
29 static constexpr buffer_handle_t kDummyBufferHandle = &kDummyNativeHandle;
30
31 using ResultProcessorCreateFunc =
32 std::function<std::unique_ptr<ResultProcessor>()>;
33
CreateBasicResultProcessor()34 std::unique_ptr<ResultProcessor> CreateBasicResultProcessor() {
35 return BasicResultProcessor::Create();
36 }
37
38 // All result processor implementation classes should be added to this
39 // vector.
40 std::vector<ResultProcessorCreateFunc> g_result_processor_create_funcs = {
__anon30af87220102() 41 []() { return CreateBasicResultProcessor(); },
42 };
43
TEST(ResultProcessorTest,Create)44 TEST(ResultProcessorTest, Create) {
45 for (auto& create_func : g_result_processor_create_funcs) {
46 auto result_processor = create_func();
47 ASSERT_NE(result_processor, nullptr)
48 << "Creating a result processor failed";
49 }
50 }
51
TEST(ResultProcessorTest,SetResultCallback)52 TEST(ResultProcessorTest, SetResultCallback) {
53 ProcessCaptureResultFunc process_capture_result =
54 ProcessCaptureResultFunc([](std::unique_ptr<CaptureResult> /*result*/) {});
55 NotifyFunc notify = NotifyFunc([](const NotifyMessage& /*message*/) {});
56
57 for (auto& create_func : g_result_processor_create_funcs) {
58 auto result_processor = create_func();
59 ASSERT_NE(result_processor, nullptr)
60 << "Creating a result processor failed";
61
62 result_processor->SetResultCallback(process_capture_result, notify);
63 }
64 }
65
SendResultsAndMessages(ResultProcessor * result_processor)66 void SendResultsAndMessages(ResultProcessor* result_processor) {
67 ProcessBlockNotifyMessage shutter_message = {
68 .message = {.type = MessageType::kShutter}};
69
70 ProcessBlockNotifyMessage error_message = {
71 .message = {.type = MessageType::kError}};
72
73 // Verify it can handle various results.
74 ProcessBlockResult null_result;
75 null_result.result = nullptr;
76 result_processor->ProcessResult(std::move(null_result));
77
78 ProcessBlockResult empty_result;
79 empty_result.result = std::make_unique<CaptureResult>();
80 result_processor->ProcessResult(std::move(empty_result));
81
82 // Verify it can handle messages.
83 result_processor->Notify(shutter_message);
84 result_processor->Notify(error_message);
85 }
86
TEST(ResultProcessorTest,AddPendingRequests)87 TEST(ResultProcessorTest, AddPendingRequests) {
88 ProcessCaptureResultFunc process_capture_result =
89 ProcessCaptureResultFunc([](std::unique_ptr<CaptureResult> /*result*/) {});
90 NotifyFunc notify = NotifyFunc([](const NotifyMessage& /*message*/) {});
91
92 for (auto& create_func : g_result_processor_create_funcs) {
93 auto result_processor = create_func();
94 ASSERT_NE(result_processor, nullptr)
95 << "Creating a result processor failed";
96
97 std::vector<ProcessBlockRequest> requests(1);
98 requests[0].request.output_buffers = {StreamBuffer{}};
99
100 EXPECT_EQ(
101 result_processor->AddPendingRequests(requests, requests[0].request), OK);
102 }
103 }
104
TEST(ResultProcessorTest,ProcessResultAndNotify)105 TEST(ResultProcessorTest, ProcessResultAndNotify) {
106 ProcessCaptureResultFunc process_capture_result =
107 ProcessCaptureResultFunc([](std::unique_ptr<CaptureResult> /*result*/) {});
108 NotifyFunc notify = NotifyFunc([](const NotifyMessage& /*message*/) {});
109
110 for (auto& create_func : g_result_processor_create_funcs) {
111 auto result_processor = create_func();
112 ASSERT_NE(result_processor, nullptr)
113 << "Creating a result processor failed";
114
115 // Test sending results and messages before setting result callback.
116 SendResultsAndMessages(result_processor.get());
117
118 // Test again after setting result callback.
119 result_processor->SetResultCallback(process_capture_result, notify);
120 SendResultsAndMessages(result_processor.get());
121 }
122 }
123
TEST(ResultProcessorTest,BasicResultProcessorResultAndNotify)124 TEST(ResultProcessorTest, BasicResultProcessorResultAndNotify) {
125 auto result_processor = CreateBasicResultProcessor();
126 bool result_received = false;
127 bool message_received = false;
128
129 ProcessCaptureResultFunc process_capture_result =
130 ProcessCaptureResultFunc([&](std::unique_ptr<CaptureResult> result) {
131 EXPECT_NE(result, nullptr);
132 result_received = true;
133 });
134
135 NotifyFunc notify = NotifyFunc(
136 [&](const NotifyMessage& /*message*/) { message_received = true; });
137
138 result_processor->SetResultCallback(process_capture_result, notify);
139
140 ProcessBlockResult null_result;
141 result_processor->ProcessResult(std::move(null_result));
142 EXPECT_EQ(result_received, false);
143 EXPECT_EQ(message_received, false);
144
145 result_received = false;
146 message_received = false;
147 ProcessBlockResult empty_result = {.result =
148 std::make_unique<CaptureResult>()};
149 result_processor->ProcessResult(std::move(empty_result));
150 EXPECT_EQ(result_received, true);
151 EXPECT_EQ(message_received, false);
152
153 result_received = false;
154 message_received = false;
155 result_processor->Notify(ProcessBlockNotifyMessage{});
156 EXPECT_EQ(result_received, false);
157 EXPECT_EQ(message_received, true);
158 }
159
TEST(ResultProcessorTest,BasicResultProcessorAddPendingRequest)160 TEST(ResultProcessorTest, BasicResultProcessorAddPendingRequest) {
161 auto result_processor = CreateBasicResultProcessor();
162
163 ProcessCaptureResultFunc process_capture_result = ProcessCaptureResultFunc(
164 [&](std::unique_ptr<CaptureResult> /*result*/) {});
165
166 NotifyFunc notify = NotifyFunc([&](const NotifyMessage& /*message*/) {});
167
168 result_processor->SetResultCallback(process_capture_result, notify);
169
170 std::vector<ProcessBlockRequest> requests(1);
171 requests[0].request.output_buffers = {StreamBuffer{}};
172
173 CaptureRequest remaining_request;
174 remaining_request.output_buffers.push_back({.buffer = kDummyBufferHandle});
175 EXPECT_NE(result_processor->AddPendingRequests(requests, remaining_request), OK)
176 << "Adding a pending request with a remaining output buffer that's not"
177 << "included in the request should fail.";
178 }
179
180 } // namespace google_camera_hal
181 } // namespace android
182