This file is indexed.

/usr/lib/python2.7/dist-packages/unity/tests/test_panel.py is in unity-autopilot 7.5.0+18.04.20180413-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# Copyright 2012 Canonical
# Author: Marco Trevisan (TreviƱo)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.

from __future__ import absolute_import

from autopilot.display import Display
#from autopilot.emulators.bamf import BamfWindow
from autopilot.process import Window
from autopilot.matchers import Eventually
from autopilot.testcase import multiply_scenarios
import logging
import os
from testtools.matchers import Equals,  GreaterThan, NotEquals
from time import sleep

from unity.emulators.panel import IndicatorEntry
from unity.emulators.X11 import drag_window_to_screen
from unity.tests import UnityTestCase

import gettext

logger = logging.getLogger(__name__)

def _make_scenarios():
    return multiply_scenarios(_make_monitor_scenarios(),
                              _make_menu_modes_scenarios())

def _make_menu_modes_scenarios():
    return [ ('Locally Integrated Menus', {'lim': True}),
             ('Global Menus', {'lim': False}) ]

def _make_monitor_scenarios():
    num_monitors = Display.create().get_num_screens()
    scenarios = []

    if num_monitors == 1:
        scenarios = [('Single Monitor', {'panel_monitor': 0})]
    else:
        for i in range(num_monitors):
            scenarios += [('Monitor %d' % (i), {'panel_monitor': i})]

    return scenarios

class PanelTestsBase(UnityTestCase):

    panel_monitor = 0
    lim = False

    def setUp(self):
        super(PanelTestsBase, self).setUp()
        self.panel = self.unity.panels.get_panel_for_monitor(self.panel_monitor)

        old_lim = self.call_gsettings_cmd('get', 'com.canonical.Unity', 'integrated-menus')
        self.call_gsettings_cmd('set', 'com.canonical.Unity', 'integrated-menus', str(self.lim).lower())
        self.addCleanup(self.call_gsettings_cmd, 'set', 'com.canonical.Unity', 'integrated-menus', old_lim)

        old_showmenus = self.call_gsettings_cmd('get', 'com.canonical.Unity', 'always-show-menus')
        self.call_gsettings_cmd('set', 'com.canonical.Unity', 'always-show-menus', 'false')
        self.addCleanup(self.call_gsettings_cmd, 'set', 'com.canonical.Unity', 'always-show-menus', old_showmenus)

        self.panel.move_mouse_below_the_panel()
        self.addCleanup(self.panel.move_mouse_below_the_panel)

        self.assertThat(self.panel.menus.integrated_menus, Eventually(Equals(self.lim)))
        if not self.lim:
            self.assertThat(self.panel.focused, Eventually(Equals(True)))

    def ensure_window_state(self, win, maximized=False):
        if maximized and not win.is_maximized:
            self.keybinding("window/maximize")
            self.addCleanup(self.keybinding, "window/restore")
        elif not maximized and win.is_maximized:
            self.keybinding("window/restore")
            self.addCleanup(self.keybinding, "window/maximize")

        self.assertThat(lambda: win.is_maximized, Eventually(Equals(maximized)))

    def open_new_application_window(self, app_name, maximized=False, move_to_monitor=True):
        """Opens a new instance of the requested application, ensuring that only
        one window is opened.

        Returns the opened BamfWindow

        """
        self.process_manager.close_all_app(app_name)
        app_win = self.process_manager.start_app_window(app_name, locale="C")
        app = app_win.application

        app_win.set_focus()
        self.assertThat(lambda: app.is_active, Eventually(Equals(True)))
        self.assertThat(lambda: app_win.is_focused, Eventually(Equals(True)))
        self.assertThat(app.desktop_file, Equals(app_win.application.desktop_file))

        if move_to_monitor:
            self.move_window_to_panel_monitor(app_win)

        app_win.set_focus()
        self.ensure_window_state(app_win, maximized)

        return app_win

    def move_window_to_panel_monitor(self, window, restore_position=False):
        """Drags a window to another monitor, eventually restoring it before"""
        if not isinstance(window, Window):
            raise TypeError("Window must be a autopilot.process.Window")

        if window.monitor == self.panel_monitor:
            return

        if window.is_maximized:
            self.keybinding("window/restore")
            self.addCleanup(self.keybinding, "window/maximize")
            sleep(.1)

        if restore_position:
            self.addCleanup(drag_window_to_screen, window, window.monitor)

        drag_window_to_screen(window, self.panel_monitor)
        sleep(.25)
        self.assertThat(window.monitor, Equals(self.panel_monitor))

    def mouse_open_indicator(self, indicator):
        """This is an utility function that safely opens an indicator,
        ensuring that it is closed at the end of the test and that the pointer
        is moved outside the panel area (to make the panel hide the menus)
        """
        if not isinstance(indicator, IndicatorEntry):
            raise TypeError("Window must be a IndicatorEntry")

        indicator.mouse_click()
        self.addCleanup(self.panel.move_mouse_below_the_panel)
        self.addCleanup(self.keyboard.press_and_release, "Escape")
        self.assertThat(indicator.active, Eventually(Equals(True)))

    def assertWinButtonsInOverlayMode(self, overlay_mode):
        """Assert that there are three panel window buttons and all of them are
        in the specified overlay mode.

        """
        if type(overlay_mode) is not bool:
            raise TypeError("overlay_mode must be True or False")

        buttons = self.panel.window_buttons.get_buttons()
        self.assertThat(len(buttons), Equals(3))
        for button in buttons:
            self.assertThat(button.overlay_mode, Eventually(Equals(overlay_mode)))

    def assertNoWindowOpenWithXid(self, x_id):
        """Assert that Bamf doesn't know of any open windows with the given xid."""
        # We can't check text_win.closed since we've just destroyed the window.
        # Instead we make sure no window with it's x_id exists.
        refresh_fn = lambda: [w for w in self.process_manager.get_open_windows() if w.x_id == x_id]
        self.assertThat(refresh_fn, Eventually(Equals([])))

    def sleep_menu_settle_period(self):
        """Sleep long enough for the menus to fade in and fade out again."""
        if not self.lim:
            sleep(self.panel.menus.fadein_duration / 1000.0)
            sleep(self.panel.menus.discovery_duration)
            sleep(self.panel.menus.fadeout_duration / 1000.0)

    # Unable to exit SDM without any active apps, need a placeholder.
    # See bug LP:1079460
    def start_placeholder_app(self):
        window_spec = {
            "Title": "Placeholder application",
        }
        self.launch_test_window(window_spec)


class PanelTitleTests(PanelTestsBase):

    scenarios = _make_scenarios()

    def setUp(self):
        super(PanelTitleTests, self).setUp()
        # Locked Launchers on all monitors
        self.set_unity_option('num_launchers', 0)
        self.set_unity_option('launcher_hide_mode', 0)

    def test_panel_title_on_empty_desktop(self):
        """With no windows shown, the panel must display the default title."""
        gettext.install("unity", unicode=True)
        self.start_placeholder_app()
        self.unity.window_manager.enter_show_desktop()
        self.addCleanup(self.unity.window_manager.leave_show_desktop)

        self.assertThat(self.panel.desktop_is_active, Eventually(Equals(True)))
        self.assertThat(self.panel.title, Equals(_("Ubuntu Desktop")))

    def test_panel_title_with_restored_application(self):
        """Panel must display application name for a non-maximised application."""
        calc_win = self.open_new_application_window("Calculator", maximized=False)

        expected = calc_win.application.name if not self.lim else ''
        self.assertThat(self.panel.title, Eventually(Equals(expected)))

    def test_panel_title_with_maximized_application(self):
        """Panel must display application name for a maximised application."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        self.assertThat(self.panel.title, Eventually(Equals(text_win.title)))
        self.assertThat(self.panel.focused, Eventually(Equals(True)))

    def test_panel_title_with_maximized_window_restored_child(self):
        """Tests the title shown in the panel when opening the restored child of
        a maximized application.
        """
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        # Ctrl+h opens the replace dialog.
        self.keyboard.press_and_release("Ctrl+h")
        self.addCleanup(self.keyboard.press_and_release, "Escape")

        self.assertThat(lambda: len(text_win.application.get_windows()),
                        Eventually(Equals(2)))
        expected = text_win.application.name if not self.lim else text_win.title
        self.assertThat(self.panel.title, Equals(expected))
        self.assertThat(self.panel.focused, Eventually(Equals(not self.lim)))

    def test_panel_shows_app_title_with_maximised_app(self):
        """Tests app titles are shown in the panel with a non-focused maximized application."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)
        calc_win = self.open_new_application_window("Calculator", maximized=False)

        expected = calc_win.application.name if not self.lim else text_win.title
        self.assertThat(self.panel.title, Eventually(Equals(expected)))
        self.assertThat(self.panel.focused, Eventually(Equals(not self.lim)))

    def test_panel_title_updates_when_switching_to_maximized_app(self):
        """Switching to a maximised app from a restored one must update the panel title."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)
        self.open_new_application_window("Calculator", maximized=False)

        icon = self.unity.launcher.model.get_icon(desktop_id=text_win.application.desktop_file)
        launcher = self.unity.launcher.get_launcher_for_monitor(self.panel_monitor)
        launcher.click_launcher_icon(icon)

        self.assertProperty(text_win, is_focused=True)
        self.assertThat(self.panel.title, Eventually(Equals(text_win.title)))

    def test_panel_title_updates_on_maximized_window_title_changes(self):
        """Panel title must change when the title of a maximized application changes."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)
        old_title = text_win.title

        text_win.set_focus()
        self.keyboard.press_and_release("Ctrl+n")

        self.assertThat(lambda: text_win.title, Eventually(NotEquals(old_title)))
        self.assertThat(self.panel.title, Eventually(Equals(text_win.title)))

    def test_panel_title_doesnt_change_with_switcher(self):
        """Switching between apps must not change the Panels title."""
        calc_win = self.open_new_application_window("Calculator")
        text_win = self.open_new_application_window("Text Editor")
        current_title = self.panel.title

        self.unity.switcher.initiate()
        self.addCleanup(self.unity.switcher.terminate)
        self.unity.switcher.next_icon()

        self.assertThat(self.panel.title,
                        Eventually(Equals(current_title)))


class PanelWindowButtonsTests(PanelTestsBase):

    scenarios = _make_scenarios()

    def setUp(self):
        super(PanelWindowButtonsTests, self).setUp()
        # Locked Launchers on all monitors
        self.set_unity_option('num_launchers', 0)
        self.set_unity_option('launcher_hide_mode', 0)
        self.always_visible = self.lim

    def test_window_buttons_dont_show_on_empty_desktop(self):
        """Tests that the window buttons are not shown on clean desktop."""
        self.start_placeholder_app()
        self.unity.window_manager.enter_show_desktop()
        self.addCleanup(self.unity.window_manager.leave_show_desktop)

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

        self.panel.move_mouse_over_window_buttons()
        # Sleep twice as long as the timeout, just to be sure. timeout is in
        # mS, we need seconds, hence the divide by 500.0
        sleep(self.panel.menus.fadein_duration / 500.0)
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_window_buttons_dont_show_for_restored_window(self):
        """Window buttons must not show for a restored window."""
        self.open_new_application_window("Calculator")
        self.panel.move_mouse_below_the_panel()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_window_buttons_dont_show_for_restored_window_with_mouse_in_panel(self):
        """Window buttons must not show for a restored window with the mouse in
        the panel."""
        self.open_new_application_window("Calculator")
        self.panel.move_mouse_over_window_buttons()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_window_buttons_show_for_maximized_window_on_mouse_out(self):
        """Window buttons might show for a maximized window when the mouse is
        outside the panel, depending on LIM setting.
        """
        self.open_new_application_window("Text Editor", maximized=True)

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))
        self.assertThat(self.panel.window_buttons.focused, Eventually(Equals(True)))

    def test_window_buttons_show_for_maximized_window_on_mouse_in(self):
        """Window buttons must show when a maximized window is focused and the
        mouse is over the menu-view panel areas.

        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.panel.move_mouse_over_window_buttons()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))
        self.assertThat(self.panel.window_buttons.focused, Eventually(Equals(True)))
        self.assertWinButtonsInOverlayMode(False)

    def test_window_buttons_show_for_maximized_unfocused_window_with_mouse_in_panel(self):
        """Window buttons might show for an unfocused maximized window when the
        mouse is over the panel, depending on LIM setting.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.open_new_application_window("Calculator")
        self.panel.move_mouse_over_window_buttons()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))
        self.assertThat(self.panel.window_buttons.focused, Eventually(Equals(not self.lim)))

    def test_window_buttons_show_for_maximized_unfocused_window_on_mouse_out(self):
        """Window buttons might show for an unfocused maximized window when the
        mouse is outside the panel, depending on LIM setting.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.open_new_application_window("Calculator")

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))
        self.assertThat(self.panel.window_buttons.focused, Eventually(Equals(not self.lim)))

    def test_window_buttons_show_with_dash(self):
        """Window buttons must be shown when the dash is open."""
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if self.unity.dash.monitor == monitor:
                self.assertThat(self.unity.dash.view.overlay_window_buttons_shown[monitor], Equals(True))
            else:
                self.assertThat(self.unity.dash.view.overlay_window_buttons_shown[monitor], Equals(False))

    def test_window_buttons_work_in_dash_after_launcher_resize(self):
        """When the launcher icons are resized, the window
        buttons must still work in the dash."""

        self.set_unity_option("icon_size", 25)
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        desired_max = not self.unity.dash.view.dash_maximized
        if desired_max:
            self.panel.window_buttons.maximize.mouse_click()
        else:
            self.panel.window_buttons.unmaximize.mouse_click()

        self.assertThat(self.unity.dash.view.dash_maximized, Eventually(Equals(desired_max)))

    def test_window_buttons_show_with_hud(self):
        """Window buttons must be shown when the HUD is open."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if self.unity.hud.monitor == monitor:
                self.assertThat(self.unity.hud.view.overlay_window_buttons_shown[monitor], Equals(True))
            else:
                self.assertThat(self.unity.hud.view.overlay_window_buttons_shown[monitor], Equals(False))

    def test_window_buttons_close_button_works_for_hud(self):
        """Tests that the window 'Close' actually closes the HUD."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.panel.window_buttons.close.mouse_click()
        self.assertThat(self.unity.hud.visible, Eventually(Equals(False)))

    def test_minimize_button_disabled_for_hud(self):
        """Minimize button must be disabled for the HUD."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.assertThat(self.panel.window_buttons.minimize.enabled, Eventually(Equals(False)))

    def test_minimize_button_does_nothing_for_hud(self):
        """Minimize button must not affect the Hud."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.panel.window_buttons.minimize.mouse_click()

        self.assertThat(self.unity.hud.visible, Eventually(Equals(True)))

    def test_maximize_button_disabled_for_hud(self):
        """Maximize button must be disabled for the HUD."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.assertThat(self.panel.window_buttons.maximize.enabled, Eventually(Equals(False)))

    def test_maximize_button_does_nothing_for_hud(self):
        """Maximize button must not affect the Hud."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.panel.window_buttons.maximize.mouse_click()
        self.assertThat(self.unity.hud.visible, Eventually(Equals(True)))

    def test_hud_maximize_button_does_not_change_dash_form_factor(self):
        """Clicking on the 'Maximize' button of the HUD must not change the dash
        layout.

        See bug #939054
        """
        inital_form_factor = self.unity.dash.view.form_factor
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.panel.window_buttons.maximize.mouse_click()
        # long sleep here to make sure that any change that might happen will
        # have already happened.
        sleep(5)
        self.assertThat(self.unity.dash.view.form_factor, Equals(inital_form_factor))

    def test_window_buttons_close_button_works_for_dash(self):
        """Tests that the window 'Close' actually closes the Dash."""
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)
        self.panel.window_buttons.close.mouse_click()

        self.assertThat(self.unity.dash.visible, Eventually(Equals(False)))

    def test_minimize_button_disabled_for_dash(self):
        """Tests that the 'Minimize' button is disabled for the dash."""
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        self.assertThat(self.panel.window_buttons.minimize.enabled, Eventually(Equals(False)))

    def test_minimize_button_does_nothing_for_dash(self):
        """Tests that the 'Minimize' button is disabled for the dash."""
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        self.panel.window_buttons.minimize.mouse_click()
        sleep(5)
        self.assertThat(self.unity.dash.visible, Eventually(Equals(True)))

    def test_window_buttons_maximize_or_restore_dash(self):
        """Tests that the Maximize/Restore button works for the dash."""

        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        desired_max = not self.unity.dash.view.dash_maximized
        if desired_max:
            self.panel.window_buttons.maximize.mouse_click()
        else:
            self.panel.window_buttons.unmaximize.mouse_click()

        self.assertThat(self.unity.dash.view.dash_maximized, Eventually(Equals(desired_max)))

    def test_window_buttons_active_inactive_states(self):
        """Tests that the maximized/restore buttons are in the correct state when the
        dash is open. Asserting states: visible, sensitive, enabled.
        """

        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        unmaximize = self.unity.dash.view.window_buttons.unmaximize
        maximize = self.unity.dash.view.window_buttons.maximize

        desired_max = not self.unity.dash.view.dash_maximized
        if desired_max:
            active   = maximize
            inactive = unmaximize
        else:
            active   = unmaximize
            inactive = maximize

        self.assertThat(active.visible, Eventually(Equals(True)))
        self.assertThat(active.sensitive, Eventually(Equals(True)))
        self.assertThat(active.enabled, Eventually(Equals(True)))
        self.assertThat(inactive.visible, Eventually(Equals(False)))

    def test_window_buttons_state_switch_on_click(self):
        """Tests that when clicking the maximize/restore window button it
        switchs its state from either maximize to restore, or restore to
        maximize.
        """

        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        unmaximize = self.unity.dash.view.window_buttons.unmaximize
        maximize = self.unity.dash.view.window_buttons.maximize

        desired_max = not self.unity.dash.view.dash_maximized
        if desired_max:
            active   = maximize
            inactive = unmaximize
        else:
            active   = unmaximize
            inactive = maximize

        active.mouse_click()

        self.assertThat(inactive.visible, Eventually(Equals(True)))
        self.assertThat(inactive.sensitive, Eventually(Equals(True)))
        self.assertThat(inactive.enabled, Eventually(Equals(True)))
        self.assertThat(active.visible, Eventually(Equals(False)))
        self.assertThat(self.unity.dash.view.dash_maximized, Eventually(Equals(desired_max)))

    def test_minimize_button_disabled_for_non_minimizable_windows(self):
        """Minimize button must be disabled for windows that don't support minimization."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        self.keyboard.press_and_release("Ctrl+S")
        self.addCleanup(self.keyboard.press_and_release, "Escape")

        wins = text_win.application.get_windows()
        self.assertThat(len(wins), Equals(2))
        [target_win] = [w for w in wins if w.x_id != text_win.x_id]
        self.move_window_to_panel_monitor(target_win, restore_position=False)

        self.keybinding("window/maximize")
        self.assertProperty(target_win, is_maximized=True)

        self.assertThat(self.panel.window_buttons.close.enabled, Eventually(Equals(True)))
        self.assertThat(self.panel.window_buttons.minimize.enabled, Eventually(Equals(False)))

    def test_window_buttons_show_when_indicator_active_and_mouse_over_panel(self):
        """Window buttons must be shown when mouse is over panel area with an
        indicator open.
        """
        self.open_new_application_window("Text Editor", maximized=True)

        indicator = self.panel.indicators.get_indicator_by_name_hint("indicator-session")
        self.mouse_open_indicator(indicator)
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))

        self.panel.move_mouse_below_the_panel()
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))

        self.panel.move_mouse_over_grab_area()
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))

    def test_window_buttons_show_when_holding_show_menu_key(self):
        """Window buttons must show when we press the show-menu keybinding."""
        self.open_new_application_window("Text Editor", maximized=True)

        self.sleep_menu_settle_period()
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))

        self.keybinding_hold("panel/show_menus")
        self.addCleanup(self.keybinding_release, "panel/show_menus")

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))

        # Sleep a bit to avoid a race with the Hud showing
        sleep(0.5)
        self.keybinding_release("panel/show_menus")
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.always_visible)))

    def test_window_buttons_cant_accept_keynav_focus(self):
        """On a mouse down event over the window buttons
        you must still be able to type into the Hud.

        """
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.keyboard.type("Hello")
        self.panel.window_buttons.minimize.mouse_click()
        self.keyboard.type("World")

        self.assertThat(self.unity.hud.search_string, Eventually(Equals("HelloWorld")))


class PanelWindowButtonsActionsTests(PanelTestsBase):
    """Test WindowButtons actions on windows, focused or not depending on LIM"""
    restored_on_top = False
    scenarios = multiply_scenarios(_make_monitor_scenarios(),
                                   (_make_menu_modes_scenarios() +
                                    [('Locally Integrated Menus with restored on Top',
                                      {'lim': True, 'restored_on_top': True})]))

    def setUp(self):
        super(PanelWindowButtonsActionsTests, self).setUp()
        self.text_win = self.open_new_application_window("Text Editor", maximized=True)

        if self.restored_on_top:
            self.open_new_application_window("Calculator")

    def test_window_buttons_update_visual_state(self):
        """Window button must update its state in response to mouse events."""
        self.panel.move_mouse_over_window_buttons()
        button = self.panel.window_buttons.unmaximize

        self.assertThat(self.panel.window_buttons.focused, Eventually(Equals(not self.restored_on_top)))
        self.assertThat(button.visual_state, Eventually(Equals("normal")))

        button.mouse_move_to()
        self.assertThat(button.visual_state, Eventually(Equals("prelight")))

        self.mouse.press()
        self.addCleanup(self.mouse.release)
        self.assertThat(button.visual_state, Eventually(Equals("pressed")))

    def test_window_buttons_cancel(self):
        """Window buttons must ignore clicks when the mouse released outside
        their area.
        """
        self.panel.move_mouse_over_window_buttons()

        button = self.panel.window_buttons.unmaximize
        button.mouse_move_to()
        self.mouse.press()
        self.assertThat(button.visual_state, Eventually(Equals("pressed")))
        self.panel.move_mouse_below_the_panel()
        self.mouse.release()

        self.assertThat(self.text_win.is_maximized, Equals(True))

    def test_window_buttons_close_button_works_for_window(self):
        """Close window button must actually closes a window."""
        win_xid = self.text_win.x_id

        self.panel.window_buttons.close.mouse_click()
        self.assertNoWindowOpenWithXid(win_xid)

    def test_window_buttons_close_follows_fitts_law(self):
        """Tests that the 'Close' button is activated when clicking at 0,0.

        See bug #839690
        """
        win_xid = self.text_win.x_id

        self.panel.move_mouse_over_window_buttons()
        screen_x, screen_y = self.display.get_screen_geometry(self.panel_monitor)[:2]
        self.mouse.move(screen_x, screen_y)
        self.mouse.click()

        self.assertNoWindowOpenWithXid(win_xid)

    def test_window_buttons_minimize_button_works_for_window(self):
        """Tests that the window button 'Minimize' actually minimizes a window."""
        self.panel.window_buttons.minimize.mouse_click()
        self.assertProperty(self.text_win, is_hidden=True)

    def test_window_buttons_minimize_follows_fitts_law(self):
        """Tests that the 'Minimize' button is conform to Fitts's Law.

        See bug #839690
        """
        self.panel.move_mouse_over_window_buttons()
        button = self.panel.window_buttons.minimize
        target_x = button.x + button.width / 2
        target_y = self.display.get_screen_geometry(self.panel_monitor)[1]
        self.mouse.move(target_x, target_y)
        self.mouse.click()

        self.assertProperty(self.text_win, is_hidden=True)

    def test_window_buttons_unmaximize_button_works_for_window(self):
        """Tests that the window button 'Unmaximize' actually unmaximizes a window."""
        self.panel.window_buttons.unmaximize.mouse_click()

        self.assertProperties(self.text_win, is_maximized=False, is_focused=True)
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_window_buttons_unmaximize_follows_fitts_law(self):
        """Tests that the 'Unmaximize' button is conform to Fitts's Law.

        See bug #839690
        """
        button = self.panel.window_buttons.unmaximize
        button.mouse_move_to()
        target_x = button.x + button.width / 2
        target_y = self.display.get_screen_geometry(self.panel_monitor)[1]
        self.mouse.move(target_x, target_y)
        sleep(1)
        self.mouse.click()

        self.assertProperty(self.text_win, is_maximized=False)


class PanelHoverTests(PanelTestsBase):
    """Tests with the mouse pointer hovering the panel area."""

    scenarios = _make_scenarios()

    def test_only_menus_show_for_restored_window_on_mouse_in_window_btn_area(self):
        """Restored windows should only show menus when the mouse is in the window
        button area.
        """
        self.open_new_application_window("Calculator")
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_window_buttons()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(not self.lim)))
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_only_menus_show_for_restored_window_on_mouse_in_menu_area(self):
        """Restored windows should only show menus when the mouse is in the window
        menu area.
        """
        self.open_new_application_window("Calculator")
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_menus()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(not self.lim)))
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_only_menus_show_for_restored_window_on_mouse_in_grab_area(self):
        """Restored windows should only show menus when the mouse is in the panel
        grab area.
        """
        self.open_new_application_window("Calculator")
        self.sleep_menu_settle_period()

        if self.panel.grab_area.width <= 0:
            self.skipTest("Grab area is too small to run test!")

        self.panel.move_mouse_over_grab_area()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(not self.lim)))
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(False)))

    def test_hovering_over_indicators_does_not_show_app_menus(self):
        """Hovering the mouse over the indicators must not show app menus."""
        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_menus()
        # This assert is repeated from above, but we use it to make sure that
        # the menus are shown before we move over the indicators.
        self.assertThat(self.panel.menus_shown, Eventually(Equals(True)))

        self.panel.move_mouse_over_indicators()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))

    def test_menus_show_for_maximized_window_on_mouse_in_btn_area(self):
        """Menus and window buttons must be shown when the mouse is in the window
        button area for a maximised application.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_window_buttons()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))
        self.assertThat(self.panel.menus_shown, Eventually(Equals(True)))

    def test_menus_show_for_maximized_window_on_mouse_in_menu_area(self):
        """Menus and window buttons must be shown when the mouse is in the menu
        area for a maximised application.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_menus()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))
        self.assertThat(self.panel.menus_shown, Eventually(Equals(True)))

    def test_menus_show_for_maximized_window_on_mouse_in_grab_area(self):
        """Menus and window buttons must be shown when the mouse is in the grab
        area for a maximised application.
        """
        if self.panel.grab_area.width <= 0:
            self.skipTest("Grab area is too small to run this test!")

        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_grab_area()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))
        self.assertTrue(self.panel.menus_shown, Eventually(Equals(True)))

    def test_menus_and_btns_hidden_with_mouse_over_indicators(self):
        """Hovering the mouse over the indicators must hide the menus and window
        buttons.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_menus()
        # We use this assert to make sure that the menus are visible before we
        # move the mouse:
        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(True)))

        self.panel.move_mouse_over_indicators()

        self.assertThat(self.panel.window_buttons_shown, Eventually(Equals(self.lim)))
        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))

    def test_hovering_indicators_open_menus(self):
        """Opening an indicator entry, and then hovering on other entries must
        open them.
        """
        self.open_new_application_window("Text Editor", maximized=self.lim)
        entries = self.panel.get_indicator_entries(include_hidden_menus=True)

        self.assertThat(len(entries), GreaterThan(0))
        self.mouse_open_indicator(entries[0])

        for entry in entries:
            entry.mouse_move_to()
            self.assertThat(entry.active, Eventually(Equals(True)))
            self.assertThat(entry.menu_y, Eventually(NotEquals(0)))


class PanelMenuTests(PanelTestsBase):

    scenarios = _make_scenarios()

    def start_test_app_with_menus(self):
        window_spec = {
            "Title": "Test Application with Menus",
            "Menu": [
                {
                    "Title": "&File",
                    "Menu": ["Open", "Save", "Save As", "Quit"]
                },
                {"Title": "&Edit"},
                {"Title": "&Quit"}
                ]
        }
        test_win = self.launch_test_window(window_spec)
        test_win.set_focus()
        self.move_window_to_panel_monitor(test_win)
        self.ensure_window_state(test_win, maximized=self.lim)

        return test_win

    def test_menus_are_added_on_new_application(self):
        """Tests that menus are added when a new application is opened."""
        self.start_test_app_with_menus()

        refresh_fn = lambda: len(self.panel.menus.get_entries())
        self.assertThat(refresh_fn, Eventually(Equals(3)))

        menu_view = self.panel.menus
        self.assertThat(lambda: menu_view.get_menu_by_label("_File"), Eventually(NotEquals(None)))
        self.assertThat(lambda: menu_view.get_menu_by_label("_Edit"), Eventually(NotEquals(None)))
        self.assertThat(lambda: menu_view.get_menu_by_label("_Quit"), Eventually(NotEquals(None)))

    def test_menus_are_not_shown_if_the_application_has_no_menus(self):
        """Applications with no menus must not show menus in the panel."""

        test_win = self.launch_test_window()
        self.move_window_to_panel_monitor(test_win)

        self.assertThat(
            lambda: len(self.panel.menus.get_entries()),
            Eventually(Equals(0)),
            "Current panel entries are: %r" % self.panel.menus.get_entries())

        self.panel.move_mouse_over_grab_area()
        expected = test_win.application.name if not self.lim else ""
        self.assertThat(self.panel.title, Eventually(Equals(expected)))

    def test_menus_shows_when_new_application_is_opened(self):
        """When starting a new application, menus must first show, then hide."""
        if self.lim:
            self.skipTest("Menu discovery is disabled when LIM are enabled.")

        # This test requires the window to be opened on the monitor that is being tested and
        # we cannot guarantee which monitor the window will open up on.
        if self.panel_monitor > 0:
            self.skipTest("Cannot guarantee which monitor the window will open on.")

        self.start_test_app_with_menus()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(True)))
        self.sleep_menu_settle_period()
        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))

    def test_menus_dont_show_if_a_new_application_window_is_opened(self):
        """This tests the menu discovery feature on new window for a know application."""
        if self.lim:
            self.skipTest("Menu discovery is disabled when LIM are enabled.")

        self.open_new_application_window("Character Map")
        self.sleep_menu_settle_period()

        self.process_manager.start_app("Character Map")
        sleep(self.panel.menus.fadein_duration / 1000.0)
        # Not using Eventually here since this is time-critical. Need to work
        # out a better way to do this.
        self.assertThat(self.panel.menus_shown, Equals(False))

    def test_menus_dont_show_for_restored_window_on_mouse_out(self):
        """Restored window menus must not show when the mouse is outside the
        panel menu area.
        """
        self.open_new_application_window("Calculator")
        self.sleep_menu_settle_period()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))

    def test_menus_show_for_restored_window_on_mouse_in(self):
        """Restored window menus must show only when the mouse is over the panel
        menu area.
        """
        self.open_new_application_window("Calculator")
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_menus()

        self.assertThat(self.panel.menus_shown, Eventually(Equals(not self.lim)))

    def test_menus_dont_show_for_maximized_window_on_mouse_out(self):
        """Maximized window menus must not show when the mouse is outside the
        panel menu area.
        """
        self.open_new_application_window("Text Editor", maximized=True)

        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))

    def test_menus_show_for_maximized_window_on_mouse_in(self):
        """Maximized window menus must only show when the mouse is over the
        panel menu area.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        self.panel.move_mouse_over_menus()
        self.assertThat(self.panel.menus_shown, Eventually(Equals(True)))

    def test_menus_dont_show_with_dash(self):
        """Tests that menus are not showing when opening the dash."""
        self.open_new_application_window("Text Editor", maximized=True)
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))

    def test_menus_dont_show_with_hud(self):
        """Tests that menus are not showing when opening the HUD."""
        self.open_new_application_window("Character Map", maximized=True)
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.assertThat(self.panel.menus_shown, Eventually(Equals(False)))


class PanelIndicatorEntryTests(PanelTestsBase):
    """Tests for the indicator entries, including both menu and indicators."""

    scenarios = _make_scenarios()

    def open_app_and_get_menu_entry(self):
        """Open the test app and wait for the menu entry to appear."""
        self.open_new_application_window("Text Editor" if self.lim else "Calculator",
                                         maximized=self.lim)

        refresh_fn = lambda: len(self.panel.menus.get_entries())
        self.assertThat(refresh_fn, Eventually(GreaterThan(0)))
        menu_entry = self.panel.menus.get_entries()[0]
        return menu_entry

    def test_menu_opens_on_click(self):
        """Tests that clicking on a menu entry, opens a menu."""
        menu_entry = self.open_app_and_get_menu_entry()
        self.mouse_open_indicator(menu_entry)

        self.assertThat(menu_entry.active, Eventually(Equals(True)))
        self.assertThat(menu_entry.menu_x, Eventually(Equals(menu_entry.x)))
        self.assertThat(menu_entry.menu_y, Eventually(Equals(self.panel.height)))

    def test_menu_opens_closes_on_click(self):
        """Clicking on an open menu entru must close it again."""
        menu_entry = self.open_app_and_get_menu_entry()
        self.mouse_open_indicator(menu_entry)

        # This assert is for timing purposes only:
        self.assertThat(menu_entry.active, Eventually(Equals(True)))
        # Make sure we wait at least enough time that the menu appeared as well
        sleep(self.panel.menus.fadein_duration / 1000.0)
        self.mouse.click()

        self.assertThat(menu_entry.active, Eventually(Equals(False)))
        self.assertThat(menu_entry.menu_x, Eventually(Equals(0)))
        self.assertThat(menu_entry.menu_y, Eventually(Equals(0)))

    def test_menu_closes_on_click_outside(self):
        """Clicking outside an open menu must close it."""
        menu_entry = self.open_app_and_get_menu_entry()
        self.mouse_open_indicator(menu_entry)

        # This assert is for timing purposes only:
        self.assertThat(menu_entry.active, Eventually(Equals(True)))
        target_x = menu_entry.menu_x + menu_entry.menu_width/2
        target_y = menu_entry.menu_y + menu_entry.menu_height + 10
        self.mouse.move(target_x, target_y)
        self.mouse.click()

        self.assertThat(menu_entry.active, Eventually(Equals(False)))
        self.assertThat(menu_entry.menu_x, Eventually(Equals(0)))
        self.assertThat(menu_entry.menu_y, Eventually(Equals(0)))

    def test_menu_closes_on_new_focused_application(self):
        """When a new app is focused, open menu should be closed only when using Global Menus."""
        menu_entry = self.open_app_and_get_menu_entry()
        self.mouse_open_indicator(menu_entry)

        # This assert is for timing purposes only:
        self.assertThat(menu_entry.active, Eventually(Equals(True)))

        self.open_new_application_window("Text Editor")
        get_active_indicator_fn = lambda: self.unity.panels.get_active_indicator()
        self.assertThat(get_active_indicator_fn, Eventually(NotEquals(None) if self.lim else Equals(None)))

    def test_indicator_opens_when_dash_is_open(self):
        """When the dash is open and a click is on an indicator the dash
        must close and the indicator must open.
        """
        self.unity.dash.ensure_visible()

        indicator = self.panel.indicators.get_indicator_by_name_hint("indicator-session")
        self.mouse_open_indicator(indicator)

        self.assertThat(indicator.active, Eventually(Equals(True)))
        self.assertThat(self.unity.dash.visible, Eventually(Equals(False)))


class PanelKeyNavigationTests(PanelTestsBase):

    scenarios = _make_scenarios()

    def get_active_indicator(self):
        """Get the active indicator in a safe manner.

        This method will wait until the active indicator has been set.

        """
        get_active_indicator_fn = lambda: self.panel.get_active_indicator()
        self.assertThat(get_active_indicator_fn, Eventually(NotEquals(None)))
        return get_active_indicator_fn()

    def test_panel_first_menu_show_works(self):
        """Pressing the open-menus keybinding must open the first indicator."""
        self.open_new_application_window("Calculator")
        refresh_fn = lambda: len(self.panel.menus.get_entries())
        self.assertThat(refresh_fn, Eventually(Equals(0) if self.lim else GreaterThan(0)))
        self.addCleanup(self.keyboard.press_and_release, "Escape")
        self.keybinding("panel/open_first_menu")

        open_indicator = self.get_active_indicator()
        expected_indicator = self.panel.get_indicator_entries(include_hidden_menus=not self.lim)[0]
        self.assertThat(open_indicator.entry_id, Eventually(Equals(expected_indicator.entry_id)))

    def test_panel_hold_show_menu_works(self):
        """Holding the show menu key must reveal the menu with mnemonics."""
        self.open_new_application_window("Text Editor", maximized=self.lim)
        refresh_fn = lambda: len(self.panel.menus.get_entries())
        self.assertThat(refresh_fn, Eventually(GreaterThan(0)))
        self.addCleanup(self.keyboard.press_and_release, "Escape")

        # Wait for menu to fade out first
        self.assertThat(self.panel.menus.get_entries()[0].visible, Eventually(Equals(0)))

        self.keyboard.press("Alt")
        self.addCleanup(self.keyboard.release, "Alt")
        self.assertTrue(self.panel.menus.get_entries()[0].visible)
        self.assertThat(self.panel.menus.get_entries()[0].label, Equals("_File"))

    def test_panel_menu_accelerators_work(self):
        """Pressing a valid menu accelerator must open the correct menu item."""
        self.open_new_application_window("Text Editor", maximized=self.lim)
        refresh_fn = lambda: len(self.panel.menus.get_entries())
        self.assertThat(refresh_fn, Eventually(GreaterThan(0)))
        self.addCleanup(self.keyboard.press_and_release, "Escape")
        self.keyboard.press_and_release("Alt+f")

        open_indicator = self.get_active_indicator()
        self.assertThat(open_indicator.label, Eventually(Equals("_File")))

    def test_panel_indicators_key_navigation_next_works(self):
        """Right arrow key must open the next menu."""
        calc_win = self.open_new_application_window("Calculator")
        self.assertProperty(calc_win, is_focused=True)

        available_indicators = self.panel.get_indicator_entries(include_hidden_menus=not self.lim)

        self.keybinding("panel/open_first_menu")
        self.addCleanup(self.keyboard.press_and_release, "Escape")

        self.keybinding("panel/next_indicator")
        open_indicator = self.get_active_indicator()
        expected_indicator = available_indicators[1]
        self.assertThat(open_indicator.entry_id, Eventually(Equals(expected_indicator.entry_id)))

    def test_panel_indicators_key_navigation_prev_works(self):
        """Left arrow key must open the previous menu."""
        calc_win = self.open_new_application_window("Calculator")
        self.assertProperty(calc_win, is_focused=True)

        available_indicators = self.panel.get_indicator_entries(include_hidden_menus=not self.lim)

        self.keybinding("panel/open_first_menu")
        self.addCleanup(self.keyboard.press_and_release, "Escape")

        self.keybinding("panel/prev_indicator")
        open_indicator = self.get_active_indicator()
        expected_indicator = available_indicators[-1]

        self.assertThat(open_indicator.entry_id, Eventually(Equals(expected_indicator.entry_id)))

    def test_mouse_does_not_break_key_navigation(self):
        """Must be able to use the mouse to open indicators after they've been
        opened with the keyboard.
        """
        self.open_new_application_window("Calculator")
        available_indicators = self.panel.get_indicator_entries(include_hidden_menus=not self.lim)

        self.keybinding("panel/open_first_menu")
        self.addCleanup(self.keyboard.press_and_release, "Escape")

        available_indicators[2].mouse_move_to()
        self.assertThat(available_indicators[2].active, Eventually(Equals(True)))

        self.keybinding("panel/prev_indicator")
        self.assertThat(available_indicators[1].active, Eventually(Equals(True)))


class PanelGrabAreaTests(PanelTestsBase):
    """Panel grab area tests."""

    scenarios = _make_scenarios()

    def move_mouse_over_grab_area(self):
        self.panel.move_mouse_over_grab_area()
        sleep(.1)

    def test_unmaximize_from_grab_area_works(self):
        """Dragging a window down from the panel must unmaximize it."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        self.move_mouse_over_grab_area()
        self.mouse.press()
        self.panel.move_mouse_below_the_panel()
        self.mouse.release()

        self.assertProperty(text_win, is_maximized=False)

    def test_focus_the_maximized_window_works(self):
        """Clicking on the grab area must put a maximized window in focus."""
        text_win = self.open_new_application_window("Text Editor", maximized=True)
        calc_win = self.open_new_application_window("Calculator")

        self.assertProperty(text_win, is_focused=False)
        self.assertProperty(calc_win, is_focused=True)

        self.mouse.click_object(self.panel.grab_area, button=1)
        self.assertProperty(text_win, is_focused=True)

    def test_lower_the_maximized_window_works(self):
        """Middle-clicking on the panel grab area must lower a maximized window."""
        calc_win = self.open_new_application_window("Calculator")
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        self.assertProperty(text_win, is_focused=True)
        self.assertProperty(calc_win, is_focused=False)

        self.mouse.click_object(self.panel.grab_area, button=2)

        self.assertProperty(calc_win, is_focused=True)

    def test_panels_dont_steal_keynav_foucs_from_hud(self):
        """On a mouse click event on the panel you must still be able to type into the Hud."""
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        self.keyboard.type("Hello")
        self.mouse.click_object(self.panel.grab_area)
        self.keyboard.type("World")

        self.assertThat(self.unity.hud.search_string, Eventually(Equals("HelloWorld")))


class PanelLimTests(PanelTestsBase):

    scenarios = _make_monitor_scenarios()

    def setUp(self):
        self.lim = True
        super(PanelLimTests, self).setUp()

    def test_title_focus_on_maximized_state_changes(self):
        text_win = self.open_new_application_window("Text Editor", maximized=True)
        self.assertThat(self.panel.focused, Eventually(Equals(True)))
        self.assertThat(self.panel.title, Eventually(Equals(text_win.title)))

        self.open_new_application_window("Calculator")
        self.assertThat(self.panel.focused, Eventually(Equals(False)))
        self.assertThat(self.panel.title, Eventually(Equals(text_win.title)))

        text_win.set_focus()
        self.assertProperty(text_win, is_focused=True)
        self.assertThat(self.panel.focused, Eventually(Equals(True)))
        self.assertThat(self.panel.title, Eventually(Equals(text_win.title)))


class PanelCrossMonitorsTests(PanelTestsBase):
    """Multimonitor panel tests."""

    def setUp(self):
        super(PanelCrossMonitorsTests, self).setUp()
        if self.display.get_num_screens() < 2:
            self.skipTest("This test requires a multimonitor setup")

    def test_panel_title_updates_moving_window(self):
        """Panel must show the title of a restored window when moved to it's monitor."""
        calc_win = self.open_new_application_window("Calculator")

        prev_monitor = None
        for monitor in range(0, self.display.get_num_screens()):
            if calc_win.monitor != monitor:
                drag_window_to_screen(calc_win, monitor)

            if prev_monitor:
                prev_panel = self.unity.panels.get_panel_for_monitor(prev_monitor)
                self.assertThat(prev_panel.active, Eventually(Equals(False)))

            panel = self.unity.panels.get_panel_for_monitor(monitor)
            self.assertThat(panel.active, Eventually(Equals(True)))
            self.assertThat(panel.title, Eventually(Equals(calc_win.application.name)))

            prev_monitor = monitor

    def test_window_buttons_dont_show_for_maximized_window_on_mouse_in(self):
        """Window buttons must not show when the mouse is hovering the panel in
        other monitors.
        """
        self.open_new_application_window("Text Editor", maximized=True)
        self.sleep_menu_settle_period()

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)
            panel.move_mouse_over_window_buttons()

            self.sleep_menu_settle_period()

            if self.panel_monitor == monitor:
                self.assertThat(panel.window_buttons_shown, Eventually(Equals(True)))
            else:
                self.assertThat(panel.window_buttons_shown, Eventually(Equals(False)))

    def test_window_buttons_dont_show_in_other_monitors_when_dash_is_open(self):
        """Window buttons must not show on the panels other than the one where
        the dash is opened.
        """
        self.unity.dash.ensure_visible()
        self.addCleanup(self.unity.dash.ensure_hidden)

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if self.unity.dash.monitor == monitor:
                self.assertThat(self.unity.dash.view.overlay_window_buttons_shown[monitor], Equals(True))
            else:
                self.assertThat(self.unity.dash.view.overlay_window_buttons_shown[monitor], Equals(False))

    def test_window_buttons_dont_show_in_other_monitors_when_hud_is_open(self):
        """Window buttons must not show on the panels other than the one where
        the hud is opened.
        """
        self.unity.hud.ensure_visible()
        self.addCleanup(self.unity.hud.ensure_hidden)

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if self.unity.hud.monitor == monitor:
                self.assertThat(self.unity.hud.view.overlay_window_buttons_shown[monitor], Equals(True))
            else:
                self.assertThat(self.unity.hud.view.overlay_window_buttons_shown[monitor], Equals(False))

    def test_window_buttons_close_inactive_when_clicked_in_another_monitor(self):
        """Clicking the close button must not affect the active maximized window
        on another monitor.

        See bug #865701
        """
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        for monitor in range(self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if monitor != text_win.monitor:
                panel.window_buttons.close.mouse_move_to()
                panel.window_buttons.close.mouse_click()
                self.assertThat(text_win.closed, Equals(False))

    def test_window_buttons_minimize_inactive_when_clicked_in_another_monitor(self):
        """Clicking the minimise button must not affect the active maximized
        window on another monitor.

        See bug #865701
        """
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        for monitor in range(self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if monitor != text_win.monitor:
                panel.window_buttons.minimize.mouse_click()
                self.assertThat(text_win.is_hidden, Equals(False))

    def test_window_buttons_unmaximize_inactive_when_clicked_in_another_monitor(self):
        """Clicking the restore button must not affect the active maximized
        window on another monitor.

        See bug #865701
        """
        text_win = self.open_new_application_window("Text Editor", maximized=True)

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            if monitor != text_win.monitor:
                panel.window_buttons.unmaximize.mouse_click()
                self.assertThat(text_win.is_maximized, Equals(True))

    def test_hovering_indicators_on_multiple_monitors(self):
        """Opening an indicator entry and then hovering others entries must open them."""
        self.open_new_application_window("Text Editor")

        for monitor in range(0, self.display.get_num_screens()):
            panel = self.unity.panels.get_panel_for_monitor(monitor)

            indicator = panel.indicators.get_indicator_by_name_hint("indicator-session")
            self.mouse_open_indicator(indicator)

            entries = panel.get_indicator_entries(include_hidden_menus=True)
            self.assertThat(len(entries), GreaterThan(0))

            for entry in entries:
                entry.mouse_move_to()

                if monitor != self.panel_monitor and entry.type == "menu":
                    # we're on the "other" monitor, so the menu should be hidden.
                    self.assertThat(entry.active, Eventually(Equals(False)))
                    self.assertThat(entry.visible, Eventually(Equals(False)))
                    self.assertThat(entry.menu_y, Eventually(Equals(0)))
                else:
                    self.assertThat(entry.visible, Eventually(Equals(True)))
                    self.assertThat(entry.active, Eventually(Equals(True)))
                    self.assertThat(entry.menu_y, Eventually(NotEquals(0)))

            # Close the last indicator on the monitor
            self.mouse.click()