Commit 367203ff authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Add utils for printing debug descriptions

This makes it easier to output debug menus. I had to change the private
property into a method because C++ things do not seem to work well with
valueForKey:

Change-Id: I62b08be230b968449a6a002a1c7e62a4c0026a03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1816288Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Commit-Queue: John Wu <jzw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699223}
parent 60f08448
...@@ -198,6 +198,8 @@ ios_web_view_sources = [ ...@@ -198,6 +198,8 @@ ios_web_view_sources = [
"internal/translate/web_view_translate_ranker_factory.mm", "internal/translate/web_view_translate_ranker_factory.mm",
"internal/translate/web_view_translate_service.h", "internal/translate/web_view_translate_service.h",
"internal/translate/web_view_translate_service.mm", "internal/translate/web_view_translate_service.mm",
"internal/utils/nsobject_description_utils.h",
"internal/utils/nsobject_description_utils.mm",
"internal/web_view_browser_state.h", "internal/web_view_browser_state.h",
"internal/web_view_browser_state.mm", "internal/web_view_browser_state.mm",
"internal/web_view_download_manager.h", "internal/web_view_download_manager.h",
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "components/autofill/core/browser/autofill_type.h" #include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h" #include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "ios/web_view/internal/app/application_context.h" #include "ios/web_view/internal/app/application_context.h"
#import "ios/web_view/internal/utils/nsobject_description_utils.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."
...@@ -116,6 +117,14 @@ ...@@ -116,6 +117,14 @@
[self setValue:email forType:autofill::EMAIL_ADDRESS]; [self setValue:email forType:autofill::EMAIL_ADDRESS];
} }
#pragma mark - NSObject
- (NSString*)debugDescription {
NSString* debugDescription = [super debugDescription];
return [debugDescription
stringByAppendingFormat:@"\n%@", CWVPropertiesDescription(self)];
}
#pragma mark - Internal Methods #pragma mark - Internal Methods
- (autofill::AutofillProfile*)internalProfile { - (autofill::AutofillProfile*)internalProfile {
......
...@@ -13,12 +13,14 @@ class AutofillProfile; ...@@ -13,12 +13,14 @@ class AutofillProfile;
@interface CWVAutofillProfile () @interface CWVAutofillProfile ()
// The internal autofill profile that is wrapped by this object.
@property(nonatomic, readonly) autofill::AutofillProfile* internalProfile;
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile - (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile
NS_DESIGNATED_INITIALIZER; NS_DESIGNATED_INITIALIZER;
// The internal autofill profile that is wrapped by this object.
// Intentionally not declared as a property to avoid issues when read by
// -[NSObject valueForKey:].
- (autofill::AutofillProfile*)internalProfile;
@end @end
#endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_PROFILE_INTERNAL_H_ #endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_AUTOFILL_PROFILE_INTERNAL_H_
...@@ -44,7 +44,7 @@ TEST_F(CWVAutofillProfileTest, Initialization) { ...@@ -44,7 +44,7 @@ TEST_F(CWVAutofillProfileTest, Initialization) {
autofill::AutofillProfile profile = autofill::test::GetFullProfile(); autofill::AutofillProfile profile = autofill::test::GetFullProfile();
CWVAutofillProfile* cwv_profile = CWVAutofillProfile* cwv_profile =
[[CWVAutofillProfile alloc] initWithProfile:profile]; [[CWVAutofillProfile alloc] initWithProfile:profile];
EXPECT_EQ(profile, *cwv_profile.internalProfile); EXPECT_EQ(profile, *[cwv_profile internalProfile]);
} }
// Tests CWVAutofillProfile updates properties. // Tests CWVAutofillProfile updates properties.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/data_model/credit_card.h" #include "components/autofill/core/browser/data_model/credit_card.h"
#include "ios/web_view/internal/app/application_context.h" #include "ios/web_view/internal/app/application_context.h"
#import "ios/web_view/internal/utils/nsobject_description_utils.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -88,6 +89,14 @@ ...@@ -88,6 +89,14 @@
return _internalCard.record_type() != autofill::CreditCard::LOCAL_CARD; return _internalCard.record_type() != autofill::CreditCard::LOCAL_CARD;
} }
#pragma mark - NSObject
- (NSString*)debugDescription {
NSString* debugDescription = [super debugDescription];
return [debugDescription
stringByAppendingFormat:@"\n%@", CWVPropertiesDescription(self)];
}
#pragma mark - Internal #pragma mark - Internal
- (autofill::CreditCard*)internalCard { - (autofill::CreditCard*)internalCard {
......
...@@ -13,12 +13,14 @@ class CreditCard; ...@@ -13,12 +13,14 @@ class CreditCard;
@interface CWVCreditCard () @interface CWVCreditCard ()
// The internal autofill credit card that is wrapped by this object.
@property(nonatomic, readonly) autofill::CreditCard* internalCard;
- (instancetype)initWithCreditCard:(const autofill::CreditCard&)creditCard - (instancetype)initWithCreditCard:(const autofill::CreditCard&)creditCard
NS_DESIGNATED_INITIALIZER; NS_DESIGNATED_INITIALIZER;
// The internal autofill credit card that is wrapped by this object.
// Intentionally not declared as a property to avoid issues when read by
// -[NSObject valueForKey:].
- (autofill::CreditCard*)internalCard;
@end @end
#endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_CREDIT_CARD_INTERNAL_H_ #endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_CREDIT_CARD_INTERNAL_H_
...@@ -59,7 +59,7 @@ TEST_F(CWVCreditCardTest, Initialization) { ...@@ -59,7 +59,7 @@ TEST_F(CWVCreditCardTest, Initialization) {
autofill::CreditCard credit_card = autofill::test::GetCreditCard(); autofill::CreditCard credit_card = autofill::test::GetCreditCard();
CWVCreditCard* cwv_credit_card = CWVCreditCard* cwv_credit_card =
[[CWVCreditCard alloc] initWithCreditCard:credit_card]; [[CWVCreditCard alloc] initWithCreditCard:credit_card];
EXPECT_EQ(credit_card, *cwv_credit_card.internalCard); EXPECT_EQ(credit_card, *[cwv_credit_card internalCard]);
// It is not sufficient to simply test for networkIcon != nil because // It is not sufficient to simply test for networkIcon != nil because
// ui::ResourceBundle will return a placeholder image at @1x scale if the // ui::ResourceBundle will return a placeholder image at @1x scale if the
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#import "ios/web_view/public/cwv_identity.h" #import "ios/web_view/public/cwv_identity.h"
#import "ios/web_view/internal/utils/nsobject_description_utils.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
...@@ -26,4 +28,12 @@ ...@@ -26,4 +28,12 @@
return self; return self;
} }
#pragma mark - NSObject
- (NSString*)debugDescription {
NSString* debugDescription = [super debugDescription];
return [debugDescription
stringByAppendingFormat:@"\n%@", CWVPropertiesDescription(self)];
}
@end @end
// Copyright 2019 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.
#ifndef IOS_WEB_VIEW_INTERNAL_UTILS_NSOBJECT_DESCRIPTION_UTILS_H_
#define IOS_WEB_VIEW_INTERNAL_UTILS_NSOBJECT_DESCRIPTION_UTILS_H_
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
// Builds a string where each line represents a declared property of |object|
// and formatted as "<key>: <value>".
NSString* CWVPropertiesDescription(id object);
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_INTERNAL_UTILS_NSOBJECT_DESCRIPTION_UTILS_H_
// Copyright 2019 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/web_view/internal/utils/nsobject_description_utils.h"
#import <objc/runtime.h>
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
NSString* CWVPropertiesDescription(id object) {
NSMutableArray* properties = [NSMutableArray array];
unsigned int outCount;
objc_property_t* propertyList =
class_copyPropertyList([object class], &outCount);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = propertyList[i];
NSString* propertyName =
[[NSString alloc] initWithCString:property_getName(property)
encoding:NSUTF8StringEncoding];
id propertyValue = [object valueForKey:propertyName];
NSString* propertyDescription =
[NSString stringWithFormat:@"%@: %@", propertyName, propertyValue];
[properties addObject:propertyDescription];
}
free(propertyList);
return [properties componentsJoinedByString:@"\n"];
}
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