Skip to content

Commit

Permalink
Merge pull request #1324 from nextcloud/add-test-for-chatviewcontroll…
Browse files Browse the repository at this point in the history
…er-deallocation

Add test for chatviewcontroller deallocation
  • Loading branch information
Ivansss committed Aug 22, 2023
2 parents fef252e + 38aa6ab commit 7091d2a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
19 changes: 19 additions & 0 deletions NextcloudTalk/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ @interface AppDelegate ()

@property (nonatomic, strong) NSTimer *keepAliveTimer;
@property (nonatomic, strong) BGTaskHelper *keepAliveBGTask;
@property (nonatomic, strong) UILabel *debugLabel;
@property (nonatomic, strong) NSTimer *debugLabelTimer;

@end

Expand Down Expand Up @@ -92,6 +94,20 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[NCUserInterfaceController sharedInstance].mainViewController.displayModeButtonVisibility = UISplitViewControllerDisplayModeButtonVisibilityNever;
}

NSArray *arguments = [[NSProcessInfo processInfo] arguments];

if ([arguments containsObject:@"-TestEnvironment"]) {
self.debugLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
[[NCUserInterfaceController sharedInstance].mainViewController.view addSubview:self.debugLabel];

__weak typeof(self) weakSelf = self;
self.debugLabelTimer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
long numChatVC = [NCUserInterfaceController sharedInstance].numberOfAllocatedChatViewControllers;
long numCallVC = [NCUserInterfaceController sharedInstance].numberOfAllocatedCallViewControllers;
[weakSelf.debugLabel setText:[NSString stringWithFormat:@"ChatVC: %ld / CallVC: %ld", numChatVC, numCallVC]];
}];
}

// When we include VLCKit we need to manually call this because otherwise, device rotation might not work
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Expand Down Expand Up @@ -149,6 +165,9 @@ - (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

// Invalidate a potentially existing label timer
[self.debugLabelTimer invalidate];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
Expand Down
3 changes: 3 additions & 0 deletions NextcloudTalk/CallViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ - (instancetype)initCallInRoom:(NCRoom *)room asUser:(NSString *)displayName aud
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeRoutingInformation:) name:AudioSessionDidChangeRoutingInformationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];

[NCUserInterfaceController sharedInstance].numberOfAllocatedCallViewControllers += 1;

return self;
}
Expand Down Expand Up @@ -346,6 +348,7 @@ - (UIStatusBarStyle)preferredStatusBarStyle
- (void)dealloc
{
NSLog(@"CallViewController dealloc");
[NCUserInterfaceController sharedInstance].numberOfAllocatedCallViewControllers -= 1;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/ChatMessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,14 @@ - (void)setDeliveryState:(ChatMessageDeliveryState)state
[checkView setImage:[UIImage imageNamed:@"check"]];
checkView.image = [checkView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[checkView setTintColor:[UIColor lightGrayColor]];
[checkView setAccessibilityValue:@"MessageSent"];
[self.statusView addSubview:checkView];
} else if (state == ChatMessageDeliveryStateRead) {
UIImageView *checkAllView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[checkAllView setImage:[UIImage imageNamed:@"check-all"]];
checkAllView.image = [checkAllView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[checkAllView setTintColor:[UIColor lightGrayColor]];
[checkAllView setAccessibilityValue:@"MessageSent"];
[self.statusView addSubview:checkAllView];
}
}
Expand Down
3 changes: 3 additions & 0 deletions NextcloudTalk/NCChatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ - (instancetype)initForRoom:(NCRoom *)room
// Notifications when runing on Mac
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:@"NSApplicationDidBecomeActiveNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:@"NSApplicationDidResignActiveNotification" object:nil];

[NCUserInterfaceController sharedInstance].numberOfAllocatedChatViewControllers += 1;
}

return self;
Expand All @@ -249,6 +251,7 @@ - (instancetype)initForRoom:(NCRoom *)room
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[NCUserInterfaceController sharedInstance].numberOfAllocatedChatViewControllers -= 1;
NSLog(@"Dealloc NCChatViewController");
}

Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/NCUserInterfaceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ typedef void (^PresentCallControllerCompletionBlock)(void);

@property (nonatomic, strong) NCSplitViewController *mainViewController;
@property (nonatomic, strong) RoomsTableViewController *roomsTableViewController;
@property (nonatomic, assign) NSInteger numberOfAllocatedChatViewControllers;
@property (nonatomic, assign) NSInteger numberOfAllocatedCallViewControllers;

+ (instancetype)sharedInstance;
- (void)presentConversationsList;
Expand Down
49 changes: 49 additions & 0 deletions NextcloudTalkUITests/NextcloudTalkUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class NextcloudTalkUITests: XCTestCase {
let app = XCUIApplication()
app.launchArguments += ["-AppleLanguages", "(en-US)"]
app.launchArguments += ["-AppleLocale", "\"en-US\""]
app.launchArguments += ["-TestEnvironment"]
app.launch()

let accountSwitcherButton = app.buttons["Nextcloud Talk"]
Expand Down Expand Up @@ -161,4 +162,52 @@ final class NextcloudTalkUITests: XCTestCase {
// Check if the conversation appears in the conversation list
XCTAssert(app.tables.cells.staticTexts[newConversationName].waitForExistence(timeout: timeoutLong))
}

func testChatViewControllerDeallocation() {
let app = launchAndLogin()
let newConversationName = "DeAllocTest"

// Create a new test conversion
app.navigationBars["Nextcloud Talk"].buttons["Create a new conversation"].tap()
XCTAssert(app.tables.cells.staticTexts["Create a new group conversation"].waitForExistence(timeout: timeoutShort))
XCTAssert(app.tables.cells.staticTexts["Create a new public conversation"].waitForExistence(timeout: timeoutShort))
XCTAssert(app.tables.cells.staticTexts["Show list of open conversations"].waitForExistence(timeout: timeoutShort))
app.tables.cells.staticTexts["Create a new group conversation"].tap()
app.navigationBars["RoomCreationTableView"].buttons["Next"].tap()
app.typeText(newConversationName)
app.navigationBars["New group conversation"].buttons["Create"].tap()

// Check if we have one chat view controller allocated
XCTAssert(app.staticTexts["ChatVC: 1 / CallVC: 0"].waitForExistence(timeout: timeoutShort))

// Send a test message
let testMessage = "TestMessage"
let toolbar = app.toolbars["Toolbar"]
let textView = toolbar.textViews["Write message, @ to mention someone …"]
XCTAssert(textView.waitForExistence(timeout: timeoutShort))
textView.tap()
app.typeText(testMessage)
let sendMessageButton = toolbar.buttons["Send message"]
sendMessageButton.tap()

// Wait for temporary message to be replaced
XCTAssert(app.images["MessageSent"].waitForExistence(timeout: timeoutShort))

// Open context menu
let tables = app.tables
XCTAssert(tables.staticTexts[username].waitForExistence(timeout: timeoutShort))
let message = tables.staticTexts[username]
message.press(forDuration: 2.0)

// Add a reaction to close the context menu
XCTAssert(app.staticTexts["👍"].waitForExistence(timeout: timeoutShort))
app.staticTexts["👍"].tap()

// Go back to the main view controller
let chatNavBar = app.navigationBars["NCChatView"]
chatNavBar.buttons["Back"].tap()

// Check if all chat view controllers are deallocated
XCTAssert(app.staticTexts["ChatVC: 0 / CallVC: 0"].waitForExistence(timeout: timeoutShort))
}
}

0 comments on commit 7091d2a

Please sign in to comment.