Add run as admin context menu item for application results returned by the Indexer Plugin (#4807)

* Added run as admin context menu item to apps returned by indexer plugin

* Added a test and localized strings

* localize strings

* Add more tests for folder and other file types

* fixed run as admin -> run as administrator

* resolved merge conflict

* refactored tests

* moved common code to helper and added logs

* moved start process to the helper class

* added more info in a comment

* fixed count in tests as open in console was added

* removed additional code that was added while fixing merge conflicts
This commit is contained in:
Alekhya
2020-07-08 13:05:57 -07:00
committed by GitHub
parent 638cd1dd48
commit 411140c3ea
11 changed files with 162 additions and 40 deletions

View File

@@ -1,14 +1,13 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using Microsoft.Search.Interop;
using Microsoft.Plugin.Indexer.SearchHelper;
using Microsoft.Plugin.Indexer.Interface;
using Moq;
using System.Linq;
using Microsoft.Plugin.Indexer;
using Moq;
using Wox.Plugin;
using System.Linq;
namespace Wox.Test.Plugins
{
@@ -217,45 +216,86 @@ namespace Wox.Test.Plugins
Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
}
[Test]
public void ContextMenuLoader_ReturnContextMenuForFolderWithOpenInConsole_WhenLoadContextMenusIsCalled()
[TestCase("item.exe")]
[TestCase("item.bat")]
[TestCase("item.appref-ms")]
[TestCase("item.lnk")]
public void LoadContextMenus_MustLoadAllItems_WhenFileIsAnApp(string path)
{
// Arrange
var mock = new Mock<IPublicAPI>();
mock.Setup(api => api.GetTranslation(It.IsAny<string>())).Returns(It.IsAny<string>());
var pluginInitContext = new PluginInitContext() { API = mock.Object };
var contextMenuLoader = new ContextMenuLoader(pluginInitContext);
var searchResult = new SearchResult() { Path = "C:/DummyFolder", Title = "DummyFolder" };
var result = new Result() { ContextData = searchResult };
// Arrange
var mockapi = new Mock<IPublicAPI>();
var pluginInitContext = new PluginInitContext() { API = mockapi.Object };
ContextMenuLoader _contextMenuLoader = new ContextMenuLoader(pluginInitContext);
// Act
List<ContextMenuResult> contextMenuResults = contextMenuLoader.LoadContextMenus(result);
Result result = new Result
{
ContextData = new SearchResult { Path = path }
};
List<ContextMenuResult> contextMenuItems = _contextMenuLoader.LoadContextMenus(result);
// Assert
Assert.AreEqual(contextMenuResults.Count, 2);
mock.Verify(x => x.GetTranslation("Microsoft_plugin_indexer_copy_path"), Times.Once());
mock.Verify(x => x.GetTranslation("Microsoft_plugin_indexer_open_in_console"), Times.Once());
Assert.AreEqual(contextMenuItems.Count, 4);
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_copy_path"), Times.Once());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_run_as_administrator"), Times.Once());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"), Times.Once());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_in_console"), Times.Once());
}
[Test]
public void ContextMenuLoader_ReturnContextMenuForFileWithOpenInConsole_WhenLoadContextMenusIsCalled()
[TestCase("item.pdf")]
[TestCase("item.xls")]
[TestCase("item.ppt")]
[TestCase("C:/DummyFile.cs")]
public void LoadContextMenus_MustNotLoadRunAsAdmin_WhenFileIsAnNotApp(string path)
{
// Arrange
var mock = new Mock<IPublicAPI>();
mock.Setup(api => api.GetTranslation(It.IsAny<string>())).Returns(It.IsAny<string>());
var pluginInitContext = new PluginInitContext() { API = mock.Object };
var contextMenuLoader = new ContextMenuLoader(pluginInitContext);
var searchResult = new SearchResult() { Path = "C:/DummyFile.cs", Title = "DummyFile.cs" };
var result = new Result() { ContextData = searchResult };
// Arrange
var mockapi = new Mock<IPublicAPI>();
var pluginInitContext = new PluginInitContext() { API = mockapi.Object };
ContextMenuLoader _contextMenuLoader = new ContextMenuLoader(pluginInitContext);
// Act
List<ContextMenuResult> contextMenuResults = contextMenuLoader.LoadContextMenus(result);
Result result = new Result
{
ContextData = new SearchResult { Path = path }
};
List<ContextMenuResult> contextMenuItems = _contextMenuLoader.LoadContextMenus(result);
// Assert
Assert.AreEqual(contextMenuResults.Count, 3);
mock.Verify(x => x.GetTranslation("Microsoft_plugin_indexer_copy_path"), Times.Once());
mock.Verify(x => x.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"), Times.Once());
mock.Verify(x => x.GetTranslation("Microsoft_plugin_indexer_open_in_console"), Times.Once());
Assert.AreEqual(contextMenuItems.Count, 3);
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_copy_path"), Times.Once());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_run_as_administrator"), Times.Never());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"), Times.Once());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_in_console"), Times.Once());
}
[TestCase("C:/DummyFolder")]
[TestCase("TestFolder")]
public void LoadContextMenus_MustNotLoadRunAsAdminAndOpenContainingFolder_ForFolder(string path)
{
// Arrange
var mockapi = new Mock<IPublicAPI>();
var pluginInitContext = new PluginInitContext() { API = mockapi.Object };
ContextMenuLoader _contextMenuLoader = new ContextMenuLoader(pluginInitContext);
// Act
Result result = new Result
{
ContextData = new SearchResult { Path = path }
};
List<ContextMenuResult> contextMenuItems = _contextMenuLoader.LoadContextMenus(result);
// Assert
Assert.AreEqual(contextMenuItems.Count, 2);
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_copy_path"), Times.Once());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_run_as_administrator"), Times.Never());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"), Times.Never());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_in_console"), Times.Once());
}
}
}