Commit ac43b757 authored by Nohemi Fernandez's avatar Nohemi Fernandez Committed by Commit Bot

[iOS] Add test coverage for Settings table view controller.

Adds a simple test to ensure that Sync settings is on when Sync is
enabled in the sign-in flow.

Bug: 1145188
Change-Id: I3f705030a5b2af9b0a31734a372bed7f2de20f58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517463
Commit-Queue: Nohemi Fernandez <fernandex@chromium.org>
Reviewed-by: default avatarJérôme Lebel <jlebel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824071}
parent d6c67df9
...@@ -210,6 +210,7 @@ source_set("constants") { ...@@ -210,6 +210,7 @@ source_set("constants") {
"settings_table_view_controller_constants.mm", "settings_table_view_controller_constants.mm",
] ]
frameworks = [ "Foundation.framework" ] frameworks = [ "Foundation.framework" ]
deps = [ "//ios/chrome/browser/ui/list_model" ]
} }
source_set("test_support") { source_set("test_support") {
...@@ -270,6 +271,7 @@ source_set("unit_tests") { ...@@ -270,6 +271,7 @@ source_set("unit_tests") {
"search_engine_table_view_controller_unittest.mm", "search_engine_table_view_controller_unittest.mm",
"settings_navigation_controller_unittest.mm", "settings_navigation_controller_unittest.mm",
"settings_root_table_view_controller_unittest.mm", "settings_root_table_view_controller_unittest.mm",
"settings_table_view_controller_unittest.mm",
"translate_table_view_controller_unittest.mm", "translate_table_view_controller_unittest.mm",
"voice_search_table_view_controller_unittest.mm", "voice_search_table_view_controller_unittest.mm",
] ]
...@@ -289,6 +291,7 @@ source_set("unit_tests") { ...@@ -289,6 +291,7 @@ source_set("unit_tests") {
"//components/prefs:test_support", "//components/prefs:test_support",
"//components/search_engines", "//components/search_engines",
"//components/strings", "//components/strings",
"//components/sync/driver:test_support",
"//components/sync_preferences", "//components/sync_preferences",
"//components/sync_preferences:test_support", "//components/sync_preferences:test_support",
"//components/translate/core/browser", "//components/translate/core/browser",
...@@ -311,9 +314,12 @@ source_set("unit_tests") { ...@@ -311,9 +314,12 @@ source_set("unit_tests") {
"//ios/chrome/browser/signin", "//ios/chrome/browser/signin",
"//ios/chrome/browser/signin:test_support", "//ios/chrome/browser/signin:test_support",
"//ios/chrome/browser/sync", "//ios/chrome/browser/sync",
"//ios/chrome/browser/sync:test_support",
"//ios/chrome/browser/translate", "//ios/chrome/browser/translate",
"//ios/chrome/browser/ui:feature_flags", "//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/commands",
"//ios/chrome/browser/ui/icons", "//ios/chrome/browser/ui/icons",
"//ios/chrome/browser/ui/settings:constants",
"//ios/chrome/browser/ui/settings/cells", "//ios/chrome/browser/ui/settings/cells",
"//ios/chrome/browser/ui/table_view", "//ios/chrome/browser/ui/table_view",
"//ios/chrome/browser/ui/table_view:test_support", "//ios/chrome/browser/ui/table_view:test_support",
...@@ -324,6 +330,7 @@ source_set("unit_tests") { ...@@ -324,6 +330,7 @@ source_set("unit_tests") {
"//ios/chrome/test:test_support", "//ios/chrome/test:test_support",
"//ios/chrome/test/app:test_support", "//ios/chrome/test/app:test_support",
"//ios/public/provider/chrome/browser", "//ios/public/provider/chrome/browser",
"//ios/public/provider/chrome/browser/signin:fake_chrome_identity",
"//ios/public/provider/chrome/browser/voice", "//ios/public/provider/chrome/browser/voice",
"//ios/web/public/test", "//ios/web/public/test",
"//net", "//net",
......
...@@ -7,6 +7,47 @@ ...@@ -7,6 +7,47 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "ios/chrome/browser/ui/list_model/list_model.h"
// Sections used in Settings page.
typedef NS_ENUM(NSInteger, SettingsSectionIdentifier) {
SettingsSectionIdentifierSignIn = kSectionIdentifierEnumZero,
SettingsSectionIdentifierAccount,
SettingsSectionIdentifierBasics,
SettingsSectionIdentifierAdvanced,
SettingsSectionIdentifierInfo,
SettingsSectionIdentifierDebug,
SettingsSectionIdentifierDefaults,
};
// Item types used per Setting section.
typedef NS_ENUM(NSInteger, SettingsItemType) {
SettingsItemTypeSignInButton = kItemTypeEnumZero,
SettingsItemTypeSigninPromo,
SettingsItemTypeAccount,
SettingsItemTypeSyncAndGoogleServices,
SettingsItemTypeGoogleSync,
SettingsItemTypeGoogleServices,
SettingsItemTypeHeader,
SettingsItemTypeSearchEngine,
SettingsItemTypeManagedDefaultSearchEngine,
SettingsItemTypePasswords,
SettingsItemTypeAutofillCreditCard,
SettingsItemTypeAutofillProfile,
SettingsItemTypeVoiceSearch,
SettingsItemTypePrivacy,
SettingsItemTypeLanguageSettings,
SettingsItemTypeContentSettings,
SettingsItemTypeBandwidth,
SettingsItemTypeAboutChrome,
SettingsItemTypeMemoryDebugging,
SettingsItemTypeViewSource,
SettingsItemTypeTableCellCatalog,
SettingsItemTypeArticlesForYou,
SettingsItemTypeSafetyCheck,
SettingsItemTypeDefaultBrowser,
};
// The accessibility identifier of the settings TableView. // The accessibility identifier of the settings TableView.
extern NSString* const kSettingsTableViewId; extern NSString* const kSettingsTableViewId;
......
// Copyright 2020 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/settings/settings_table_view_controller.h"
#import "base/test/task_environment.h"
#import "components/sync/driver/mock_sync_service.h"
#import "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "ios/chrome/browser/main/test_browser.h"
#import "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/signin/authentication_service_factory.h"
#import "ios/chrome/browser/signin/authentication_service_fake.h"
#import "ios/chrome/browser/sync/profile_sync_service_factory.h"
#import "ios/chrome/browser/sync/sync_setup_service_factory.h"
#import "ios/chrome/browser/sync/sync_setup_service_mock.h"
#import "ios/chrome/browser/ui/commands/application_commands.h"
#import "ios/chrome/browser/ui/commands/browser_commands.h"
#import "ios/chrome/browser/ui/commands/browsing_data_commands.h"
#import "ios/chrome/browser/ui/commands/command_dispatcher.h"
#import "ios/chrome/browser/ui/settings/cells/settings_image_detail_text_item.h"
#import "ios/chrome/browser/ui/settings/settings_table_view_controller_constants.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h"
#import "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/ios_chrome_scoped_testing_local_state.h"
#import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h"
#import "ios/web/public/test/web_task_environment.h"
#import "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/gtest_support.h"
#import "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using ::testing::NiceMock;
using ::testing::Return;
namespace {
std::unique_ptr<KeyedService> CreateMockSyncService(
web::BrowserState* context) {
return std::make_unique<NiceMock<syncer::MockSyncService>>();
}
std::unique_ptr<KeyedService> CreateMockSyncSetupService(
web::BrowserState* context) {
ChromeBrowserState* browser_state =
ChromeBrowserState::FromBrowserState(context);
return std::make_unique<SyncSetupServiceMock>(
ProfileSyncServiceFactory::GetForBrowserState(browser_state));
}
} // namespace
class SettingsTableViewControllerTest : public ChromeTableViewControllerTest {
public:
void SetUp() override {
ChromeTableViewControllerTest::SetUp();
TestChromeBrowserState::Builder builder;
builder.AddTestingFactory(ProfileSyncServiceFactory::GetInstance(),
base::BindRepeating(&CreateMockSyncService));
builder.AddTestingFactory(SyncSetupServiceFactory::GetInstance(),
base::BindRepeating(&CreateMockSyncSetupService));
builder.AddTestingFactory(
ios::TemplateURLServiceFactory::GetInstance(),
ios::TemplateURLServiceFactory::GetDefaultFactory());
builder.AddTestingFactory(
AuthenticationServiceFactory::GetInstance(),
base::BindRepeating(
&AuthenticationServiceFake::CreateAuthenticationService));
chrome_browser_state_ = builder.Build();
WebStateList* web_state_list = nullptr;
browser_ = std::make_unique<TestBrowser>(chrome_browser_state_.get(),
web_state_list);
sync_setup_service_mock_ = static_cast<SyncSetupServiceMock*>(
SyncSetupServiceFactory::GetForBrowserState(
chrome_browser_state_.get()));
sync_service_mock_ = static_cast<syncer::MockSyncService*>(
ProfileSyncServiceFactory::GetForBrowserState(
chrome_browser_state_.get()));
auth_service_ = static_cast<AuthenticationServiceFake*>(
AuthenticationServiceFactory::GetInstance()->GetForBrowserState(
chrome_browser_state_.get()));
fake_identity_ = [FakeChromeIdentity identityWithEmail:@"foo1@gmail.com"
gaiaID:@"foo1ID"
name:@"Fake Foo 1"];
}
void TearDown() override {
[static_cast<SettingsTableViewController*>(controller())
settingsWillBeDismissed];
ChromeTableViewControllerTest::TearDown();
}
ChromeTableViewController* InstantiateController() override {
return [[SettingsTableViewController alloc]
initWithBrowser:browser_.get()
dispatcher:static_cast<id<ApplicationCommands, BrowserCommands,
BrowsingDataCommands>>(
browser_->GetCommandDispatcher())];
}
void SetupSyncServiceEnabledExpectations() {
ON_CALL(*sync_setup_service_mock_, IsSyncEnabled())
.WillByDefault(Return(true));
ON_CALL(*sync_setup_service_mock_, IsSyncingAllDataTypes())
.WillByDefault(Return(true));
ON_CALL(*sync_setup_service_mock_, HasFinishedInitialSetup())
.WillByDefault(Return(true));
ON_CALL(*sync_service_mock_, GetTransportState())
.WillByDefault(Return(syncer::SyncService::TransportState::ACTIVE));
ON_CALL(*sync_service_mock_->GetMockUserSettings(), IsFirstSetupComplete())
.WillByDefault(Return(true));
}
protected:
// Needed for test browser state created by TestChromeBrowserState().
web::WebTaskEnvironment task_environment_;
IOSChromeScopedTestingLocalState local_state_;
FakeChromeIdentity* fake_identity_ = nullptr;
AuthenticationServiceFake* auth_service_ = nullptr;
syncer::MockSyncService* sync_service_mock_ = nullptr;
SyncSetupServiceMock* sync_setup_service_mock_ = nullptr;
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
std::unique_ptr<TestBrowser> browser_;
SettingsTableViewController* controller_ = nullptr;
};
// Verifies that the Sync & Google Services icon displays Sync on state when
// the user has turned on sync during sign-in.
TEST_F(SettingsTableViewControllerTest, SyncOn) {
SetupSyncServiceEnabledExpectations();
ON_CALL(*sync_setup_service_mock_, GetSyncServiceState())
.WillByDefault(Return(SyncSetupService::kNoSyncServiceError));
auth_service_->SignIn(fake_identity_);
CreateController();
CheckController();
NSArray* account_items = [controller().tableViewModel
itemsInSectionWithIdentifier:SettingsSectionIdentifier::
SettingsSectionIdentifierAccount];
ASSERT_EQ(2U, account_items.count);
SettingsImageDetailTextItem* sync_item =
static_cast<SettingsImageDetailTextItem*>(account_items[1]);
ASSERT_NSEQ(sync_item.text, l10n_util::GetNSString(
IDS_IOS_GOOGLE_SERVICES_SYNC_SETTINGS_TITLE));
ASSERT_NSEQ(
sync_item.detailText,
l10n_util::GetNSString(IDS_IOS_SIGN_IN_TO_CHROME_SETTING_SYNC_ON));
}
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