1 /*
2  * Copyright 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 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include "EventGenerator.h"
21 #include "MockEngine.h"
22 #include "OutputConfig.pb.h"
23 #include "RunnerComponent.h"
24 #include "StreamEngineInterface.h"
25 #include "StreamManager.h"
26 #include "gmock/gmock-matchers.h"
27 #include "types/Status.h"
28 
29 using namespace android::automotive::computepipe::runner::stream_manager;
30 using namespace android::automotive::computepipe;
31 using android::automotive::computepipe::runner::RunnerComponentInterface;
32 using android::automotive::computepipe::runner::RunnerEvent;
33 using android::automotive::computepipe::runner::generator::DefaultEvent;
34 using testing::Return;
35 
36 class SemanticManagerTest : public ::testing::Test {
37   protected:
38     static constexpr uint32_t kMaxSemanticDataSize = 1024;
39     /**
40      * Setup for the test fixture to initialize the semantic manager
41      * After this, the semantic manager should be in RESET state.
42      */
SetUp()43     void SetUp() override {
44     }
TearDown()45     void TearDown() override {
46         if (mCurrentPacket) {
47             mCurrentPacket = nullptr;
48             ;
49         }
50     }
51 
SetupStreamManager(std::shared_ptr<MockEngine> & engine)52     std::unique_ptr<StreamManager> SetupStreamManager(std::shared_ptr<MockEngine>& engine) {
53         proto::OutputConfig config;
54         config.set_type(proto::PacketType::SEMANTIC_DATA);
55         config.set_stream_name("semantic_stream");
56 
57         return mFactory.getStreamManager(config, engine, 0);
58     }
59     StreamManagerFactory mFactory;
60     std::shared_ptr<MemHandle> mCurrentPacket;
61 };
62 
63 /**
64  * Checks Packet Queing without start.
65  * Checks Packet Queuing with bad arguments.
66  * Checks successful packet queuing.
67  */
TEST_F(SemanticManagerTest,PacketQueueTest)68 TEST_F(SemanticManagerTest, PacketQueueTest) {
69     DefaultEvent e = DefaultEvent::generateEntryEvent(DefaultEvent::Phase::RUN);
70     std::shared_ptr<MockEngine> mockEngine = std::make_shared<MockEngine>();
71     std::unique_ptr<StreamManager> manager = SetupStreamManager(mockEngine);
72     ASSERT_EQ(manager->handleExecutionPhase(e), Status::SUCCESS);
73     std::string fakeData("FakeData");
74     uint32_t size = fakeData.size();
75     EXPECT_EQ(manager->queuePacket(nullptr, size, 0), Status::INVALID_ARGUMENT);
76     EXPECT_EQ(manager->queuePacket(fakeData.c_str(), kMaxSemanticDataSize + 1, 0),
77               Status::INVALID_ARGUMENT);
78     EXPECT_CALL((*mockEngine), dispatchPacket)
79         .WillOnce(testing::DoAll(testing::SaveArg<0>(&mCurrentPacket), (Return(Status::SUCCESS))));
80 
81     manager->queuePacket(fakeData.c_str(), size, 0);
82     EXPECT_STREQ(mCurrentPacket->getData(), fakeData.c_str());
83 }
84