|
| 1 | +// |
| 2 | +// Copyright (C) 2026 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 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/combined_android_build.h" |
| 17 | + |
| 18 | +#include <functional> |
| 19 | +#include <memory> |
| 20 | +#include <set> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include "gmock/gmock-matchers.h" |
| 26 | +#include "gtest/gtest.h" |
| 27 | + |
| 28 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h" |
| 29 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/fake_android_build.h" |
| 30 | +#include "cuttlefish/result/result.h" |
| 31 | +#include "cuttlefish/result/result_matchers.h" |
| 32 | + |
| 33 | +namespace cuttlefish { |
| 34 | +namespace { |
| 35 | + |
| 36 | +Result<std::unique_ptr<AndroidBuild>> CombineFakeBuilds( |
| 37 | + std::vector<FakeAndroidBuild> fakes) { |
| 38 | + std::vector<std::unique_ptr<AndroidBuild>> build_ptrs; |
| 39 | + for (FakeAndroidBuild fake : fakes) { |
| 40 | + build_ptrs.emplace_back( |
| 41 | + std::make_unique<FakeAndroidBuild>(std::move(fake))); |
| 42 | + } |
| 43 | + return CF_EXPECT(CombinedAndroidBuild("Fakes", std::move(build_ptrs))); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(CombinedAndroidBuild, Construct) { |
| 47 | + EXPECT_THAT(CombineFakeBuilds({FakeAndroidBuild(), FakeAndroidBuild()}), |
| 48 | + IsOk()); |
| 49 | +} |
| 50 | + |
| 51 | +TEST(CombinedAndroidBuild, MergesImages) { |
| 52 | + FakeAndroidBuild with_a; |
| 53 | + with_a.AddExtractedImage("a", "a_file"); |
| 54 | + |
| 55 | + FakeAndroidBuild with_b; |
| 56 | + with_b.AddExtractedImage("b", "b_file"); |
| 57 | + |
| 58 | + std::unique_ptr<AndroidBuild> combined = *CombineFakeBuilds({with_a, with_b}); |
| 59 | + |
| 60 | + std::set<std::string, std::less<void>> expected = {"a", "b"}; |
| 61 | + EXPECT_THAT(combined->Images(), IsOkAndValue(expected)); |
| 62 | + EXPECT_THAT(combined->ImageFile("a"), IsOkAndValue("a_file")); |
| 63 | + EXPECT_THAT(combined->ImageFile("b"), IsOkAndValue("b_file")); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(CombinedAndroidBuild, PrefersExtractedImage) { |
| 67 | + FakeAndroidBuild unextracted; |
| 68 | + unextracted.AddUnextractedImage("img"); |
| 69 | + |
| 70 | + FakeAndroidBuild extracted; |
| 71 | + extracted.AddExtractedImage("img", "extracted"); |
| 72 | + |
| 73 | + std::unique_ptr<AndroidBuild> combined = |
| 74 | + *CombineFakeBuilds({extracted, unextracted}); |
| 75 | + |
| 76 | + EXPECT_THAT(combined->ImageFile("img", false), IsOkAndValue("extracted")); |
| 77 | + EXPECT_THAT(combined->ImageFile("img", true), IsOkAndValue("extracted")); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(CombinedAndroidBuild, IgnoresMissingImage) { |
| 81 | + FakeAndroidBuild unextracted; |
| 82 | + unextracted.AddUnextractedImage("img"); |
| 83 | + EXPECT_THAT(unextracted.SetExtractDir("extract"), IsOk()); |
| 84 | + |
| 85 | + FakeAndroidBuild missing; |
| 86 | + missing.AddMissingImage("img"); |
| 87 | + |
| 88 | + std::unique_ptr<AndroidBuild> combined = |
| 89 | + *CombineFakeBuilds({missing, unextracted}); |
| 90 | + |
| 91 | + EXPECT_THAT(combined->ImageFile("img", false), IsError()); |
| 92 | + EXPECT_THAT(combined->ImageFile("img", true), IsOkAndValue("extract/img")); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(CombinedAndroidBuild, MergesLogicalPartitions) { |
| 96 | + FakeAndroidBuild with_a; |
| 97 | + with_a.SetLogicalPartitions({"a"}); |
| 98 | + |
| 99 | + FakeAndroidBuild with_b; |
| 100 | + with_b.SetLogicalPartitions({"b"}); |
| 101 | + |
| 102 | + std::unique_ptr<AndroidBuild> combined = *CombineFakeBuilds({with_a, with_b}); |
| 103 | + |
| 104 | + std::set<std::string, std::less<void>> expected = {"a", "b"}; |
| 105 | + EXPECT_THAT(combined->LogicalPartitions(), IsOkAndValue(expected)); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace |
| 109 | +} // namespace cuttlefish |
0 commit comments