JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "ContactsTester.h"
 
#import "ContactDetailsTableView.h"
#import "UIContactCell.h"
 
@implementation ContactsTester
 
#pragma mark - Setup
 
- (void)beforeAll {
    [self switchToValidAccountIfNeeded];
}
 
- (void)beforeEach {
    [super beforeEach];
    if ([tester tryFindingTappableViewWithAccessibilityLabel:@"Back" error:nil]) {
        [tester tapViewWithAccessibilityLabel:@"Back"];
    }
    [tester tapViewWithAccessibilityLabel:@"Contacts"];
}
 
#pragma mark - Utils
 
- (void)tapCellForRowAtIndexPath:(NSInteger)idx inSection:(NSInteger)section atX:(CGFloat)x {
    UITableView *tv = [self findTableView:@"Contact table"];
    NSIndexPath *path = [NSIndexPath indexPathForRow:idx inSection:section];
    UITableViewCell *cell =
        [tester waitForCellAtIndexPath:path inTableViewWithAccessibilityIdentifier:@"Contact table"];
    XCTAssertNotNil(cell);
 
    CGRect cellFrame = [cell.contentView convertRect:cell.contentView.frame toView:tv];
    [tv tapAtPoint:CGPointMake(x > 0 ? x : tv.superview.frame.size.width + x,
                               cellFrame.origin.y + cellFrame.size.height / 2.)];
    [tester waitForAnimationsToFinish];
}
 
- (void)tapRemoveButtonForRowAtIndexPath:(NSInteger)idx inSection:(NSInteger)section {
    [self tapCellForRowAtIndexPath:idx inSection:section atX:-7];
}
 
- (void)setText:(NSString *)text forIndex:(NSInteger)idx inSection:(NSInteger)section {
    [tester tapRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:section]
        inTableViewWithAccessibilityIdentifier:@"Contact table"];
    [tester clearTextFromAndThenEnterTextIntoCurrentFirstResponder:text];
}
 
- (void)addEntries:(NSArray *)numbers inSection:(NSInteger)section {
    [tester tapViewWithAccessibilityLabel:@"Edit"];
    NSString *name = (section == ContactSections_Sip) ? @"Add new SIP address" : @"Add new phone number";
    [self setText:[numbers objectAtIndex:0] forIndex:0 inSection:section];
    for (NSInteger i = 1; i < numbers.count; i++) {
        [tester tapViewWithAccessibilityLabel:name traits:UIAccessibilityTraitButton];
        [self setText:[numbers objectAtIndex:i] forIndex:i inSection:section];
    }
    [tester tapViewWithAccessibilityLabel:@"Edit"];
 
    for (NSInteger i = 0; i < numbers.count; i++) {
        [tester waitForViewWithAccessibilityLabel:[@"Call " stringByAppendingString:[numbers objectAtIndex:i]]
                                           traits:UIAccessibilityTraitButton];
    }
}
 
- (void)deleteContactEntryForRowAtIndexPath:(NSInteger)idx inSection:(NSInteger)section {
    //    if ([tester tryFindingTappableViewWithAccessibilityLabel:@"Delete" error:nil]) {
    //        [tester tapViewWithAccessibilityLabel:@"Delete"];
    //    } else {
    // hack: Travis seems to be unable to click on delete for what ever reason
        [self tapRemoveButtonForRowAtIndexPath:idx inSection:section];
        //    }
}
 
#pragma mark - Tests
 
- (void)testCallContactWithInvalidPhoneNumber {
    NSString *contactName = [self getUUID];
    NSString *phone = @"+5 15 #0664;447*46";
    [self createContact:contactName lastName:@"dummy" phoneNumber:phone SIPAddress:nil];
    [tester tapViewWithAccessibilityLabel:[@"Call " stringByAppendingString:phone]];
    [tester waitForViewWithAccessibilityLabel:[phone stringByAppendingString:@" is not connected."]];
    [tester tapViewWithAccessibilityLabel:@"Cancel"];
}
 
- (void)testDeleteContact {
    NSString *contactName = [self getUUID];
    [self createContact:contactName lastName:@"dummy" phoneNumber:@"0102030405" SIPAddress:[self me]];
    [tester tapViewWithAccessibilityLabel:@"Back"];
 
    NSString *fullName = [contactName stringByAppendingString:@" dummy"];
 
    [tester tapViewWithAccessibilityLabel:fullName traits:UIAccessibilityTraitStaticText];
 
    [tester tapViewWithAccessibilityLabel:@"Edit"];
 
    [tester tapViewWithAccessibilityLabel:@"Delete" traits:UIAccessibilityTraitButton];
    [tester tapViewWithAccessibilityLabel:@"DELETE" traits:UIAccessibilityTraitButton];
}
 
- (void)testDeleteContactWithSwipe {
    NSString *contactName = [self getUUID];
    [self createContact:contactName lastName:@"dummy" phoneNumber:@"123" SIPAddress:@"ola"];
    [tester tapViewWithAccessibilityLabel:@"Back"];
    NSString *fullName = [contactName stringByAppendingString:@" dummy"];
 
    [tester swipeViewWithAccessibilityLabel:fullName inDirection:KIFSwipeDirectionLeft];
    [tester tapViewWithAccessibilityLabel:@"Delete"];
 
    // we should not find this contact anymore
    XCTAssert([tester tryFindingViewWithAccessibilityLabel:fullName error:nil] == NO);
}
 
- (void)testEditContact {
    NSString *contactName = [self getUUID];
    [self createContact:contactName lastName:@"dummy" phoneNumber:@"111" SIPAddress:nil];
 
    /* Phone number */
    NSArray *phones = @[ @"01234", @"56789" ];
    [self addEntries:phones inSection:ContactSections_Number];
    NSArray *SIPs = @[ @"sip1", @"sip2" ];
    [self addEntries:SIPs inSection:ContactSections_Sip];
 
    [tester tapViewWithAccessibilityLabel:@"Edit"];
    // remove all numbers
    for (NSInteger i = 0; i < phones.count; i++) {
        [self deleteContactEntryForRowAtIndexPath:0 inSection:ContactSections_Number];
    }
    // remove all SIPs
    for (NSInteger i = 0; i < SIPs.count; i++) {
        [self deleteContactEntryForRowAtIndexPath:0 inSection:ContactSections_Sip];
    }
    [tester tapViewWithAccessibilityLabel:@"Edit"];
 
    // then remove the contact
    [tester tapViewWithAccessibilityLabel:@"Edit"];
 
    [tester scrollViewWithAccessibilityIdentifier:@"Contact table" byFractionOfSizeHorizontal:0 vertical:-0.9];
 
    [tester tapViewWithAccessibilityLabel:@"Delete" traits:UIAccessibilityTraitButton];
    [tester tapViewWithAccessibilityLabel:@"DELETE" traits:UIAccessibilityTraitButton];
}
 
@end