-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
app_preload_service_unittest.cc
206 lines (169 loc) · 7.95 KB
/
app_preload_service_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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/apps/app_preload_service/app_preload_service.h"
#include <algorithm>
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/apps/app_preload_service/app_preload_service_factory.h"
#include "chrome/browser/apps/app_preload_service/proto/app_provisioning.pb.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kFirstLoginFlowStartedKey[] = "first_login_flow_started";
constexpr char kFirstLoginFlowCompletedKey[] = "first_login_flow_completed";
constexpr char kApsStateManager[] = "apps.app_preload_service.state_manager";
const base::Value::Dict& GetStateManager(Profile* profile) {
return profile->GetPrefs()->GetDict(kApsStateManager);
}
} // namespace
namespace apps {
class AppPreloadServiceTest : public testing::Test {
protected:
AppPreloadServiceTest()
: scoped_user_manager_(std::make_unique<ash::FakeChromeUserManager>()) {
scoped_feature_list_.InitAndEnableFeature(features::kAppPreloadService);
}
void SetUp() override {
testing::Test::SetUp();
GetFakeUserManager()->set_current_user_new(true);
TestingProfile::Builder profile_builder;
profile_builder.SetSharedURLLoaderFactory(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&url_loader_factory_));
profile_ = profile_builder.Build();
web_app::test::AwaitStartWebAppProviderAndSubsystems(GetProfile());
}
Profile* GetProfile() { return profile_.get(); }
ash::FakeChromeUserManager* GetFakeUserManager() const {
return static_cast<ash::FakeChromeUserManager*>(
user_manager::UserManager::Get());
}
network::TestURLLoaderFactory url_loader_factory_;
private:
content::BrowserTaskEnvironment task_environment_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<TestingProfile> profile_;
user_manager::ScopedUserManager scoped_user_manager_;
};
TEST_F(AppPreloadServiceTest, ServiceAccessPerProfile) {
// We expect the App Preload Service to be available in a normal profile.
TestingProfile::Builder profile_builder;
auto profile = profile_builder.Build();
EXPECT_TRUE(AppPreloadServiceFactory::IsAvailable(profile.get()));
auto* service = AppPreloadServiceFactory::GetForProfile(profile.get());
EXPECT_NE(nullptr, service);
// The service is unsupported in incognito.
TestingProfile::Builder incognito_builder;
auto* incognito_profile = incognito_builder.BuildIncognito(profile.get());
EXPECT_FALSE(AppPreloadServiceFactory::IsAvailable(incognito_profile));
EXPECT_EQ(nullptr,
AppPreloadServiceFactory::GetForProfile(incognito_profile));
// We expect the App Preload Service to not be available in a guest profile as
// the App Service isn't available.
TestingProfile::Builder guest_builder;
guest_builder.SetGuestSession();
auto guest_profile = guest_builder.Build();
EXPECT_FALSE(AppPreloadServiceFactory::IsAvailable(guest_profile.get()));
EXPECT_EQ(nullptr,
AppPreloadServiceFactory::GetForProfile(guest_profile.get()));
// The service is available for the OTR profile in guest mode, as the App
// Service supports this mode.
auto* guest_otr_profile =
guest_profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
EXPECT_TRUE(AppPreloadServiceFactory::IsAvailable(guest_otr_profile));
auto* otr_guest_service =
AppPreloadServiceFactory::GetForProfile(guest_otr_profile);
EXPECT_NE(nullptr, otr_guest_service);
// We expect a different service in the Guest Session profile.
EXPECT_NE(otr_guest_service, service);
}
TEST_F(AppPreloadServiceTest, FirstLoginStartedPrefSet) {
// Ensure that the AppPreloadService is created.
AppPreloadService::Get(GetProfile());
auto flow_started =
GetStateManager(GetProfile()).FindBool(kFirstLoginFlowStartedKey);
auto flow_completed =
GetStateManager(GetProfile()).FindBool(kFirstLoginFlowCompletedKey);
// Since we're creating a new profile with no saved state, we expect the state
// to be "started", but not "completed".
EXPECT_TRUE(flow_started.has_value() && flow_started.value());
EXPECT_EQ(flow_completed, absl::nullopt);
}
TEST_F(AppPreloadServiceTest, FirstLoginCompletedPrefSetAfterSuccess) {
proto::AppProvisioningResponse response;
url_loader_factory_.AddResponse(
AppPreloadServerConnector::GetServerUrl().spec(),
response.SerializeAsString());
base::test::TestFuture<bool> result;
auto* service = AppPreloadService::Get(GetProfile());
service->SetInstallationCompleteCallbackForTesting(result.GetCallback());
ASSERT_TRUE(result.Get());
// We expect that the key has been set after the first login flow has been
// completed.
auto flow_completed =
GetStateManager(GetProfile()).FindBool(kFirstLoginFlowCompletedKey);
EXPECT_NE(flow_completed, absl::nullopt);
EXPECT_TRUE(flow_completed.value());
}
TEST_F(AppPreloadServiceTest, FirstLoginExistingUserNotStarted) {
GetFakeUserManager()->set_current_user_new(false);
TestingProfile existing_user_profile;
// Ensure that the AppPreloadService is created.
AppPreloadService::Get(&existing_user_profile);
auto flow_started = GetStateManager(&existing_user_profile)
.FindBool(kFirstLoginFlowStartedKey);
// Existing users should not start the first-login flow.
EXPECT_FALSE(flow_started.has_value());
}
TEST_F(AppPreloadServiceTest, WebAppInstall) {
proto::AppProvisioningResponse response;
auto* app = response.add_apps_to_install();
app->set_name("Peanut Types");
app->set_platform(proto::AppProvisioningResponse::PLATFORM_WEB);
app->set_install_reason(proto::AppProvisioningResponse::INSTALL_REASON_OEM);
auto* web_extras = app->mutable_web_extras();
web_extras->set_start_url("https://peanuttypes.com/app");
web_extras->set_scope("https://peanuttypes.com/");
web_extras->set_display_mode(
proto::AppProvisioningResponse::DISPLAY_MODE_STANDALONE);
url_loader_factory_.AddResponse(
AppPreloadServerConnector::GetServerUrl().spec(),
response.SerializeAsString());
base::test::TestFuture<bool> result;
auto* service = AppPreloadService::Get(GetProfile());
service->SetInstallationCompleteCallbackForTesting(result.GetCallback());
ASSERT_TRUE(result.Get());
auto app_id = web_app::GenerateAppId(absl::nullopt,
GURL("https://peanuttypes.com/app"));
bool found =
AppServiceProxyFactory::GetForProfile(GetProfile())
->AppRegistryCache()
.ForOneApp(app_id, [](const AppUpdate& update) {
EXPECT_EQ(update.Name(), "Peanut Types");
EXPECT_EQ(update.InstallReason(), InstallReason::kOem);
EXPECT_EQ(update.PublisherId(), "https://peanuttypes.com/app");
});
ASSERT_TRUE(found);
}
} // namespace apps