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 <gtest/gtest.h>
19 #include <log/log.h>
20 
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(
63         process_capture_result, notify,
64         /*process_batch_capture_result=*/nullptr);
65   }
66 }
67 
SendResultsAndMessages(ResultProcessor * result_processor)68 void SendResultsAndMessages(ResultProcessor* result_processor) {
69   ProcessBlockNotifyMessage shutter_message = {
70       .message = {.type = MessageType::kShutter}};
71 
72   ProcessBlockNotifyMessage error_message = {
73       .message = {.type = MessageType::kError}};
74 
75   // Verify it can handle various results.
76   ProcessBlockResult null_result;
77   null_result.result = nullptr;
78   result_processor->ProcessResult(std::move(null_result));
79 
80   ProcessBlockResult empty_result;
81   empty_result.result = std::make_unique<CaptureResult>();
82   result_processor->ProcessResult(std::move(empty_result));
83 
84   // Verify it can handle messages.
85   result_processor->Notify(shutter_message);
86   result_processor->Notify(error_message);
87 }
88 
TEST(ResultProcessorTest,AddPendingRequests)89 TEST(ResultProcessorTest, AddPendingRequests) {
90   ProcessCaptureResultFunc process_capture_result =
91       ProcessCaptureResultFunc([](std::unique_ptr<CaptureResult> /*result*/) {});
92   NotifyFunc notify = NotifyFunc([](const NotifyMessage& /*message*/) {});
93 
94   for (auto& create_func : g_result_processor_create_funcs) {
95     auto result_processor = create_func();
96     ASSERT_NE(result_processor, nullptr)
97         << "Creating a result processor failed";
98 
99     std::vector<ProcessBlockRequest> requests(1);
100     requests[0].request.output_buffers = {StreamBuffer{}};
101 
102     EXPECT_EQ(
103         result_processor->AddPendingRequests(requests, requests[0].request), OK);
104   }
105 }
106 
TEST(ResultProcessorTest,ProcessResultAndNotify)107 TEST(ResultProcessorTest, ProcessResultAndNotify) {
108   ProcessCaptureResultFunc process_capture_result =
109       ProcessCaptureResultFunc([](std::unique_ptr<CaptureResult> /*result*/) {});
110   NotifyFunc notify = NotifyFunc([](const NotifyMessage& /*message*/) {});
111 
112   for (auto& create_func : g_result_processor_create_funcs) {
113     auto result_processor = create_func();
114     ASSERT_NE(result_processor, nullptr)
115         << "Creating a result processor failed";
116 
117     // Test sending results and messages before setting result callback.
118     SendResultsAndMessages(result_processor.get());
119 
120     // Test again after setting result callback.
121     result_processor->SetResultCallback(
122         process_capture_result, notify,
123         /*process_batch_capture_result=*/nullptr);
124     SendResultsAndMessages(result_processor.get());
125   }
126 }
127 
TEST(ResultProcessorTest,BasicResultProcessorResultAndNotify)128 TEST(ResultProcessorTest, BasicResultProcessorResultAndNotify) {
129   auto result_processor = CreateBasicResultProcessor();
130   bool result_received = false;
131   bool message_received = false;
132 
133   ProcessCaptureResultFunc process_capture_result =
134       ProcessCaptureResultFunc([&](std::unique_ptr<CaptureResult> result) {
135         EXPECT_NE(result, nullptr);
136         result_received = true;
137       });
138 
139   NotifyFunc notify = NotifyFunc(
140       [&](const NotifyMessage& /*message*/) { message_received = true; });
141 
142   result_processor->SetResultCallback(process_capture_result, notify,
143                                       /*process_batch_capture_result=*/nullptr);
144 
145   ProcessBlockResult null_result;
146   result_processor->ProcessResult(std::move(null_result));
147   EXPECT_EQ(result_received, false);
148   EXPECT_EQ(message_received, false);
149 
150   result_received = false;
151   message_received = false;
152   ProcessBlockResult empty_result = {.result =
153                                          std::make_unique<CaptureResult>()};
154   result_processor->ProcessResult(std::move(empty_result));
155   EXPECT_EQ(result_received, true);
156   EXPECT_EQ(message_received, false);
157 
158   result_received = false;
159   message_received = false;
160   result_processor->Notify(ProcessBlockNotifyMessage{});
161   EXPECT_EQ(result_received, false);
162   EXPECT_EQ(message_received, true);
163 }
164 
TEST(ResultProcessorTest,BasicResultProcessorAddPendingRequest)165 TEST(ResultProcessorTest, BasicResultProcessorAddPendingRequest) {
166   auto result_processor = CreateBasicResultProcessor();
167 
168   ProcessCaptureResultFunc process_capture_result = ProcessCaptureResultFunc(
169       [&](std::unique_ptr<CaptureResult> /*result*/) {});
170 
171   NotifyFunc notify = NotifyFunc([&](const NotifyMessage& /*message*/) {});
172 
173   result_processor->SetResultCallback(process_capture_result, notify,
174                                       /*process_batch_capture_result=*/nullptr);
175 
176   std::vector<ProcessBlockRequest> requests(1);
177   requests[0].request.output_buffers = {StreamBuffer{}};
178 
179   CaptureRequest remaining_request;
180   remaining_request.output_buffers.push_back({.buffer = kDummyBufferHandle});
181   EXPECT_NE(result_processor->AddPendingRequests(requests, remaining_request), OK)
182       << "Adding a pending request with a remaining output buffer that's not"
183       << "included in the request should fail.";
184 }
185 
186 }  // namespace google_camera_hal
187 }  // namespace android
188