Commit d7b3298d authored by harrisonsean's avatar harrisonsean Committed by Commit Bot

[iOS][Safety Check] Add mediator tests

Added Tests:
StartingCheckPutsChecksInRunningState
CheckStartButtonDefaultUI
ClickingCancelTakesChecksToPrevious
CheckStartButtonCancelUI
TimestampSetIfIssueFound
SafeBrowsingEnabledReturnsSafeState
SafeBrowsingSafeUI
SafeBrowsingDisabledReturnsInfoState
SafeBrowsingUnSafeUI
SafeBrowsingManagedUI
PasswordCheckSafeCheck
PasswordCheckSafeUI
PasswordCheckUnSafeCheck
PasswordCheckUnSafeUI
PasswordCheckErrorCheck
PasswordCheckErrorUI
OmahaRespondsUpToDate
UpdateCheckUpToDateUI
OmahaRespondsOutOfDateAndUpdatesInfobarTime
UpdateCheckOutOfDateUI
OmahaRespondsError
UpdateCheckOmahaErrorUI
UpdateCheckNetErrorUI
UpdateCheckManagedUI
UpdateCheckChannelUI

Bug: 1078782
Change-Id: I61d155817bc13b96fb581fa4a77426ac56deae6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2436530
Commit-Queue: Sean Harrison <harrisonsean@chromium.org>
Reviewed-by: default avatarViktor Semeniuk <vsemeniuk@google.com>
Cr-Commit-Position: refs/heads/master@{#813001}
parent 7843b3b3
...@@ -83,13 +83,32 @@ source_set("unit_tests") { ...@@ -83,13 +83,32 @@ source_set("unit_tests") {
sources = [ "safety_check_mediator_unittest.mm" ] sources = [ "safety_check_mediator_unittest.mm" ]
deps = [ deps = [
":safety_check", ":safety_check",
":safety_check_ui",
"//base/test:test_support", "//base/test:test_support",
"//components/password_manager/core/browser:test_support",
"//components/password_manager/core/common",
"//components/prefs:test_support",
"//components/safe_browsing/core:features",
"//components/safe_browsing/core/common:safe_browsing_prefs",
"//components/strings", "//components/strings",
"//components/sync_preferences:test_support",
"//ios/chrome/app/strings", "//ios/chrome/app/strings",
"//ios/chrome/browser", "//ios/chrome/browser",
"//ios/chrome/browser/browser_state:test_support", "//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/main:test_support", "//ios/chrome/browser/main:test_support",
"//ios/chrome/browser/passwords",
"//ios/chrome/browser/signin",
"//ios/chrome/browser/signin:test_support",
"//ios/chrome/browser/sync",
"//ios/chrome/browser/sync:test_support",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/settings/cells",
"//ios/chrome/browser/ui/settings/utils",
"//ios/chrome/browser/ui/table_view:test_support", "//ios/chrome/browser/ui/table_view:test_support",
"//ios/chrome/browser/ui/table_view/cells",
"//ios/chrome/browser/upgrade",
"//ios/chrome/browser/upgrade:public",
"//ios/chrome/common",
"//ios/chrome/test:test_support", "//ios/chrome/test:test_support",
"//ios/web/public/test", "//ios/web/public/test",
"//testing/gtest", "//testing/gtest",
......
...@@ -29,4 +29,62 @@ extern NSString* const kSafeBrowsingSafetyCheckStringURL; ...@@ -29,4 +29,62 @@ extern NSString* const kSafeBrowsingSafetyCheckStringURL;
// issue. // issue.
extern NSString* const kTimestampOfLastIssueFoundKey; extern NSString* const kTimestampOfLastIssueFoundKey;
// Enum with all possible states of the update check.
typedef NS_ENUM(NSInteger, UpdateCheckRowStates) {
// When the user is up to date.
UpdateCheckRowStateUpToDate,
// When the check has not been run yet.
UpdateCheckRowStateDefault,
// When the user is out of date.
UpdateCheckRowStateOutOfDate,
// When the user is managed.
UpdateCheckRowStateManaged,
// When the check is running.
UpdateCheckRowStateRunning,
// When Omaha encountered an error.
UpdateCheckRowStateOmahaError,
// When there is a connectivity issue.
UpdateCheckRowStateNetError,
// When the device is on a non-supported channel.
UpdateCheckRowStateChannel,
};
// Enum with all possible states of the password check.
typedef NS_ENUM(NSInteger, PasswordCheckRowStates) {
// When no compromised passwords were detected.
PasswordCheckRowStateSafe,
// When user has compromised passwords.
PasswordCheckRowStateUnSafe,
// When check has not been run yet.
PasswordCheckRowStateDefault,
// When password check is running.
PasswordCheckRowStateRunning,
// When user has no passwords and check can't be performed.
PasswordCheckRowStateDisabled,
// When password check failed due to network issues, quota limit or others.
PasswordCheckRowStateError,
};
// Enum with all possible states of the Safe Browsing check.
typedef NS_ENUM(NSInteger, SafeBrowsingCheckRowStates) {
// When check was not run yet.
SafeBrowsingCheckRowStateDefault,
// When Safe Browsing is managed by admin.
SafeBrowsingCheckRowStateManaged,
// When the Safe Browsing check is running.
SafeBrowsingCheckRowStateRunning,
// When Safe Browsing is enabled.
SafeBrowsingCheckRowStateSafe,
// When Safe Browsing is disabled.
SafeBrowsingCheckRowStateUnsafe,
};
// Enum with all possible states of the button to start the check.
typedef NS_ENUM(NSInteger, CheckStartStates) {
// When the check is not running.
CheckStartStateDefault,
// When the check is running.
CheckStartStateCancel,
};
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SAFETY_CHECK_SAFETY_CHECK_CONSTANTS_H_ #endif // IOS_CHROME_BROWSER_UI_SETTINGS_SAFETY_CHECK_SAFETY_CHECK_CONSTANTS_H_
...@@ -78,7 +78,7 @@ constexpr char kSafetyCheckInteractions[] = "Settings.SafetyCheck.Interactions"; ...@@ -78,7 +78,7 @@ constexpr char kSafetyCheckInteractions[] = "Settings.SafetyCheck.Interactions";
typedef NSArray<TableViewItem*>* ItemArray; typedef NSArray<TableViewItem*>* ItemArray;
typedef NS_ENUM(NSInteger, ItemType) { typedef NS_ENUM(NSInteger, SafteyCheckItemType) {
// CheckTypes section. // CheckTypes section.
UpdateItemType = kItemTypeEnumZero, UpdateItemType = kItemTypeEnumZero,
PasswordItemType, PasswordItemType,
...@@ -89,64 +89,6 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -89,64 +89,6 @@ typedef NS_ENUM(NSInteger, ItemType) {
TimestampFooterItem, TimestampFooterItem,
}; };
// Enum with all possible states of the update check.
typedef NS_ENUM(NSInteger, UpdateCheckRowStates) {
// When the user is up to date.
UpdateCheckRowStateUpToDate,
// When the check has not been run yet.
UpdateCheckRowStateDefault,
// When the user is out of date.
UpdateCheckRowStateOutOfDate,
// When the user is managed.
UpdateCheckRowStateManaged,
// When the check is running.
UpdateCheckRowStateRunning,
// When Omaha encountered an error.
UpdateCheckRowStateOmahaError,
// When there is a connectivity issue.
UpdateCheckRowStateNetError,
// When the device is on a non-supported channel.
UpdateCheckRowStateChannel,
};
// Enum with all possible states of the password check.
typedef NS_ENUM(NSInteger, PasswordCheckRowStates) {
// When no compromised passwords were detected.
PasswordCheckRowStateSafe,
// When user has compromised passwords.
PasswordCheckRowStateUnSafe,
// When check has not been run yet.
PasswordCheckRowStateDefault,
// When password check is running.
PasswordCheckRowStateRunning,
// When user has no passwords and check can't be performed.
PasswordCheckRowStateDisabled,
// When password check failed due to network issues, quota limit or others.
PasswordCheckRowStateError,
};
// Enum with all possible states of the Safe Browsing check.
typedef NS_ENUM(NSInteger, SafeBrowsingCheckRowStates) {
// When check was not run yet.
SafeBrowsingCheckRowStateDefault,
// When Safe Browsing is managed by admin.
SafeBrowsingCheckRowStateManaged,
// When the Safe Browsing check is running.
SafeBrowsingCheckRowStateRunning,
// When Safe Browsing is enabled.
SafeBrowsingCheckRowStateSafe,
// When Safe Browsing is disabled.
SafeBrowsingCheckRowStateUnsafe,
};
// Enum with all possible states of the button to start the check.
typedef NS_ENUM(NSInteger, CheckStartStates) {
// When the check is not running.
CheckStartStateDefault,
// When the check is running.
CheckStartStateCancel,
};
} // namespace } // namespace
@interface SafetyCheckMediator () <BooleanObserver, PasswordCheckObserver> { @interface SafetyCheckMediator () <BooleanObserver, PasswordCheckObserver> {
...@@ -376,7 +318,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) { ...@@ -376,7 +318,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) {
#pragma mark - SafetyCheckServiceDelegate #pragma mark - SafetyCheckServiceDelegate
- (void)didSelectItem:(TableViewItem*)item { - (void)didSelectItem:(TableViewItem*)item {
ItemType type = static_cast<ItemType>(item.type); SafteyCheckItemType type = static_cast<SafteyCheckItemType>(item.type);
switch (type) { switch (type) {
// Few selections are handled here explicitly, but all states are laid out // Few selections are handled here explicitly, but all states are laid out
// to have one location that shows all actions that are taken from the // to have one location that shows all actions that are taken from the
...@@ -449,7 +391,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) { ...@@ -449,7 +391,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) {
} }
- (BOOL)isItemWithErrorInfo:(TableViewItem*)item { - (BOOL)isItemWithErrorInfo:(TableViewItem*)item {
ItemType type = static_cast<ItemType>(item.type); SafteyCheckItemType type = static_cast<SafteyCheckItemType>(item.type);
return (type != CheckStartItemType); return (type != CheckStartItemType);
} }
...@@ -490,7 +432,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) { ...@@ -490,7 +432,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) {
// Computes the text needed for a popover on |itemType| if available. // Computes the text needed for a popover on |itemType| if available.
- (NSAttributedString*)getPopoverInfoForType:(NSInteger)itemType { - (NSAttributedString*)getPopoverInfoForType:(NSInteger)itemType {
ItemType type = static_cast<ItemType>(itemType); SafteyCheckItemType type = static_cast<SafteyCheckItemType>(itemType);
switch (type) { switch (type) {
case PasswordItemType: case PasswordItemType:
return [self passwordCheckErrorInfo]; return [self passwordCheckErrorInfo];
...@@ -552,7 +494,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) { ...@@ -552,7 +494,7 @@ typedef NS_ENUM(NSInteger, CheckStartStates) {
: PasswordCheckRowStateUnSafe; : PasswordCheckRowStateUnSafe;
case PasswordCheckState::kCanceled: case PasswordCheckState::kCanceled:
case PasswordCheckState::kIdle: { case PasswordCheckState::kIdle: {
if (noCompromisedPasswords) { if (!noCompromisedPasswords) {
base::UmaHistogramEnumeration( base::UmaHistogramEnumeration(
kSafetyCheckMetricsPasswords, kSafetyCheckMetricsPasswords,
safety_check::SafetyCheck::PasswordsStatus::kCompromisedExist); safety_check::SafetyCheck::PasswordsStatus::kCompromisedExist);
......
...@@ -7,24 +7,495 @@ ...@@ -7,24 +7,495 @@
#include <memory> #include <memory>
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "base/test/scoped_feature_list.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/password_manager/core/browser/password_manager_test_utils.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/safe_browsing/core/features.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "ios/chrome/browser/application_context.h" #include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "ios/chrome/browser/main/test_browser.h" #import "ios/chrome/browser/main/test_browser.h"
#include "ios/chrome/browser/passwords/ios_chrome_password_check_manager.h"
#include "ios/chrome/browser/passwords/ios_chrome_password_check_manager_factory.h"
#include "ios/chrome/browser/passwords/ios_chrome_password_store_factory.h"
#include "ios/chrome/browser/passwords/password_check_observer_bridge.h"
#include "ios/chrome/browser/signin/authentication_service_factory.h"
#import "ios/chrome/browser/signin/authentication_service_fake.h"
#include "ios/chrome/browser/sync/profile_sync_service_factory.h"
#include "ios/chrome/browser/sync/sync_setup_service_factory.h"
#include "ios/chrome/browser/sync/sync_setup_service_mock.h"
#import "ios/chrome/browser/ui/settings/cells/settings_check_item.h"
#import "ios/chrome/browser/ui/settings/safety_check/safety_check_constants.h"
#import "ios/chrome/browser/ui/settings/safety_check/safety_check_consumer.h"
#import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_item.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h" #import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h"
#include "ios/chrome/browser/ui/ui_feature_flags.h"
#include "ios/chrome/browser/upgrade/upgrade_constants.h"
#include "ios/chrome/browser/upgrade/upgrade_recommended_details.h"
#import "ios/chrome/common/string_util.h"
#include "ios/chrome/grit/ios_chromium_strings.h" #include "ios/chrome/grit/ios_chromium_strings.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h"
#include "ios/web/public/test/web_task_environment.h" #include "ios/web/public/test/web_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/time_format.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
@interface SafetyCheckMediator (Test)
- (void)checkAndReconfigureSafeBrowsingState;
- (void)resetsCheckStartItemIfNeeded;
- (void)passwordCheckStateDidChange:(PasswordCheckState)state;
- (void)reconfigurePasswordCheckItem;
- (void)reconfigureUpdateCheckItem;
- (void)reconfigureSafeBrowsingCheckItem;
- (void)reconfigureCheckStartSection;
- (void)handleOmahaResponse:(const UpgradeRecommendedDetails&)details;
@property(nonatomic, strong) SettingsCheckItem* updateCheckItem;
@property(nonatomic, assign) UpdateCheckRowStates updateCheckRowState;
@property(nonatomic, assign) UpdateCheckRowStates previousUpdateCheckRowState;
@property(nonatomic, strong) SettingsCheckItem* passwordCheckItem;
@property(nonatomic, assign) PasswordCheckRowStates passwordCheckRowState;
@property(nonatomic, assign)
PasswordCheckRowStates previousPasswordCheckRowState;
@property(nonatomic, strong) SettingsCheckItem* safeBrowsingCheckItem;
@property(nonatomic, assign)
SafeBrowsingCheckRowStates safeBrowsingCheckRowState;
@property(nonatomic, assign)
SafeBrowsingCheckRowStates previousSafeBrowsingCheckRowState;
@property(nonatomic, strong) TableViewTextItem* checkStartItem;
@property(nonatomic, assign) CheckStartStates checkStartState;
@property(nonatomic, assign) BOOL checkDidRun;
@property(nonatomic, assign) PasswordCheckState currentPasswordCheckState;
@property(nonatomic, strong, readonly)
PrefBackedBoolean* safeBrowsingPreference;
@end
namespace { namespace {
// TODO(crbug.com/1078782): Add tests. typedef NS_ENUM(NSInteger, SafteyCheckItemType) {
// CheckTypes section.
UpdateItemType = kItemTypeEnumZero,
PasswordItemType,
SafeBrowsingItemType,
HeaderItem,
// CheckStart section.
CheckStartItemType,
TimestampFooterItem,
};
using password_manager::CompromisedCredentials;
using password_manager::CompromiseType;
using password_manager::TestPasswordStore;
using l10n_util::GetNSString;
// Sets test sync setup service and returns pointer to it.
std::unique_ptr<KeyedService> BuildMockSyncSetupService(
web::BrowserState* context) {
ChromeBrowserState* browser_state =
ChromeBrowserState::FromBrowserState(context);
return std::make_unique<SyncSetupServiceMock>(
ProfileSyncServiceFactory::GetForBrowserState(browser_state));
}
// Sets test password store and returns pointer to it.
scoped_refptr<TestPasswordStore> BuildTestPasswordStore(
ChromeBrowserState* _browserState) {
return base::WrapRefCounted(static_cast<password_manager::TestPasswordStore*>(
IOSChromePasswordStoreFactory::GetInstance()
->SetTestingFactoryAndUse(
_browserState,
base::BindRepeating(&password_manager::BuildPasswordStore<
web::BrowserState, TestPasswordStore>))
.get()));
}
// Registers account preference that will be used for Safe Browsing.
PrefService* SetPrefService() {
TestingPrefServiceSimple* prefs = new TestingPrefServiceSimple();
PrefRegistrySimple* registry = prefs->registry();
registry->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
return prefs;
}
} // namespace } // namespace
class SafetyCheckMediatorTest : public PlatformTest {
public:
SafetyCheckMediatorTest() {
scoped_feature_list_.InitAndEnableFeature(
password_manager::features::kPasswordCheck);
TestChromeBrowserState::Builder test_cbs_builder;
test_cbs_builder.AddTestingFactory(
AuthenticationServiceFactory::GetInstance(),
base::BindRepeating(
&AuthenticationServiceFake::CreateAuthenticationService));
test_cbs_builder.AddTestingFactory(
SyncSetupServiceFactory::GetInstance(),
base::BindRepeating(&BuildMockSyncSetupService));
browser_state_ = test_cbs_builder.Build();
auth_service_ = static_cast<AuthenticationServiceFake*>(
AuthenticationServiceFactory::GetInstance()->GetForBrowserState(
browser_state_.get()));
store_ = BuildTestPasswordStore(browser_state_.get());
password_check_ = IOSChromePasswordCheckManagerFactory::GetForBrowserState(
browser_state_.get());
pref_service_ = SetPrefService();
mediator_ =
[[SafetyCheckMediator alloc] initWithUserPrefService:pref_service_
passwordCheckManager:password_check_
authService:auth_service_
syncService:syncService()];
}
SyncSetupService* syncService() {
return SyncSetupServiceFactory::GetForBrowserState(browser_state_.get());
}
void RunUntilIdle() { environment_.RunUntilIdle(); }
void AddPasswordForm(std::unique_ptr<autofill::PasswordForm> form) {
GetTestStore().AddLogin(*form);
RunUntilIdle();
}
void resetNSUserDefaultsForTesting() {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kTimestampOfLastIssueFoundKey];
[defaults removeObjectForKey:kIOSChromeUpToDateKey];
[defaults removeObjectForKey:kIOSChromeNextVersionKey];
[defaults removeObjectForKey:kIOSChromeUpgradeURLKey];
}
// Creates and adds a saved password form.
void AddSavedForm() {
auto form = std::make_unique<autofill::PasswordForm>();
form->url = GURL("http://www.example.com/accounts/LoginAuth");
form->action = GURL("http://www.example.com/accounts/Login");
form->username_element = base::ASCIIToUTF16("Email");
form->username_value = base::ASCIIToUTF16("test@egmail.com");
form->password_element = base::ASCIIToUTF16("Passwd");
form->password_value = base::ASCIIToUTF16("test");
form->submit_element = base::ASCIIToUTF16("signIn");
form->signon_realm = "http://www.example.com/";
form->scheme = autofill::PasswordForm::Scheme::kHtml;
form->blocked_by_user = false;
AddPasswordForm(std::move(form));
}
password_manager::CompromisedCredentials MakeCompromised(
base::StringPiece signon_realm,
base::StringPiece username) {
return {
std::string(signon_realm),
base::ASCIIToUTF16(username),
base::Time::Now(),
CompromiseType::kLeaked,
};
}
TestPasswordStore& GetTestStore() {
return *static_cast<TestPasswordStore*>(
IOSChromePasswordStoreFactory::GetForBrowserState(
browser_state_.get(), ServiceAccessType::EXPLICIT_ACCESS)
.get());
}
void AddCompromisedCredential() {
GetTestStore().AddCompromisedCredentials(
MakeCompromised("http://www.example.com/", "test@egmail.com"));
RunUntilIdle();
}
protected:
web::WebTaskEnvironment environment_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<TestChromeBrowserState> browser_state_;
scoped_refptr<TestPasswordStore> store_;
AuthenticationServiceFake* auth_service_;
scoped_refptr<IOSChromePasswordCheckManager> password_check_;
SafetyCheckMediator* mediator_;
PrefService* pref_service_;
PrefBackedBoolean* safe_browsing_preference_;
};
// Check start button tests.
TEST_F(SafetyCheckMediatorTest, StartingCheckPutsChecksInRunningState) {
TableViewItem* start =
[[TableViewItem alloc] initWithType:CheckStartItemType];
[mediator_ didSelectItem:start];
EXPECT_EQ(mediator_.updateCheckRowState, UpdateCheckRowStateRunning);
EXPECT_EQ(mediator_.passwordCheckRowState, PasswordCheckRowStateRunning);
EXPECT_EQ(mediator_.safeBrowsingCheckRowState,
SafeBrowsingCheckRowStateRunning);
EXPECT_EQ(mediator_.checkStartState, CheckStartStateCancel);
}
TEST_F(SafetyCheckMediatorTest, CheckStartButtonDefaultUI) {
mediator_.checkStartState = CheckStartStateDefault;
[mediator_ reconfigureCheckStartSection];
EXPECT_NSEQ(mediator_.checkStartItem.text,
GetNSString(IDS_IOS_CHECK_PASSWORDS_NOW_BUTTON));
}
TEST_F(SafetyCheckMediatorTest, ClickingCancelTakesChecksToPrevious) {
TableViewItem* start =
[[TableViewItem alloc] initWithType:CheckStartItemType];
[mediator_ didSelectItem:start];
EXPECT_EQ(mediator_.updateCheckRowState, UpdateCheckRowStateRunning);
EXPECT_EQ(mediator_.passwordCheckRowState, PasswordCheckRowStateRunning);
EXPECT_EQ(mediator_.safeBrowsingCheckRowState,
SafeBrowsingCheckRowStateRunning);
EXPECT_EQ(mediator_.checkStartState, CheckStartStateCancel);
[mediator_ didSelectItem:start];
EXPECT_EQ(mediator_.updateCheckRowState,
mediator_.previousUpdateCheckRowState);
EXPECT_EQ(mediator_.passwordCheckRowState,
mediator_.previousPasswordCheckRowState);
EXPECT_EQ(mediator_.safeBrowsingCheckRowState,
mediator_.previousSafeBrowsingCheckRowState);
EXPECT_EQ(mediator_.checkStartState, CheckStartStateDefault);
}
TEST_F(SafetyCheckMediatorTest, CheckStartButtonCancelUI) {
mediator_.checkStartState = CheckStartStateCancel;
[mediator_ reconfigureCheckStartSection];
EXPECT_NSEQ(mediator_.checkStartItem.text,
GetNSString(IDS_IOS_CANCEL_PASSWORD_CHECK_BUTTON));
}
// Timestamp test.
TEST_F(SafetyCheckMediatorTest, TimestampSetIfIssueFound) {
mediator_.checkDidRun = true;
mediator_.passwordCheckRowState = PasswordCheckRowStateUnSafe;
[mediator_ resetsCheckStartItemIfNeeded];
base::Time lastCompletedCheck =
base::Time::FromDoubleT([[NSUserDefaults standardUserDefaults]
doubleForKey:kTimestampOfLastIssueFoundKey]);
EXPECT_GE(lastCompletedCheck,
base::Time::Now() - base::TimeDelta::FromSeconds(1));
EXPECT_LE(lastCompletedCheck,
base::Time::Now() + base::TimeDelta::FromSeconds(1));
resetNSUserDefaultsForTesting();
}
// Safe Browsing check tests.
TEST_F(SafetyCheckMediatorTest, SafeBrowsingEnabledReturnsSafeState) {
mediator_.safeBrowsingPreference.value = true;
RunUntilIdle();
[mediator_ checkAndReconfigureSafeBrowsingState];
RunUntilIdle();
EXPECT_EQ(mediator_.safeBrowsingCheckRowState, SafeBrowsingCheckRowStateSafe);
}
TEST_F(SafetyCheckMediatorTest, SafeBrowsingSafeUI) {
mediator_.safeBrowsingCheckRowState = SafeBrowsingCheckRowStateSafe;
[mediator_ reconfigureSafeBrowsingCheckItem];
EXPECT_NSEQ(
mediator_.safeBrowsingCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_ENABLED_DESC));
EXPECT_EQ(mediator_.safeBrowsingCheckItem.trailingImage,
[[UIImage imageNamed:@"settings_safe_state"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
}
TEST_F(SafetyCheckMediatorTest, SafeBrowsingDisabledReturnsInfoState) {
mediator_.safeBrowsingPreference.value = false;
RunUntilIdle();
[mediator_ checkAndReconfigureSafeBrowsingState];
EXPECT_EQ(mediator_.safeBrowsingCheckRowState,
SafeBrowsingCheckRowStateUnsafe);
}
TEST_F(SafetyCheckMediatorTest, SafeBrowsingUnSafeUI) {
mediator_.safeBrowsingCheckRowState = SafeBrowsingCheckRowStateUnsafe;
[mediator_ reconfigureSafeBrowsingCheckItem];
EXPECT_NSEQ(
mediator_.safeBrowsingCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_DISABLED_DESC));
EXPECT_FALSE(mediator_.safeBrowsingCheckItem.infoButtonHidden);
}
TEST_F(SafetyCheckMediatorTest, SafeBrowsingManagedUI) {
mediator_.safeBrowsingCheckRowState = SafeBrowsingCheckRowStateManaged;
[mediator_ reconfigureSafeBrowsingCheckItem];
EXPECT_NSEQ(
mediator_.safeBrowsingCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_MANAGED_DESC));
EXPECT_FALSE(mediator_.safeBrowsingCheckItem.infoButtonHidden);
}
// Password check tests.
TEST_F(SafetyCheckMediatorTest, PasswordCheckSafeCheck) {
AddSavedForm();
mediator_.currentPasswordCheckState = PasswordCheckState::kRunning;
[mediator_ passwordCheckStateDidChange:PasswordCheckState::kIdle];
EXPECT_EQ(mediator_.passwordCheckRowState, PasswordCheckRowStateSafe);
}
TEST_F(SafetyCheckMediatorTest, PasswordCheckSafeUI) {
mediator_.passwordCheckRowState = PasswordCheckRowStateSafe;
[mediator_ reconfigurePasswordCheckItem];
EXPECT_NSEQ(mediator_.passwordCheckItem.detailText,
base::SysUTF16ToNSString(l10n_util::GetPluralStringFUTF16(
IDS_IOS_CHECK_PASSWORDS_COMPROMISED_COUNT, 0)));
EXPECT_EQ(mediator_.passwordCheckItem.trailingImage,
[[UIImage imageNamed:@"settings_safe_state"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
}
TEST_F(SafetyCheckMediatorTest, PasswordCheckUnSafeCheck) {
AddSavedForm();
AddCompromisedCredential();
mediator_.currentPasswordCheckState = PasswordCheckState::kRunning;
[mediator_ passwordCheckStateDidChange:PasswordCheckState::kIdle];
EXPECT_EQ(mediator_.passwordCheckRowState, PasswordCheckRowStateUnSafe);
}
TEST_F(SafetyCheckMediatorTest, PasswordCheckUnSafeUI) {
AddSavedForm();
AddCompromisedCredential();
mediator_.passwordCheckRowState = PasswordCheckRowStateUnSafe;
[mediator_ reconfigurePasswordCheckItem];
EXPECT_NSEQ(mediator_.passwordCheckItem.detailText,
base::SysUTF16ToNSString(l10n_util::GetPluralStringFUTF16(
IDS_IOS_CHECK_PASSWORDS_COMPROMISED_COUNT, 1)));
EXPECT_EQ(mediator_.passwordCheckItem.trailingImage,
[[UIImage imageNamed:@"settings_unsafe_state"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
}
TEST_F(SafetyCheckMediatorTest, PasswordCheckErrorCheck) {
AddSavedForm();
mediator_.currentPasswordCheckState = PasswordCheckState::kRunning;
[mediator_ passwordCheckStateDidChange:PasswordCheckState::kQuotaLimit];
EXPECT_EQ(mediator_.passwordCheckRowState, PasswordCheckRowStateError);
}
TEST_F(SafetyCheckMediatorTest, PasswordCheckErrorUI) {
mediator_.passwordCheckRowState = PasswordCheckRowStateError;
[mediator_ reconfigurePasswordCheckItem];
EXPECT_NSEQ(mediator_.passwordCheckItem.detailText,
GetNSString(IDS_IOS_PASSWORD_CHECK_ERROR));
EXPECT_FALSE(mediator_.passwordCheckItem.infoButtonHidden);
}
// Update check tests.
TEST_F(SafetyCheckMediatorTest, OmahaRespondsUpToDate) {
mediator_.updateCheckRowState = UpdateCheckRowStateRunning;
UpgradeRecommendedDetails details;
details.is_up_to_date = true;
details.next_version = "9999.9999.9999.9999";
details.upgrade_url = GURL("http://foobar.org");
[mediator_ handleOmahaResponse:details];
EXPECT_EQ(mediator_.updateCheckRowState, UpdateCheckRowStateUpToDate);
resetNSUserDefaultsForTesting();
}
TEST_F(SafetyCheckMediatorTest, UpdateCheckUpToDateUI) {
mediator_.updateCheckRowState = UpdateCheckRowStateUpToDate;
[mediator_ reconfigureUpdateCheckItem];
EXPECT_NSEQ(
mediator_.updateCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_UPDATES_UP_TO_DATE_DESC));
EXPECT_EQ(mediator_.updateCheckItem.trailingImage,
[[UIImage imageNamed:@"settings_safe_state"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
}
TEST_F(SafetyCheckMediatorTest, OmahaRespondsOutOfDateAndUpdatesInfobarTime) {
mediator_.updateCheckRowState = UpdateCheckRowStateRunning;
UpgradeRecommendedDetails details;
details.is_up_to_date = false;
details.next_version = "9999.9999.9999.9999";
details.upgrade_url = GURL("http://foobar.org");
[mediator_ handleOmahaResponse:details];
EXPECT_EQ(mediator_.updateCheckRowState, UpdateCheckRowStateOutOfDate);
NSDate* lastDisplay = [[NSUserDefaults standardUserDefaults]
objectForKey:kLastInfobarDisplayTimeKey];
EXPECT_GE([lastDisplay timeIntervalSinceNow], -1);
EXPECT_LE([lastDisplay timeIntervalSinceNow], 1);
resetNSUserDefaultsForTesting();
}
TEST_F(SafetyCheckMediatorTest, UpdateCheckOutOfDateUI) {
mediator_.updateCheckRowState = UpdateCheckRowStateOutOfDate;
[mediator_ reconfigureUpdateCheckItem];
EXPECT_NSEQ(
mediator_.updateCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_UPDATES_OUT_OF_DATE_DESC));
EXPECT_EQ(mediator_.updateCheckItem.trailingImage,
[[UIImage imageNamed:@"settings_unsafe_state"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]);
}
TEST_F(SafetyCheckMediatorTest, OmahaRespondsError) {
mediator_.updateCheckRowState = UpdateCheckRowStateRunning;
UpgradeRecommendedDetails details;
details.is_up_to_date = false;
details.next_version = "";
details.upgrade_url = GURL("7");
[mediator_ handleOmahaResponse:details];
EXPECT_EQ(mediator_.updateCheckRowState, UpdateCheckRowStateOmahaError);
}
TEST_F(SafetyCheckMediatorTest, UpdateCheckOmahaErrorUI) {
mediator_.updateCheckRowState = UpdateCheckRowStateOmahaError;
[mediator_ reconfigureUpdateCheckItem];
EXPECT_NSEQ(mediator_.updateCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_UPDATES_ERROR_DESC));
EXPECT_FALSE(mediator_.updateCheckItem.infoButtonHidden);
}
TEST_F(SafetyCheckMediatorTest, UpdateCheckNetErrorUI) {
mediator_.updateCheckRowState = UpdateCheckRowStateNetError;
[mediator_ reconfigureUpdateCheckItem];
EXPECT_NSEQ(mediator_.updateCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_UPDATES_OFFLINE_DESC));
EXPECT_FALSE(mediator_.updateCheckItem.infoButtonHidden);
}
TEST_F(SafetyCheckMediatorTest, UpdateCheckManagedUI) {
mediator_.updateCheckRowState = UpdateCheckRowStateManaged;
[mediator_ reconfigureUpdateCheckItem];
EXPECT_NSEQ(mediator_.updateCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_UPDATES_MANAGED_DESC));
EXPECT_FALSE(mediator_.updateCheckItem.infoButtonHidden);
}
TEST_F(SafetyCheckMediatorTest, UpdateCheckChannelUI) {
mediator_.updateCheckRowState = UpdateCheckRowStateChannel;
[mediator_ reconfigureUpdateCheckItem];
EXPECT_NSEQ(
mediator_.updateCheckItem.detailText,
GetNSString(IDS_IOS_SETTINGS_SAFETY_CHECK_UPDATES_CHANNEL_CANARY_DESC));
EXPECT_TRUE(mediator_.updateCheckItem.infoButtonHidden);
}
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