Commit 4da02a0f authored by stkhapugin@chromium.org's avatar stkhapugin@chromium.org Committed by Commit Bot

[iOS] Add support for shortcuts in the popup view controller.

Adds support for showing shortcuts in the popup view controller when no
suggestions are shown.

Since the actual shortcuts are not implemented yet, a placeholder is
displayed.

Bug: 886897
Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: Ie9afb4933b42b846d5177c1024feebf9241afdee
Reviewed-on: https://chromium-review.googlesource.com/1233706
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarEric Noyau <noyau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595814}
parent 8488f525
......@@ -64,3 +64,19 @@ source_set("popup_internal") {
]
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"omnibox_popup_view_controller_unittest.mm",
]
deps = [
":popup",
"//base",
"//ios/chrome/app/strings",
"//ios/chrome/browser",
"//testing/gtest",
"//ui/base",
]
}
......@@ -22,6 +22,12 @@
@interface OmniboxPopupViewController
: UIViewController<AutocompleteResultConsumer, OmniboxSuggestionCommands>
// When enabled, this view controller will display shortcuts when no suggestions
// are available.
// This can be toggled at runtime, for example to only show shortcuts on regular
// pages and not show them on NTP.
@property(nonatomic, assign) BOOL shortcutsEnabled;
@property(nonatomic, assign) BOOL incognito;
@property(nonatomic, weak) id<AutocompleteResultConsumerDelegate> delegate;
@property(nonatomic, weak) id<ImageRetriever> imageRetriever;
......
......@@ -26,6 +26,7 @@
namespace {
const int kRowCount = 6;
const CGFloat kRowHeight = 48.0;
const CGFloat kShortcutsRowHeight = 100;
const CGFloat kAnswerRowHeight = 64.0;
const CGFloat kTopAndBottomPadding = 8.0;
UIColor* BackgroundColorTablet() {
......@@ -211,6 +212,15 @@ UIColor* BackgroundColorIncognito() {
_incognito = incognito;
}
- (void)setShortcutsEnabled:(BOOL)shortcutsEnabled {
if (shortcutsEnabled == _shortcutsEnabled) {
return;
}
_shortcutsEnabled = shortcutsEnabled;
[self.tableView reloadData];
}
#pragma mark - AutocompleteResultConsumer
- (void)updateMatches:(NSArray<id<AutocompleteSuggestion>>*)result
......@@ -608,6 +618,11 @@ UIColor* BackgroundColorIncognito() {
- (CGFloat)tableView:(UITableView*)tableView
heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if (self.shortcutsEnabled && indexPath.row == 0 &&
_currentResult.count == 0) {
return kShortcutsRowHeight;
}
DCHECK_EQ(0U, (NSUInteger)indexPath.section);
DCHECK_LT((NSUInteger)indexPath.row, _currentResult.count);
return ((OmniboxPopupRow*)(_rows[indexPath.row])).rowHeight;
......@@ -620,6 +635,9 @@ UIColor* BackgroundColorIncognito() {
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
DCHECK_EQ(0, section);
if (self.shortcutsEnabled && _currentResult.count == 0) {
return 1;
}
return _currentResult.count;
}
......@@ -627,6 +645,17 @@ UIColor* BackgroundColorIncognito() {
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
DCHECK_EQ(0U, (NSUInteger)indexPath.section);
if (self.shortcutsEnabled && indexPath.row == 0 &&
_currentResult.count == 0) {
// This is a placeholder cell.
UITableViewCell* cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.textLabel.text = @"Shortcuts placeholder";
return cell;
}
DCHECK_LT((NSUInteger)indexPath.row, _currentResult.count);
return _rows[indexPath.row];
}
......@@ -634,6 +663,12 @@ UIColor* BackgroundColorIncognito() {
- (BOOL)tableView:(UITableView*)tableView
canEditRowAtIndexPath:(NSIndexPath*)indexPath {
DCHECK_EQ(0U, (NSUInteger)indexPath.section);
if (self.shortcutsEnabled && indexPath.row == 0 &&
_currentResult.count == 0) {
return NO;
}
// iOS doesn't check -numberOfRowsInSection before checking
// -canEditRowAtIndexPath in a reload call. If |indexPath.row| is too large,
// simple return |NO|.
......
// 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.
#import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_view_controller.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
class OmniboxPopupViewControllerTest : public PlatformTest {
protected:
void SetUp() override {
PlatformTest::SetUp();
popup_view_controller_ = [[OmniboxPopupViewController alloc] init];
};
OmniboxPopupViewController* popup_view_controller_;
};
TEST_F(OmniboxPopupViewControllerTest, hasCellsWhenShortcutsEnabled) {
// This test makes an assumption that this view controller is a datasource for
// a table view. Rewrite this test if this is not the case anymore.
EXPECT_TRUE([popup_view_controller_
conformsToProtocol:@protocol(UITableViewDataSource)]);
id<UITableViewDataSource> datasource =
(id<UITableViewDataSource>)popup_view_controller_;
UITableView* tableView = [[UITableView alloc] init];
// Shortcuts are not enabled by default.
EXPECT_FALSE(popup_view_controller_.shortcutsEnabled);
// Check that the shorcuts row doesn't appear.
[popup_view_controller_ updateMatches:@[] withAnimation:NO];
EXPECT_EQ([datasource tableView:tableView numberOfRowsInSection:0], 0);
// Enable shortcuts and verify they appear.
popup_view_controller_.shortcutsEnabled = YES;
EXPECT_EQ([datasource tableView:tableView numberOfRowsInSection:0], 1);
// Disable and verify it disappears again.
popup_view_controller_.shortcutsEnabled = NO;
EXPECT_EQ([datasource tableView:tableView numberOfRowsInSection:0], 0);
}
} // namespace
......@@ -214,6 +214,7 @@ test("ios_chrome_unittests") {
"//ios/chrome/browser/ui/ntp:unit_tests",
"//ios/chrome/browser/ui/ntp/recent_tabs:unit_tests",
"//ios/chrome/browser/ui/omnibox:unit_tests",
"//ios/chrome/browser/ui/omnibox/popup:unit_tests",
"//ios/chrome/browser/ui/overlays:unit_tests",
"//ios/chrome/browser/ui/payments:unit_tests",
"//ios/chrome/browser/ui/payments/cells:unit_tests",
......
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