|
10 | 10 | #include "flutter/flow/layers/picture_layer.h" |
11 | 11 | #include "flutter/fml/command_line.h" |
12 | 12 | #include "flutter/fml/file.h" |
| 13 | +#include "flutter/fml/log_settings.h" |
13 | 14 | #include "flutter/fml/unique_fd.h" |
14 | 15 | #include "flutter/shell/common/persistent_cache.h" |
15 | 16 | #include "flutter/shell/common/shell_test.h" |
@@ -123,5 +124,99 @@ TEST_F(ShellTest, CacheSkSLWorks) { |
123 | 124 | DestroyShell(std::move(shell)); |
124 | 125 | } |
125 | 126 |
|
| 127 | +static void CheckTextSkData(sk_sp<SkData> data, const std::string& expected) { |
| 128 | + std::string data_string(reinterpret_cast<const char*>(data->bytes()), |
| 129 | + data->size()); |
| 130 | + ASSERT_EQ(data_string, expected); |
| 131 | +} |
| 132 | + |
| 133 | +void ResetAssetPath() { |
| 134 | + PersistentCache::UpdateAssetPath("some_path_that_does_not_exist"); |
| 135 | + ASSERT_EQ(PersistentCache::GetCacheForProcess()->LoadSkSLs().size(), 0u); |
| 136 | +} |
| 137 | + |
| 138 | +void CheckTwoSkSLsAreLoaded() { |
| 139 | + auto shaders = PersistentCache::GetCacheForProcess()->LoadSkSLs(); |
| 140 | + ASSERT_EQ(shaders.size(), 2u); |
| 141 | +} |
| 142 | + |
| 143 | +TEST_F(ShellTest, CanLoadSkSLsFromAsset) { |
| 144 | + // Avoid polluting unit tests output by hiding INFO level logging. |
| 145 | + fml::LogSettings warning_only = {fml::LOG_WARNING}; |
| 146 | + fml::ScopedSetLogSettings scoped_set_log_settings(warning_only); |
| 147 | + |
| 148 | + // Create an empty shell to test its service protocol handlers. |
| 149 | + auto empty_settings = CreateSettingsForFixture(); |
| 150 | + auto empty_config = RunConfiguration::InferFromSettings(empty_settings); |
| 151 | + std::unique_ptr<Shell> empty_shell = CreateShell(empty_settings); |
| 152 | + |
| 153 | + // Temp dir for the asset. |
| 154 | + fml::ScopedTemporaryDirectory asset_dir; |
| 155 | + fml::UniqueFD sksl_asset_dir = |
| 156 | + fml::OpenDirectory(asset_dir.fd(), PersistentCache::kSkSLSubdirName, true, |
| 157 | + fml::FilePermission::kReadWrite); |
| 158 | + |
| 159 | + // The SkSL filenames are Base32 encoded strings. "IE" is the encoding of "A" |
| 160 | + // and "II" is the encoding of "B". |
| 161 | + const std::string kFileNames[2] = {"IE", "II"}; |
| 162 | + const std::string kFileData[2] = {"x", "y"}; |
| 163 | + |
| 164 | + // Prepare 2 SkSL files in the asset directory. |
| 165 | + for (int i = 0; i < 2; i += 1) { |
| 166 | + auto data = std::make_unique<fml::DataMapping>( |
| 167 | + std::vector<uint8_t>{kFileData[i].begin(), kFileData[i].end()}); |
| 168 | + fml::WriteAtomically(sksl_asset_dir, kFileNames[i].c_str(), *data); |
| 169 | + } |
| 170 | + |
| 171 | + // 1st, test that RunConfiguration::InferFromSettings sets the path. |
| 172 | + ResetAssetPath(); |
| 173 | + auto settings = CreateSettingsForFixture(); |
| 174 | + settings.assets_path = asset_dir.path(); |
| 175 | + RunConfiguration::InferFromSettings(settings); |
| 176 | + CheckTwoSkSLsAreLoaded(); |
| 177 | + |
| 178 | + // 2nd, test that Shell::OnServiceProtocolSetAssetBundlePath sets the path. |
| 179 | + ResetAssetPath(); |
| 180 | + ServiceProtocol::Handler::ServiceProtocolMap params; |
| 181 | + rapidjson::Document document; |
| 182 | + params["assetDirectory"] = asset_dir.path(); |
| 183 | + OnServiceProtocol( |
| 184 | + empty_shell.get(), ShellTest::ServiceProtocolEnum::kSetAssetBundlePath, |
| 185 | + empty_shell->GetTaskRunners().GetUITaskRunner(), params, document); |
| 186 | + CheckTwoSkSLsAreLoaded(); |
| 187 | + |
| 188 | + // 3rd, test that Shell::OnServiceProtocolRunInView sets the path. |
| 189 | + ResetAssetPath(); |
| 190 | + params["assetDirectory"] = asset_dir.path(); |
| 191 | + params["mainScript"] = "no_such_script.dart"; |
| 192 | + OnServiceProtocol( |
| 193 | + empty_shell.get(), ShellTest::ServiceProtocolEnum::kSetAssetBundlePath, |
| 194 | + empty_shell->GetTaskRunners().GetUITaskRunner(), params, document); |
| 195 | + CheckTwoSkSLsAreLoaded(); |
| 196 | + |
| 197 | + // 4th, test the content of the SkSLs in the asset. |
| 198 | + { |
| 199 | + auto shaders = PersistentCache::GetCacheForProcess()->LoadSkSLs(); |
| 200 | + ASSERT_EQ(shaders.size(), 2u); |
| 201 | + |
| 202 | + // Make sure that the 2 shaders are sorted by their keys. Their keys should |
| 203 | + // be "A" and "B" (decoded from "II" and "IE"). |
| 204 | + if (shaders[0].first->bytes()[0] == 'B') { |
| 205 | + std::swap(shaders[0], shaders[1]); |
| 206 | + } |
| 207 | + |
| 208 | + CheckTextSkData(shaders[0].first, "A"); |
| 209 | + CheckTextSkData(shaders[1].first, "B"); |
| 210 | + CheckTextSkData(shaders[0].second, "x"); |
| 211 | + CheckTextSkData(shaders[1].second, "y"); |
| 212 | + } |
| 213 | + |
| 214 | + // Cleanup. |
| 215 | + DestroyShell(std::move(empty_shell)); |
| 216 | + fml::UnlinkFile(sksl_asset_dir, kFileNames[0].c_str()); |
| 217 | + fml::UnlinkFile(sksl_asset_dir, kFileNames[1].c_str()); |
| 218 | + fml::UnlinkDirectory(asset_dir.fd(), PersistentCache::kSkSLSubdirName); |
| 219 | +} |
| 220 | + |
126 | 221 | } // namespace testing |
127 | 222 | } // namespace flutter |
0 commit comments