Commit b759b203 authored by edchin's avatar edchin Committed by Commit Bot

[ios] TabGridMediator initial unittests

Initial set of unittests tests whether
the consumer protocol methods are called.

Bug: 804496
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ib252da01784d407720b7980ee4ee1378cdb13c2d
Reviewed-on: https://chromium-review.googlesource.com/942696Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Reviewed-by: default avataredchin <edchin@chromium.org>
Commit-Queue: edchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#540357}
parent 45d0a37b
......@@ -74,14 +74,22 @@ source_set("unit_tests") {
testonly = true
sources = [
"tab_grid_coordinator_unittest.mm",
"tab_grid_mediator_unittest.mm",
]
deps = [
":tab_grid",
":tab_grid_ui",
"//base",
"//base/test:test_support",
"//ios/chrome/browser/tabs",
"//ios/chrome/browser/ui/tab_switcher",
"//ios/chrome/browser/web",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/browser/web_state_list:test_support",
"//ios/chrome/test:block_cleanup_test",
"//ios/web/public/test",
"//ios/web/public/test/fakes",
"//testing/gtest",
"//third_party/ocmock",
]
}
......@@ -19,10 +19,8 @@
// The source tab model.
@property(nonatomic, weak) TabModel* tabModel;
// Initializer with |tabModel| as the source model. |consumer| is the receiver
// of model layer updates.
- (instancetype)initWithTabModel:(TabModel*)tabModel
consumer:(id<GridConsumer>)consumer
// Initializer with |consumer| as the receiver of model layer updates.
- (instancetype)initWithConsumer:(id<GridConsumer>)consumer
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
@end
......
......@@ -69,29 +69,40 @@ NSArray* CreateItems(WebStateList* webStateList) {
@synthesize webStateList = _webStateList;
@synthesize consumer = _consumer;
- (instancetype)initWithTabModel:(TabModel*)tabModel
consumer:(id<GridConsumer>)consumer {
- (instancetype)initWithConsumer:(id<GridConsumer>)consumer {
if (self = [super init]) {
_tabModel = tabModel;
_webStateList = tabModel.webStateList;
_consumer = consumer;
_webStateListObserverBridge =
std::make_unique<WebStateListObserverBridge>(self);
_scopedWebStateListObserver =
std::make_unique<ScopedObserver<WebStateList, WebStateListObserver>>(
_webStateListObserverBridge.get());
_scopedWebStateListObserver->Add(_webStateList);
_webStateObserverBridge =
std::make_unique<web::WebStateObserverBridge>(self);
_scopedWebStateObserver =
std::make_unique<ScopedObserver<web::WebState, web::WebStateObserver>>(
_webStateObserverBridge.get());
[self populateConsumerItems];
}
return self;
}
#pragma mark - Public properties
- (void)setTabModel:(TabModel*)tabModel {
_scopedWebStateListObserver->RemoveAll();
_scopedWebStateObserver->RemoveAll();
_tabModel = tabModel;
_webStateList = tabModel.webStateList;
if (_webStateList) {
_scopedWebStateListObserver->Add(_webStateList);
for (int i = 0; i < self.webStateList->count(); i++) {
web::WebState* webState = self.webStateList->GetWebStateAt(i);
_scopedWebStateObserver->Add(webState);
}
[self populateConsumerItems];
}
}
#pragma mark - WebStateListObserving
- (void)webStateList:(WebStateList*)webStateList
......@@ -134,7 +145,7 @@ NSArray* CreateItems(WebStateList* webStateList) {
didChangeActiveWebState:(web::WebState*)newWebState
oldWebState:(web::WebState*)oldWebState
atIndex:(int)atIndex
userAction:(BOOL)userAction {
reason:(int)reason {
[self.consumer selectItemAtIndex:atIndex];
}
......
// 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/tab_grid/tab_grid_mediator.h"
#import <Foundation/Foundation.h>
#include <memory>
#include "base/test/scoped_task_environment.h"
#import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/ui/tab_grid/grid_consumer.h"
#import "ios/chrome/browser/ui/tab_grid/grid_item.h"
#import "ios/chrome/browser/web/tab_id_tab_helper.h"
#include "ios/chrome/browser/web_state_list/fake_web_state_list_delegate.h"
#include "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/browser/web_state_list/web_state_opener.h"
#import "ios/web/public/test/fakes/test_web_state.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
class TabGridMediatorTest : public PlatformTest {
public:
TabGridMediatorTest() {
web_state_list_ = std::make_unique<WebStateList>(&web_state_list_delegate_);
tab_model_ = OCMClassMock([TabModel class]);
OCMStub([tab_model_ webStateList]).andReturn(web_state_list_.get());
// Insert some web states.
for (int i = 0; i < 3; i++) {
auto web_state = std::make_unique<web::TestWebState>();
TabIdTabHelper::CreateForWebState(web_state.get());
web_state_list_->InsertWebState(i, std::move(web_state),
WebStateList::INSERT_FORCE_INDEX,
WebStateOpener());
}
web_state_list_->ActivateWebStateAt(1);
consumer_ = OCMProtocolMock(@protocol(GridConsumer));
mediator_ = [[TabGridMediator alloc] initWithConsumer:consumer_];
mediator_.tabModel = tab_model_;
}
~TabGridMediatorTest() override {}
protected:
base::test::ScopedTaskEnvironment task_environment_;
std::unique_ptr<WebStateList> web_state_list_;
FakeWebStateListDelegate web_state_list_delegate_;
id tab_model_;
id consumer_;
TabGridMediator* mediator_;
};
// Tests that the consumer is populated after the tab model is set on the
// mediator.
TEST_F(TabGridMediatorTest, ConsumerPopulateItems) {
[[consumer_ verify] populateItems:[OCMArg checkWithBlock:^BOOL(id value) {
NSArray* items = static_cast<NSArray*>(value);
EXPECT_EQ(3UL, items.count);
return YES;
}]
selectedIndex:1];
}
// Tests that the consumer is notified when a web state is inserted.
TEST_F(TabGridMediatorTest, ConsumerInsertItem) {
auto web_state = std::make_unique<web::TestWebState>();
TabIdTabHelper::CreateForWebState(web_state.get());
NSString* item_identifier =
TabIdTabHelper::FromWebState(web_state.get())->tab_id();
web_state_list_->InsertWebState(1, std::move(web_state),
WebStateList::INSERT_FORCE_INDEX,
WebStateOpener());
[[consumer_ verify] insertItem:[OCMArg checkWithBlock:^BOOL(id value) {
GridItem* item = static_cast<GridItem*>(value);
EXPECT_NSEQ(item_identifier, item.identifier);
return YES;
}]
atIndex:1
selectedIndex:2];
}
// Tests that the consumer is notified when a web state is removed.
TEST_F(TabGridMediatorTest, ConsumerRemoveItem) {
web_state_list_->CloseWebStateAt(1, WebStateList::CLOSE_NO_FLAGS);
[[consumer_ verify] removeItemAtIndex:1 selectedIndex:1];
}
// Tests that the consumer is notified when the active web state is changed.
TEST_F(TabGridMediatorTest, ConsumerUpdateSelectedItem) {
// Selected index is 1 before the update.
web_state_list_->ActivateWebStateAt(2);
[[consumer_ verify] selectItemAtIndex:2];
}
// Tests that the consumer is notified when a web state is replaced.
TEST_F(TabGridMediatorTest, ConsumerReplaceItem) {
auto new_web_state = std::make_unique<web::TestWebState>();
TabIdTabHelper::CreateForWebState(new_web_state.get());
NSString* new_item_identifier =
TabIdTabHelper::FromWebState(new_web_state.get())->tab_id();
web_state_list_->ReplaceWebStateAt(1, std::move(new_web_state));
[[consumer_ verify]
replaceItemAtIndex:1
withItem:[OCMArg checkWithBlock:^BOOL(id value) {
GridItem* item = static_cast<GridItem*>(value);
EXPECT_NSEQ(new_item_identifier, item.identifier);
return YES;
}]];
}
// Tests that the consumer is notified when a web state is moved.
TEST_F(TabGridMediatorTest, ConsumerMoveItem) {
// Selected index is 1 before the move.
web_state_list_->MoveWebStateAt(1, 2);
[[consumer_ verify] moveItemFromIndex:1 toIndex:2 selectedIndex:2];
}
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