Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Unified Diff: components/wallpaper/wallpaper_color_calculator_unittest.cc

Issue 2824883006: [ash-md] Reduce thread hopping for cheap wallpaper color extraction. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/wallpaper/wallpaper_color_calculator_unittest.cc
diff --git a/components/wallpaper/wallpaper_color_calculator_unittest.cc b/components/wallpaper/wallpaper_color_calculator_unittest.cc
index 31268f70e3be1b2f14f7d31586a4dc40d8dfe25e..8c25a02bb470e8318338720c0d326e8d80a6e28a 100644
--- a/components/wallpaper/wallpaper_color_calculator_unittest.cc
+++ b/components/wallpaper/wallpaper_color_calculator_unittest.cc
@@ -8,6 +8,7 @@
#include "base/macros.h"
#include "base/memory/ptr_util.h"
+#include "base/test/histogram_tester.h"
#include "base/test/null_task_runner.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
@@ -47,10 +48,11 @@ class TestWallpaperColorCalculatorObserver
} // namespace
-class WallPaperColorCalculatorTest : public testing::Test {
+// Test fixture targeting asynchronous color calculations.
+class WallPaperColorCalculatorAsyncTest : public testing::Test {
public:
- WallPaperColorCalculatorTest();
- ~WallPaperColorCalculatorTest() override;
+ WallPaperColorCalculatorAsyncTest();
+ ~WallPaperColorCalculatorAsyncTest() override;
protected:
void InstallTaskRunner(
@@ -64,14 +66,16 @@ class WallPaperColorCalculatorTest : public testing::Test {
TestWallpaperColorCalculatorObserver observer_;
+ base::HistogramTester histograms_;
+
private:
// Required by PostTaskAndReplyImpl.
std::unique_ptr<base::ThreadTaskRunnerHandle> task_runner_handle_;
- DISALLOW_COPY_AND_ASSIGN(WallPaperColorCalculatorTest);
+ DISALLOW_COPY_AND_ASSIGN(WallPaperColorCalculatorAsyncTest);
};
-WallPaperColorCalculatorTest::WallPaperColorCalculatorTest()
+WallPaperColorCalculatorAsyncTest::WallPaperColorCalculatorAsyncTest()
: task_runner_(new base::TestMockTimeTaskRunner()) {
// Creates an |image_| that will yield a non-default prominent color.
const gfx::Size kImageSize(300, 200);
@@ -92,9 +96,9 @@ WallPaperColorCalculatorTest::WallPaperColorCalculatorTest()
InstallTaskRunner(task_runner_);
}
-WallPaperColorCalculatorTest::~WallPaperColorCalculatorTest() {}
+WallPaperColorCalculatorAsyncTest::~WallPaperColorCalculatorAsyncTest() {}
-void WallPaperColorCalculatorTest::InstallTaskRunner(
+void WallPaperColorCalculatorAsyncTest::InstallTaskRunner(
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
task_runner_handle_.reset();
task_runner_handle_ =
@@ -102,7 +106,50 @@ void WallPaperColorCalculatorTest::InstallTaskRunner(
calculator_->SetTaskRunnerForTest(task_runner);
}
-TEST_F(WallPaperColorCalculatorTest,
+TEST_F(WallPaperColorCalculatorAsyncTest,
+ AsynchronousMetricsForSuccessfulExtraction) {
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.ExecutionType",
+ 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.TimeSpentExtractingColors_v2", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.AsyncTimeSpentExtractingColors",
+ 0);
+
+ EXPECT_TRUE(calculator_->StartCalculation());
+ task_runner_->RunUntilIdle();
+
+ histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtraction.ExecutionType",
+ false, 1);
+ histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtractionResult", true,
+ 1);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.TimeSpentExtractingColors_v2", 1);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.AsyncTimeSpentExtractingColors",
+ 1);
+}
+
+TEST_F(WallPaperColorCalculatorAsyncTest,
+ AsynchronousMetricsForFailedExtraction) {
+ scoped_refptr<base::NullTaskRunner> task_runner = new base::NullTaskRunner();
+ InstallTaskRunner(task_runner);
+
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.ExecutionType",
+ 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.TimeSpentExtractingColors_v2", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.AsyncTimeSpentExtractingColors",
+ 0);
+
+ EXPECT_FALSE(calculator_->StartCalculation());
+
+ histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtraction.ExecutionType",
+ false, 1);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.TimeSpentExtractingColors_v2", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.AsyncTimeSpentExtractingColors",
+ 0);
+}
+
+TEST_F(WallPaperColorCalculatorAsyncTest,
StartCalculationReturnsFalseWhenPostingTaskFails) {
scoped_refptr<base::NullTaskRunner> task_runner = new base::NullTaskRunner();
InstallTaskRunner(task_runner);
@@ -112,7 +159,8 @@ TEST_F(WallPaperColorCalculatorTest,
EXPECT_EQ(kDefaultColor, calculator_->prominent_color());
}
-TEST_F(WallPaperColorCalculatorTest, ObserverNotifiedOnSuccessfulCalculation) {
+TEST_F(WallPaperColorCalculatorAsyncTest,
+ ObserverNotifiedOnSuccessfulCalculation) {
EXPECT_FALSE(observer_.WasNotified());
EXPECT_TRUE(calculator_->StartCalculation());
@@ -122,7 +170,7 @@ TEST_F(WallPaperColorCalculatorTest, ObserverNotifiedOnSuccessfulCalculation) {
EXPECT_TRUE(observer_.WasNotified());
}
-TEST_F(WallPaperColorCalculatorTest, ColorUpdatedOnSuccessfulCalculation) {
+TEST_F(WallPaperColorCalculatorAsyncTest, ColorUpdatedOnSuccessfulCalculation) {
calculator_->set_prominent_color_for_test(kDefaultColor);
EXPECT_TRUE(calculator_->StartCalculation());
@@ -132,7 +180,7 @@ TEST_F(WallPaperColorCalculatorTest, ColorUpdatedOnSuccessfulCalculation) {
EXPECT_NE(kDefaultColor, calculator_->prominent_color());
}
-TEST_F(WallPaperColorCalculatorTest,
+TEST_F(WallPaperColorCalculatorAsyncTest,
NoCrashWhenCalculatorDestroyedBeforeTaskProcessing) {
EXPECT_TRUE(calculator_->StartCalculation());
calculator_.reset();
@@ -144,4 +192,68 @@ TEST_F(WallPaperColorCalculatorTest,
EXPECT_FALSE(task_runner_->HasPendingTask());
}
+// Test fixture targeting synchronous color calculations.
+class WallPaperColorCalculatorSyncTest : public testing::Test {
+ public:
+ WallPaperColorCalculatorSyncTest();
+ ~WallPaperColorCalculatorSyncTest() override;
+
+ protected:
+ void InstallTaskRunner(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner);
+
+ gfx::ImageSkia image_;
+
+ std::unique_ptr<WallpaperColorCalculator> calculator_;
+
+ TestWallpaperColorCalculatorObserver observer_;
+
+ base::HistogramTester histograms_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(WallPaperColorCalculatorSyncTest);
+};
+
+WallPaperColorCalculatorSyncTest::WallPaperColorCalculatorSyncTest() {
+ // Creates an |image_| that will yield a non-default prominent color.
+ const gfx::Size kImageSize(5, 5);
+ const SkColor kGray = SkColorSetRGB(10, 10, 10);
+ const SkColor kVibrantGreen = SkColorSetRGB(25, 200, 25);
+
+ gfx::Canvas canvas(kImageSize, 1.0f, true);
+ canvas.FillRect(gfx::Rect(kImageSize), kGray);
+ canvas.FillRect(gfx::Rect(0, 1, 5, 1), kVibrantGreen);
+
+ image_ = gfx::ImageSkia::CreateFrom1xBitmap(canvas.GetBitmap());
+
+ calculator_ = base::MakeUnique<WallpaperColorCalculator>(
+ image_, color_utils::LumaRange::NORMAL,
+ color_utils::SaturationRange::VIBRANT, nullptr);
+ calculator_->AddObserver(&observer_);
+
+ calculator_->SetTaskRunnerForTest(nullptr);
+}
+
+WallPaperColorCalculatorSyncTest::~WallPaperColorCalculatorSyncTest() {}
+
+TEST_F(WallPaperColorCalculatorSyncTest, SynchronousMetrics) {
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.ExecutionType",
+ 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.TimeSpentExtractingColors_v2", 0);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.AsyncTimeSpentExtractingColors",
+ 0);
+
+ EXPECT_TRUE(calculator_->StartCalculation());
+
+ histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtraction.ExecutionType",
+ true, 1);
+ histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtractionResult", true,
+ 1);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.TimeSpentExtractingColors_v2", 1);
+ histograms_.ExpectTotalCount("Ash.Wallpaper.AsyncTimeSpentExtractingColors",
+ 0);
+ ;
+}
+
} // namespace wallpaper

Powered by Google App Engine
This is Rietveld 408576698