Skip to content

Commit

Permalink
Merge pull request #2840 from Wox-launcher/bao
Browse files Browse the repository at this point in the history
generic query test and less exception
  • Loading branch information
bao-qian authored Apr 22, 2020
2 parents e8e4aa8 + 552010a commit 24a4b73
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 76 deletions.
28 changes: 19 additions & 9 deletions Plugins/Wox.Plugin.Everything/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
Expand All @@ -16,10 +17,11 @@ namespace Wox.Plugin.Everything
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable
{

public const string DLL = "Everything.dll";
private readonly IEverythingApi _api = new EverythingApi();



private PluginInitContext _context;

Expand All @@ -40,7 +42,7 @@ public List<Result> Query(Query query)
if (!string.IsNullOrEmpty(query.Search))
{
var keyword = query.Search;

try
{
var searchList = _api.Search(keyword, cts.Token, maxCount: _settings.MaxSearchCount);
Expand Down Expand Up @@ -105,7 +107,9 @@ private Result CreateResult(string keyword, SearchResult searchResult)
{
Process.Start(new ProcessStartInfo
{
FileName = path, UseShellExecute = true, WorkingDirectory = workingDir
FileName = path,
UseShellExecute = true,
WorkingDirectory = workingDir
});
hide = true;
}
Expand All @@ -125,7 +129,7 @@ private Result CreateResult(string keyword, SearchResult searchResult)
return r;
}



private List<ContextMenu> GetDefaultContextMenu()
{
Expand Down Expand Up @@ -167,18 +171,24 @@ public void Init(PluginInitContext context)

var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
const string sdk = "EverythingSDK";
var bundledSDKDirectory = Path.Combine(pluginDirectory, sdk, CpuType());
var sdkDirectory = Path.Combine(_storage.DirectoryPath, sdk, CpuType());
Helper.ValidateDataDirectory(bundledSDKDirectory, sdkDirectory);

var sdkDirectory = Path.Combine(pluginDirectory, sdk, CpuType());
var sdkPath = Path.Combine(sdkDirectory, DLL);
Log.Info("Everything", $"sdk path {sdkPath}");
Constant.EverythingSDKPath = sdkPath;
_api.Load(sdkPath);
}

private static string CpuType()
{
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
if (!Environment.Is64BitProcess)
{
return "x86";
}
else
{
return "x64";
}

}

public string GetTranslatedPluginTitle()
Expand Down
42 changes: 23 additions & 19 deletions Plugins/Wox.Plugin.Program/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public Main()
{
_settingsStorage = new PluginJsonStorage<Settings>();
_settings = _settingsStorage.Load();

preloadPrograms();
IndexPrograms();
}

private static void preloadPrograms()
{
Stopwatch.Normal("|Wox.Plugin.Program.Main|Preload programs cost", () =>
{
_win32Storage = new BinaryStorage<Win32[]>("Win32");
Expand All @@ -42,22 +48,6 @@ public Main()
});
Log.Info($"|Wox.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Length}>");
Log.Info($"|Wox.Plugin.Program.Main|Number of preload uwps <{_uwps.Length}>");

var a = Task.Run(() =>
{
if (IsStartupIndexProgramsRequired || !_win32s.Any())
Stopwatch.Normal("|Wox.Plugin.Program.Main|Win32Program index cost", IndexWin32Programs);
});

var b = Task.Run(() =>
{
if (IsStartupIndexProgramsRequired || !_uwps.Any())
Stopwatch.Normal("|Wox.Plugin.Program.Main|Win32Program index cost", IndexUWPPrograms);
});

Task.WaitAll(a, b);

_settings.LastIndexTime = DateTime.Today;
}

public void Save()
Expand Down Expand Up @@ -131,12 +121,26 @@ public static void IndexUWPPrograms()

public static void IndexPrograms()
{
var t1 = Task.Run(() => IndexWin32Programs());
var a = Task.Run(() =>
{
if (IsStartupIndexProgramsRequired || !_win32s.Any())
Stopwatch.Normal("|Wox.Plugin.Program.Main|Win32Program index cost", IndexWin32Programs);
});

var t2 = Task.Run(() => IndexUWPPrograms());
var b = Task.Run(() =>
{
if (IsStartupIndexProgramsRequired || !_uwps.Any())
Stopwatch.Normal("|Wox.Plugin.Program.Main|Win32Program index cost", IndexUWPPrograms);
});

Task.WaitAll(t1, t2);
Task.WaitAll(a, b);

Log.Info($"|Wox.Plugin.Program.Main|Number of indexed win32 programs <{_win32s.Length}>");
Log.Info($"|Wox.Plugin.Program.Main|Number of indexed uwps <{_uwps.Length}>");
foreach (var uwp in _uwps)
{
Log.Info($"|Wox.Plugin.Program.Main| uwp: {uwp.DisplayName}>");
}
_settings.LastIndexTime = DateTime.Today;
}

Expand Down
5 changes: 1 addition & 4 deletions Plugins/Wox.Plugin.Program/Programs/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static Win32 Win32Program(string path)
return new Win32() { Valid = false, Enabled = false };
}
}

// todo lnk.resolve has been removed, need test to get description and image for lnk only instead of target
private static Win32 LnkProgram(string path)
{
var program = Win32Program(path);
Expand All @@ -185,9 +185,6 @@ private static Win32 LnkProgram(string path)
var link = new ShellLink();
const uint STGM_READ = 0;
((IPersistFile)link).Load(path, STGM_READ);
var hwnd = new _RemotableHandle();
link.Resolve(ref hwnd, 0);

const int MAX_DESCRIPTION = 150;
StringBuilder bufferDescription = new StringBuilder(MAX_DESCRIPTION);
String description = String.Empty;
Expand Down
1 change: 1 addition & 0 deletions Wox.Core/Resource/Internationalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private void AddPluginLanguageDirectories()
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
{
var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location;
Log.Debug(nameof(Internationalization), $"Plugin language location {plugin.Plugin}: {location}");
var dir = Path.GetDirectoryName(location);
if (dir != null)
{
Expand Down
39 changes: 29 additions & 10 deletions Wox.Infrastructure/Constant.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using JetBrains.Annotations;

namespace Wox.Infrastructure
{
Expand All @@ -9,21 +11,38 @@ public static class Constant
public const string Wox = "Wox";
public const string Plugins = "Plugins";

private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString();
public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe");
public static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString();
public static readonly string RootDirectory = Directory.GetParent(ApplicationDirectory).ToString();
public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins);
private static Assembly Assembly;
public static string ProgramDirectory;
public static string ExecutablePath;
public static string ApplicationDirectory;
public static string RootDirectory;

public static string PreinstalledDirectory;
public const string Issue = "https://github.com/Wox-launcher/Wox/issues/new";
public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location.NonNull()).ProductVersion;
public static string Version;

public static readonly int ThumbnailSize = 64;
public static readonly string DefaultIcon = Path.Combine(ProgramDirectory, "Images", "app.png");
public static readonly string ErrorIcon = Path.Combine(ProgramDirectory, "Images", "app_error.png");
public static string ImagesDirectory;
public static string DefaultIcon;
public static string ErrorIcon;

public static string PythonPath;
public static string EverythingSDKPath;

public static void Initialize()
{
Assembly = Assembly.GetExecutingAssembly();
Version = FileVersionInfo.GetVersionInfo(Assembly.Location.NonNull()).ProductVersion;
ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString();

ApplicationDirectory = Directory.GetParent(ProgramDirectory).ToString();
RootDirectory = Directory.GetParent(ApplicationDirectory).ToString();
ExecutablePath = Path.Combine(ProgramDirectory, Wox + ".exe");
ImagesDirectory = Path.Combine(ProgramDirectory, "Images");
PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins);
DefaultIcon = Path.Combine(ImagesDirectory, "app.png");
ErrorIcon = Path.Combine(ImagesDirectory, "app_error.png");

}
}
}
15 changes: 11 additions & 4 deletions Wox.Infrastructure/Image/ImageLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,16 +43,21 @@ public static void Initialize()
img.Freeze();
ImageCache[icon] = img;
}

Task.Run(() =>
{
Stopwatch.Normal("|ImageLoader.Initialize|Preload images cost", () =>
{
ImageCache.Usage.AsParallel().ForAll(x =>
foreach (string key in ImageCache.Usage.Keys)
{
Load(x.Key);
});
Load(key);
}
});
Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
string info = "|ImageLoader.Initialize|" +
$"Number of preload images is <{ImageCache.Usage.Count}>, " +
$"Images Number: {ImageCache.CacheSize()}, " +
$"Unique Items {ImageCache.UniqueImagesInCache()}";
Log.Info(info);
});
}

Expand Down Expand Up @@ -85,6 +91,7 @@ private enum ImageType

private static ImageResult LoadInternal(string path, bool loadFullImage = false)
{
Log.Debug(nameof(ImageLoader), $"image {path} {loadFullImage}");
ImageSource image;
ImageType type = ImageType.Error;
try
Expand Down
8 changes: 6 additions & 2 deletions Wox.Infrastructure/Logger/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ private static bool FormatValid(string message)
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
{
#if DEBUG
throw exception;
#else
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);

ExceptionInternal(classNameWithMethod, message, exception);
#endif
}

private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,
Expand Down Expand Up @@ -86,7 +90,7 @@ private static void ExceptionInternal(string classAndMethod, string message, Sys
{
var logger = LogManager.GetLogger(classAndMethod);

System.Diagnostics.Debug.WriteLine($"ERROR|{message}");
System.Diagnostics.Debug.WriteLine($"ERROR|{classAndMethod}|{message}");

logger.Error("-------------------------- Begin exception --------------------------");
logger.Error(message);
Expand Down Expand Up @@ -161,7 +165,7 @@ private static void LogInternal(LogLevel level, string className, string message

var logger = LogManager.GetLogger(classNameWithMethod);

System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
System.Diagnostics.Debug.WriteLine($"{level.Name}|{classNameWithMethod}|{message}");
logger.Log(level, message);
}

Expand Down
66 changes: 66 additions & 0 deletions Wox.Test/PluginManagerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Wox.Core;
using Wox.Core.Configuration;
using Wox.Core.Plugin;
using Wox.Infrastructure;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.ViewModel;

namespace Wox.Test
{
[TestFixture]
class PluginManagerTest
{
[OneTimeSetUp]
public void setUp()
{
// todo remove i18n from application / ui, so it can be tested in a modular way
new App();

Constant.Initialize();
ImageLoader.Initialize();

Updater updater = new Updater("");
Portable portable = new Portable();
SettingWindowViewModel settingsVm = new SettingWindowViewModel(updater, portable);
Settings settings = settingsVm.Settings;

Alphabet alphabet = new Alphabet();
alphabet.Initialize(settings);
StringMatcher stringMatcher = new StringMatcher(alphabet);
StringMatcher.Instance = stringMatcher;
stringMatcher.UserSettingSearchPrecision = settings.QuerySearchPrecision;

PluginManager.LoadPlugins(settings.PluginSettings);
MainViewModel mainVm = new MainViewModel(settings, false);
PublicAPIInstance api = new PublicAPIInstance(settingsVm, mainVm, alphabet);
PluginManager.InitializePlugins(api);

}

[TestCase("powershell", "PowerShell")]
[TestCase("note", "Notepad")]
[TestCase("setting", "Settings")]
[TestCase("compu", "computer")]
[TestCase("netwo", "Network and Sharing Center")]
public void ProgramPluginTest(string QueryText, string ResultTitle)
{

Query query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
List<PluginPair> plugins = PluginManager.ValidPluginsForQuery(query);
Result result = plugins.SelectMany(
p => PluginManager.QueryForPlugin(p, query)
)
.OrderByDescending(r => r.Score)
.First();

// we won't compre all content, since content description may too long to write testcase
Assert.IsTrue(result.Title.StartsWith(ResultTitle));
}
}
}
17 changes: 0 additions & 17 deletions Wox.Test/Plugins/PluginInitTest.cs

This file was deleted.

Loading

0 comments on commit 24a4b73

Please sign in to comment.