-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
os_integration_synchronize_command_unittest.cc
174 lines (147 loc) · 6.74 KB
/
os_integration_synchronize_command_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/web_applications/commands/os_integration_synchronize_command.h"
#include <memory>
#include "base/files/file_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_file_handler_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_protocol_handler_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_shortcut.h"
#include "chrome/browser/web_applications/os_integration/web_app_shortcut_manager.h"
#include "chrome/browser/web_applications/proto/web_app_os_integration_state.pb.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/test/web_app_test_utils.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/chrome_features.h"
#include "components/services/app_service/public/cpp/protocol_handler_info.h"
#include "components/webapps/browser/install_result_code.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace web_app {
namespace {
using ::testing::Eq;
class OsIntegrationSynchronizeCommandTest
: public WebAppTest,
public ::testing::WithParamInterface<OsIntegrationSubManagersState> {
public:
const GURL kWebAppUrl = GURL("https://example.com");
OsIntegrationSynchronizeCommandTest() {
if (GetParam() == OsIntegrationSubManagersState::kEnabled) {
scoped_feature_list_.InitWithFeaturesAndParameters(
{{features::kOsIntegrationSubManagers, {{"stage", "write_config"}}}},
/*disabled_features=*/{});
} else {
scoped_feature_list_.InitWithFeatures(
{}, {features::kOsIntegrationSubManagers});
}
}
~OsIntegrationSynchronizeCommandTest() override = default;
void SetUp() override {
WebAppTest::SetUp();
{
base::ScopedAllowBlockingForTesting allow_blocking;
shortcut_override_ =
ShortcutOverrideForTesting::OverrideForTesting(base::GetHomeDir());
}
provider_ = FakeWebAppProvider::Get(profile());
auto file_handler_manager =
std::make_unique<WebAppFileHandlerManager>(profile());
auto protocol_handler_manager =
std::make_unique<WebAppProtocolHandlerManager>(profile());
auto shortcut_manager = std::make_unique<WebAppShortcutManager>(
profile(), /*icon_manager=*/nullptr, file_handler_manager.get(),
protocol_handler_manager.get());
auto os_integration_manager = std::make_unique<OsIntegrationManager>(
profile(), std::move(shortcut_manager), std::move(file_handler_manager),
std::move(protocol_handler_manager), /*url_handler_manager=*/nullptr);
provider_->SetOsIntegrationManager(std::move(os_integration_manager));
test::AwaitStartWebAppProviderAndSubsystems(profile());
}
void TearDown() override {
EXPECT_TRUE(test::UninstallAllWebApps(profile()));
{
base::ScopedAllowBlockingForTesting allow_blocking;
shortcut_override_.reset();
}
WebAppTest::TearDown();
}
AppId InstallAppWithProtocolHandlers(
const std::vector<apps::ProtocolHandlerInfo>& protocol_handlers) {
auto info = std::make_unique<WebAppInstallInfo>();
info->start_url = kWebAppUrl;
info->title = u"Test App";
info->user_display_mode = web_app::UserDisplayMode::kStandalone;
info->protocol_handlers = protocol_handlers;
base::test::TestFuture<const AppId&, webapps::InstallResultCode> result;
// InstallFromInfo is used so that the DB states are updated but OS
// integration is not triggered.
provider()->scheduler().InstallFromInfo(
std::move(info), /*overwrite_existing_manifest_fields=*/true,
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
result.GetCallback());
bool success = result.Wait();
EXPECT_TRUE(success);
if (!success) {
return AppId();
}
EXPECT_EQ(result.Get<webapps::InstallResultCode>(),
webapps::InstallResultCode::kSuccessNewInstall);
return result.Get<AppId>();
}
protected:
WebAppProvider* provider() { return provider_; }
private:
raw_ptr<FakeWebAppProvider> provider_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<ShortcutOverrideForTesting::BlockingRegistration>
shortcut_override_;
};
TEST_P(OsIntegrationSynchronizeCommandTest, SynchronizeWorks) {
apps::ProtocolHandlerInfo protocol_handler;
const std::string handler_url =
std::string(kWebAppUrl.spec()) + "/testing=%s";
protocol_handler.url = GURL(handler_url);
protocol_handler.protocol = "web+test";
const AppId& app_id = InstallAppWithProtocolHandlers({protocol_handler});
absl::optional<proto::WebAppOsIntegrationState> current_states =
provider()->registrar().GetAppCurrentOsIntegrationState(app_id);
ASSERT_FALSE(current_states.has_value());
// OS Integration should be triggered now.
base::test::TestFuture<void> synchronize_future;
provider()->scheduler().SynchronizeOsIntegration(
app_id, synchronize_future.GetCallback());
EXPECT_TRUE(synchronize_future.Wait());
absl::optional<proto::WebAppOsIntegrationState> updated_states =
provider()->registrar().GetAppCurrentOsIntegrationState(app_id);
if (base::FeatureList::IsEnabled(features::kOsIntegrationSubManagers)) {
ASSERT_TRUE(updated_states.has_value());
const proto::WebAppOsIntegrationState& os_integration_state =
updated_states.value();
ASSERT_THAT(os_integration_state.manifest_protocol_handlers_states_size(),
testing::Eq(1));
const proto::WebAppProtocolHandler& protocol_handler_state =
os_integration_state.manifest_protocol_handlers_states(0);
ASSERT_THAT(protocol_handler_state.protocol(),
testing::Eq(protocol_handler.protocol));
ASSERT_THAT(protocol_handler_state.url(), testing::Eq(handler_url));
} else {
ASSERT_FALSE(updated_states.has_value());
}
}
INSTANTIATE_TEST_SUITE_P(
All,
OsIntegrationSynchronizeCommandTest,
::testing::Values(OsIntegrationSubManagersState::kEnabled,
OsIntegrationSubManagersState::kDisabled),
test::GetOsIntegrationSubManagersTestName);
} // namespace
} // namespace web_app