1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/bookmarks/bookmark_stats_tab_helper.h"
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace {
constexpr char kTestUrl1[] = "https://www.test-url-1.com/";
constexpr char kTestUrl2[] = "https://www.test-url-2.com/";
} // namespace
namespace test {
class BookmarkStatsTabHelperTest : public ::testing::Test {
public:
// ::testing::Test:
void SetUp() override {
web_contents_ =
content::WebContentsTester::CreateTestWebContents(&profile_, nullptr);
Test::SetUp();
}
protected:
void LoadUrl(const char* url) {
web_contents()->GetController().LoadURL(GURL(url), content::Referrer(),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
void CommitPendingNavigation() {
content::WebContentsTester::For(web_contents())->CommitPendingNavigation();
}
BookmarkStatsTabHelper* GetHelper() {
BookmarkStatsTabHelper::CreateForWebContents(web_contents_.get());
return BookmarkStatsTabHelper::FromWebContents(web_contents_.get());
}
content::WebContents* web_contents() { return web_contents_.get(); }
base::HistogramTester& histogram_tester() { return histogram_tester_; }
private:
content::BrowserTaskEnvironment task_environment_;
content::RenderViewHostTestEnabler rvh_enabler_;
TestingProfile profile_;
std::unique_ptr<content::WebContents> web_contents_;
base::HistogramTester histogram_tester_;
};
TEST_F(BookmarkStatsTabHelperTest, LaunchActionAddedWithPendingEntry) {
// The launch action should start unset.
BookmarkStatsTabHelper* helper = GetHelper();
EXPECT_FALSE(helper->launch_action_for_testing().has_value());
EXPECT_FALSE(helper->tab_disposition_for_testing().has_value());
// Attempting to set the launch action without a pending navigation having
// been set should result in a no-op.
helper->SetLaunchAction(
{BookmarkLaunchLocation::kAttachedBar, base::TimeTicks::Now()},
WindowOpenDisposition::CURRENT_TAB);
EXPECT_FALSE(helper->launch_action_for_testing().has_value());
EXPECT_FALSE(helper->tab_disposition_for_testing().has_value());
// Begin a navigation and attempt to set the launch action. This should now
// be reflected in the helper.
LoadUrl(kTestUrl1);
helper->SetLaunchAction(
{BookmarkLaunchLocation::kAttachedBar, base::TimeTicks::Now()},
WindowOpenDisposition::CURRENT_TAB);
EXPECT_TRUE(helper->launch_action_for_testing().has_value());
EXPECT_TRUE(helper->tab_disposition_for_testing().has_value());
// After the navigation completes the launch action data should remain.
CommitPendingNavigation();
EXPECT_TRUE(helper->launch_action_for_testing().has_value());
EXPECT_TRUE(helper->tab_disposition_for_testing().has_value());
// A following navigation should clear the launch action data.
LoadUrl(kTestUrl2);
CommitPendingNavigation();
EXPECT_FALSE(helper->launch_action_for_testing().has_value());
EXPECT_FALSE(helper->tab_disposition_for_testing().has_value());
}
TEST_F(BookmarkStatsTabHelperTest, EmitsNonEmptyPaintMetrics) {
// The launch action should start unset.
BookmarkStatsTabHelper* helper = GetHelper();
EXPECT_FALSE(helper->launch_action_for_testing().has_value());
EXPECT_FALSE(helper->tab_disposition_for_testing().has_value());
histogram_tester().ExpectTotalCount(
"Bookmarks.AttachedBar.CurrentTab.TimeToFirstVisuallyNonEmptyPaint", 0);
// Begin a navigation and attempt to set the launch action. This should now
// be reflected in the helper.
LoadUrl(kTestUrl1);
helper->SetLaunchAction(
{BookmarkLaunchLocation::kAttachedBar, base::TimeTicks::Now()},
WindowOpenDisposition::CURRENT_TAB);
EXPECT_TRUE(helper->launch_action_for_testing().has_value());
EXPECT_TRUE(helper->tab_disposition_for_testing().has_value());
// Simulate a non-empty paint event. The non-empty paint metric should be
// emitted.
helper->DidFirstVisuallyNonEmptyPaint();
histogram_tester().ExpectTotalCount(
"Bookmarks.AttachedBar.CurrentTab.TimeToFirstVisuallyNonEmptyPaint", 1);
// A following navigation should clear the launch action data.
LoadUrl(kTestUrl2);
CommitPendingNavigation();
EXPECT_FALSE(helper->launch_action_for_testing().has_value());
EXPECT_FALSE(helper->tab_disposition_for_testing().has_value());
// Simulate a non-empty paint event. No metrics should have been emitted as
// the launch action should have been reset.
helper->DidFirstVisuallyNonEmptyPaint();
histogram_tester().ExpectTotalCount(
"Bookmarks.AttachedBar.CurrentTab.TimeToFirstVisuallyNonEmptyPaint", 1);
}
} // namespace test
|