Skip to content

Commit

Permalink
add support for multiple Edge profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegiacometti committed Jan 20, 2024
1 parent 76171c7 commit 77e41c9
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
// Copyright (c) Davide Giacometti. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests
{
[TestClass]
public class FavoriteQueryTest
{
private readonly IFavoriteProvider _favoriteProvider;
private readonly IProfileManager _singleProfileManager;
private readonly FavoriteQuery _singleFavoriteQuery;

private readonly IProfileManager _multiProfileManager;
private readonly FavoriteQuery _multiFavoriteQuery;

public FavoriteQueryTest()
{
_favoriteProvider = new MockFavoriteProvider();
_singleProfileManager = new SingleProfileManager();
_singleFavoriteQuery = new FavoriteQuery(_singleProfileManager);

_multiProfileManager = new MultiProfileManager();
_multiFavoriteQuery = new FavoriteQuery(_multiProfileManager);
}

[TestMethod]
public void Should_Get_All_Urls()
{
var sut = new FavoriteQuery();
var result = sut.GetAll(_favoriteProvider.Root);
var result = _singleFavoriteQuery.GetAll();
Assert.AreEqual(result.Count(), 10);
}

[TestMethod]
public void Should_Get_All_Urls_From_All_Profiles()
{
var result = _multiFavoriteQuery.GetAll();
Assert.AreEqual(result.Count(), 14);
}

[DataTestMethod]
[DataRow("", 5)]
[DataRow("/", 0)]
Expand All @@ -36,11 +52,25 @@ public void Should_Get_All_Urls()
[DataRow("Coding/Tools", 1)]
[DataRow("Coding/Tools/", 2)]
[DataRow("coding/tools/j", 1)]
[DataRow("coding\\tools\\j", 1)]
public void Should_Get_Expected_Result(string search, int expectedResult)
{
var sut = new FavoriteQuery();
var result = sut.Search(_favoriteProvider.Root, search.Split('/'), 0);
var result = _singleFavoriteQuery.Search(search);
Assert.AreEqual(result.Count(), expectedResult);
}

[TestMethod]
public void Should_Get_Single_Folder()
{
var result = _multiFavoriteQuery.Search("Codi");
Assert.AreEqual(result.Count(), 1);
}

[TestMethod]
public void Should_Merge_Folders_Content()
{
var result = _multiFavoriteQuery.Search("Coding/");
Assert.AreEqual(result.Count(), 7);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@
using Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests
namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks
{
public class MockFavoriteProvider : IFavoriteProvider
public class DefaultFavoriteProvider : IFavoriteProvider
{
private readonly ProfileInfo _profileInfo = new("Default", "Default");
private readonly FavoriteItem _root;

public FavoriteItem Root => _root;

public MockFavoriteProvider()
public DefaultFavoriteProvider()
{
var coding = new FavoriteItem("Coding", null, "Coding", FavoriteType.Folder);
coding.AddChildren(new FavoriteItem("GitHub", "https://github.com/", "Coding/GitHub", FavoriteType.Url));
coding.AddChildren(new FavoriteItem("Microsoft Azure", "https://portal.azure.com/", "Coding/Microsoft Azure", FavoriteType.Url));
coding.AddChildren(new FavoriteItem("Microsoft Developer Blogs", "https://devblogs.microsoft.com/", "Coding/Microsoft Developer Blogs", FavoriteType.Url));
var coding = new FavoriteItem("Coding", "Coding");
coding.AddChildren(new FavoriteItem("GitHub", "https://github.com/", "Coding/GitHub", _profileInfo));
coding.AddChildren(new FavoriteItem("Microsoft Azure", "https://portal.azure.com/", "Coding/Microsoft Azure", _profileInfo));
coding.AddChildren(new FavoriteItem("Microsoft Developer Blogs", "https://devblogs.microsoft.com/", "Coding/Microsoft Developer Blogs", _profileInfo));

var tools = new FavoriteItem("Tools", null, "Coding", FavoriteType.Folder);
tools.AddChildren(new FavoriteItem("JWT", "https://jwt.io/", "Coding/Tools/JWT", FavoriteType.Url));
tools.AddChildren(new FavoriteItem("Pigment", "https://pigment.shapefactory.co/", "Coding/Tools/Pigment", FavoriteType.Url));
var tools = new FavoriteItem("Tools", "Coding");
tools.AddChildren(new FavoriteItem("JWT", "https://jwt.io/", "Coding/Tools/JWT", _profileInfo));
tools.AddChildren(new FavoriteItem("Pigment", "https://pigment.shapefactory.co/", "Coding/Tools/Pigment", _profileInfo));
coding.AddChildren(tools);

var shopping = new FavoriteItem("Shopping", null, "Shopping", FavoriteType.Folder);
shopping.AddChildren(new FavoriteItem("Amazon", "https://www.amazon.com/", "Shopping/Amazon", FavoriteType.Url));
shopping.AddChildren(new FavoriteItem("eBay", "https://www.ebay.com/", "Shopping/eBay", FavoriteType.Url));
var shopping = new FavoriteItem("Shopping", "Shopping");
shopping.AddChildren(new FavoriteItem("Amazon", "https://www.amazon.com/", "Shopping/Amazon", _profileInfo));
shopping.AddChildren(new FavoriteItem("eBay", "https://www.ebay.com/", "Shopping/eBay", _profileInfo));

_root = new FavoriteItem("Favorites bar", null, string.Empty, FavoriteType.Folder);
_root.AddChildren(new FavoriteItem("YouTube", "https://www.youtube.com/", "YouTube", FavoriteType.Url));
_root.AddChildren(new FavoriteItem("Spotify", "https://open.spotify.com/", "Spotify", FavoriteType.Url));
_root.AddChildren(new FavoriteItem("LinkedIn", "https://www.linkedin.com/", "LinkedIn", FavoriteType.Url));
_root = new FavoriteItem("Favorites bar", string.Empty);
_root.AddChildren(new FavoriteItem("YouTube", "https://www.youtube.com/", "YouTube", _profileInfo));
_root.AddChildren(new FavoriteItem("Spotify", "https://open.spotify.com/", "Spotify", _profileInfo));
_root.AddChildren(new FavoriteItem("LinkedIn", "https://www.linkedin.com/", "LinkedIn", _profileInfo));
_root.AddChildren(coding);
_root.AddChildren(shopping);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Davide Giacometti. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers;

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks
{
public class MultiProfileManager : IProfileManager
{
public ReadOnlyCollection<IFavoriteProvider> FavoriteProviders => (new IFavoriteProvider[] { new DefaultFavoriteProvider(), new WorkFavoriteProvider() }).AsReadOnly();

public void ReloadProfiles(bool all)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Davide Giacometti. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers;

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks
{
public class SingleProfileManager : IProfileManager
{
public ReadOnlyCollection<IFavoriteProvider> FavoriteProviders => (new IFavoriteProvider[] { new DefaultFavoriteProvider() }).AsReadOnly();

public void ReloadProfiles(bool all)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Davide Giacometti. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks
{
public class WorkFavoriteProvider : IFavoriteProvider
{
private readonly ProfileInfo _profileInfo = new("Work", "Profile 1");
private readonly FavoriteItem _root;

public FavoriteItem Root => _root;

public WorkFavoriteProvider()
{
var coding = new FavoriteItem("Coding", "Coding");
coding.AddChildren(new FavoriteItem("AWS", "https://aws.amazon.com/", "Coding/AWS", _profileInfo));
coding.AddChildren(new FavoriteItem("Bitbucket", "https://bitbucket.org/", "Coding/Bitbucket", _profileInfo));
coding.AddChildren(new FavoriteItem("Microsoft Azure", "https://portal.azure.com/", "Coding/Microsoft Azure", _profileInfo));

_root = new FavoriteItem("Favorites bar", string.Empty);
_root.AddChildren(new FavoriteItem("Gmail", "https://mail.google.com/", "Gmail", _profileInfo));
_root.AddChildren(coding);
}
}
}
34 changes: 34 additions & 0 deletions Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/EdgeHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Davide Giacometti. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;
using Wox.Infrastructure;
using Wox.Plugin.Logger;

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers
{
public static class EdgeHelpers
{
public static void OpenInEdge(FavoriteItem favorite, bool inPrivate)
{
var args = $"{favorite.Url}";

if (inPrivate)
{
args += " -inprivate";
}

args += $" -profile-directory=\"{favorite.Profile.Directory}\"";

try
{
Helper.OpenInShell(@"shell:AppsFolder\Microsoft.MicrosoftEdge.Stable_8wekyb3d8bbwe!App", args);
}
catch (Exception ex)
{
Log.Exception("Failed to launch Microsoft Edge", ex, typeof(EdgeHelpers));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers
{
public class FavoriteProvider : IFavoriteProvider
{
private readonly string _path = Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Bookmarks");
private readonly string _path;
private readonly FileSystemWatcher _watcher;
private FavoriteItem _root;

public FavoriteItem Root => _root;

public FavoriteProvider()
public ProfileInfo ProfileInfo { get; }

public FavoriteProvider(string path, ProfileInfo profileInfo)
{
_path = path;
ProfileInfo = profileInfo;
_root = new FavoriteItem();
InitFavorites();

Expand All @@ -35,39 +39,46 @@ public FavoriteProvider()

private void InitFavorites()
{
if (!Path.Exists(_path))
try
{
Log.Warn($"Failed to find bookmarks file {_path}", typeof(FavoriteProvider));
return;
}
if (!Path.Exists(_path))
{
Log.Warn($"Failed to find Bookmarks file: {_path}", typeof(FavoriteProvider));
return;
}

using var fs = new FileStream(_path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs);
string json = sr.ReadToEnd();
var parsed = JsonDocument.Parse(json);
parsed.RootElement.TryGetProperty("roots", out var rootElement);
if (rootElement.ValueKind != JsonValueKind.Object)
{
return;
}
using var fs = new FileStream(_path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs);
string json = sr.ReadToEnd();
var parsed = JsonDocument.Parse(json);
parsed.RootElement.TryGetProperty("roots", out var rootElement);
if (rootElement.ValueKind != JsonValueKind.Object)
{
return;
}

var newRoot = new FavoriteItem();
rootElement.TryGetProperty("bookmark_bar", out var bookmarkBarElement);
if (bookmarkBarElement.ValueKind == JsonValueKind.Object)
{
ProcessFavorites(bookmarkBarElement, newRoot, string.Empty, true);
}
var newRoot = new FavoriteItem();
rootElement.TryGetProperty("bookmark_bar", out var bookmarkBarElement);
if (bookmarkBarElement.ValueKind == JsonValueKind.Object)
{
ProcessFavorites(bookmarkBarElement, newRoot, string.Empty, true);
}

rootElement.TryGetProperty("other", out var otherElement);
if (otherElement.ValueKind == JsonValueKind.Object)
{
ProcessFavorites(otherElement, newRoot, string.Empty, newRoot.Childrens.Count == 0);
}

rootElement.TryGetProperty("other", out var otherElement);
if (otherElement.ValueKind == JsonValueKind.Object)
_root = newRoot;
}
catch (Exception ex)
{
ProcessFavorites(otherElement, newRoot, string.Empty, newRoot.Childrens.Count == 0);
Log.Exception($"Failed to read favorites: {_path}", ex, typeof(FavoriteProvider));
}

_root = newRoot;
}

private static void ProcessFavorites(JsonElement element, FavoriteItem parent, string path, bool root)
private void ProcessFavorites(JsonElement element, FavoriteItem parent, string path, bool root)
{
if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty("children", out var children))
{
Expand All @@ -79,7 +90,7 @@ private static void ProcessFavorites(JsonElement element, FavoriteItem parent, s
path += $"{(string.IsNullOrWhiteSpace(path) ? string.Empty : "/")}{name}";
}

var folder = new FavoriteItem(name, null, path, FavoriteType.Folder);
var folder = new FavoriteItem(name, path);

if (root)
{
Expand All @@ -105,7 +116,7 @@ private static void ProcessFavorites(JsonElement element, FavoriteItem parent, s
if (!string.IsNullOrWhiteSpace(name))
{
path += $"{(string.IsNullOrWhiteSpace(path) ? string.Empty : "/")}{name}";
var favorite = new FavoriteItem(name, url.GetString(), path, FavoriteType.Url);
var favorite = new FavoriteItem(name, url.GetString(), path, ProfileInfo);
parent.AddChildren(favorite);
}
}
Expand Down
Loading

0 comments on commit 77e41c9

Please sign in to comment.