mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 06:29:44 +08:00
Fix for internet shortcut app not showing up on installation (#5131)
* added changed for internet shortcut app * Added AppChanged event for url files * Tests added * refactoring
This commit is contained in:
parent
f7ad935dfb
commit
b59ec5e78b
@ -198,23 +198,72 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
||||
}
|
||||
|
||||
[TestCase("path.url")]
|
||||
public void Win32ProgramRepository_MustCallOnAppCreatedForUrlApps_WhenCreatedEventIsRaised(string path)
|
||||
public void Win32ProgramRepository_MustCallOnAppChangedForUrlApps_WhenChangedEventIsRaised(string path)
|
||||
{
|
||||
// Arrange
|
||||
Win32ProgramRepository _win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage<IList<Win32>>("Win32"), _settings, _pathsToWatch);
|
||||
FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Changed, "directory", path);
|
||||
|
||||
// File.ReadAllLines must be mocked for url applications
|
||||
var mockFile = new Mock<IFileWrapper>();
|
||||
mockFile.Setup(m => m.ReadAllLines(It.IsAny<string>())).Returns(new string[] { "URL=steam://rungameid/1258080", "IconFile=iconFile" });
|
||||
Win32._fileWrapper = mockFile.Object;
|
||||
|
||||
// Act
|
||||
_fileSystemMocks[0].Raise(m => m.Changed += null, e);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(_win32ProgramRepository.Count(), 1);
|
||||
Assert.AreEqual(_win32ProgramRepository.ElementAt(0).AppType, 1); // Internet Shortcut Application
|
||||
}
|
||||
|
||||
[TestCase("path.url")]
|
||||
public void Win32ProgramRepository_MustNotCreateUrlApp_WhenCreatedEventIsRaised(string path)
|
||||
{
|
||||
// We are handing internet shortcut apps using the Changed event instead
|
||||
|
||||
// Arrange
|
||||
Win32ProgramRepository _win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage<IList<Win32>>("Win32"), _settings, _pathsToWatch);
|
||||
FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Created, "directory", path);
|
||||
|
||||
// File.ReadAllLines must be mocked for url applications
|
||||
var mockFile = new Mock<IFileWrapper>();
|
||||
mockFile.Setup(m => m.ReadAllLines(It.IsAny<string>())).Returns(new string[] { "URL=steam://rungameid/1258080" , "IconFile=iconFile"});
|
||||
mockFile.Setup(m => m.ReadAllLines(It.IsAny<string>())).Returns(new string[] { "URL=steam://rungameid/1258080", "IconFile=iconFile" });
|
||||
Win32._fileWrapper = mockFile.Object;
|
||||
|
||||
// Act
|
||||
_fileSystemMocks[0].Raise(m => m.Created += null, e);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(_win32ProgramRepository.Count(), 1);
|
||||
Assert.AreEqual(_win32ProgramRepository.ElementAt(0).AppType, 1); // Internet Shortcut Application
|
||||
Assert.AreEqual(_win32ProgramRepository.Count(), 0);
|
||||
}
|
||||
|
||||
[TestCase("path.exe")]
|
||||
[TestCase("path.lnk")]
|
||||
[TestCase("path.appref-ms")]
|
||||
public void Win32ProgramRepository_MustNotCreateAnyAppOtherThanUrlApp_WhenChangedEventIsRaised(string path)
|
||||
{
|
||||
// We are handing internet shortcut apps using the Changed event instead
|
||||
|
||||
// Arrange
|
||||
Win32ProgramRepository _win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, new BinaryStorage<IList<Win32>>("Win32"), _settings, _pathsToWatch);
|
||||
FileSystemEventArgs e = new FileSystemEventArgs(WatcherChangeTypes.Changed, "directory", path);
|
||||
|
||||
// FileVersionInfo must be mocked for exe applications
|
||||
var mockFileVersionInfo = new Mock<IFileVersionInfoWrapper>();
|
||||
mockFileVersionInfo.Setup(m => m.GetVersionInfo(It.IsAny<string>())).Returns((FileVersionInfo)null);
|
||||
Win32._fileVersionInfoWrapper = mockFileVersionInfo.Object;
|
||||
|
||||
// ShellLinkHelper must be mocked for lnk applications
|
||||
var mockShellLink = new Mock<IShellLinkHelper>();
|
||||
mockShellLink.Setup(m => m.RetrieveTargetPath(It.IsAny<string>())).Returns(String.Empty);
|
||||
Win32._helper = mockShellLink.Object;
|
||||
|
||||
// Act
|
||||
_fileSystemMocks[0].Raise(m => m.Changed += null, e);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(_win32ProgramRepository.Count(), 0);
|
||||
}
|
||||
|
||||
[TestCase("directory", "path.url")]
|
||||
|
@ -39,7 +39,7 @@ namespace Microsoft.Plugin.Program.Storage
|
||||
_fileSystemWatcherHelpers[index].Path = _pathsToWatch[index];
|
||||
|
||||
// to be notified when there is a change to a file
|
||||
_fileSystemWatcherHelpers[index].NotifyFilter = NotifyFilters.FileName;
|
||||
_fileSystemWatcherHelpers[index].NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
|
||||
|
||||
// filtering the app types that we want to monitor
|
||||
_fileSystemWatcherHelpers[index].Filters = extensionsToWatch;
|
||||
@ -48,6 +48,7 @@ namespace Microsoft.Plugin.Program.Storage
|
||||
_fileSystemWatcherHelpers[index].Created += OnAppCreated;
|
||||
_fileSystemWatcherHelpers[index].Deleted += OnAppDeleted;
|
||||
_fileSystemWatcherHelpers[index].Renamed += OnAppRenamed;
|
||||
_fileSystemWatcherHelpers[index].Changed += OnAppChanged;
|
||||
|
||||
// Enable the file system watcher
|
||||
_fileSystemWatcherHelpers[index].EnableRaisingEvents = true;
|
||||
@ -166,10 +167,26 @@ namespace Microsoft.Plugin.Program.Storage
|
||||
private void OnAppCreated(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
string path = e.FullPath;
|
||||
Programs.Win32 app = Programs.Win32.GetAppFromPath(path);
|
||||
if (app != null)
|
||||
if(!Path.GetExtension(path).Equals(urlExtension))
|
||||
{
|
||||
Add(app);
|
||||
Programs.Win32 app = Programs.Win32.GetAppFromPath(path);
|
||||
if (app != null)
|
||||
{
|
||||
Add(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAppChanged(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
string path = e.FullPath;
|
||||
if (Path.GetExtension(path).Equals(urlExtension))
|
||||
{
|
||||
Programs.Win32 app = Programs.Win32.GetAppFromPath(path);
|
||||
if (app != null)
|
||||
{
|
||||
Add(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,29 +13,15 @@ namespace Wox.Infrastructure.FileSystemHelper
|
||||
|
||||
public string[] ReadAllLines(string path)
|
||||
{
|
||||
int attempt = 0;
|
||||
int maxRetries = 5;
|
||||
|
||||
// Sometimes when files are being installed, url applications are written to line by line.
|
||||
// During this process their contents cannot be read as they are being accessed by an other process.
|
||||
// This ensures that if we do face this scenario, we retry after some time.
|
||||
while(attempt < maxRetries)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
return File.ReadAllLines(path);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
attempt++;
|
||||
Thread.Sleep(500);
|
||||
Log.Info($"File {path} is being accessed by another process| {ex.Message}");
|
||||
|
||||
}
|
||||
return File.ReadAllLines(path);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Log.Info($"File {path} is being accessed by another process| {ex.Message}");
|
||||
return new string[] { String.Empty };
|
||||
}
|
||||
|
||||
return new string[] { String.Empty };
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ namespace Wox.Infrastructure.Storage
|
||||
// Events to watch out for
|
||||
event FileSystemEventHandler Created;
|
||||
event FileSystemEventHandler Deleted;
|
||||
event FileSystemEventHandler Changed;
|
||||
event RenamedEventHandler Renamed;
|
||||
|
||||
// Properties of File System watcher
|
||||
|
Loading…
Reference in New Issue
Block a user