OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "tools/gn/ninja_create_bundle_target_writer.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <sstream> |
| 9 |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "tools/gn/target.h" |
| 12 #include "tools/gn/test_with_scope.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 void SetupBundleDataDir(BundleData* bundle_data, const std::string& root_dir) { |
| 17 bundle_data->root_dir().assign(root_dir + "/bar.bundle"); |
| 18 bundle_data->resources_dir().assign(bundle_data->root_dir() + "/Resources"); |
| 19 bundle_data->executable_dir().assign(bundle_data->root_dir() + "/Executable"); |
| 20 bundle_data->plugins_dir().assign(bundle_data->root_dir() + "/PlugIns"); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 // Tests multiple files with an output pattern. |
| 26 TEST(NinjaCreateBundleTargetWriter, Run) { |
| 27 TestWithScope setup; |
| 28 Err err; |
| 29 |
| 30 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 31 Target target(setup.settings(), Label(SourceDir("//baz/"), "bar")); |
| 32 target.set_output_type(Target::CREATE_BUNDLE); |
| 33 |
| 34 SetupBundleDataDir(&target.bundle_data(), "//out/Debug"); |
| 35 |
| 36 std::vector<SourceFile> sources; |
| 37 sources.push_back(SourceFile("//foo/input1.txt")); |
| 38 sources.push_back(SourceFile("//foo/input2.txt")); |
| 39 target.bundle_data().file_rules().push_back(BundleFileRule( |
| 40 sources, SubstitutionPattern::MakeForTest( |
| 41 "{{bundle_resources_dir}}/{{source_file_part}}"))); |
| 42 |
| 43 target.SetToolchain(setup.toolchain()); |
| 44 ASSERT_TRUE(target.OnResolved(&err)); |
| 45 |
| 46 std::ostringstream out; |
| 47 NinjaCreateBundleTargetWriter writer(&target, out); |
| 48 writer.Run(); |
| 49 |
| 50 const char expected[] = |
| 51 "build bar.bundle/Resources/input1.txt: copy_bundle_data " |
| 52 "../../foo/input1.txt\n" |
| 53 "build bar.bundle/Resources/input2.txt: copy_bundle_data " |
| 54 "../../foo/input2.txt\n" |
| 55 "\n" |
| 56 "build obj/baz/bar.stamp: stamp " |
| 57 "bar.bundle/Resources/input1.txt " |
| 58 "bar.bundle/Resources/input2.txt\n"; |
| 59 std::string out_str = out.str(); |
| 60 EXPECT_EQ(expected, out_str); |
| 61 } |
| 62 |
| 63 // Tests multiple files from asset catalog. |
| 64 TEST(NinjaCreateBundleTargetWriter, AssetCatalog) { |
| 65 TestWithScope setup; |
| 66 Err err; |
| 67 |
| 68 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 69 Target target(setup.settings(), Label(SourceDir("//baz/"), "bar")); |
| 70 target.set_output_type(Target::CREATE_BUNDLE); |
| 71 |
| 72 SetupBundleDataDir(&target.bundle_data(), "//out/Debug"); |
| 73 |
| 74 std::vector<SourceFile>& asset_catalog_sources = |
| 75 target.bundle_data().asset_catalog_sources(); |
| 76 asset_catalog_sources.push_back( |
| 77 SourceFile("//foo/Foo.xcassets/foo.imageset/Contents.json")); |
| 78 asset_catalog_sources.push_back( |
| 79 SourceFile("//foo/Foo.xcassets/foo.imageset/FooIcon-29.png")); |
| 80 asset_catalog_sources.push_back( |
| 81 SourceFile("//foo/Foo.xcassets/foo.imageset/FooIcon-29@2x.png")); |
| 82 asset_catalog_sources.push_back( |
| 83 SourceFile("//foo/Foo.xcassets/foo.imageset/FooIcon-29@3x.png")); |
| 84 |
| 85 target.SetToolchain(setup.toolchain()); |
| 86 ASSERT_TRUE(target.OnResolved(&err)); |
| 87 |
| 88 std::ostringstream out; |
| 89 NinjaCreateBundleTargetWriter writer(&target, out); |
| 90 writer.Run(); |
| 91 |
| 92 const char expected[] = |
| 93 "build bar.bundle/Resources/Assets.car: compile_xcassets " |
| 94 "../../foo/Foo.xcassets | " |
| 95 "../../foo/Foo.xcassets/foo.imageset/Contents.json " |
| 96 "../../foo/Foo.xcassets/foo.imageset/FooIcon-29.png " |
| 97 "../../foo/Foo.xcassets/foo.imageset/FooIcon-29@2x.png " |
| 98 "../../foo/Foo.xcassets/foo.imageset/FooIcon-29@3x.png\n" |
| 99 "\n" |
| 100 "build obj/baz/bar.stamp: stamp bar.bundle/Resources/Assets.car\n"; |
| 101 std::string out_str = out.str(); |
| 102 EXPECT_EQ(expected, out_str); |
| 103 } |
| 104 |
| 105 // Tests complex target with multiple bundle_data sources, including |
| 106 // some asset catalog. |
| 107 TEST(NinjaCreateBundleTargetWriter, OrderOnlyDeps) { |
| 108 TestWithScope setup; |
| 109 Err err; |
| 110 |
| 111 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 112 Target target(setup.settings(), Label(SourceDir("//baz/"), "bar")); |
| 113 target.set_output_type(Target::CREATE_BUNDLE); |
| 114 |
| 115 SetupBundleDataDir(&target.bundle_data(), "//out/Debug"); |
| 116 |
| 117 std::vector<SourceFile> sources1; |
| 118 sources1.push_back(SourceFile("//foo/input1.txt")); |
| 119 sources1.push_back(SourceFile("//foo/input2.txt")); |
| 120 target.bundle_data().file_rules().push_back(BundleFileRule( |
| 121 sources1, SubstitutionPattern::MakeForTest( |
| 122 "{{bundle_resources_dir}}/{{source_file_part}}"))); |
| 123 |
| 124 std::vector<SourceFile> sources2; |
| 125 sources2.push_back(SourceFile("//qux/Info.plist")); |
| 126 target.bundle_data().file_rules().push_back(BundleFileRule( |
| 127 sources2, |
| 128 SubstitutionPattern::MakeForTest("{{bundle_root_dir}}/Info.plist"))); |
| 129 |
| 130 std::vector<SourceFile> empty_source; |
| 131 target.bundle_data().file_rules().push_back(BundleFileRule( |
| 132 empty_source, SubstitutionPattern::MakeForTest( |
| 133 "{{bundle_plugins_dir}}/{{source_file_part}}"))); |
| 134 |
| 135 std::vector<SourceFile>& asset_catalog_sources = |
| 136 target.bundle_data().asset_catalog_sources(); |
| 137 asset_catalog_sources.push_back( |
| 138 SourceFile("//foo/Foo.xcassets/foo.imageset/Contents.json")); |
| 139 asset_catalog_sources.push_back( |
| 140 SourceFile("//foo/Foo.xcassets/foo.imageset/FooIcon-29.png")); |
| 141 asset_catalog_sources.push_back( |
| 142 SourceFile("//foo/Foo.xcassets/foo.imageset/FooIcon-29@2x.png")); |
| 143 asset_catalog_sources.push_back( |
| 144 SourceFile("//foo/Foo.xcassets/foo.imageset/FooIcon-29@3x.png")); |
| 145 |
| 146 target.SetToolchain(setup.toolchain()); |
| 147 ASSERT_TRUE(target.OnResolved(&err)); |
| 148 |
| 149 std::ostringstream out; |
| 150 NinjaCreateBundleTargetWriter writer(&target, out); |
| 151 writer.Run(); |
| 152 |
| 153 const char expected[] = |
| 154 "build bar.bundle/Resources/input1.txt: copy_bundle_data " |
| 155 "../../foo/input1.txt\n" |
| 156 "build bar.bundle/Resources/input2.txt: copy_bundle_data " |
| 157 "../../foo/input2.txt\n" |
| 158 "build bar.bundle/Info.plist: copy_bundle_data ../../qux/Info.plist\n" |
| 159 "build bar.bundle/Resources/Assets.car: compile_xcassets " |
| 160 "../../foo/Foo.xcassets | " |
| 161 "../../foo/Foo.xcassets/foo.imageset/Contents.json " |
| 162 "../../foo/Foo.xcassets/foo.imageset/FooIcon-29.png " |
| 163 "../../foo/Foo.xcassets/foo.imageset/FooIcon-29@2x.png " |
| 164 "../../foo/Foo.xcassets/foo.imageset/FooIcon-29@3x.png\n" |
| 165 "\n" |
| 166 "build obj/baz/bar.stamp: stamp " |
| 167 "bar.bundle/Resources/input1.txt " |
| 168 "bar.bundle/Resources/input2.txt " |
| 169 "bar.bundle/Info.plist " |
| 170 "bar.bundle/Resources/Assets.car\n"; |
| 171 std::string out_str = out.str(); |
| 172 EXPECT_EQ(expected, out_str); |
| 173 } |
OLD | NEW |