blob: 852b0d3d41a78e5fbb93796652b643fedee01705 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "resource_provider.h"
#include "resource_requester.h"
#include "server_connection.h"
#include "test_utilities.h"
#include <gapic/mock_connection.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <string>
#include <vector>
using ::testing::_;
using ::testing::DoAll;
using ::testing::Return;
using ::testing::ReturnArg;
using ::testing::StrictMock;
using ::testing::ElementsAreArray;
namespace gapir {
namespace test {
namespace {
class ResourceRequesterTest : public ::testing::Test {
protected:
virtual void SetUp() {
mConnection = new gapic::test::MockConnection();
mServer = createServerConnection(mConnection, "", 0);
mResourceProvider = ResourceRequester::create();
}
gapic::test::MockConnection* mConnection;
std::unique_ptr<ServerConnection> mServer;
std::unique_ptr<ResourceProvider> mResourceProvider;
std::vector<uint8_t> mBuffer;
};
} // anonymous namespace
TEST_F(ResourceRequesterTest, Prefetch) {
mBuffer.resize(4096);
EXPECT_TRUE(mResourceProvider->prefetch({{"A", 16}, {"B", 32}}, *mServer, mBuffer.data(),
mBuffer.size()));
}
TEST_F(ResourceRequesterTest, SingleGet) {
std::vector<uint8_t> payload = {'X', 'Y', 'Z'};
mBuffer.resize(payload.size());
std::vector<uint8_t> expected;
pushUint8(&expected, ServerConnection::MESSAGE_TYPE_GET);
pushUint32(&expected, 1);
pushString(&expected, "A");
pushBytes(&mConnection->in, payload);
EXPECT_TRUE(mResourceProvider->get("A", *mServer, mBuffer.data(), 3));
EXPECT_THAT(mBuffer, ElementsAreArray(payload));
EXPECT_EQ(mConnection->out, expected);
}
TEST_F(ResourceRequesterTest, MultiGet) {
std::vector<uint8_t> payload = {'X', 'Y', 'Z', '1', '2', '3', '4', '5'};
mBuffer.resize(payload.size());
std::vector<uint8_t> expected;
pushUint8(&expected, ServerConnection::MESSAGE_TYPE_GET);
pushUint32(&expected, 2);
pushString(&expected, "A");
pushString(&expected, "B");
pushBytes(&mConnection->in, payload);
EXPECT_TRUE(mResourceProvider->get({{"A", 3}, {"B", 5}}, *mServer, mBuffer.data(), mBuffer.size()));
EXPECT_THAT(mBuffer, ElementsAreArray(payload));
EXPECT_EQ(mConnection->out, expected);
}
} // namespace gapir
} // namespace test