Commit c5a4502e authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Allow WebStateObserver to observe N WebStates [37/N].

Convert FaviconCallbackTest to directly track the
registration with the observed WebState instead of
relying on the deprecated code in WebStateObserver.

Bug: 775684
Change-Id: Icafd83b54dfb598ec6cbe22ddb2f0c35d926b945
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Reviewed-on: https://chromium-review.googlesource.com/768692
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517808}
parent ba0c9a6b
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "ios/web/public/favicon_url.h" #include "ios/web/public/favicon_url.h"
#import "ios/web/public/test/web_test_with_web_state.h" #import "ios/web/public/test/web_test_with_web_state.h"
#include "ios/web/public/web_state/web_state_observer.h" #import "ios/web/public/web_state/web_state.h"
#import "ios/web/public/web_state/web_state_observer.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -16,10 +18,10 @@ namespace web { ...@@ -16,10 +18,10 @@ namespace web {
namespace { namespace {
// Observes and waits for FaviconUrlUpdated call. // Observes and waits for FaviconUrlUpdated call.
class FaviconUrlObserver : public web::WebStateObserver { class FaviconUrlObserver : public WebStateObserver {
public: public:
explicit FaviconUrlObserver(web::WebState* web_state) FaviconUrlObserver() = default;
: web::WebStateObserver(web_state) {}
// Returns vavicon url candidates received in FaviconUrlUpdated. // Returns vavicon url candidates received in FaviconUrlUpdated.
const std::vector<FaviconURL>& favicon_url_candidates() const { const std::vector<FaviconURL>& favicon_url_candidates() const {
return favicon_url_candidates_; return favicon_url_candidates_;
...@@ -27,39 +29,56 @@ class FaviconUrlObserver : public web::WebStateObserver { ...@@ -27,39 +29,56 @@ class FaviconUrlObserver : public web::WebStateObserver {
// Returns true if FaviconUrlUpdated was called. // Returns true if FaviconUrlUpdated was called.
bool favicon_url_updated() const { return favicon_url_updated_; } bool favicon_url_updated() const { return favicon_url_updated_; }
// WebStateObserver overrides: // WebStateObserver overrides:
void FaviconUrlUpdated(web::WebState* web_state, void FaviconUrlUpdated(WebState* web_state,
const std::vector<FaviconURL>& candidates) override { const std::vector<FaviconURL>& candidates) override {
favicon_url_candidates_ = candidates; favicon_url_candidates_ = candidates;
favicon_url_updated_ = true; favicon_url_updated_ = true;
} }
void WebStateDestroyed(WebState* web_state) override { NOTREACHED(); }
private: private:
bool favicon_url_updated_ = false; bool favicon_url_updated_ = false;
std::vector<FaviconURL> favicon_url_candidates_; std::vector<FaviconURL> favicon_url_candidates_;
DISALLOW_COPY_AND_ASSIGN(FaviconUrlObserver);
}; };
} // namespace } // namespace
// Test fixture for WebStateDelegate::FaviconUrlUpdated and integration tests. // Test fixture for WebStateDelegate::FaviconUrlUpdated and integration tests.
class FaviconCallbackTest : public web::WebTestWithWebState { class FaviconCallbackTest : public web::WebTestWithWebState {
public:
FaviconCallbackTest() = default;
protected: protected:
void SetUp() override { void SetUp() override {
web::WebTestWithWebState::SetUp(); web::WebTestWithWebState::SetUp();
observer_ = base::MakeUnique<FaviconUrlObserver>(web_state()); web_state()->AddObserver(observer());
}
void TearDown() override {
web_state()->RemoveObserver(observer());
web::WebTestWithWebState::TearDown();
} }
std::unique_ptr<FaviconUrlObserver> observer_;
FaviconUrlObserver* observer() { return &observer_; }
private:
FaviconUrlObserver observer_;
DISALLOW_COPY_AND_ASSIGN(FaviconCallbackTest);
}; };
// Tests page with shortcut icon link. // Tests page with shortcut icon link.
TEST_F(FaviconCallbackTest, ShortcutIconFavicon) { TEST_F(FaviconCallbackTest, ShortcutIconFavicon) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<link rel='shortcut icon' href='http://fav.ico'>"); LoadHtml(@"<link rel='shortcut icon' href='http://fav.ico'>");
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(1U, favicons.size()); ASSERT_EQ(1U, favicons.size());
EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type);
...@@ -68,14 +87,15 @@ TEST_F(FaviconCallbackTest, ShortcutIconFavicon) { ...@@ -68,14 +87,15 @@ TEST_F(FaviconCallbackTest, ShortcutIconFavicon) {
// Tests page with icon link. // Tests page with icon link.
TEST_F(FaviconCallbackTest, IconFavicon) { TEST_F(FaviconCallbackTest, IconFavicon) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<link rel='icon' href='http://fav.ico'>"); LoadHtml(@"<link rel='icon' href='http://fav.ico'>");
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(1U, favicons.size()); ASSERT_EQ(1U, favicons.size());
EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type);
...@@ -84,15 +104,16 @@ TEST_F(FaviconCallbackTest, IconFavicon) { ...@@ -84,15 +104,16 @@ TEST_F(FaviconCallbackTest, IconFavicon) {
// Tests page with apple-touch-icon link. // Tests page with apple-touch-icon link.
TEST_F(FaviconCallbackTest, AppleTouchIconFavicon) { TEST_F(FaviconCallbackTest, AppleTouchIconFavicon) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<link rel='apple-touch-icon' href='http://fav.ico'>", LoadHtml(@"<link rel='apple-touch-icon' href='http://fav.ico'>",
GURL("https://chromium.test")); GURL("https://chromium.test"));
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(2U, favicons.size()); ASSERT_EQ(2U, favicons.size());
EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kTouchIcon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kTouchIcon, favicons[0].icon_type);
...@@ -104,15 +125,16 @@ TEST_F(FaviconCallbackTest, AppleTouchIconFavicon) { ...@@ -104,15 +125,16 @@ TEST_F(FaviconCallbackTest, AppleTouchIconFavicon) {
// Tests page with apple-touch-icon-precomposed link. // Tests page with apple-touch-icon-precomposed link.
TEST_F(FaviconCallbackTest, AppleTouchIconPrecomposedFavicon) { TEST_F(FaviconCallbackTest, AppleTouchIconPrecomposedFavicon) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<link rel='apple-touch-icon-precomposed' href='http://fav.ico'>", LoadHtml(@"<link rel='apple-touch-icon-precomposed' href='http://fav.ico'>",
GURL("https://chromium.test")); GURL("https://chromium.test"));
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(2U, favicons.size()); ASSERT_EQ(2U, favicons.size());
EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kTouchPrecomposedIcon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kTouchPrecomposedIcon, favicons[0].icon_type);
...@@ -124,14 +146,15 @@ TEST_F(FaviconCallbackTest, AppleTouchIconPrecomposedFavicon) { ...@@ -124,14 +146,15 @@ TEST_F(FaviconCallbackTest, AppleTouchIconPrecomposedFavicon) {
// Tests page without favicon link. // Tests page without favicon link.
TEST_F(FaviconCallbackTest, NoFavicon) { TEST_F(FaviconCallbackTest, NoFavicon) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<html></html>", GURL("https://chromium.test/test/test.html")); LoadHtml(@"<html></html>", GURL("https://chromium.test/test/test.html"));
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(1U, favicons.size()); ASSERT_EQ(1U, favicons.size());
EXPECT_EQ(GURL("https://chromium.test/favicon.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("https://chromium.test/favicon.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type);
...@@ -140,17 +163,18 @@ TEST_F(FaviconCallbackTest, NoFavicon) { ...@@ -140,17 +163,18 @@ TEST_F(FaviconCallbackTest, NoFavicon) {
// Tests page with multiple favicon links. // Tests page with multiple favicon links.
TEST_F(FaviconCallbackTest, MultipleFavicons) { TEST_F(FaviconCallbackTest, MultipleFavicons) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<link rel='shortcut icon' href='http://fav.ico'>" LoadHtml(@"<link rel='shortcut icon' href='http://fav.ico'>"
"<link rel='icon' href='http://fav1.ico'>" "<link rel='icon' href='http://fav1.ico'>"
"<link rel='apple-touch-icon' href='http://fav2.ico'>" "<link rel='apple-touch-icon' href='http://fav2.ico'>"
"<link rel='apple-touch-icon-precomposed' href='http://fav3.ico'>"); "<link rel='apple-touch-icon-precomposed' href='http://fav3.ico'>");
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(4U, favicons.size()); ASSERT_EQ(4U, favicons.size());
EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("http://fav.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type);
...@@ -168,15 +192,16 @@ TEST_F(FaviconCallbackTest, MultipleFavicons) { ...@@ -168,15 +192,16 @@ TEST_F(FaviconCallbackTest, MultipleFavicons) {
// Tests page with invalid favicon url. // Tests page with invalid favicon url.
TEST_F(FaviconCallbackTest, InvalidFaviconUrl) { TEST_F(FaviconCallbackTest, InvalidFaviconUrl) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<html><head><link rel='icon' href='http://'></head></html>", LoadHtml(@"<html><head><link rel='icon' href='http://'></head></html>",
GURL("https://chromium.test")); GURL("https://chromium.test"));
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(1U, favicons.size()); ASSERT_EQ(1U, favicons.size());
EXPECT_EQ(GURL("https://chromium.test/favicon.ico"), favicons[0].icon_url); EXPECT_EQ(GURL("https://chromium.test/favicon.ico"), favicons[0].icon_url);
EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type); EXPECT_EQ(FaviconURL::IconType::kFavicon, favicons[0].icon_type);
...@@ -185,14 +210,15 @@ TEST_F(FaviconCallbackTest, InvalidFaviconUrl) { ...@@ -185,14 +210,15 @@ TEST_F(FaviconCallbackTest, InvalidFaviconUrl) {
// Tests page with empty favicon url. // Tests page with empty favicon url.
TEST_F(FaviconCallbackTest, EmptyFaviconUrl) { TEST_F(FaviconCallbackTest, EmptyFaviconUrl) {
ASSERT_TRUE(observer_->favicon_url_candidates().empty()); ASSERT_TRUE(observer()->favicon_url_candidates().empty());
LoadHtml(@"<head><link rel='icon' href=''></head>"); LoadHtml(@"<head><link rel='icon' href=''></head>");
WaitForCondition(^{ WaitForCondition(^{
return observer_->favicon_url_updated(); return observer()->favicon_url_updated();
}); });
const std::vector<FaviconURL>& favicons = observer_->favicon_url_candidates(); const std::vector<FaviconURL>& favicons =
observer()->favicon_url_candidates();
ASSERT_EQ(1U, favicons.size()); ASSERT_EQ(1U, favicons.size());
// TODO(crbug.com/721852): This result is not correct. // TODO(crbug.com/721852): This result is not correct.
EXPECT_EQ(GURL("https://chromium.test/"), favicons[0].icon_url); EXPECT_EQ(GURL("https://chromium.test/"), favicons[0].icon_url);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment