1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "seal.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::AllOf;
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::Property;
using ::testing::SizeIs;
using ::testing::StrNe;
namespace ash {
namespace {
class FakeSnapperProvider : public manta::SnapperProvider {
public:
FakeSnapperProvider(
scoped_refptr<network::SharedURLLoaderFactory> test_url_loader_factory,
signin::IdentityManager* identity_manager,
manta::MantaStatus fake_status,
manta::proto::Response fake_response)
: SnapperProvider(std::move(test_url_loader_factory), identity_manager),
fake_status_(fake_status),
fake_response_(fake_response) {}
void Call(manta::proto::Request& request,
net::NetworkTrafficAnnotationTag traffic_annotation,
manta::MantaProtoResponseCallback done_callback) override {
std::move(done_callback)
.Run(std::make_unique<manta::proto::Response>(fake_response_),
fake_status_);
}
private:
manta::MantaStatus fake_status_;
manta::proto::Response fake_response_;
};
class SealTest : public testing::Test {
protected:
std::unique_ptr<SealService> CreateSealService(
std::unique_ptr<manta::SnapperProvider> snapper_provider) {
return std::make_unique<SealService>(
/*receiver=*/seal_service_.BindNewPipeAndPassReceiver(),
/*snapper_provider=*/std::move(snapper_provider));
}
std::unique_ptr<FakeSnapperProvider> CreateSnapperProvider(
manta::MantaStatus fake_status,
manta::proto::Response fake_response) {
return std::make_unique<FakeSnapperProvider>(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_),
identity_test_env_.identity_manager(), fake_status, fake_response);
}
private:
base::test::TaskEnvironment task_environment_;
mojo::Remote<seal::mojom::SealService> seal_service_;
network::TestURLLoaderFactory test_url_loader_factory_;
signin::IdentityTestEnvironment identity_test_env_;
};
TEST_F(SealTest, GetImagesReturnsNotEnabledErrorIfNotInitialized) {
std::unique_ptr<SealService> service = CreateSealService(nullptr);
base::test::TestFuture<seal::mojom::Status,
std::vector<seal::mojom::ImagePtr>>
future;
service->GetImages("cat", future.GetCallback());
const auto& [status, images] = future.Get();
EXPECT_EQ(status, seal::mojom::Status::kNotEnabledError);
EXPECT_THAT(images, IsEmpty());
}
TEST_F(SealTest, GetImagesReturnsUnknownErrorDueToSnapperError) {
manta::proto::Response fake_response;
std::unique_ptr<SealService> service =
CreateSealService(CreateSnapperProvider(
manta::MantaStatus{.status_code =
manta::MantaStatusCode::kGenericError},
fake_response));
base::test::TestFuture<seal::mojom::Status,
std::vector<seal::mojom::ImagePtr>>
future;
service->GetImages("cat", future.GetCallback());
const auto& [status, images] = future.Get();
EXPECT_EQ(status, seal::mojom::Status::kUnknownError);
EXPECT_THAT(images, IsEmpty());
}
TEST_F(SealTest, GetImagesReturnsOk) {
manta::proto::Response fake_response;
for (int i = 0; i < 10; ++i) {
manta::proto::OutputData& fake_output_data =
*fake_response.add_output_data();
manta::proto::Image& image = *fake_output_data.mutable_image();
image.set_serialized_bytes("[image content]");
}
std::unique_ptr<SealService> service =
CreateSealService(CreateSnapperProvider(
manta::MantaStatus{.status_code = manta::MantaStatusCode::kOk},
fake_response));
base::test::TestFuture<seal::mojom::Status,
std::vector<seal::mojom::ImagePtr>>
future;
service->GetImages("cat", future.GetCallback());
const auto& [status, images] = future.Get();
EXPECT_EQ(status, seal::mojom::Status::kOk);
ASSERT_THAT(images, SizeIs(10));
EXPECT_THAT(
images,
Each(Pointee(AllOf(
Field(&seal::mojom::Image::url, Property(&GURL::spec, StrNe(""))),
Field(&seal::mojom::Image::dimensions,
AllOf(Property(&gfx::Size::width, Eq(1024)),
Property(&gfx::Size::height, Eq(1024))))))));
}
} // namespace
} // namespace ash
|