Skip to content

Commit

Permalink
Correct logging url
Browse files Browse the repository at this point in the history
  • Loading branch information
anayw2001 committed Nov 19, 2024
1 parent 0e73a05 commit 1ff6045
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ using flutter::EncodableMap;
using flutter::EncodableValue;
using ::testing::_;
using ::testing::DoAll;
using ::testing::HasSubstr;
using ::testing::Pointee;
using ::testing::Return;
using ::testing::SetArgPointee;
Expand Down Expand Up @@ -160,5 +161,28 @@ TEST(UrlLauncherPlugin, LaunchUTF8EncodedFileURLSuccess) {
EXPECT_TRUE(result.value());
}

TEST(UrlLauncherPlugin, LaunchUTF8LogsUnescapedOnFail) {
std::unique_ptr<MockSystemApis> system = std::make_unique<MockSystemApis>();

// Return a failure value (<32) from launching.
EXPECT_CALL(
*system,
ShellExecuteW(
_, StrEq(L"open"),
// 家の管理/スキャナ"),
StrEq(
L"file:///G:/\x5bb6\x306e\x7ba1\x7406/\x30b9\x30ad\x30e3\x30ca"),
_, _, _))
.WillOnce(Return(reinterpret_cast<HINSTANCE>(0)));

UrlLauncherPlugin plugin(std::move(system));
ErrorOr<bool> result = plugin.LaunchUrl(
"file:///G:/%E5%AE%B6%E3%81%AE%E7%AE%A1%E7%90%86/"
"%E3%82%B9%E3%82%AD%E3%83%A3%E3%83%8A");

ASSERT_TRUE(result.has_error());
EXPECT_THAT(result.error().message(), HasSubstr("家の管理/スキャナ"));
}

} // namespace test
} // namespace url_launcher_windows
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ErrorOr<bool> UrlLauncherPlugin::CanLaunchUrl(const std::string& url) {
}

ErrorOr<bool> UrlLauncherPlugin::LaunchUrl(const std::string& url) {
std::wstring url_wide;
std::string url_to_open;
if (url.find("file:") == 0) {
// ShellExecuteW does not process %-encoded UTF8 strings in file URLs.
DWORD unescaped_len = 0;
Expand All @@ -108,13 +108,13 @@ ErrorOr<bool> UrlLauncherPlugin::LaunchUrl(const std::string& url) {
&unescaped_len, URL_UNESCAPE_INPLACE))) {
return FlutterError("open_error", "Failed to unescape file URL");
}
url_wide = Utf16FromUtf8(unescaped_url);
url_to_open = unescaped_url;
} else {
url_wide = Utf16FromUtf8(url);
url_to_open = url;
}

int status = static_cast<int>(reinterpret_cast<INT_PTR>(
system_apis_->ShellExecuteW(nullptr, TEXT("open"), url_wide.c_str(),
system_apis_->ShellExecuteW(nullptr, TEXT("open"), Utf16FromUtf8(url_to_open).c_str(),
nullptr, nullptr, SW_SHOWNORMAL)));

// Per ::ShellExecuteW documentation, anything >32 indicates success.
Expand All @@ -126,7 +126,7 @@ ErrorOr<bool> UrlLauncherPlugin::LaunchUrl(const std::string& url) {
return false;
}
std::ostringstream error_message;
error_message << "Failed to open " << url << ": ShellExecute error code "
error_message << "Failed to open " << url_to_open << ": ShellExecute error code "
<< status;
return FlutterError("open_error", error_message.str());
}
Expand Down

0 comments on commit 1ff6045

Please sign in to comment.