Commit 65a02c56 authored by Mike Baxley's avatar Mike Baxley Committed by Commit Bot

BrowserTest verifying TopSites updated on navigation.

Test that navigates to a URL, and then verifies that the URL
is included in TopSites.

Bug: 
Change-Id: Idd7f35241dfe83ba9950975664c380d81243560c
Reviewed-on: https://chromium-review.googlesource.com/559540
Commit-Queue: Mike Baxley <baxley@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485246}
parent 24e21016
// Copyright 2017 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.
//
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ntp_tiles/chrome_most_visited_sites_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/ntp_tiles/most_visited_sites.h"
#include "components/ntp_tiles/ntp_tile.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace ntp_tiles {
namespace {
using testing::Contains;
std::string PrintTile(const std::string& title,
const std::string& url,
TileSource source) {
return std::string("has title \"") + title + std::string("\" and url \"") +
url + std::string("\" and source ") +
testing::PrintToString(static_cast<int>(source));
}
MATCHER_P3(MatchesTile, title, url, source, PrintTile(title, url, source)) {
return arg.title == base::ASCIIToUTF16(title) && arg.url == GURL(url) &&
arg.source == source;
}
// Waits for most visited URLs to be made available.
class MostVisitedSitesWaiter : public MostVisitedSites::Observer {
public:
MostVisitedSitesWaiter() : tiles_(NTPTilesVector()) {}
// Waits until most visited URLs are available, and then returns all the
// tiles.
NTPTilesVector WaitForTiles() {
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
return tiles_;
}
void OnMostVisitedURLsAvailable(const NTPTilesVector& tiles) override {
tiles_ = tiles;
if (!quit_closure_.is_null()) {
quit_closure_.Run();
}
}
void OnIconMadeAvailable(const GURL& site_url) override {}
private:
base::Closure quit_closure_;
NTPTilesVector tiles_;
};
} // namespace
class NTPTilesTest : public InProcessBrowserTest {
public:
NTPTilesTest() {}
protected:
void SetUpOnMainThread() override {
most_visited_sites_ =
ChromeMostVisitedSitesFactory::NewForProfile(browser()->profile());
}
void TearDownOnMainThread() override {
// Reset most_visited_sites_, otherwise there is a CHECK in callback_list.h
// because callbacks_.size() is not 0.
most_visited_sites_.reset();
}
std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
};
// Tests that after navigating to a URL, ntp tiles will include the URL.
IN_PROC_BROWSER_TEST_F(NTPTilesTest, LoadURL) {
ASSERT_TRUE(embedded_test_server()->Start());
const GURL page_url = embedded_test_server()->GetURL("/simple.html");
ui_test_utils::NavigateToURLWithDisposition(
browser(), page_url, WindowOpenDisposition::CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
MostVisitedSitesWaiter waiter;
// This call will call SyncWithHistory(), which means the new URL will be in
// the next set of tiles that the waiter retrieves.
most_visited_sites_->SetMostVisitedURLsObserver(&waiter, /*num_sites=*/8);
NTPTilesVector tiles = waiter.WaitForTiles();
EXPECT_THAT(tiles, Contains(MatchesTile("OK", page_url.spec().c_str(),
TileSource::TOP_SITES)));
}
} // namespace ntp_tiles
......@@ -1357,6 +1357,7 @@ test("browser_tests") {
"../browser/net/sdch_browsertest.cc",
"../browser/net/websocket_browsertest.cc",
"../browser/ntp_snippets/content_suggestions_service_factory_browsertest.cc",
"../browser/ntp_tiles/ntp_tiles_browsertest.cc",
"../browser/page_load_metrics/observers/ads_page_load_metrics_observer_browsertest.cc",
"../browser/page_load_metrics/observers/https_engagement_metrics/https_engagement_page_load_metrics_observer_browsertest.cc",
"../browser/page_load_metrics/observers/multi_tab_loading_page_load_metrics_observer_browsertest.cc",
......
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