Skip to content

Commit

Permalink
speedreader: Add session logic tests
Browse files Browse the repository at this point in the history
We add two browser test cases for Speedreader. In these tests
Speedreader is enabled.
  (1) Run a readable page through Speedreader. Close the browser and
      open it back up. The page should restore as readable.
  (2) Navigate to a non-readable page, then to a readable page, then do
      a back navigation. The readable state should not "stick".
  • Loading branch information
Kevin Kuehler committed Sep 2, 2021
1 parent 1a34c05 commit 7f31be1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
96 changes: 87 additions & 9 deletions browser/speedreader/speedreader_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@
#include "brave/components/speedreader/features.h"
#include "brave/components/speedreader/speedreader_service.h"
#include "brave/components/speedreader/speedreader_switches.h"
#include "chrome/browser/profiles/profile_keep_alive_types.h"
#include "chrome/browser/profiles/scoped_profile_keep_alive.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

const char kTestHost[] = "theguardian.com";
const char kTestPage[] = "/guardian.html";
const char kTestPageSimple[] = "/simple.html";
const char kTestPageReadable[] = "/articles/guardian.html";
const base::FilePath::StringPieceType kTestWhitelist =
FILE_PATH_LITERAL("speedreader_whitelist.json");

Expand Down Expand Up @@ -69,29 +79,99 @@ class SpeedReaderBrowserTest : public InProcessBrowserTest {
return browser()->tab_strip_model()->GetActiveWebContents();
}

speedreader::SpeedreaderTabHelper* tab_helper() {
return speedreader::SpeedreaderTabHelper::FromWebContents(
ActiveWebContents());
}

speedreader::SpeedreaderService* speedreader_service() {
return speedreader::SpeedreaderServiceFactory::GetForProfile(
browser()->profile());
}

void ToggleSpeedreader() {
auto* speedreader_service =
speedreader::SpeedreaderServiceFactory::GetForProfile(
browser()->profile());
speedreader_service->ToggleSpeedreader();
speedreader_service()->ToggleSpeedreader();
ActiveWebContents()->GetController().Reload(content::ReloadType::NORMAL,
false);
}

void EnableSpeedreader() {
speedreader_service()->EnableSpeedreaderForTest();
}

void DisableSpeedreader() {
speedreader_service()->DisableSpeedreaderForTest();
}

void GoBack(Browser* browser) {
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
chrome::GoBack(browser, WindowOpenDisposition::CURRENT_TAB);
observer.Wait();
}

void NavigateToPageSynchronously(
const char* path,
WindowOpenDisposition disposition =
WindowOpenDisposition::NEW_FOREGROUND_TAB) {
const GURL url = https_server_.GetURL(kTestHost, path);
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, disposition,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
}

protected:
base::test::ScopedFeatureList feature_list_;
net::EmbeddedTestServer https_server_;
};

IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, RestoreSpeedreaderPage) {
EnableSpeedreader();
const GURL url = https_server_.GetURL(kTestHost, kTestPageReadable);
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
EXPECT_TRUE(
speedreader::PageStateIsDistilled(tab_helper()->PageDistillState()));

Profile* profile = browser()->profile();

ScopedKeepAlive test_keep_alive(KeepAliveOrigin::PANEL_VIEW,
KeepAliveRestartOption::DISABLED);
ScopedProfileKeepAlive test_profile_keep_alive(
profile, ProfileKeepAliveOrigin::kBrowserWindow);
CloseBrowserSynchronously(browser());

EXPECT_EQ(0u, BrowserList::GetInstance()->size());
chrome::OpenWindowWithRestoredTabs(profile);
EXPECT_EQ(1u, BrowserList::GetInstance()->size());
SelectFirstBrowser();
EXPECT_TRUE(
speedreader::PageStateIsDistilled(tab_helper()->PageDistillState()));
}

IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, NavigationNostickTest) {
EnableSpeedreader();
NavigateToPageSynchronously(kTestPageSimple);
EXPECT_FALSE(
speedreader::PageStateIsDistilled(tab_helper()->PageDistillState()));
NavigateToPageSynchronously(kTestPageReadable,
WindowOpenDisposition::CURRENT_TAB);
EXPECT_TRUE(
speedreader::PageStateIsDistilled(tab_helper()->PageDistillState()));

// Ensure distill state doesn't stick when we back-navigate from a readable
// page to a non-readable one.
GoBack(browser());
EXPECT_FALSE(
speedreader::PageStateIsDistilled(tab_helper()->PageDistillState()));
}

// disabled in https://github.com/brave/brave-browser/issues/11328
IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, DISABLED_SmokeTest) {
ToggleSpeedreader();
const GURL url = https_server_.GetURL(kTestHost, kTestPage);
const GURL url = https_server_.GetURL(kTestHost, kTestPageReadable);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents = ActiveWebContents();
content::RenderFrameHost* rfh = contents->GetMainFrame();
Expand All @@ -114,11 +194,9 @@ IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, DISABLED_SmokeTest) {
}

IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, P3ATest) {
DisableSpeedreader();
base::HistogramTester tester;

const GURL url = https_server_.GetURL(kTestHost, kTestPage);
ui_test_utils::NavigateToURL(browser(), url);

// SpeedReader never enabled
EXPECT_FALSE(speedreader_service()->IsEnabled());
tester.ExpectBucketCount(kSpeedreaderEnabledUMAHistogramName, 0, 1);
Expand Down
8 changes: 8 additions & 0 deletions components/speedreader/speedreader_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ void SpeedreaderService::ToggleSpeedreader() {
!enabled); // toggling - now enabled
}

void SpeedreaderService::EnableSpeedreaderForTest() {
prefs_->SetBoolean(kSpeedreaderPrefEnabled, true);
}

void SpeedreaderService::DisableSpeedreaderForTest() {
prefs_->SetBoolean(kSpeedreaderPrefEnabled, false);
}

bool SpeedreaderService::IsEnabled() {
if (!base::FeatureList::IsEnabled(kSpeedreaderFeature)) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions components/speedreader/speedreader_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SpeedreaderService : public KeyedService {
static void RegisterProfilePrefs(PrefRegistrySimple* registry);

void ToggleSpeedreader();
void EnableSpeedreaderForTest();
void DisableSpeedreaderForTest();
bool IsEnabled();
bool ShouldPromptUserToEnable() const;
void IncrementPromptCount();
Expand Down
File renamed without changes.

0 comments on commit 7f31be1

Please sign in to comment.