Chromium Code Reviews| 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..147ef38d4f9e10ccaafc1f25a2dfe33befdad3a7 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,38 @@ void WallPaperColorCalculatorTest::InstallTaskRunner( |
| calculator_->SetTaskRunnerForTest(task_runner); |
| } |
| -TEST_F(WallPaperColorCalculatorTest, |
| +TEST_F(WallPaperColorCalculatorAsyncTest, |
| + AsynchronousMetricsForSuccessfulExtraction) { |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.Durations", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.UserDelay", 0); |
| + |
| + EXPECT_TRUE(calculator_->StartCalculation()); |
| + task_runner_->RunUntilIdle(); |
| + |
| + histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtractionResult", true, |
| + 1); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.Durations", 1); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.UserDelay", 1); |
| +} |
| + |
| +TEST_F(WallPaperColorCalculatorAsyncTest, |
| + AsynchronousMetricsForFailedExtraction) { |
|
Evan Stade
2017/04/20 21:37:15
is this actually failed extraction or failed task
bruthig
2017/04/21 15:42:08
Renamed and added test for failed extraction.
|
| + scoped_refptr<base::NullTaskRunner> task_runner = new base::NullTaskRunner(); |
| + InstallTaskRunner(task_runner); |
| + |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.Durations", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.UserDelay", 0); |
| + |
| + EXPECT_FALSE(calculator_->StartCalculation()); |
| + |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtractionResult", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.Durations", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.UserDelay", 0); |
| +} |
| + |
| +TEST_F(WallPaperColorCalculatorAsyncTest, |
| StartCalculationReturnsFalseWhenPostingTaskFails) { |
| scoped_refptr<base::NullTaskRunner> task_runner = new base::NullTaskRunner(); |
| InstallTaskRunner(task_runner); |
| @@ -112,7 +147,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 +158,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 +168,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 +180,62 @@ 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.ColorExtractionResult", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.Durations", 0); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.UserDelay", 0); |
| + |
| + EXPECT_TRUE(calculator_->StartCalculation()); |
| + |
| + histograms_.ExpectUniqueSample("Ash.Wallpaper.ColorExtractionResult", true, |
| + 1); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.Durations", 1); |
| + histograms_.ExpectTotalCount("Ash.Wallpaper.ColorExtraction.UserDelay", 0); |
| + ; |
| +} |
| + |
| } // namespace wallpaper |