Commit 1bbbdca3 authored by Qiang Xu's avatar Qiang Xu Committed by Commit Bot

arc: add ArcAppDataSearchProviderTest

Bug: 818902
Test: added test coverage
Change-Id: Ie5c0edf874c50be60b48b37833fcf1e8991e5728
Reviewed-on: https://chromium-review.googlesource.com/963089
Commit-Queue: Qiang Xu <warx@google.com>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarLuis Hector Chavez <lhchavez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543529}
parent d86d1e44
// Copyright 2018 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 "chrome/browser/ui/app_list/search/arc/arc_app_data_search_provider.h"
#include <memory>
#include <utility>
#include "ash/app_list/model/search/search_result.h"
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/app_list/app_list_test_util.h"
#include "chrome/browser/ui/app_list/arc/arc_app_test.h"
#include "chrome/browser/ui/app_list/search/arc/icon_decode_request.h"
#include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
namespace app_list {
class ArcAppDataSearchProviderTest : public AppListTestBase {
protected:
ArcAppDataSearchProviderTest() = default;
~ArcAppDataSearchProviderTest() override = default;
// AppListTestBase:
void SetUp() override {
AppListTestBase::SetUp();
arc_test_.SetUp(profile());
controller_ = std::make_unique<test::TestAppListControllerDelegate>();
}
void TearDown() override {
controller_.reset();
arc_test_.TearDown();
AppListTestBase::TearDown();
}
std::unique_ptr<ArcAppDataSearchProvider> CreateSearch(int max_results) {
return std::make_unique<ArcAppDataSearchProvider>(max_results, profile(),
controller_.get());
}
private:
std::unique_ptr<test::TestAppListControllerDelegate> controller_;
ArcAppTest arc_test_;
DISALLOW_COPY_AND_ASSIGN(ArcAppDataSearchProviderTest);
};
TEST_F(ArcAppDataSearchProviderTest, Basic) {
constexpr size_t kMaxResults = 6;
constexpr char kQuery[] = "App Data Search";
std::unique_ptr<ArcAppDataSearchProvider> provider =
CreateSearch(kMaxResults);
EXPECT_TRUE(provider->results().empty());
IconDecodeRequest::DisableSafeDecodingForTesting();
provider->Start(base::UTF8ToUTF16(kQuery));
const auto& results = provider->results();
EXPECT_EQ(kMaxResults, results.size());
// Check that information is correctly set in each result.
for (size_t i = 0; i < results.size(); ++i) {
SCOPED_TRACE(base::StringPrintf("Testing result %zu", i));
EXPECT_EQ(base::UTF16ToUTF8(results[i]->title()),
base::StringPrintf("Label %s %zu", kQuery, i));
EXPECT_EQ(SearchResult::DISPLAY_TILE, results[i]->display_type());
}
}
} // namespace app_list
......@@ -88,10 +88,7 @@ ArcAppDataSearchResult::ArcAppDataSearchResult(
icon_decode_request_ = std::make_unique<IconDecodeRequest>(
base::BindOnce(&ArcAppDataSearchResult::SetIconToAvatarIcon,
weak_ptr_factory_.GetWeakPtr()));
ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data(),
ImageDecoder::DEFAULT_CODEC, true,
gfx::Size());
icon_decode_request_->StartWithOptions(icon_png_data());
}
ArcAppDataSearchResult::~ArcAppDataSearchResult() = default;
......
......@@ -15,6 +15,7 @@
#include "chrome/browser/ui/app_list/app_list_test_util.h"
#include "chrome/browser/ui/app_list/arc/arc_app_test.h"
#include "chrome/browser/ui/app_list/search/arc/arc_playstore_search_result.h"
#include "chrome/browser/ui/app_list/search/arc/icon_decode_request.h"
#include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/common/extension_builder.h"
......@@ -74,7 +75,7 @@ TEST_F(ArcPlayStoreSearchProviderTest, Basic) {
std::unique_ptr<ArcPlayStoreSearchProvider> provider =
CreateSearch(kMaxResults);
EXPECT_TRUE(provider->results().empty());
ArcPlayStoreSearchResult::DisableSafeDecodingForTesting();
IconDecodeRequest::DisableSafeDecodingForTesting();
AddExtension(CreateExtension(extension_misc::kGmailAppId).get());
......
......@@ -19,13 +19,11 @@
#include "components/crx_file/id_util.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/app_list/vector_icons/vector_icons.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/paint_vector_icon.h"
namespace {
bool disable_safe_decoding_for_testing = false;
// The id prefix to identify a Play Store search result.
constexpr char kPlayAppPrefix[] = "play://";
// Badge icon color, #000 at 54% opacity.
......@@ -57,11 +55,6 @@ bool LaunchIntent(const std::string& intent_uri, int64_t display_id) {
namespace app_list {
// static
void ArcPlayStoreSearchResult::DisableSafeDecodingForTesting() {
disable_safe_decoding_for_testing = true;
}
ArcPlayStoreSearchResult::ArcPlayStoreSearchResult(
arc::mojom::AppDiscoveryResultPtr data,
Profile* profile,
......@@ -83,21 +76,7 @@ ArcPlayStoreSearchResult::ArcPlayStoreSearchResult(
icon_decode_request_ = std::make_unique<IconDecodeRequest>(base::BindOnce(
&ArcPlayStoreSearchResult::SetIcon, weak_ptr_factory_.GetWeakPtr()));
if (disable_safe_decoding_for_testing) {
SkBitmap bitmap;
if (!icon_png_data().empty() &&
gfx::PNGCodec::Decode(
reinterpret_cast<const unsigned char*>(icon_png_data().data()),
icon_png_data().size(), &bitmap)) {
icon_decode_request_->OnImageDecoded(bitmap);
} else {
icon_decode_request_->OnDecodeImageFailed();
}
} else {
ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data(),
ImageDecoder::DEFAULT_CODEC, true,
gfx::Size());
}
icon_decode_request_->StartWithOptions(icon_png_data());
}
ArcPlayStoreSearchResult::~ArcPlayStoreSearchResult() = default;
......
......@@ -39,13 +39,6 @@ class ArcPlayStoreSearchResult : public SearchResult,
// app_list::AppContextMenuDelegate overrides:
void ExecuteLaunchCommand(int event_flags) override;
// Disables async safe decoding requests when unit tests are executed.
// Icons are decoded at a separate process created by ImageDecoder. In unit
// tests these tasks may not finish before the test exits, which causes a
// failure in the base::MessageLoop::current()->IsIdleForTesting() check
// in test_browser_thread_bundle.cc.
static void DisableSafeDecodingForTesting();
private:
const base::Optional<std::string>& install_intent_uri() const {
return data_->install_intent_uri;
......
......@@ -4,10 +4,15 @@
#include "chrome/browser/ui/app_list/search/arc/icon_decode_request.h"
#include "ash/app_list/model/search/search_result.h"
#include <memory>
#include <utility>
#include <vector>
#include "chrome/grit/component_extension_resources.h"
#include "content/public/browser/browser_thread.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/image/image_skia_source.h"
......@@ -18,6 +23,8 @@ namespace app_list {
namespace {
bool disable_safe_decoding_for_testing = false;
class IconSource : public gfx::ImageSkiaSource {
public:
IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip);
......@@ -67,11 +74,37 @@ gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) {
} // namespace
// static
void IconDecodeRequest::DisableSafeDecodingForTesting() {
disable_safe_decoding_for_testing = true;
}
IconDecodeRequest::IconDecodeRequest(SetIconCallback set_icon_callback)
: set_icon_callback_(std::move(set_icon_callback)) {}
IconDecodeRequest::~IconDecodeRequest() = default;
void IconDecodeRequest::StartWithOptions(
const std::vector<uint8_t>& image_data) {
if (disable_safe_decoding_for_testing) {
if (image_data.empty()) {
OnDecodeImageFailed();
return;
}
SkBitmap bitmap;
if (!gfx::PNGCodec::Decode(
reinterpret_cast<const unsigned char*>(image_data.data()),
image_data.size(), &bitmap)) {
OnDecodeImageFailed();
return;
}
OnImageDecoded(bitmap);
return;
}
ImageDecoder::StartWithOptions(this, image_data, ImageDecoder::DEFAULT_CODEC,
true, gfx::Size());
}
void IconDecodeRequest::OnImageDecoded(const SkBitmap& bitmap) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
......
......@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_UI_APP_LIST_SEARCH_ARC_ICON_DECODE_REQUEST_H_
#define CHROME_BROWSER_UI_APP_LIST_SEARCH_ARC_ICON_DECODE_REQUEST_H_
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/image_decoder.h"
......@@ -22,6 +24,17 @@ class IconDecodeRequest : public ImageDecoder::ImageRequest {
explicit IconDecodeRequest(SetIconCallback set_icon_callback);
~IconDecodeRequest() override;
// Disables async safe decoding requests when unit tests are executed.
// Icons are decoded at a separate process created by ImageDecoder. In unit
// tests these tasks may not finish before the test exits, which causes a
// failure in the base::MessageLoop::current()->IsIdleForTesting() check
// in test_browser_thread_bundle.cc.
static void DisableSafeDecodingForTesting();
// Starts image decoding. Safe asynchronous decoding is used unless
// DisableSafeDecodingForTesting() is called.
void StartWithOptions(const std::vector<uint8_t>& image_data);
// ImageDecoder::ImageRequest:
void OnImageDecoded(const SkBitmap& bitmap) override;
void OnDecodeImageFailed() override;
......
......@@ -4311,6 +4311,7 @@ test("unit_tests") {
"../browser/ui/app_list/profile_loader_unittest.cc",
"../browser/ui/app_list/search/answer_card/answer_card_result_unittest.cc",
"../browser/ui/app_list/search/answer_card/answer_card_search_provider_unittest.cc",
"../browser/ui/app_list/search/arc/arc_app_data_search_provider_unittest.cc",
"../browser/ui/app_list/search/arc/arc_playstore_search_provider_unittest.cc",
"../browser/ui/app_list/search/launcher_search/launcher_search_icon_image_loader_unittest.cc",
"../browser/ui/app_list/search/tests/app_search_provider_unittest.cc",
......
......@@ -345,8 +345,22 @@ void FakeAppInstance::GetIcingGlobalQueryResults(
const std::string& query,
int32_t max_results,
GetIcingGlobalQueryResultsCallback callback) {
// Fake successful app data search results.
std::vector<arc::mojom::AppDataResultPtr> fake_app_data_results;
for (int i = 0; i < max_results; ++i) {
// Fake icon data.
std::string png_data_as_string;
GetFakeIcon(mojom::ScaleFactor::SCALE_FACTOR_100P, &png_data_as_string);
std::vector<uint8_t> fake_icon_png_data(png_data_as_string.begin(),
png_data_as_string.end());
fake_app_data_results.emplace_back(mojom::AppDataResult::New(
base::StringPrintf("LaunchIntentUri %d", i),
base::StringPrintf("Label %s %d", query.c_str(), i),
fake_icon_png_data));
}
std::move(callback).Run(arc::mojom::AppDataRequestState::REQUEST_SUCCESS,
std::vector<arc::mojom::AppDataResultPtr>());
std::move(fake_app_data_results));
}
void FakeAppInstance::StartPaiFlow() {
......
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