blob: cf4616ba574dce60f8e52f9611fe159b4c0ad966 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_WEB_APPLICATIONS_TEST_FAKE_DATA_RETRIEVER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_TEST_FAKE_DATA_RETRIEVER_H_
#include <memory>
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_data_retriever.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom-forward.h"
#include "url/gurl.h"
namespace web_app {
// All WebAppDataRetriever operations are async, so this class posts tasks
// when running callbacks to simulate async behavior in tests as well.
class FakeDataRetriever : public WebAppDataRetriever {
public:
FakeDataRetriever();
FakeDataRetriever(const FakeDataRetriever&) = delete;
FakeDataRetriever& operator=(const FakeDataRetriever&) = delete;
~FakeDataRetriever() override;
// WebAppDataRetriever:
void GetWebAppInstallInfo(content::WebContents* web_contents,
GetWebAppInstallInfoCallback callback) override;
void CheckInstallabilityAndRetrieveManifest(
content::WebContents* web_contents,
bool bypass_service_worker_check,
CheckInstallabilityCallback callback) override;
void GetIcons(content::WebContents* web_contents,
const std::vector<GURL>& icon_urls,
bool skip_page_favicons,
GetIconsCallback callback) override;
// Set info to respond on |GetWebAppInstallInfo|.
void SetRendererWebAppInstallInfo(
std::unique_ptr<WebAppInstallInfo> web_app_info);
void SetEmptyRendererWebAppInstallInfo();
// Set arguments to respond on |CheckInstallabilityAndRetrieveManifest|.
void SetManifest(blink::mojom::ManifestPtr manifest,
bool is_installable,
GURL manifest_url = GURL());
// Set icons to respond on |GetIcons|.
void SetIcons(IconsMap icons_map);
using GetIconsDelegate =
base::RepeatingCallback<IconsMap(content::WebContents* web_contents,
const std::vector<GURL>& icon_urls,
bool skip_page_favicons)>;
void SetGetIconsDelegate(GetIconsDelegate get_icons_delegate);
// Sets `IconsDownloadedResult` to respond on `GetIcons`.
void SetIconsDownloadedResult(IconsDownloadedResult result);
// Sets `DownloadedIconsHttpResults` to respond on `GetIcons`.
void SetDownloadedIconsHttpResults(
DownloadedIconsHttpResults icons_http_results);
void SetDestructionCallback(base::OnceClosure callback);
WebAppInstallInfo& web_app_info() { return *web_app_info_; }
// Builds minimal data for install to succeed. Data includes: empty renderer
// info, manifest with |url| and |scope|, installability checked as |true|,
// empty icons.
void BuildDefaultDataToRetrieve(const GURL& url, const GURL& scope);
private:
void ScheduleCompletionCallback();
void CallCompletionCallback();
base::OnceClosure completion_callback_;
std::unique_ptr<WebAppInstallInfo> web_app_info_;
blink::mojom::ManifestPtr manifest_;
GURL manifest_url_;
bool is_installable_ = false;
IconsMap icons_map_;
GetIconsDelegate get_icons_delegate_;
IconsDownloadedResult icons_downloaded_result_ =
IconsDownloadedResult::kCompleted;
DownloadedIconsHttpResults icons_http_results_;
base::OnceClosure destruction_callback_;
base::WeakPtrFactory<FakeDataRetriever> weak_ptr_factory_{this};
};
} // namespace web_app
#endif // CHROME_BROWSER_WEB_APPLICATIONS_TEST_FAKE_DATA_RETRIEVER_H_