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
|
// Copyright 2025 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/upgrade_detector/version_history_client.h"
#include <string_view>
#include <utility>
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "chrome/test/base/testing_browser_process.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
class VersionHistoryClientTest : public ::testing::Test {
protected:
void SetUp() override {
TestingBrowserProcess::GetGlobal()->SetSharedURLLoaderFactory(
url_loader_factory_.GetSafeWeakWrapper());
}
network::TestURLLoaderFactory& url_loader_factory() {
return url_loader_factory_;
}
void SetAPIResponse(const GURL& url,
net::HttpStatusCode status_code,
std::string_view content) {
network::mojom::URLResponseHeadPtr head =
network::CreateURLResponseHead(status_code);
head->mime_type = "application/json";
network::URLLoaderCompletionStatus status;
url_loader_factory().AddResponse(url, std::move(head), content, status);
}
private:
base::test::TaskEnvironment task_environment_;
network::TestURLLoaderFactory url_loader_factory_;
};
#if BUILDFLAG(IS_WIN)
#if defined(ARCH_CPU_ARM64)
#define CURRENT_PLATFORM "win_arm64"
#elif defined(ARCH_CPU_X86_64)
#define CURRENT_PLATFORM "win64"
#else
#define CURRENT_PLATFORM "win"
#endif
#elif BUILDFLAG(IS_LINUX)
#define CURRENT_PLATFORM "linux"
#elif BUILDFLAG(IS_MAC)
#if defined(ARCH_CPU_ARM64)
#define CURRENT_PLATFORM "mac_arm64"
#else
#define CURRENT_PLATFORM "mac"
#endif
#elif BUILDFLAG(IS_CHROMEOS)
#define CURRENT_PLATFORM "chromeos"
#else
#error Unsupported platform
#endif // BUILDFLAG(IS_WIN)
// Tests that GetLastServedDate() returns the correct date when the server is
// responsive.
TEST_F(VersionHistoryClientTest, GetLastServedDateSimple) {
SetAPIResponse(GURL("https://versionhistory.googleapis.com/v1/chrome/"
"platforms/" CURRENT_PLATFORM
"/channels/stable/versions/131.0.6778.205/releases/"
"?order_by=endtime%20desc&page_size=1"),
net::HTTP_OK, R"({
"releases": [
{
"name": "chrome/platforms/win/channels/stable/versions/131.0.6778.205/releases/1736364451",
"serving": {
"startTime": "2025-01-08T19:27:31.216008Z",
"endTime": "2025-01-09T21:01:31.167025Z"
},
"fraction": 0.4975,
"version": "131.0.6778.205",
"fractionGroup": "144",
"pinnable": false
}
]
})");
base::Time expected_time;
ASSERT_TRUE(
base::Time::FromUTCString("2025-01-09T21:01:31.167025Z", &expected_time));
base::RunLoop run_loop;
LastServedDateCallback callback =
base::BindLambdaForTesting([&](std::optional<base::Time> last_served) {
EXPECT_EQ(expected_time, last_served);
run_loop.Quit();
});
GetLastServedDate(base::Version("131.0.6778.205"), std::move(callback));
run_loop.Run();
}
// Tests that GetLastServedDate() returns nullopt when the server returns a 404.
TEST_F(VersionHistoryClientTest, GetLastServedDate404) {
SetAPIResponse(GURL("https://versionhistory.googleapis.com/v1/chrome/"
"platforms/" CURRENT_PLATFORM
"/channels/stable/versions/131.0.6778.205/releases/"
"?order_by=endtime%20desc&page_size=1"),
net::HTTP_NOT_FOUND, "");
base::RunLoop run_loop;
LastServedDateCallback callback =
base::BindLambdaForTesting([&](std::optional<base::Time> last_served) {
EXPECT_EQ(std::nullopt, last_served);
run_loop.Quit();
});
GetLastServedDate(base::Version("131.0.6778.205"), std::move(callback));
run_loop.Run();
}
// Tests that GetLastServedDate() returns nullopt when the server returns
// invalid JSON.
TEST_F(VersionHistoryClientTest, GetLastServedDateInvalidJSON) {
SetAPIResponse(GURL("https://versionhistory.googleapis.com/v1/chrome/"
"platforms/" CURRENT_PLATFORM
"/channels/stable/versions/131.0.6778.205/releases/"
"?order_by=endtime%20desc&page_size=1"),
net::HTTP_OK, "invalid JSON");
base::RunLoop run_loop;
LastServedDateCallback callback =
base::BindLambdaForTesting([&](std::optional<base::Time> last_served) {
EXPECT_EQ(std::nullopt, last_served);
run_loop.Quit();
});
GetLastServedDate(base::Version("131.0.6778.205"), std::move(callback));
run_loop.Run();
}
// Tests that GetLastServedDate() returns nullopt when the server returns a
// response with a missing endTime.
TEST_F(VersionHistoryClientTest, GetLastServedDateMissingEndTime) {
SetAPIResponse(GURL("https://versionhistory.googleapis.com/v1/chrome/"
"platforms/" CURRENT_PLATFORM
"/channels/stable/versions/131.0.6778.205/releases/"
"?order_by=endtime%20desc&page_size=1"),
net::HTTP_OK, R"({
"releases": [
{
"name": "chrome/platforms/win/channels/stable/versions/131.0.6778.205/releases/1736364451",
"serving": {
"startTime": "2025-01-08T19:27:31.216008Z"
},
"fraction": 0.4975,
"version": "131.0.6778.205",
"fractionGroup": "144",
"pinnable": false
}
]
})");
base::RunLoop run_loop;
LastServedDateCallback callback =
base::BindLambdaForTesting([&](std::optional<base::Time> last_served) {
EXPECT_EQ(std::nullopt, last_served);
run_loop.Quit();
});
GetLastServedDate(base::Version("131.0.6778.205"), std::move(callback));
run_loop.Run();
}
// Tests that GetLastServedDate() returns nullopt when the server returns a
// response with an empty releases array.
TEST_F(VersionHistoryClientTest, GetLastServedDateEmptyReleases) {
SetAPIResponse(GURL("https://versionhistory.googleapis.com/v1/chrome/"
"platforms/" CURRENT_PLATFORM
"/channels/stable/versions/131.0.6778.205/releases/"
"?order_by=endtime%20desc&page_size=1"),
net::HTTP_OK, R"({"releases":[]})");
base::RunLoop run_loop;
LastServedDateCallback callback =
base::BindLambdaForTesting([&](std::optional<base::Time> last_served) {
EXPECT_EQ(std::nullopt, last_served);
run_loop.Quit();
});
GetLastServedDate(base::Version("131.0.6778.205"), std::move(callback));
run_loop.Run();
}
|