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:
Alekhya 2020-07-22 10:58:01 -07:00 committed by GitHub
parent f7ad935dfb
commit b59ec5e78b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 29 deletions

View File

@ -198,8 +198,30 @@ 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);
@ -213,8 +235,35 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
_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")]

View File

@ -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,12 +167,28 @@ namespace Microsoft.Plugin.Program.Storage
private void OnAppCreated(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);
}
}
}
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);
}
}
}
public void IndexPrograms()
{

View File

@ -12,14 +12,6 @@ namespace Wox.Infrastructure.FileSystemHelper
public FileWrapper() { }
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
{
@ -27,15 +19,9 @@ namespace Wox.Infrastructure.FileSystemHelper
}
catch (IOException ex)
{
attempt++;
Thread.Sleep(500);
Log.Info($"File {path} is being accessed by another process| {ex.Message}");
}
}
return new string[] { String.Empty };
}
}
}
}

View File

@ -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