fix(settings): keep the typed proxy ports when a new server is saved with them

main
reionwong 1 week ago
parent fd1d86aac7
commit f6c150a8c8

@ -200,6 +200,9 @@ private:
void reload(unsigned fields)
{
// The page's own values coming back while it saves them.
if (m_saving)
return;
if (fields & NetworkProxy::FlagField) {
const int flag = m_proxy.flag();
m_noProxy->setChecked(flag == 0);
@ -231,6 +234,7 @@ private:
void saveFields()
{
m_needSave = false;
m_saving = true;
m_proxy.setScriptProxy(m_script->text());
m_proxy.setHttpProxy(m_http->text());
m_proxy.setHttpProxyPort(m_httpPort->text());
@ -239,6 +243,7 @@ private:
m_proxy.setFtpProxyPort(m_ftpPort->text());
m_proxy.setSocksProxy(m_socks->text());
m_proxy.setSocksProxyPort(m_socksPort->text());
m_saving = false;
update();
}
@ -257,6 +262,7 @@ private:
cutefish::base::Subscription m_subscription;
ButtonGroup m_modes;
bool m_needSave = false;
bool m_saving = false;
RadioButton* m_noProxy;
RadioButton* m_auto;
RadioButton* m_manual;

@ -227,9 +227,16 @@ static void skipSpaces(const std::string& text, size_t& pos)
namespace gsettings {
static std::string s_path;
std::string path()
{
return cutefish::base::paths::configHome() + "/glib-2.0/settings/keyfile";
return s_path.empty() ? cutefish::base::paths::configHome() + "/glib-2.0/settings/keyfile" : s_path;
}
void setPath(std::string path)
{
s_path = std::move(path);
}
std::string toString(const std::string& text, const std::string& fallback)

@ -32,6 +32,8 @@ bool writeGroup(const std::string& path, const std::string& group, const Group&
namespace gsettings {
std::string path();
// Points path() at another file; tests keep the session's own untouched.
void setPath(std::string path);
std::string toString(const std::string& text, const std::string& fallback = {});
int toInt(const std::string& text, int fallback);
bool toBool(const std::string& text, bool fallback);

@ -9,9 +9,16 @@
#include <sstream>
#include <string>
#include "input_test_support.h"
#include "pagetest.h"
#include "pages/network/common/textchecks.h"
#include "pages/network/proxy/proxysettings.h"
#include "ui/button.h"
#include "ui/test_input.h"
#include "ui/text_field.h"
#include "window/page.h"
using namespace cutefish::ui;
using namespace settings::network;
namespace {
@ -45,6 +52,21 @@ struct KeyFile : testing::Test
std::string path;
};
template<typename T>
void findAll(View* view, std::vector<T*>* found)
{
if (auto* match = dynamic_cast<T*>(view))
found->push_back(match);
for (const auto& child : view->children())
findAll<T>(child.get(), found);
}
PointF centre(View* view)
{
const PointF origin = view->mapToWindow({0, 0});
return {origin.x + view->geometry().width / 2, origin.y + view->geometry().height / 2};
}
} // namespace
TEST_F(KeyFile, ReadsGroupsAndKeys)
@ -103,3 +125,41 @@ TEST(EditFieldText, PortsAndNumbers)
EXPECT_TRUE(isNumberText("10", 10));
EXPECT_FALSE(isNumberText("11", 10));
}
// A new server and its port saved together: the server going in first must
// not put the old port back into the field before it is read.
TEST_F(KeyFile, ANewServerKeepsTheTypedPort)
{
write("[system/proxy]\nmode='manual'\n");
gsettings::setPath(path);
{
PageFixture page("network", "proxy");
Window& window = page.window->window();
std::vector<TextField*> fields;
findAll<TextField>(page.window->currentPage(), &fields);
// The script URL, then HTTP, FTP and SOCKS, each with its port.
ASSERT_EQ(fields.size(), 7u);
const std::vector<std::string> typed = {"", "http.test", "3128", "", "", "socks.test", "1081"};
for (size_t i = 0; i < fields.size(); ++i) {
if (typed[i].empty())
continue;
window.grab();
TestInput::click(window, centre(fields[i]));
TestInput::typeText(window, typed[i]);
ASSERT_EQ(fields[i]->text(), typed[i]);
}
std::vector<Button*> buttons;
findAll<Button>(page.window->currentPage(), &buttons);
ASSERT_FALSE(buttons.empty());
buttons.back()->onClicked();
EXPECT_EQ(fields[2]->text(), "3128");
EXPECT_EQ(fields[6]->text(), "1081");
EXPECT_TRUE(settings::testing::waitFor([&] { return contents().find("[system/proxy/socks]") != std::string::npos; }));
}
const ProxySettings saved = ProxySettings::load();
gsettings::setPath({});
EXPECT_EQ(saved.http, (ProxySettings::Server{"http.test", 3128}));
EXPECT_EQ(saved.socks, (ProxySettings::Server{"socks.test", 1081}));
}

Loading…
Cancel
Save