Commit 9abd41a8 authored by olivierrobin's avatar olivierrobin Committed by Commit bot

Do not reload pengingItem to open Reading List offline page.

Reload will delete Pending Item so altering it and reload will reload
the lastCommittedItem.
If there is a pending item, stop loading and trigger a new navigation.

BUG=678293

Review-Url: https://codereview.chromium.org/2610953002
Cr-Commit-Position: refs/heads/master@{#441926}
parent 8b0fd02a
...@@ -49,7 +49,7 @@ class ReadingListWebStateObserver : public web::WebStateObserver, ...@@ -49,7 +49,7 @@ class ReadingListWebStateObserver : public web::WebStateObserver,
void StopCheckingProgress(); void StopCheckingProgress();
// Loads the offline version of the URL in place of the current page. // Loads the offline version of the URL in place of the current page.
void LoadOfflineReadingListEntry(web::NavigationItem* item); void LoadOfflineReadingListEntry();
// Returns if the current page with |url| has an offline version that can be // Returns if the current page with |url| has an offline version that can be
// displayed if the normal loading fails. // displayed if the normal loading fails.
......
...@@ -125,7 +125,7 @@ void ReadingListWebStateObserver::ReadingListModelLoaded( ...@@ -125,7 +125,7 @@ void ReadingListWebStateObserver::ReadingListModelLoaded(
const GURL& currentURL = item->GetVirtualURL(); const GURL& currentURL = item->GetVirtualURL();
if (IsUrlAvailableOffline(currentURL)) { if (IsUrlAvailableOffline(currentURL)) {
pending_url_ = currentURL; pending_url_ = currentURL;
LoadOfflineReadingListEntry(item); LoadOfflineReadingListEntry();
StopCheckingProgress(); StopCheckingProgress();
} }
} }
...@@ -203,7 +203,7 @@ void ReadingListWebStateObserver::PageLoaded( ...@@ -203,7 +203,7 @@ void ReadingListWebStateObserver::PageLoaded(
reading_list_model_->SetReadStatus(pending_url_, true); reading_list_model_->SetReadStatus(pending_url_, true);
UMA_HISTOGRAM_BOOLEAN("ReadingList.OfflineVersionDisplayed", false); UMA_HISTOGRAM_BOOLEAN("ReadingList.OfflineVersionDisplayed", false);
} else { } else {
LoadOfflineReadingListEntry(item); LoadOfflineReadingListEntry();
} }
StopCheckingProgress(); StopCheckingProgress();
} }
...@@ -245,7 +245,7 @@ void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() { ...@@ -245,7 +245,7 @@ void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() {
double progress = web_state()->GetLoadingProgress(); double progress = web_state()->GetLoadingProgress();
const double kMinimumExpectedProgressPerStep = 0.25; const double kMinimumExpectedProgressPerStep = 0.25;
if (progress < try_number_ * kMinimumExpectedProgressPerStep) { if (progress < try_number_ * kMinimumExpectedProgressPerStep) {
LoadOfflineReadingListEntry(item); LoadOfflineReadingListEntry();
StopCheckingProgress(); StopCheckingProgress();
return; return;
} }
...@@ -258,9 +258,7 @@ void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() { ...@@ -258,9 +258,7 @@ void ReadingListWebStateObserver::VerifyIfReadingListEntryStartedLoading() {
} }
} }
void ReadingListWebStateObserver::LoadOfflineReadingListEntry( void ReadingListWebStateObserver::LoadOfflineReadingListEntry() {
web::NavigationItem* item) {
DCHECK(item);
DCHECK(reading_list_model_); DCHECK(reading_list_model_);
if (!pending_url_.is_valid() || !IsUrlAvailableOffline(pending_url_)) { if (!pending_url_.is_valid() || !IsUrlAvailableOffline(pending_url_)) {
return; return;
...@@ -270,9 +268,20 @@ void ReadingListWebStateObserver::LoadOfflineReadingListEntry( ...@@ -270,9 +268,20 @@ void ReadingListWebStateObserver::LoadOfflineReadingListEntry(
DCHECK(entry->DistilledState() == ReadingListEntry::PROCESSED); DCHECK(entry->DistilledState() == ReadingListEntry::PROCESSED);
GURL url = GURL url =
reading_list::DistilledURLForPath(entry->DistilledPath(), entry->URL()); reading_list::DistilledURLForPath(entry->DistilledPath(), entry->URL());
item->SetURL(url); web::NavigationManager* manager = web_state()->GetNavigationManager();
item->SetVirtualURL(pending_url_); web::NavigationItem* item = manager->GetPendingItem();
web_state()->GetNavigationManager()->Reload(false); if (item) {
web_state()->Stop();
web::WebState::OpenURLParams params(url, item->GetReferrer(),
WindowOpenDisposition::CURRENT_TAB,
item->GetTransitionType(), NO);
web_state()->OpenURL(params);
} else {
item = manager->GetLastCommittedItem();
item->SetURL(url);
item->SetVirtualURL(pending_url_);
web_state()->GetNavigationManager()->Reload(false);
}
reading_list_model_->SetReadStatus(entry->URL(), true); reading_list_model_->SetReadStatus(entry->URL(), true);
UMA_HISTOGRAM_BOOLEAN("ReadingList.OfflineVersionDisplayed", true); UMA_HISTOGRAM_BOOLEAN("ReadingList.OfflineVersionDisplayed", true);
} }
...@@ -31,6 +31,18 @@ class TestNavigationManager : public web::TestNavigationManager { ...@@ -31,6 +31,18 @@ class TestNavigationManager : public web::TestNavigationManager {
bool reload_called_ = false; bool reload_called_ = false;
}; };
// A Test navigation manager that remembers the last opened parameters.
class TestWebState : public web::TestWebState {
public:
void OpenURL(const web::WebState::OpenURLParams& params) override {
last_opened_url_ = params.url;
}
const GURL& LastOpenedUrl() { return last_opened_url_; }
private:
GURL last_opened_url_;
};
// Test fixture to test loading of Reading list entries. // Test fixture to test loading of Reading list entries.
class ReadingListWebStateObserverTest : public web::WebTest { class ReadingListWebStateObserverTest : public web::WebTest {
public: public:
...@@ -54,7 +66,7 @@ class ReadingListWebStateObserverTest : public web::WebTest { ...@@ -54,7 +66,7 @@ class ReadingListWebStateObserverTest : public web::WebTest {
std::unique_ptr<web::NavigationItem> last_committed_item_; std::unique_ptr<web::NavigationItem> last_committed_item_;
std::unique_ptr<ReadingListModelImpl> reading_list_model_; std::unique_ptr<ReadingListModelImpl> reading_list_model_;
TestNavigationManager* test_navigation_manager_; TestNavigationManager* test_navigation_manager_;
web::TestWebState test_web_state_; TestWebState test_web_state_;
}; };
// Tests that failing loading an online version does not mark it read. // Tests that failing loading an online version does not mark it read.
...@@ -97,8 +109,8 @@ TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListOnline) { ...@@ -97,8 +109,8 @@ TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListOnline) {
EXPECT_TRUE(entry->IsRead()); EXPECT_TRUE(entry->IsRead());
} }
// Tests that loading a distilled version of an entry. // Tests that loading a distilled version of an entry from a commited entry.
TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListDistilled) { TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListDistilledCommitted) {
GURL url(kTestURL); GURL url(kTestURL);
std::string distilled_path = kTestDistilledPath; std::string distilled_path = kTestDistilledPath;
reading_list_model_->SetEntryDistilledPath(url, reading_list_model_->SetEntryDistilledPath(url,
...@@ -112,10 +124,32 @@ TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListDistilled) { ...@@ -112,10 +124,32 @@ TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListDistilled) {
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::FAILURE); test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::FAILURE);
test_web_state_.SetLoading(false); test_web_state_.SetLoading(false);
EXPECT_FALSE(test_navigation_manager_->ReloadCalled());
EXPECT_EQ(test_web_state_.LastOpenedUrl(), distilled_url);
EXPECT_TRUE(entry->IsRead());
}
// Tests that loading a distilled version of an entry.
TEST_F(ReadingListWebStateObserverTest, TestLoadReadingListDistilledPending) {
GURL url(kTestURL);
std::string distilled_path = kTestDistilledPath;
reading_list_model_->SetEntryDistilledPath(url,
base::FilePath(distilled_path));
const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
GURL distilled_url =
reading_list::DistilledURLForPath(entry->DistilledPath(), entry->URL());
test_navigation_manager_->SetPendingItem(nil);
test_navigation_manager_->GetLastCommittedItem()->SetURL(url);
test_web_state_.SetLoading(true);
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::FAILURE);
test_web_state_.SetLoading(false);
EXPECT_TRUE(test_navigation_manager_->ReloadCalled()); EXPECT_TRUE(test_navigation_manager_->ReloadCalled());
EXPECT_EQ(test_navigation_manager_->GetLastCommittedItem()->GetVirtualURL(), EXPECT_EQ(test_navigation_manager_->GetLastCommittedItem()->GetVirtualURL(),
url); url);
EXPECT_EQ(test_navigation_manager_->GetLastCommittedItem()->GetURL(), EXPECT_EQ(test_navigation_manager_->GetLastCommittedItem()->GetURL(),
distilled_url); distilled_url);
EXPECT_TRUE(entry->IsRead()); EXPECT_TRUE(entry->IsRead());
} }
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