This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[url_launcher] Add native unit tests for Windows #4156
Merged
stuartmorgan-g
merged 12 commits into
flutter:master
from
stuartmorgan-g:windows-unit-test
Aug 17, 2021
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6b6d723
Initial proof of concept
stuartmorgan-g 99b2ab1
Refactor file structure, plugin class, for testability
stuartmorgan-g bc94f32
Add real tests, and make them build.
stuartmorgan-g e239b3d
Tweaks
stuartmorgan-g ec3b3e2
Update changelog
stuartmorgan-g a6a1e8b
Improve the filename mess
stuartmorgan-g ce8bb54
Format
stuartmorgan-g eb432df
Merge branch 'master' into windows-unit-test
stuartmorgan-g 8542f52
Missing license
stuartmorgan-g 3032865
Merge branch 'master' into windows-unit-test
stuartmorgan-g cef2f07
Fix weird autoformat
stuartmorgan-g c5c0a8f
Fix weird autoformat
stuartmorgan-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Refactor file structure, plugin class, for testability
- Loading branch information
commit 99b2ab190e4f089ad03145d33057c298172e48c8
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
packages/url_launcher/url_launcher_windows/windows/system_apis.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
| #include "system_apis.h" | ||
|
|
||
| #include <windows.h> | ||
|
|
||
| namespace url_launcher_plugin { | ||
|
|
||
| SystemApis::SystemApis() {} | ||
|
|
||
| SystemApisImpl::SystemApisImpl() {} | ||
|
|
||
| LSTATUS SystemApisImpl::RegCloseKey(HKEY key) { return ::RegCloseKey(key); } | ||
|
|
||
| LSTATUS SystemApisImpl::RegOpenKeyExW(HKEY key, LPCWSTR sub_key, DWORD options, | ||
| REGSAM desired, PHKEY result) { | ||
| return ::RegOpenKeyExW(key, sub_key, options, desired, result); | ||
| } | ||
|
|
||
| LSTATUS SystemApisImpl::RegQueryValueExW(HKEY key, LPCWSTR value_name, | ||
| LPDWORD type, LPBYTE data, | ||
| LPDWORD data_size) { | ||
| return ::RegQueryValueExW(key, value_name, nullptr, type, data, data_size); | ||
| } | ||
|
|
||
| HINSTANCE SystemApisImpl::ShellExecuteW(HWND hwnd, LPCWSTR operation, | ||
| LPCWSTR file, LPCWSTR parameters, | ||
| LPCWSTR directory, int show_flags) { | ||
| return ::ShellExecuteW(hwnd, operation, file, parameters, directory, | ||
| show_flags); | ||
| } | ||
|
|
||
| } // namespace url_launcher_plugin |
56 changes: 56 additions & 0 deletions
56
packages/url_launcher/url_launcher_windows/windows/system_apis.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
| #include <windows.h> | ||
|
|
||
| namespace url_launcher_plugin { | ||
|
|
||
| // An interface wrapping system APIs used by the plugin, for mocking. | ||
| class SystemApis { | ||
| public: | ||
| SystemApis(); | ||
| virtual ~SystemApis(); | ||
|
|
||
| // Disallow copy and move. | ||
| SystemApis(const SystemApis&) = delete; | ||
| SystemApis& operator=(const SystemApis&) = delete; | ||
|
|
||
| // Wrapper for RegCloseKey. | ||
| virtual LSTATUS RegCloseKey(HKEY key) = 0; | ||
|
|
||
| // Wrapper for RegQueryValueEx. | ||
| virtual LSTATUS RegQueryValueExW(HKEY key, LPCWSTR value_name, LPDWORD type, | ||
| LPBYTE data, LPDWORD data_size) = 0; | ||
|
|
||
| // Wrapper for RegOpenKeyEx. | ||
| virtual LSTATUS RegOpenKeyExW(HKEY key, LPCWSTR sub_key, DWORD options, | ||
| REGSAM desired, PHKEY result) = 0; | ||
|
|
||
| // Wrapper for ShellExecute. | ||
| virtual HINSTANCE ShellExecuteW(HWND hwnd, LPCWSTR operation, LPCWSTR file, | ||
| LPCWSTR parameters, LPCWSTR directory, | ||
| int show_flags) = 0; | ||
| }; | ||
|
|
||
| // Implementation of SystemApis using the Win32 APIs. | ||
| class SystemApisImpl : public SystemApis { | ||
| public: | ||
| SystemApisImpl(); | ||
| virtual ~SystemApisImpl(); | ||
|
|
||
| // Disallow copy and move. | ||
| SystemApisImpl(const SystemApisImpl&) = delete; | ||
| SystemApisImpl& operator=(const SystemApisImpl&) = delete; | ||
|
|
||
| // SystemApis Implementation: | ||
| virtual LSTATUS RegCloseKey(HKEY key); | ||
| virtual LSTATUS RegOpenKeyExW(HKEY key, LPCWSTR sub_key, DWORD options, | ||
| REGSAM desired, PHKEY result); | ||
| virtual LSTATUS RegQueryValueExW(HKEY key, LPCWSTR value_name, LPDWORD type, | ||
| LPBYTE data, LPDWORD data_size); | ||
| virtual HINSTANCE ShellExecuteW(HWND hwnd, LPCWSTR operation, LPCWSTR file, | ||
| LPCWSTR parameters, LPCWSTR directory, | ||
| int show_flags); | ||
| }; | ||
|
|
||
| } // namespace url_launcher_plugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/url_launcher/url_launcher_windows/windows/url_launcher_plugin.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
| #include <flutter/method_channel.h> | ||
| #include <flutter/plugin_registrar_windows.h> | ||
| #include <windows.h> | ||
|
|
||
| #include <memory> | ||
| #include <optional> | ||
| #include <sstream> | ||
| #include <string> | ||
|
|
||
| #include "system_apis.h" | ||
|
|
||
| namespace url_launcher_plugin { | ||
|
|
||
| class UrlLauncherPlugin : public flutter::Plugin { | ||
| public: | ||
| static void RegisterWithRegistrar(flutter::PluginRegistrar* registrar); | ||
|
|
||
| virtual ~UrlLauncherPlugin(); | ||
|
|
||
| // Disallow copy and move. | ||
| UrlLauncherPlugin(const UrlLauncherPlugin&) = delete; | ||
| UrlLauncherPlugin& operator=(const UrlLauncherPlugin&) = delete; | ||
|
|
||
| private: | ||
| UrlLauncherPlugin(); | ||
|
|
||
| // Creates a plugin instance with the given SystemApi instance. | ||
| // | ||
| // Exists for unit testing with mock implementations. | ||
| UrlLauncherPlugin(std::unique_ptr<SystemApis> system_apis); | ||
|
|
||
| // Called when a method is called on plugin channel; | ||
| void HandleMethodCall(const flutter::MethodCall<>& method_call, | ||
| std::unique_ptr<flutter::MethodResult<>> result); | ||
|
|
||
| // Returns whether or not the given URL has a registered handler. | ||
| bool CanLaunchUrl(const std::string& url); | ||
|
|
||
| // Attempts to launch the given URL. On failure, returns an error string. | ||
| std::optional<std::string> LaunchUrl(const std::string& url); | ||
|
|
||
| std::unique_ptr<SystemApis> system_apis_; | ||
| }; | ||
|
|
||
| } // namespace url_launcher_plugin |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off-topic for this patch, but I bet there's some Win32 API for parsing URLs out there. If the Dart side of this is already using the
Urlclass it's much less of an issue; otherwise, : can appear as the port separator etc., or just as part of some garbage string the person passed in here. I suppse the net result would mostly be them shooting themselves in their own foot regardless unless they're writing an app to poke at a very specific and maybe not-particularly-valuable to know subset of users' registry keys.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Dart side of the API is kind of a hot mess (flutter/flutter#79043), and one of the things I dislike most about it is that it takes a
String. Someday I want to get back to that proposal and do the overhaul.Yes, and at least unlike some of the other issues (like including unencoded spaces, which people do all the time) trying to pass a "URL" that doesn't actually have a scheme should completely fail on every platform, so be very easy to notice during development.