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
ash / ambient / test / test_ambient_client.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/ambient/test/test_ambient_client.h"
#include <utility>
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/time/time.h"
#include "google_apis/gaia/gaia_id.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace ash {
namespace {
constexpr base::TimeDelta kDefaultTokenExpirationDelay = base::Seconds(60);
// A simple SharedURLLoaderFactory implementation for tests.
class FakeSharedURLLoaderFactory : public network::SharedURLLoaderFactory {
public:
FakeSharedURLLoaderFactory() = default;
FakeSharedURLLoaderFactory(const FakeSharedURLLoaderFactory&) = delete;
FakeSharedURLLoaderFactory& operator=(const FakeSharedURLLoaderFactory&) =
delete;
// network::mojom::URLLoaderFactory implementation:
void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver)
override {
test_url_loader_factory_.Clone(std::move(receiver));
}
void CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> loader,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& request,
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
override {
test_url_loader_factory_.CreateLoaderAndStart(
std::move(loader), request_id, options, request, std::move(client),
traffic_annotation);
}
// network::SharedURLLoaderFactory implementation:
std::unique_ptr<network::PendingSharedURLLoaderFactory> Clone() override {
NOTREACHED();
}
network::TestURLLoaderFactory& test_url_loader_factory() {
return test_url_loader_factory_;
}
private:
friend class base::RefCounted<FakeSharedURLLoaderFactory>;
~FakeSharedURLLoaderFactory() override = default;
network::TestURLLoaderFactory test_url_loader_factory_;
};
} // namespace
const char* TestAmbientClient::kTestGaiaId = "test_gaia_id";
const char* TestAmbientClient::kTestAccessToken = "test_access_token";
TestAmbientClient::TestAmbientClient(
device::TestWakeLockProvider* wake_lock_provider)
: url_loader_factory_(new FakeSharedURLLoaderFactory()),
wake_lock_provider_(wake_lock_provider) {}
TestAmbientClient::~TestAmbientClient() = default;
bool TestAmbientClient::IsAmbientModeAllowed() {
// Only enable ambient mode for primary user to test multi login.
return Shell::Get()->session_controller()->IsUserPrimary();
}
void TestAmbientClient::RequestAccessToken(GetAccessTokenCallback callback) {
pending_callback_ = std::move(callback);
if (is_automatic_)
IssueAccessToken(/*is_empty=*/false);
}
void TestAmbientClient::DownloadImage(
const std::string& url,
ash::ImageDownloader::DownloadCallback callback) {
// Return fake image.
std::move(callback).Run(gfx::test::CreateImageSkia(10, 10));
}
scoped_refptr<network::SharedURLLoaderFactory>
TestAmbientClient::GetURLLoaderFactory() {
return url_loader_factory_;
}
scoped_refptr<network::SharedURLLoaderFactory>
TestAmbientClient::GetSigninURLLoaderFactory() {
return url_loader_factory_;
}
void TestAmbientClient::RequestWakeLockProvider(
mojo::PendingReceiver<device::mojom::WakeLockProvider> receiver) {
wake_lock_provider_->BindReceiver(std::move(receiver));
}
void TestAmbientClient::IssueAccessToken(bool is_empty) {
if (!pending_callback_)
return;
if (is_empty) {
std::move(pending_callback_)
.Run(GaiaId(),
/*access_token=*/std::string(),
/*expiration_time=*/base::Time::Now());
} else {
std::move(pending_callback_)
.Run(GaiaId(kTestGaiaId), kTestAccessToken,
base::Time::Now() + kDefaultTokenExpirationDelay);
}
}
void TestAmbientClient::SetAutomaticalyIssueToken(bool is_automatic) {
is_automatic_ = is_automatic;
if (is_automatic_)
IssueAccessToken(/*is_empty=*/false);
}
bool TestAmbientClient::ShouldUseProdServer() {
return false;
}
bool TestAmbientClient::IsAccessTokenRequestPending() const {
return !!pending_callback_;
}
network::TestURLLoaderFactory& TestAmbientClient::test_url_loader_factory() {
return static_cast<FakeSharedURLLoaderFactory*>(url_loader_factory_.get())
->test_url_loader_factory();
}
} // namespace ash