fix(vpn): keep a removed WireGuard peer's card until its click is done

main
reionwong 2 weeks ago
parent 5eedf525f5
commit ab8c7ec5ff

@ -6,6 +6,7 @@
#include <algorithm>
#include <climits>
#include "base/event_loop.h"
#include "networkmanager/types.h"
#include "pages/network/common/textchecks.h"
#include "pages/network/common/rows.h"
@ -611,8 +612,10 @@ private:
for (Label* label : {found->labels[0], found->labels[1], found->labels[2], found->labels[3], found->labels[4]})
std::erase(m_labels, label);
m_peerCards.erase(found);
m_peers->takeChild(owner);
// The card holds the button being clicked.
std::shared_ptr<View> gone = m_peers->takeChild(owner);
update();
cutefish::base::EventLoop::current()->post([gone] {});
};
card.publicKey = addField(card.card, "Public key", "Required");
card.publicKey->setText(peer.publicKey);

@ -3,12 +3,16 @@
#include <gtest/gtest.h>
#include <functional>
#include <memory>
#include <string>
#include "networkmanager/handler.h"
#include "networkmanager/vpnschema.h"
#include "pages/network/vpn/vpneditor.h"
#include "ui/label.h"
#include "ui/test_input.h"
#include "ui/window.h"
using namespace settings::network;
namespace nm = cutefish::networkmanager;
@ -91,3 +95,37 @@ TEST(VpnEditor, WireGuardKeepsItsPeers)
EXPECT_EQ(values.wireguard.peers[0].keepalive, "25");
EXPECT_TRUE(values.wireguard.peerRoutes);
}
TEST(VpnEditor, RemovingAWireGuardPeerKeepsTheOthers)
{
nm::VpnForm form;
form.kind = "wireguard";
form.installed = true;
form.wireguard.peers = {{.publicKey = "first"}, {.publicKey = "second"}};
cutefish::ui::Window window;
auto content = std::make_unique<VpnEditor>();
VpnEditor* editor = content.get();
window.setContentView(std::move(content));
window.resize({480, 1200});
editor->load(form);
window.grab();
cutefish::ui::View* remove = nullptr;
const std::function<void(cutefish::ui::View*)> findRemove = [&](cutefish::ui::View* view) {
auto* label = dynamic_cast<cutefish::ui::Label*>(view);
if (!remove && label && label->text() == "Remove")
remove = view->parent();
for (const auto& child : view->children())
findRemove(child.get());
};
findRemove(editor);
ASSERT_NE(remove, nullptr);
// The button goes with the peer's card it sits in.
const cutefish::ui::PointF origin = remove->mapToWindow({0, 0});
cutefish::ui::TestInput::click(window, {origin.x + remove->geometry().width / 2,
origin.y + remove->geometry().height / 2});
const nm::VpnValues values = editor->collect();
ASSERT_EQ(values.wireguard.peers.size(), 1u);
EXPECT_EQ(values.wireguard.peers[0].publicKey, "second");
}

Loading…
Cancel
Save