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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <set>
#include <vector>
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "components/services/app_service/public/cpp/instance.h"
#include "components/services/app_service/public/cpp/instance_registry.h"
#include "components/services/app_service/public/cpp/instance_update.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
class InstanceRegistryTest : public testing::Test,
public apps::InstanceRegistry::Observer {
protected:
void MakeInstanceWithWindow(
const char* app_id,
aura::Window* window,
apps::InstanceState state = apps::InstanceState::kUnknown,
base::Time time = base::Time()) {
MakeInstance(app_id, window, state, time);
}
void MakeInstance(const char* app_id,
aura::Window* window,
apps::InstanceState state = apps::InstanceState::kUnknown,
base::Time time = base::Time()) {
apps::InstanceParams params(app_id, window);
params.state = std::make_pair(state, time);
instance_registry_.CreateOrUpdateInstance(std::move(params));
}
void CallForEachInstance(apps::InstanceRegistry& instance_registry) {
instance_registry.ForEachInstance(
[this](const apps::InstanceUpdate& update) {
OnInstanceUpdate(update);
});
}
std::set<apps::InstanceState> GetStates(const std::string& app_id,
const aura::Window* window) {
std::set<apps::InstanceState> states;
instance_registry_.ForInstancesWithWindow(
window, [&](const apps::InstanceUpdate& update) {
if (update.AppId() == app_id) {
states.insert(update.State());
}
});
return states;
}
// apps::InstanceRegistry::Observer overrides.
void OnInstanceUpdate(const apps::InstanceUpdate& update) override {
EXPECT_NE("", update.AppId());
if (update.StateChanged() &&
update.State() == apps::InstanceState::kRunning) {
num_running_apps_++;
}
updated_ids_.insert(update.AppId());
updated_enclosing_windows_.insert(update.Window());
}
void OnInstanceRegistryWillBeDestroyed(
apps::InstanceRegistry* instance_registry) override {
// The test code explicitly calls both AddObserver and RemoveObserver.
NOTREACHED();
}
apps::InstanceRegistry& instance_registry() { return instance_registry_; }
int num_running_apps_ = 0;
std::set<std::string> updated_ids_;
std::set<raw_ptr<const aura::Window, SetExperimental>>
updated_enclosing_windows_;
apps::InstanceRegistry instance_registry_;
};
// In the tests below, just "recursive" means that instance_registry.OnInstance
// calls observer.OnInstanceUpdate which calls instance_registry.ForEachInstance
// and instance_registry.ForOneInstance. "Super-recursive" means that
// instance_registry.OnInstance calls observer.OnInstanceUpdate calls
// instance_registry.OnInstance which calls observer.OnInstanceUpdate.
class InstanceRecursiveObserver : public apps::InstanceRegistry::Observer {
public:
explicit InstanceRecursiveObserver(apps::InstanceRegistry* instance_registry)
: instance_registry_(instance_registry) {
instance_registry_observation_.Observe(instance_registry);
}
~InstanceRecursiveObserver() override = default;
void PrepareParamsForOnInstances(
int expected_num_instances,
std::vector<std::unique_ptr<apps::InstanceParams>>*
super_recursive_instance_params = nullptr) {
expected_num_instances_ = expected_num_instances;
num_instances_seen_on_instance_update_ = 0;
if (super_recursive_instance_params) {
super_recursive_instance_params_.swap(*super_recursive_instance_params);
}
}
void PrepareForOnInstances(int expected_num_instances,
std::vector<std::unique_ptr<apps::Instance>>*
super_recursive_instances = nullptr) {
expected_num_instances_ = expected_num_instances;
num_instances_seen_on_instance_update_ = 0;
if (super_recursive_instances) {
super_recursive_instances_.swap(*super_recursive_instances);
}
}
int NumInstancesSeenOnInstanceUpdate() {
return num_instances_seen_on_instance_update_;
}
protected:
// apps::InstanceRegistry::Observer overrides.
void OnInstanceUpdate(const apps::InstanceUpdate& outer) override {
int num_instance = 0;
instance_registry_->ForEachInstance(
[&outer, &num_instance](const apps::InstanceUpdate& inner) {
if (outer.InstanceId() == inner.InstanceId()) {
ExpectEq(outer, inner);
}
num_instance++;
});
EXPECT_TRUE(instance_registry_->ForOneInstance(
outer.InstanceId(), [&outer](const apps::InstanceUpdate& inner) {
ExpectEq(outer, inner);
}));
EXPECT_TRUE(instance_registry_->ForInstancesWithWindow(
outer.Window(), [&outer](const apps::InstanceUpdate& inner) {
if (outer.InstanceId() == inner.InstanceId()) {
ExpectEq(outer, inner);
}
}));
if (expected_num_instances_ >= 0) {
EXPECT_EQ(expected_num_instances_, num_instance);
}
while (!super_recursive_instance_params_.empty()) {
std::unique_ptr<apps::InstanceParams> params =
std::move(super_recursive_instance_params_.back());
if (params.get() == nullptr) {
// This is the placeholder 'punctuation'.
super_recursive_instance_params_.pop_back();
break;
}
instance_registry_->CreateOrUpdateInstance(std::move(*params));
super_recursive_instance_params_.pop_back();
}
while (!super_recursive_instances_.empty()) {
std::unique_ptr<apps::Instance> instance =
std::move(super_recursive_instances_.back());
if (instance.get() == nullptr) {
// This is the placeholder 'punctuation'.
super_recursive_instances_.pop_back();
break;
}
instance_registry_->OnInstance(std::move(instance));
super_recursive_instances_.pop_back();
}
num_instances_seen_on_instance_update_++;
}
void OnInstanceRegistryWillBeDestroyed(
apps::InstanceRegistry* instance_registry) override {
instance_registry_observation_.Reset();
}
static void ExpectEq(const apps::InstanceUpdate& outer,
const apps::InstanceUpdate& inner) {
EXPECT_EQ(outer.AppId(), inner.AppId());
EXPECT_EQ(outer.InstanceId(), inner.InstanceId());
EXPECT_EQ(outer.Window(), inner.Window());
EXPECT_EQ(outer.LaunchId(), inner.LaunchId());
EXPECT_EQ(outer.State(), inner.State());
EXPECT_EQ(outer.LastUpdatedTime(), inner.LastUpdatedTime());
EXPECT_EQ(outer.BrowserContext(), inner.BrowserContext());
}
raw_ptr<apps::InstanceRegistry> instance_registry_;
int expected_num_instances_ = -1;
int num_instances_seen_on_instance_update_ = 0;
// Non-empty when this.OnInstanceUpdate should trigger more
// instance_registry_.OnInstance calls.
//
// During OnInstanceUpdate, this vector (a stack) is popped from the back
// until a nullptr 'punctuation' element (a group terminator) is seen. If that
// group of popped elements (in LIFO order) is non-empty, that group forms the
// vector of App's passed to instance_registry_.OnInstance.
std::vector<std::unique_ptr<apps::InstanceParams>>
super_recursive_instance_params_;
std::vector<std::unique_ptr<apps::Instance>> super_recursive_instances_;
base::ScopedObservation<apps::InstanceRegistry,
apps::InstanceRegistry::Observer>
instance_registry_observation_{this};
};
TEST_F(InstanceRegistryTest, ForEachInstance) {
updated_enclosing_windows_.clear();
updated_ids_.clear();
CallForEachInstance(instance_registry());
EXPECT_EQ(0u, updated_enclosing_windows_.size());
EXPECT_EQ(0u, updated_ids_.size());
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("a", &window1);
MakeInstanceWithWindow("b", &window2);
MakeInstanceWithWindow("c", &window3);
updated_enclosing_windows_.clear();
updated_ids_.clear();
CallForEachInstance(instance_registry());
EXPECT_EQ(3u, updated_enclosing_windows_.size());
EXPECT_EQ(3u, updated_ids_.size());
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window1));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window2));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window3));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("a"));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("b"));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("c"));
aura::Window window4(nullptr);
window4.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("a", &window1, apps::InstanceState::kRunning);
MakeInstanceWithWindow("c", &window4);
updated_enclosing_windows_.clear();
updated_ids_.clear();
CallForEachInstance(instance_registry());
EXPECT_EQ(4u, updated_enclosing_windows_.size());
EXPECT_EQ(3u, updated_ids_.size());
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window1));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window2));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window3));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window4));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("a"));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("b"));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("c"));
// Test that ForOneApp succeeds for window4 and fails for window5.
bool found_window4 = false;
EXPECT_TRUE(instance_registry().ForInstancesWithWindow(
&window4, [&found_window4](const apps::InstanceUpdate& update) {
found_window4 = true;
EXPECT_EQ("c", update.AppId());
}));
EXPECT_TRUE(found_window4);
bool found_window5 = false;
aura::Window window5(nullptr);
window5.Init(ui::LAYER_NOT_DRAWN);
EXPECT_FALSE(instance_registry().ForInstancesWithWindow(
&window5, [&found_window5](const apps::InstanceUpdate& update) {
found_window5 = true;
}));
EXPECT_FALSE(found_window5);
}
TEST_F(InstanceRegistryTest, Observer) {
instance_registry().AddObserver(this);
num_running_apps_ = 0;
updated_enclosing_windows_.clear();
updated_ids_.clear();
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("a", &window1);
MakeInstanceWithWindow("c", &window2);
MakeInstanceWithWindow("a", &window3);
EXPECT_EQ(0, num_running_apps_);
EXPECT_EQ(3u, updated_enclosing_windows_.size());
EXPECT_EQ(2u, updated_ids_.size());
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window1));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window2));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window3));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("a"));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("c"));
num_running_apps_ = 0;
updated_ids_.clear();
aura::Window window4(nullptr);
window4.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("b", &window4);
MakeInstanceWithWindow("c", &window2, apps::InstanceState::kRunning);
EXPECT_EQ(1, num_running_apps_);
EXPECT_EQ(2u, updated_ids_.size());
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window2));
EXPECT_NE(updated_enclosing_windows_.end(),
updated_enclosing_windows_.find(&window4));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("b"));
EXPECT_NE(updated_ids_.end(), updated_ids_.find("c"));
instance_registry().RemoveObserver(this);
num_running_apps_ = 0;
updated_enclosing_windows_.clear();
updated_ids_.clear();
aura::Window window5(nullptr);
window5.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("f", &window5, apps::InstanceState::kRunning);
EXPECT_EQ(0, num_running_apps_);
EXPECT_EQ(0u, updated_enclosing_windows_.size());
EXPECT_EQ(0u, updated_ids_.size());
}
TEST_F(InstanceRegistryTest, WholeProcessForOneWindow) {
InstanceRecursiveObserver observer(&instance_registry());
apps::InstanceState instance_state = apps::InstanceState::kStarted;
aura::Window window(nullptr);
window.Init(ui::LAYER_NOT_DRAWN);
observer.PrepareForOnInstances(1);
MakeInstanceWithWindow("p", &window, instance_state);
// Instance is updated twice, for creation and state modification.
EXPECT_EQ(1, observer.NumInstancesSeenOnInstanceUpdate());
instance_state = static_cast<apps::InstanceState>(
instance_state | apps::InstanceState::kRunning |
apps::InstanceState::kActive | apps::InstanceState::kVisible);
observer.PrepareForOnInstances(1);
MakeInstanceWithWindow("p", &window, instance_state);
EXPECT_EQ(1, observer.NumInstancesSeenOnInstanceUpdate());
apps::InstanceState state1 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState state2 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState state3 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState state4 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning |
apps::InstanceState::kActive | apps::InstanceState::kVisible);
apps::InstanceState state5 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning |
apps::InstanceState::kVisible);
apps::InstanceState state6 = apps::InstanceState::kDestroyed;
observer.PrepareForOnInstances(1);
MakeInstanceWithWindow("p", &window, state1);
MakeInstanceWithWindow("p", &window, state2);
MakeInstanceWithWindow("p", &window, state3);
MakeInstanceWithWindow("p", &window, state4);
MakeInstanceWithWindow("p", &window, state5);
MakeInstanceWithWindow("p", &window, state6);
// OnInstanceUpdate is called for state1, because state1 is different with
// previous instance_state. state2 and state3 is not changed, so
// OnInstanceUpdate is not called. OnInstanceUpdate is called for state4,
// state5, and state6 separately, because they are different. So
// OnInstanceUpdate is called 4 times, for state1, state4, state5, and state6.
EXPECT_EQ(4, observer.NumInstancesSeenOnInstanceUpdate());
EXPECT_FALSE(instance_registry().ContainsAppId("p"));
bool found_window = false;
EXPECT_FALSE(instance_registry().ForInstancesWithWindow(
&window, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_FALSE(found_window);
observer.PrepareForOnInstances(1);
MakeInstanceWithWindow("p", &window, state5);
EXPECT_EQ(1, observer.NumInstancesSeenOnInstanceUpdate());
found_window = false;
EXPECT_TRUE(instance_registry().ForInstancesWithWindow(
&window, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_TRUE(found_window);
}
TEST_F(InstanceRegistryTest, Recursive) {
InstanceRecursiveObserver observer(&instance_registry());
apps::InstanceState instance_state1 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState instance_state2 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
observer.PrepareForOnInstances(-1);
MakeInstanceWithWindow("o", &window1, instance_state1);
MakeInstanceWithWindow("p", &window2, instance_state2);
EXPECT_EQ(2, observer.NumInstancesSeenOnInstanceUpdate());
apps::InstanceState instance_state3 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState instance_state4 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
std::vector<apps::InstanceState> latest_state;
latest_state.push_back(instance_state3);
latest_state.push_back(instance_state3);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
aura::Window window4(nullptr);
window4.Init(ui::LAYER_NOT_DRAWN);
observer.PrepareForOnInstances(-1);
MakeInstanceWithWindow("p", &window2, instance_state3);
MakeInstanceWithWindow("q", &window3, instance_state4);
MakeInstanceWithWindow("p", &window4, instance_state3);
EXPECT_EQ(2, observer.NumInstancesSeenOnInstanceUpdate());
apps::InstanceState instance_state5 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState instance_state6 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning);
apps::InstanceState instance_state7 = static_cast<apps::InstanceState>(
apps::InstanceState::kStarted | apps::InstanceState::kRunning |
apps::InstanceState::kActive);
observer.PrepareForOnInstances(4);
MakeInstanceWithWindow("p", &window2, instance_state5);
MakeInstanceWithWindow("p", &window2, instance_state6);
MakeInstanceWithWindow("p", &window2, instance_state7);
EXPECT_EQ(1, observer.NumInstancesSeenOnInstanceUpdate());
apps::InstanceState instance_state8 =
static_cast<apps::InstanceState>(apps::InstanceState::kDestroyed);
observer.PrepareForOnInstances(-1);
MakeInstanceWithWindow("p", &window2, instance_state8);
MakeInstanceWithWindow("p", &window4, instance_state8);
MakeInstanceWithWindow("q", &window3, instance_state8);
MakeInstanceWithWindow("o", &window1, instance_state8);
EXPECT_EQ(4, observer.NumInstancesSeenOnInstanceUpdate());
EXPECT_FALSE(instance_registry().ContainsAppId("o"));
EXPECT_FALSE(instance_registry().ContainsAppId("p"));
EXPECT_FALSE(instance_registry().ContainsAppId("q"));
bool found_window = false;
EXPECT_FALSE(instance_registry().ForInstancesWithWindow(
&window2, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_FALSE(found_window);
found_window = false;
EXPECT_FALSE(instance_registry().ForInstancesWithWindow(
&window4, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_FALSE(found_window);
found_window = false;
EXPECT_FALSE(instance_registry().ForInstancesWithWindow(
&window3, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_FALSE(found_window);
found_window = false;
EXPECT_FALSE(instance_registry().ForInstancesWithWindow(
&window1, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_FALSE(found_window);
observer.PrepareForOnInstances(1);
MakeInstanceWithWindow("p", &window2, instance_state7);
EXPECT_EQ(1, observer.NumInstancesSeenOnInstanceUpdate());
found_window = false;
EXPECT_TRUE(instance_registry().ForInstancesWithWindow(
&window2, [&found_window](const apps::InstanceUpdate& update) {
found_window = true;
}));
EXPECT_TRUE(found_window);
}
TEST_F(InstanceRegistryTest, SuperRecursive) {
InstanceRecursiveObserver observer(&instance_registry());
// Set up a series of OnInstance to be called during
// observer.OnInstanceUpdate:
// - the 1st update is {'a', &window2, kActive}.
// - the 2nd update is {'b', &window3, kActive}.
// - the 3rd update is {'c', &window4, kActive}.
// - the 4th update is {'b', &window5, kVisible}.
// - the 5th update is {'c', &window4, kVisible}.
// - the 6td update is {'b', &window3, kRunning}.
// - the 7th update is {'a', &window2, kRunning}.
// - the 8th update is {'a', &window2, kDestroyed}.
// - the 9th update is {'a', &window2, kUnknown}.
// - the 10th update is {'a', &window1, kStarted}.
//
// The vector is processed in LIFO order with nullptr punctuation to
// terminate each group. See the comment on the
// RecursiveObserver::super_recursive_apps_ field.
std::vector<std::unique_ptr<apps::InstanceParams>> super_recursive_apps;
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
aura::Window window4(nullptr);
window4.Init(ui::LAYER_NOT_DRAWN);
aura::Window window5(nullptr);
window5.Init(ui::LAYER_NOT_DRAWN);
super_recursive_apps.push_back(nullptr);
std::unique_ptr<apps::InstanceParams> params =
std::make_unique<apps::InstanceParams>("a", &window1);
params->state = std::make_pair(apps::InstanceState::kStarted, base::Time());
super_recursive_apps.push_back(std::move(params));
super_recursive_apps.push_back(nullptr);
super_recursive_apps.push_back(nullptr);
super_recursive_apps.push_back(
std::make_unique<apps::InstanceParams>("a", &window2));
super_recursive_apps.push_back(nullptr);
super_recursive_apps.push_back(
std::make_unique<apps::InstanceParams>("b", &window3));
params = std::make_unique<apps::InstanceParams>("a", &window2);
params->state = std::make_pair(apps::InstanceState::kDestroyed, base::Time());
super_recursive_apps.push_back(std::move(params));
params = std::make_unique<apps::InstanceParams>("a", &window2);
params->state = std::make_pair(apps::InstanceState::kRunning, base::Time());
super_recursive_apps.push_back(std::move(params));
params = std::make_unique<apps::InstanceParams>("b", &window3);
params->state = std::make_pair(apps::InstanceState::kRunning, base::Time());
super_recursive_apps.push_back(std::move(params));
super_recursive_apps.push_back(nullptr);
super_recursive_apps.push_back(nullptr);
params = std::make_unique<apps::InstanceParams>("c", &window4);
params->state = std::make_pair(apps::InstanceState::kVisible, base::Time());
super_recursive_apps.push_back(std::move(params));
params = std::make_unique<apps::InstanceParams>("b", &window5);
params->state = std::make_pair(apps::InstanceState::kVisible, base::Time());
super_recursive_apps.push_back(std::move(params));
params = std::make_unique<apps::InstanceParams>("a", &window2);
params->state = std::make_pair(apps::InstanceState::kActive, base::Time());
MakeInstanceWithWindow("a", &window2, apps::InstanceState::kActive);
params = std::make_unique<apps::InstanceParams>("b", &window3);
params->state = std::make_pair(apps::InstanceState::kActive, base::Time());
MakeInstanceWithWindow("b", &window3, apps::InstanceState::kActive);
observer.PrepareParamsForOnInstances(-1, &super_recursive_apps);
params = std::make_unique<apps::InstanceParams>("c", &window4);
params->state = std::make_pair(apps::InstanceState::kActive, base::Time());
MakeInstanceWithWindow("c", &window4, apps::InstanceState::kActive);
EXPECT_EQ(8, observer.NumInstancesSeenOnInstanceUpdate());
// After all of that, check that for each window, the last delta won.
EXPECT_EQ(apps::InstanceState::kStarted,
instance_registry().GetState(&window1));
EXPECT_EQ(apps::InstanceState::kUnknown,
instance_registry().GetState(&window2));
EXPECT_EQ(apps::InstanceState::kRunning,
instance_registry().GetState(&window3));
EXPECT_EQ(apps::InstanceState::kVisible,
instance_registry().GetState(&window4));
EXPECT_EQ(apps::InstanceState::kVisible,
instance_registry().GetState(&window5));
EXPECT_TRUE(instance_registry().Exists(&window1));
EXPECT_TRUE(instance_registry().Exists(&window2));
EXPECT_TRUE(instance_registry().Exists(&window3));
EXPECT_TRUE(instance_registry().Exists(&window4));
EXPECT_TRUE(instance_registry().Exists(&window5));
}
TEST_F(InstanceRegistryTest, GetInstances) {
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("a", &window1);
MakeInstanceWithWindow("b", &window2);
MakeInstanceWithWindow("a", &window3);
auto instances = instance_registry().GetInstances("a");
EXPECT_EQ(2U, instances.size());
std::set<aura::Window*> windows;
for (const apps::Instance* instance : instances) {
EXPECT_EQ("a", instance->AppId());
windows.insert(instance->Window());
}
EXPECT_TRUE(base::Contains(windows, &window1));
EXPECT_TRUE(base::Contains(windows, &window3));
instances = instance_registry().GetInstances("b");
EXPECT_EQ(1U, instances.size());
auto it = instances.begin();
EXPECT_EQ("b", (*it)->AppId());
EXPECT_EQ(&window2, (*it)->Window());
MakeInstanceWithWindow("a", &window1, apps::InstanceState::kDestroyed);
MakeInstanceWithWindow("b", &window2, apps::InstanceState::kDestroyed);
instances = instance_registry().GetInstances("a");
EXPECT_EQ(1U, instances.size());
it = instances.begin();
EXPECT_EQ("a", (*it)->AppId());
EXPECT_EQ(&window3, (*it)->Window());
instances = instance_registry().GetInstances("b");
EXPECT_TRUE(instances.empty());
}
TEST_F(InstanceRegistryTest, ContainsAppId) {
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
MakeInstanceWithWindow("a", &window1);
MakeInstanceWithWindow("b", &window2);
MakeInstanceWithWindow("a", &window3);
EXPECT_TRUE(instance_registry().ContainsAppId("a"));
EXPECT_TRUE(instance_registry().ContainsAppId("b"));
EXPECT_FALSE(instance_registry().ContainsAppId("c"));
MakeInstanceWithWindow("a", &window1, apps::InstanceState::kDestroyed);
MakeInstanceWithWindow("b", &window2, apps::InstanceState::kDestroyed);
EXPECT_TRUE(instance_registry().ContainsAppId("a"));
EXPECT_FALSE(instance_registry().ContainsAppId("b"));
}
TEST_F(InstanceRegistryTest, WindowIsChanged) {
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
auto instance_id1 = base::UnguessableToken::Create();
auto delta = std::make_unique<apps::Instance>("a", instance_id1, &window1);
instance_registry().OnInstance(std::move(delta));
EXPECT_TRUE(instance_registry().Exists(&window1));
EXPECT_EQ(1U, instance_registry().GetInstances("a").size());
// Modify window for the instance of `a`.
delta = std::make_unique<apps::Instance>("a", instance_id1, &window2);
instance_registry().OnInstance(std::move(delta));
EXPECT_FALSE(instance_registry().Exists(&window1));
EXPECT_TRUE(instance_registry().Exists(&window2));
EXPECT_EQ(1U, instance_registry().GetInstances("a").size());
// Create an instance of `window2` for `b`.
auto instance_id2 = base::UnguessableToken::Create();
delta = std::make_unique<apps::Instance>("b", instance_id2, &window2);
instance_registry().OnInstance(std::move(delta));
EXPECT_TRUE(instance_registry().Exists(&window2));
EXPECT_EQ(1U, instance_registry().GetInstances("b").size());
// Close window for the instance of `a`.
delta = std::make_unique<apps::Instance>("a", instance_id1, &window2);
delta->UpdateState(apps::InstanceState::kDestroyed, base::Time());
instance_registry().OnInstance(std::move(delta));
EXPECT_TRUE(instance_registry().Exists(&window2));
EXPECT_TRUE(instance_registry().GetInstances("a").empty());
// Close window for the instance of `b`.
delta = std::make_unique<apps::Instance>("b", instance_id2, &window2);
delta->UpdateState(apps::InstanceState::kDestroyed, base::Time());
instance_registry().OnInstance(std::move(delta));
EXPECT_FALSE(instance_registry().Exists(&window2));
EXPECT_TRUE(instance_registry().GetInstances("b").empty());
}
TEST_F(InstanceRegistryTest, SuperRecursiveWithWindowChanged) {
InstanceRecursiveObserver observer(&instance_registry());
// Set up a series of OnInstance to be called during
// observer.OnInstanceUpdate:
// - the 1st update is {'a', id1, &window2, kActive}.
// - the 2nd update is {'b', id2, &window3, kActive}.
// - the 3rd update is {'c', id3, &window4, kActive}.
// - the 4th update is {'b', id4, &window3, kVisible}.
// - the 5th update is {'c', id3, &window3, kVisible}.
// - the 6td update is {'b', id2, &window3, kDestroyed}.
// - the 7th update is {'a', id1, &window5, kRunning}.
// - the 8th update is {'a', id1, &window5, kDestroyed}.
// - the 9th update is {'a', id5, &window2, kUnknown}.
// - the 10th update is {'a', id6, &window1, kStarted}.
//
// The vector is processed in LIFO order with nullptr punctuation to
// terminate each group. See the comment on the
// RecursiveObserver::super_recursive_apps_ field.
aura::Window window1(nullptr);
window1.Init(ui::LAYER_NOT_DRAWN);
aura::Window window2(nullptr);
window2.Init(ui::LAYER_NOT_DRAWN);
aura::Window window3(nullptr);
window3.Init(ui::LAYER_NOT_DRAWN);
aura::Window window4(nullptr);
window4.Init(ui::LAYER_NOT_DRAWN);
aura::Window window5(nullptr);
window5.Init(ui::LAYER_NOT_DRAWN);
auto instance_id1 = base::UnguessableToken::Create();
auto instance_id2 = base::UnguessableToken::Create();
auto instance_id3 = base::UnguessableToken::Create();
auto instance_id4 = base::UnguessableToken::Create();
auto instance_id5 = base::UnguessableToken::Create();
auto instance_id6 = base::UnguessableToken::Create();
std::vector<std::unique_ptr<apps::Instance>> super_recursive_apps;
super_recursive_apps.push_back(nullptr);
super_recursive_apps.push_back(nullptr);
// the 10th update is {'a', id6, &window1, kStarted}.
auto delta = std::make_unique<apps::Instance>("a", instance_id6, &window1);
delta->UpdateState(apps::InstanceState::kStarted, base::Time());
super_recursive_apps.push_back(std::move(delta));
delta = std::make_unique<apps::Instance>("a", instance_id6, &window1);
delta->UpdateState(apps::InstanceState::kDestroyed, base::Time());
super_recursive_apps.push_back(std::move(delta));
super_recursive_apps.push_back(nullptr);
// The 9th update is {'a', id5, &window2, kUnknown}.
super_recursive_apps.push_back(
std::make_unique<apps::Instance>("a", instance_id5, &window2));
// The 8th update is {'a', id1, &window5, kDestroyed}.
delta = std::make_unique<apps::Instance>("a", instance_id1, &window5);
delta->UpdateState(apps::InstanceState::kDestroyed, base::Time());
super_recursive_apps.push_back(std::move(delta));
// The 7th update is {'a', id1, &window5, kRunning}.
delta = std::make_unique<apps::Instance>("a", instance_id1, &window5);
delta->UpdateState(apps::InstanceState::kRunning, base::Time());
super_recursive_apps.push_back(std::move(delta));
// The 6td update is {'b', id2, &window3, kDestroyed}.
delta = std::make_unique<apps::Instance>("b", instance_id2, &window3);
delta->UpdateState(apps::InstanceState::kDestroyed, base::Time());
super_recursive_apps.push_back(std::move(delta));
super_recursive_apps.push_back(nullptr);
super_recursive_apps.push_back(nullptr);
// The 5th update is {'c', id3, &window3, kVisible}.
delta = std::make_unique<apps::Instance>("c", instance_id3, &window3);
delta->UpdateState(apps::InstanceState::kVisible, base::Time());
super_recursive_apps.push_back(std::move(delta));
// The 4th update is {'b', id4, &window3, kVisible}.
delta = std::make_unique<apps::Instance>("b", instance_id4, &window3);
delta->UpdateState(apps::InstanceState::kVisible, base::Time());
super_recursive_apps.push_back(std::move(delta));
// The 1st update is {'a', id1, &window2, kActive}
delta = std::make_unique<apps::Instance>("a", instance_id1, &window2);
delta->UpdateState(apps::InstanceState::kActive, base::Time());
instance_registry().OnInstance(std::move(delta));
// The 2nd update is {'b', id2, &window3, kActive}.
delta = std::make_unique<apps::Instance>("b", instance_id2, &window3);
delta->UpdateState(apps::InstanceState::kActive, base::Time());
instance_registry().OnInstance(std::move(delta));
// The 3rd update is {'c', id3, &window4, kActive}.
delta = std::make_unique<apps::Instance>("c", instance_id3, &window4);
delta->UpdateState(apps::InstanceState::kActive, base::Time());
observer.PrepareForOnInstances(-1, &super_recursive_apps);
instance_registry().OnInstance(std::move(delta));
EXPECT_EQ(8, observer.NumInstancesSeenOnInstanceUpdate());
// After all of that, check that for each window, the last delta won.
auto states = GetStates("a", &window1);
EXPECT_EQ(1U, states.size());
EXPECT_EQ(apps::InstanceState::kStarted, *states.begin());
states = GetStates("a", &window2);
EXPECT_EQ(1U, states.size());
EXPECT_EQ(apps::InstanceState::kUnknown, *states.begin());
EXPECT_EQ(2U, instance_registry().GetInstances("a").size());
states = GetStates("b", &window3);
EXPECT_EQ(1U, states.size());
EXPECT_EQ(apps::InstanceState::kVisible, *states.begin());
EXPECT_EQ(1U, instance_registry().GetInstances("b").size());
states = GetStates("c", &window3);
EXPECT_EQ(1U, states.size());
EXPECT_EQ(apps::InstanceState::kVisible, *states.begin());
EXPECT_EQ(1U, instance_registry().GetInstances("c").size());
EXPECT_TRUE(GetStates("c", &window4).empty());
EXPECT_TRUE(GetStates("a", &window5).empty());
EXPECT_TRUE(instance_registry().Exists(&window1));
EXPECT_TRUE(instance_registry().Exists(&window2));
EXPECT_TRUE(instance_registry().Exists(&window3));
EXPECT_FALSE(instance_registry().Exists(&window4));
EXPECT_FALSE(instance_registry().Exists(&window5));
}
|