blob: fbb7d1e2377f30a485c0a4b31a7017c991c87065 [file] [log] [blame]
// 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.
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
#include "ios/chrome/credential_provider_extension/metrics_util.h"
#import <Foundation/Foundation.h>
#import <Security/Security.h>
#include "ios/chrome/common/app_group/app_group_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
namespace credential_provider_extension {
NSString* kMetric1 = @"CpeMetricTest1";
NSString* kMetric2 = @"CpeMetricTest2";
void RemoveMetricForKey(NSString* key) {
NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
[sharedDefaults removeObjectForKey:key];
}
void VerifyMetricForKey(NSString* key, int valueExpected) {
NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
NSInteger value = [sharedDefaults integerForKey:key];
ASSERT_EQ(value, valueExpected);
}
class MetricsUtilTest : public PlatformTest {
public:
void SetUp() override;
void TearDown() override;
};
void MetricsUtilTest::SetUp() {
RemoveMetricForKey(kMetric1);
RemoveMetricForKey(kMetric2);
}
void MetricsUtilTest::TearDown() {
RemoveMetricForKey(kMetric1);
RemoveMetricForKey(kMetric2);
}
// Tests increase and reset of metrics.
TEST_F(MetricsUtilTest, CheckUMACountUpdatesAsExpected) {
VerifyMetricForKey(kMetric1, 0);
VerifyMetricForKey(kMetric2, 0);
UpdateUMACountForKey(kMetric1);
VerifyMetricForKey(kMetric1, 1);
VerifyMetricForKey(kMetric2, 0);
UpdateUMACountForKey(kMetric2);
UpdateUMACountForKey(kMetric2);
VerifyMetricForKey(kMetric1, 1);
VerifyMetricForKey(kMetric2, 2);
RemoveMetricForKey(kMetric2);
VerifyMetricForKey(kMetric1, 1);
VerifyMetricForKey(kMetric2, 0);
}
} // credential_provider_extension