Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Unified Diff: chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc

Issue 2741283002: Idea for AddToHomescreenDataFetcher tests
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc
diff --git a/chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc b/chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc
index df56a43677e174441786283a5e50241bc5f869fb..f88f7d86847dc217cebde41a2ea80ed5ea3a032e 100644
--- a/chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc
+++ b/chrome/browser/android/webapps/add_to_homescreen_data_fetcher_unittest.cc
@@ -7,6 +7,7 @@
#include <memory>
#include <string>
+#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/files/file_path.h"
#include "base/macros.h"
@@ -16,10 +17,15 @@
#include "base/strings/nullable_string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
+#include "chrome/browser/favicon/favicon_service_factory.h"
+#include "chrome/browser/installable/installable_manager.h"
#include "chrome/common/web_application_info.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
+#include "components/favicon/core/favicon_service.h"
+#include "components/favicon/core/test/mock_favicon_service.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/common/service_worker/service_worker_status_code.h"
@@ -29,6 +35,8 @@
#include "content/public/common/manifest.h"
#include "content/test/test_web_contents.h"
#include "net/http/http_status_code.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/WebKit/public/platform/WebDisplayMode.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "url/gurl.h"
@@ -38,84 +46,76 @@
namespace {
-const char* kDefaultManifestUrl = "https://www.example.com/manifest.json";
const char* kDefaultManifestName = "Default Name";
const char* kDefaultManifestShortName = "Default Short Name";
-const char* kDefaultStartUrl = "https://www.example.com/index.html";
const blink::WebDisplayMode kDefaultManifestDisplayMode =
blink::WebDisplayModeStandalone;
+const GURL kDefaultManifestUrl("https://www.example.com/manifest.json");
+const GURL kDefaultStartUrl("https://www.example.com/index.html");
+const GURL kDefaultPrimaryIconUrl("https://www.example.com/icon.png");
+const GURL kEmptyUrl;
-// WebContents subclass which mocks out image and manifest fetching.
-class MockWebContents : public content::TestWebContents {
+
+class TestInstallableManager : public InstallableManager {
public:
- explicit MockWebContents(content::BrowserContext* browser_context)
- : content::TestWebContents(browser_context) {}
+ TestInstallableManager() : InstallableManager(nullptr) {}
+ ~TestInstallableManager() override {}
- ~MockWebContents() override {
+ static void CreateForWebContents(content::WebContents* web_contents) {
+ if (!FromWebContents(web_contents))
+ web_contents->SetUserData(UserDataKey(), new TestInstallableManager());
}
- // Sets the manifest to be returned by GetManifest().
- // |fetch_delay_ms| is the time in milliseconds that the simulated fetch of
- // the web manifest should take.
- void SetManifest(const GURL& manifest_url,
- const content::Manifest& manifest,
- int fetch_delay_ms) {
- manifest_url_ = manifest_url;
- manifest_ = manifest;
- manifest_fetch_delay_ms_ = fetch_delay_ms;
+ void GetData(const InstallableParams& params,
+ const InstallableCallback& callback) override {
+ callback_ = callback;
}
- int DownloadImage(const GURL& url,
- bool is_favicon,
- uint32_t max_bitmap_size,
- bool bypass_cache,
- const ImageDownloadCallback& callback) override {
- const int kIconSizePx = 144;
- SkBitmap icon = gfx::test::CreateBitmap(kIconSizePx, kIconSizePx);
- std::vector<SkBitmap> icons(1u, icon);
- std::vector<gfx::Size> pixel_sizes(1u, gfx::Size(kIconSizePx, kIconSizePx));
- content::BrowserThread::PostTask(
- content::BrowserThread::UI,
- FROM_HERE,
- base::Bind(callback, 0, net::HTTP_OK, url, icons, pixel_sizes));
- return 0;
- }
+ InstallableCallback callback_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestInstallableManager);
+};
- void GetManifest(const GetManifestCallback& callback) override {
- content::BrowserThread::PostDelayedTask(
- content::BrowserThread::UI, FROM_HERE,
- base::Bind(callback, manifest_url_, manifest_),
- base::TimeDelta::FromMilliseconds(manifest_fetch_delay_ms_));
+// Builds non-null base::NullableString16 from a UTF8 string.
+base::NullableString16 NullableStringFromUTF8(const std::string& value) {
+ return base::NullableString16(base::UTF8ToUTF16(value), false);
+}
+
+class InstallableDataBuilder {
+ public:
+ InstallableDataBuilder() {}
+
+ InstallableData Build() {
+ primary_icon_ = gfx::test::CreateBitmap(144, 144);
+ manifest_.name = NullableStringFromUTF8(kDefaultManifestName);
+ manifest_.short_name = NullableStringFromUTF8(kDefaultManifestShortName);
+ manifest_.start_url = GURL(kDefaultStartUrl);
+ manifest_.display = kDefaultManifestDisplayMode;
+
+ return {NO_ERROR_DETECTED, kDefaultManifestUrl,
+ manifest_, kDefaultPrimaryIconUrl,
+ &primary_icon_, kEmptyUrl,
+ nullptr, true};
}
private:
- GURL manifest_url_;
content::Manifest manifest_;
- int manifest_fetch_delay_ms_;
+ SkBitmap primary_icon_;
- DISALLOW_COPY_AND_ASSIGN(MockWebContents);
+ DISALLOW_COPY_AND_ASSIGN(InstallableDataBuilder);
};
// Tracks which of the AddToHomescreenDataFetcher::Observer callbacks have been
// called.
-class ObserverWaiter : public AddToHomescreenDataFetcher::Observer {
+class TestObserver : public AddToHomescreenDataFetcher::Observer {
public:
- ObserverWaiter()
+ TestObserver()
: is_webapk_compatible_(false),
determined_webapk_compatibility_(false),
title_available_(false),
data_available_(false) {}
- ~ObserverWaiter() override {}
-
- // Waits till the OnDataAvailable() callback is called.
- void WaitForDataAvailable() {
- if (data_available_)
- return;
-
- base::RunLoop run_loop;
- quit_closure_ = run_loop.QuitClosure();
- run_loop.Run();
- }
+ ~TestObserver() override {}
void OnDidDetermineWebApkCompatibility(bool is_webapk_compatible) override {
determined_webapk_compatibility_ = true;
@@ -137,8 +137,6 @@ class ObserverWaiter : public AddToHomescreenDataFetcher::Observer {
const SkBitmap& primary_icon,
const SkBitmap& badge_icon) override {
data_available_ = true;
- if (!quit_closure_.is_null())
- quit_closure_.Run();
}
bool is_webapk_compatible() const { return is_webapk_compatible_; }
@@ -146,30 +144,21 @@ class ObserverWaiter : public AddToHomescreenDataFetcher::Observer {
return determined_webapk_compatibility_;
}
bool title_available() const { return title_available_; }
+ bool data_available() const { return data_available_; }
private:
bool is_webapk_compatible_;
bool determined_webapk_compatibility_;
bool title_available_;
bool data_available_;
- base::Closure quit_closure_;
- DISALLOW_COPY_AND_ASSIGN(ObserverWaiter);
+ DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
-// Builds non-null base::NullableString16 from a UTF8 string.
-base::NullableString16 NullableStringFromUTF8(const std::string& value) {
- return base::NullableString16(base::UTF8ToUTF16(value), false);
-}
-
-// Builds WebAPK compatible content::Manifest.
-content::Manifest BuildDefaultManifest() {
- content::Manifest manifest;
- manifest.name = NullableStringFromUTF8(kDefaultManifestName);
- manifest.short_name = NullableStringFromUTF8(kDefaultManifestShortName);
- manifest.start_url = GURL(kDefaultStartUrl);
- manifest.display = kDefaultManifestDisplayMode;
- return manifest;
+std::unique_ptr<KeyedService> BuildMockFaviconService(
+ content::BrowserContext* context) {
+ return std::unique_ptr<KeyedService>(
+ new testing::NiceMock<favicon::MockFaviconService>);
}
} // anonymous namespace
@@ -178,95 +167,64 @@ content::Manifest BuildDefaultManifest() {
// Android does not support browser tests yet (crbug.com/611756).
class AddToHomescreenDataFetcherTest : public ChromeRenderViewHostTestHarness {
public:
- AddToHomescreenDataFetcherTest() {}
+ AddToHomescreenDataFetcherTest()
+ : timer_task_runner_(new base::TestMockTimeTaskRunner) {}
~AddToHomescreenDataFetcherTest() override {}
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
- ASSERT_TRUE(profile()->CreateHistoryService(false, true));
- profile()->CreateFaviconService();
+ // Make InstallableManager::GetForWebContents() return a
+ // TestInstallableManager.
+ TestInstallableManager::CreateForWebContents(web_contents());
- embedded_worker_test_helper_.reset(
- new content::EmbeddedWorkerTestHelper(base::FilePath()));
-
- scoped_refptr<content::SiteInstance> site_instance =
- content::SiteInstance::Create(browser_context());
- site_instance->GetProcess()->Init();
- MockWebContents* mock_web_contents = new MockWebContents(browser_context());
- mock_web_contents->Init(content::WebContents::CreateParams(
- browser_context(), std::move(site_instance)));
- SetContents(mock_web_contents);
+ FaviconServiceFactory::GetInstance()->SetTestingFactory(
+ browser_context(), &BuildMockFaviconService);
}
- void TearDown() override {
- embedded_worker_test_helper_.reset();
- ChromeRenderViewHostTestHarness::TearDown();
+ TestInstallableManager* test_installable_manager() {
+ return static_cast<TestInstallableManager*>(
+ InstallableManager::FromWebContents(web_contents()));
}
scoped_refptr<AddToHomescreenDataFetcher> BuildFetcher(
bool check_webapk_compatible,
AddToHomescreenDataFetcher::Observer* observer) {
- return new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1,
- check_webapk_compatible, observer);
- }
-
- // Set the manifest to be returned as a result of WebContents::GetManifest().
- void SetManifest(const GURL& manifest_url,
- const content::Manifest& manifest,
- int fetch_delay_ms) {
- MockWebContents* mock_web_contents =
- static_cast<MockWebContents*>(web_contents());
- mock_web_contents->SetManifest(manifest_url, manifest, fetch_delay_ms);
+ scoped_refptr<AddToHomescreenDataFetcher> fetcher(
+ new AddToHomescreenDataFetcher(web_contents(), 1, 1, 1, 1, 1,
+ check_webapk_compatible, observer));
+ fetcher->data_timeout_timer_.SetTaskRunner(timer_task_runner_);
+ return fetcher;
}
- // Registers service worker at |url|. Blocks till the service worker is
- // registered.
- void RegisterServiceWorker(const GURL& url) {
- base::RunLoop run_loop;
- embedded_worker_test_helper_->context()->RegisterServiceWorker(
- url, GURL(url.spec() + "/service_worker.js"), nullptr,
- base::Bind(&AddToHomescreenDataFetcherTest::OnServiceWorkerRegistered,
- base::Unretained(this), run_loop.QuitClosure()));
- }
+ scoped_refptr<base::TestMockTimeTaskRunner> timer_task_runner_;
private:
- // Callback for RegisterServiceWorker() for when service worker registration
- // has completed.
- void OnServiceWorkerRegistered(const base::Closure& callback,
- content::ServiceWorkerStatusCode status,
- const std::string& status_message,
- int64_t registration_id) {
- ASSERT_EQ(content::SERVICE_WORKER_OK, status)
- << content::ServiceWorkerStatusToString(status);
- callback.Run();
- }
-
- std::unique_ptr<content::EmbeddedWorkerTestHelper>
- embedded_worker_test_helper_;
-
DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcherTest);
};
-// Checks that AddToHomescreenDataFetcher::Observer::OnUserTitleAvailable() is
-// called when the web manifest fetch times out. The add-to-homescreen dialog
-// makes the dialog's text field editable once OnUserTitleAvailable() is called.
-TEST_F(AddToHomescreenDataFetcherTest,
- DISABLED_ManifestFetchTimesOutNoServiceWorker) {
- SetManifest(GURL(kDefaultManifestUrl), BuildDefaultManifest(), 10000);
-
- ObserverWaiter waiter;
+TEST_F(AddToHomescreenDataFetcherTest, ManifestFetchTimesOut) {
+ TestObserver observer;
scoped_refptr<AddToHomescreenDataFetcher> fetcher(
- BuildFetcher(false, &waiter));
+ BuildFetcher(false, &observer));
fetcher->OnDidGetWebApplicationInfo(WebApplicationInfo());
- waiter.WaitForDataAvailable();
+ ASSERT_FALSE(test_installable_manager()->callback_.is_null());
+ ASSERT_TRUE(timer_task_runner_->HasPendingTask());
+ timer_task_runner_->FastForwardUntilNoTasksRemain();
+ content::BrowserThread::GetBlockingPool()->FlushForTesting();
+
+ // Eventually manifest fetch finishes.
+ InstallableDataBuilder builder;
+ test_installable_manager()->callback_.Run(builder.Build());
- EXPECT_FALSE(waiter.determined_webapk_compatibility());
- EXPECT_TRUE(waiter.title_available());
+ EXPECT_FALSE(observer.determined_webapk_compatibility());
+ EXPECT_TRUE(observer.title_available());
+ EXPECT_TRUE(observer.data_available());
fetcher->set_weak_observer(nullptr);
}
+/*
// Class for tests which should be run with AddToHomescreenDataFetcher built
// with both true and false values of |check_webapk_compatible|.
class AddToHomescreenDataFetcherTestCommon
@@ -369,3 +327,4 @@ TEST_P(AddToHomescreenDataFetcherTestCommon, DISABLED_ManifestFetchTimesOut) {
INSTANTIATE_TEST_CASE_P(CheckWebApkCompatibility,
AddToHomescreenDataFetcherTestCommon,
::testing::Values(false, true));
+*/
« no previous file with comments | « chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698