Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
@testable import Client
7
import WebKit
8
9
import XCTest
10
11
class TabEventHandlerTests: XCTestCase {
12
13
func testEventDelivery() {
14
let tab = Tab(bvc: BrowserViewController.foregroundBVC(), configuration: WKWebViewConfiguration())
15
let handler = DummyHandler()
16
17
XCTAssertNil(handler.isFocused)
18
19
TabEvent.post(.didGainFocus, for: tab)
20
XCTAssertTrue(handler.isFocused!)
21
22
TabEvent.post(.didLoseFocus, for: tab)
23
XCTAssertFalse(handler.isFocused!)
24
}
25
}
26
27
28
class DummyHandler: TabEventHandler {
29
// This is not how this should be written in production — the handler shouldn't be keeping track
30
// of individual tab state.
31
var isFocused: Bool? = nil
32
33
init() {
34
register(self, forTabEvents: .didGainFocus, .didLoseFocus)
35
}
36
37
func tabDidGainFocus(_ tab: Tab) {
38
isFocused = true
39
}
40
41
func tabDidLoseFocus(_ tab: Tab) {
42
isFocused = false
43
}
44
}