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 "PipelineRequestIdManagerTests"
18 #include <log/log.h>
19 
20 #include <gtest/gtest.h>
21 #include <hal_camera_metadata.h>
22 #include <hal_types.h>
23 #include <pipeline_request_id_manager.h>
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 struct SampleRequest {
29   uint32_t pipeline_id;
30   uint32_t request_id;
31 };
32 
33 const uint32_t kSampleFrameNumber = 10;
34 const size_t kMaxPendingRequest = 8;
35 
36 const SampleRequest kSampleRequest[2] = {
37     {
38         .pipeline_id = 1,
39         .request_id = 3,
40     },
41     {
42         .pipeline_id = 2,
43         .request_id = 4,
44     },
45 };
46 
TEST(PipelineRequestIdManagerTests,SetPipelineRequestId)47 TEST(PipelineRequestIdManagerTests, SetPipelineRequestId) {
48   auto id_manager = PipelineRequestIdManager::Create(kMaxPendingRequest);
49   ASSERT_NE(id_manager, nullptr) << "Creating PipelineRequestIdManager failed.";
50 
51   for (const auto& r : kSampleRequest) {
52     EXPECT_EQ(id_manager->SetPipelineRequestId(r.request_id, kSampleFrameNumber,
53                                                r.pipeline_id),
54               OK)
55         << "SetPipelineRequestId failed.";
56   }
57 
58   // Set same frame number again should failed.
59   EXPECT_NE(id_manager->SetPipelineRequestId(kSampleRequest[0].request_id + 1,
60                                              kSampleFrameNumber,
61                                              kSampleRequest[0].pipeline_id),
62             OK)
63       << "Set a frame number which already set to SetPipelineRequestId should "
64          "failed.";
65 }
66 
TEST(PipelineRequestIdManagerTests,GetPipelineRequestId)67 TEST(PipelineRequestIdManagerTests, GetPipelineRequestId) {
68   auto id_manager = PipelineRequestIdManager::Create(kMaxPendingRequest);
69   ASSERT_NE(id_manager, nullptr) << "Creating PipelineRequestIdManager failed.";
70 
71   for (const auto& r : kSampleRequest) {
72     ASSERT_EQ(id_manager->SetPipelineRequestId(r.request_id, kSampleFrameNumber,
73                                                r.pipeline_id),
74               OK)
75         << "SetPipelineRequestId failed.";
76   }
77 
78   uint32_t returned_request_id;
79 
80   // Provide unset pipeline id should failed
81   EXPECT_NE(id_manager->GetPipelineRequestId(99, kSampleFrameNumber,
82                                              &returned_request_id),
83             OK)
84       << "Provide unset pipeline_id to GetPipelineRequestId should failed.";
85 
86   // Provide invalid frame number should failed
87   EXPECT_NE(id_manager->GetPipelineRequestId(kSampleRequest[0].pipeline_id,
88                                              999999, &returned_request_id),
89             OK)
90       << "Provide empty result to GetPipelineRequestId should failed.";
91 
92   for (const auto& request : kSampleRequest) {
93     EXPECT_EQ(id_manager->GetPipelineRequestId(
94                   request.pipeline_id, kSampleFrameNumber, &returned_request_id),
95               OK)
96         << "GetPipelineRequestId failed.";
97     EXPECT_EQ(returned_request_id, request.request_id)
98         << "The getting request_id is different from the setting.";
99   }
100 }
101 
TEST(PipelineRequestIdManagerTests,SetPipelineRequestIdWithOverflow)102 TEST(PipelineRequestIdManagerTests, SetPipelineRequestIdWithOverflow) {
103   auto id_manager = PipelineRequestIdManager::Create(kMaxPendingRequest);
104   ASSERT_NE(id_manager, nullptr) << "Creating PipelineRequestIdManager failed.";
105 
106   ASSERT_EQ(id_manager->SetPipelineRequestId(kSampleRequest[0].request_id,
107                                              kSampleFrameNumber,
108                                              kSampleRequest[0].pipeline_id),
109             OK)
110       << "SetPipelineRequestId failed.";
111 
112   uint32_t returned_request_id;
113 
114   ASSERT_EQ(id_manager->GetPipelineRequestId(kSampleRequest[0].pipeline_id,
115                                              kSampleFrameNumber,
116                                              &returned_request_id),
117             OK)
118       << "GetPipelineRequestId failed.";
119   ASSERT_EQ(returned_request_id, kSampleRequest[0].request_id)
120       << "The getting request_id is different from the setting.";
121 
122   // Set frame number with same modulo value.
123   // This should overwrite original frame number's data.
124   uint32_t new_frame_number = kSampleFrameNumber + kMaxPendingRequest;
125   ASSERT_EQ(id_manager->SetPipelineRequestId(kSampleRequest[0].request_id,
126                                              new_frame_number,
127                                              kSampleRequest[0].pipeline_id),
128             OK)
129       << "SetPipelineRequestId failed.";
130 
131   EXPECT_NE(id_manager->GetPipelineRequestId(kSampleRequest[0].pipeline_id,
132                                              kSampleFrameNumber,
133                                              &returned_request_id),
134             OK)
135       << "GetPipelineRequestId should failed after overwrite frame number.";
136 }
137 
138 }  // namespace google_camera_hal
139 }  // namespace android
140