window_occlusion_tracker_unittest.cc 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/aura/window_occlusion_tracker.h"
  5. #include "base/callback_helpers.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/bind.h"
  9. #include "base/test/gtest_util.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "build/build_config.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "ui/aura/env.h"
  14. #include "ui/aura/test/aura_test_base.h"
  15. #include "ui/aura/test/test_window_delegate.h"
  16. #include "ui/aura/test/test_windows.h"
  17. #include "ui/aura/test/window_occlusion_tracker_test_api.h"
  18. #include "ui/aura/window_observer.h"
  19. #include "ui/base/ui_base_features.h"
  20. #include "ui/compositor/layer.h"
  21. #include "ui/compositor/layer_animation_observer.h"
  22. #include "ui/compositor/layer_animation_sequence.h"
  23. #include "ui/compositor/layer_animator.h"
  24. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  25. #include "ui/compositor/scoped_layer_animation_settings.h"
  26. #include "ui/compositor/test/layer_animator_test_controller.h"
  27. #include "ui/gfx/interpolated_transform.h"
  28. namespace aura {
  29. namespace {
  30. constexpr base::TimeDelta kTransitionDuration = base::Seconds(3);
  31. class MockWindowDelegate : public test::ColorTestWindowDelegate {
  32. public:
  33. MockWindowDelegate() : test::ColorTestWindowDelegate(SK_ColorWHITE) {}
  34. MockWindowDelegate(const MockWindowDelegate&) = delete;
  35. MockWindowDelegate& operator=(const MockWindowDelegate&) = delete;
  36. ~MockWindowDelegate() override { EXPECT_FALSE(is_expecting_call()); }
  37. void set_window(Window* window) { window_ = window; }
  38. Window* window() { return window_; }
  39. void SetName(const std::string& name) { window_->SetName(name); }
  40. void set_expectation(Window::OcclusionState occlusion_state,
  41. SkRegion occluded_region = SkRegion()) {
  42. expected_occlusion_state_ = occlusion_state;
  43. expected_occluded_region_ = occluded_region;
  44. }
  45. bool is_expecting_call() const {
  46. return expected_occlusion_state_ != Window::OcclusionState::UNKNOWN;
  47. }
  48. void OnWindowOcclusionChanged(
  49. Window::OcclusionState occlusion_state) override {
  50. SCOPED_TRACE(window_->GetName());
  51. ASSERT_TRUE(window_);
  52. EXPECT_NE(occlusion_state, Window::OcclusionState::UNKNOWN);
  53. EXPECT_EQ(occlusion_state, expected_occlusion_state_);
  54. EXPECT_EQ(window_->occluded_region_in_root(), expected_occluded_region_);
  55. expected_occlusion_state_ = Window::OcclusionState::UNKNOWN;
  56. expected_occluded_region_ = SkRegion();
  57. }
  58. private:
  59. Window::OcclusionState expected_occlusion_state_ =
  60. Window::OcclusionState::UNKNOWN;
  61. SkRegion expected_occluded_region_ = SkRegion();
  62. raw_ptr<Window> window_ = nullptr;
  63. };
  64. class WindowOcclusionTrackerTest : public test::AuraTestBase {
  65. public:
  66. WindowOcclusionTrackerTest() = default;
  67. WindowOcclusionTrackerTest(const WindowOcclusionTrackerTest&) = delete;
  68. WindowOcclusionTrackerTest& operator=(const WindowOcclusionTrackerTest&) =
  69. delete;
  70. #if BUILDFLAG(IS_WIN)
  71. void SetUp() override {
  72. // Native Window Occlusion calculation runs in the background and can
  73. // interfere with the expectations of these tests, so, disable it.
  74. scoped_feature_list_.InitWithFeatures(
  75. /*enabled_features=*/{},
  76. /*disabled_features=*/{features::kCalculateNativeWinOcclusion});
  77. AuraTestBase::SetUp();
  78. }
  79. #endif
  80. Window* CreateTrackedWindow(MockWindowDelegate* delegate,
  81. const gfx::Rect& bounds,
  82. Window* parent = nullptr,
  83. bool transparent = false,
  84. ui::LayerType layer_type = ui::LAYER_TEXTURED) {
  85. Window* window = new Window(delegate);
  86. delegate->set_window(window);
  87. window->SetType(client::WINDOW_TYPE_NORMAL);
  88. window->Init(layer_type);
  89. if (layer_type == ui::LAYER_SOLID_COLOR)
  90. window->layer()->SetColor(SK_ColorBLACK);
  91. window->SetTransparent(transparent);
  92. window->SetBounds(bounds);
  93. window->Show();
  94. parent = parent ? parent : root_window();
  95. parent->AddChild(window);
  96. window->TrackOcclusionState();
  97. return window;
  98. }
  99. Window* CreateUntrackedWindow(const gfx::Rect& bounds,
  100. Window* parent = nullptr,
  101. ui::LayerType layer_type = ui::LAYER_TEXTURED) {
  102. if (layer_type == ui::LAYER_TEXTURED) {
  103. return test::CreateTestWindow(SK_ColorWHITE, 1, bounds,
  104. parent ? parent : root_window());
  105. }
  106. DCHECK_EQ(ui::LAYER_SOLID_COLOR, layer_type);
  107. Window* window = new Window(nullptr);
  108. window->SetType(client::WINDOW_TYPE_NORMAL);
  109. window->Init(ui::LAYER_SOLID_COLOR);
  110. window->layer()->SetColor(SK_ColorBLACK);
  111. window->SetBounds(bounds);
  112. root_window()->AddChild(window);
  113. window->Show();
  114. return window;
  115. }
  116. WindowOcclusionTracker& GetOcclusionTracker() {
  117. return *Env::GetInstance()->GetWindowOcclusionTracker();
  118. }
  119. private:
  120. base::test::ScopedFeatureList scoped_feature_list_;
  121. };
  122. SkRegion SkRegionFromSkIRects(std::initializer_list<SkIRect> rects) {
  123. SkRegion r;
  124. r.setRects(rects.begin(), rects.size());
  125. return r;
  126. }
  127. } // namespace
  128. // Verify that non-overlapping windows have a VISIBLE occlusion state.
  129. // _____ _____
  130. // | | | |
  131. // |____| |____|
  132. TEST_F(WindowOcclusionTrackerTest, NonOverlappingWindows) {
  133. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  134. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  135. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  136. EXPECT_FALSE(delegate_a->is_expecting_call());
  137. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  138. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  139. SkRegion(SkIRect::MakeXYWH(15, 0, 10, 10)));
  140. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  141. CreateTrackedWindow(delegate_b, gfx::Rect(15, 0, 10, 10));
  142. EXPECT_FALSE(delegate_a->is_expecting_call());
  143. EXPECT_FALSE(delegate_b->is_expecting_call());
  144. }
  145. // Verify that partially overlapping windows have a VISIBLE occlusion state.
  146. // ______
  147. // |__| |
  148. // |_____|
  149. TEST_F(WindowOcclusionTrackerTest, PartiallyOverlappingWindow) {
  150. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  151. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  152. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  153. EXPECT_FALSE(delegate_a->is_expecting_call());
  154. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  155. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  156. SkRegion(SkIRect::MakeWH(5, 5)));
  157. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  158. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 5));
  159. EXPECT_FALSE(delegate_a->is_expecting_call());
  160. EXPECT_FALSE(delegate_b->is_expecting_call());
  161. }
  162. // Verify that a window whose bounds are covered by a hidden window is not
  163. // occluded. Also, verify that calling Show() on the hidden window causes
  164. // occlusion states to be recomputed.
  165. // __.... ... = hidden window
  166. // |__| .
  167. // .......
  168. TEST_F(WindowOcclusionTrackerTest, HiddenWindowCoversWindow) {
  169. // Create window a. Expect it to be non-occluded.
  170. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  171. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  172. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  173. EXPECT_FALSE(delegate_a->is_expecting_call());
  174. // Create window b. Expect it to be non-occluded and expect window a to be
  175. // occluded.
  176. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  177. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  178. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  179. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 15, 15));
  180. EXPECT_FALSE(delegate_a->is_expecting_call());
  181. EXPECT_FALSE(delegate_b->is_expecting_call());
  182. // Hide window b. Expect window a to be non-occluded and window b to be
  183. // occluded.
  184. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  185. delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  186. window_b->Hide();
  187. EXPECT_FALSE(delegate_a->is_expecting_call());
  188. EXPECT_FALSE(delegate_b->is_expecting_call());
  189. // Show window b. Expect window a to be occluded and window b to be non-
  190. // occluded.
  191. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  192. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  193. window_b->Show();
  194. EXPECT_FALSE(delegate_a->is_expecting_call());
  195. EXPECT_FALSE(delegate_b->is_expecting_call());
  196. }
  197. class WindowOcclusionTrackerOpacityTest
  198. : public WindowOcclusionTrackerTest,
  199. public testing::WithParamInterface<bool> {
  200. public:
  201. WindowOcclusionTrackerOpacityTest() = default;
  202. ~WindowOcclusionTrackerOpacityTest() override = default;
  203. WindowOcclusionTrackerOpacityTest(const WindowOcclusionTrackerOpacityTest&) =
  204. delete;
  205. WindowOcclusionTrackerOpacityTest& operator=(
  206. const WindowOcclusionTrackerOpacityTest&) = delete;
  207. // WindowOcclusionTrackerTest:
  208. void SetUp() override {
  209. use_solid_color_layer_ = GetParam();
  210. WindowOcclusionTrackerTest::SetUp();
  211. }
  212. void SetOpacity(aura::Window* window, float opacity) {
  213. if (use_solid_color_layer_)
  214. window->layer()->SetColor(SkColorSetARGB(255 * opacity, 255, 255, 255));
  215. else
  216. window->layer()->SetOpacity(opacity);
  217. }
  218. ui::LayerType layer_type() const {
  219. return use_solid_color_layer_ ? ui::LAYER_SOLID_COLOR : ui::LAYER_TEXTURED;
  220. }
  221. private:
  222. bool use_solid_color_layer_;
  223. };
  224. // Verify that a window whose bounds are covered by a semi-transparent window is
  225. // not occluded. Also, verify that that when the opacity of a window changes,
  226. // occlusion states are updated.
  227. // __.... ... = semi-transparent window
  228. // |__| .
  229. // .......
  230. TEST_P(WindowOcclusionTrackerOpacityTest, SemiTransparentWindowCoversWindow) {
  231. // Create window a. Expect it to be non-occluded.
  232. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  233. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  234. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  235. EXPECT_FALSE(delegate_a->is_expecting_call());
  236. // Create window b. Expect it to be non-occluded and expect window a to be
  237. // occluded.
  238. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  239. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  240. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  241. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 15, 15),
  242. nullptr, false, layer_type());
  243. EXPECT_FALSE(delegate_a->is_expecting_call());
  244. EXPECT_FALSE(delegate_b->is_expecting_call());
  245. // Change the opacity of window b to 0.5f. Expect both windows to be non-
  246. // occluded.
  247. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  248. SetOpacity(window_b, 0.5f);
  249. EXPECT_FALSE(delegate_a->is_expecting_call());
  250. // Change the opacity of window b back to 1.0f. Expect window a to be
  251. // occluded.
  252. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  253. SetOpacity(window_b, 1.f);
  254. EXPECT_FALSE(delegate_a->is_expecting_call());
  255. }
  256. // Same as previous test, but the occlusion state of the semi-transparent is not
  257. // tracked.
  258. TEST_P(WindowOcclusionTrackerOpacityTest,
  259. SemiTransparentUntrackedWindowCoversWindow) {
  260. // Create window a. Expect it to be non-occluded.
  261. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  262. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  263. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  264. EXPECT_FALSE(delegate_a->is_expecting_call());
  265. // Create untracked window b. Expect window a to be occluded.
  266. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  267. Window* window_b =
  268. CreateUntrackedWindow(gfx::Rect(0, 0, 15, 15), nullptr, layer_type());
  269. EXPECT_FALSE(delegate_a->is_expecting_call());
  270. // Change the opacity of window b to 0.5f. Expect both windows to be non-
  271. // occluded.
  272. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  273. SetOpacity(window_b, 0.5f);
  274. EXPECT_FALSE(delegate_a->is_expecting_call());
  275. // Change the opacity of window b back to 1.0f. Expect window a to be
  276. // occluded.
  277. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  278. SetOpacity(window_b, 1.0f);
  279. EXPECT_FALSE(delegate_a->is_expecting_call());
  280. }
  281. // Verify that one window whose bounds are covered by a set of two opaque
  282. // windows is occluded.
  283. // ______
  284. // | | | <-- these two windows cover another window
  285. // |__|__|
  286. TEST_F(WindowOcclusionTrackerTest, TwoWindowsOccludeOneWindow) {
  287. // Create window a. Expect it to be non-occluded.
  288. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  289. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  290. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  291. EXPECT_FALSE(delegate_a->is_expecting_call());
  292. // Create window b with bounds that partially cover window a. Expect both
  293. // windows to be non-occluded.
  294. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  295. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  296. SkRegion(SkIRect::MakeWH(5, 10)));
  297. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  298. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 10));
  299. EXPECT_FALSE(delegate_a->is_expecting_call());
  300. EXPECT_FALSE(delegate_b->is_expecting_call());
  301. // Create window c with bounds that cover the portion of window a that isn't
  302. // already covered by window b. Expect window a to be occluded and window a/b
  303. // to be non-occluded.
  304. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  305. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  306. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  307. SkRegion(SkIRect::MakeXYWH(5, 0, 5, 10)));
  308. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  309. CreateTrackedWindow(delegate_c, gfx::Rect(5, 0, 5, 10));
  310. EXPECT_FALSE(delegate_a->is_expecting_call());
  311. EXPECT_FALSE(delegate_b->is_expecting_call());
  312. EXPECT_FALSE(delegate_c->is_expecting_call());
  313. }
  314. // Verify that a window and its child that are covered by a sibling are
  315. // occluded.
  316. TEST_F(WindowOcclusionTrackerTest, SiblingOccludesWindowAndChild) {
  317. // Create window a. Expect it to be non-occluded.
  318. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  319. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  320. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  321. EXPECT_FALSE(delegate_a->is_expecting_call());
  322. // Create window b, with bounds that occlude half of its parent window a.
  323. // Expect it to be non-occluded.
  324. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  325. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  326. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 20), window_a);
  327. EXPECT_FALSE(delegate_b->is_expecting_call());
  328. // Create window c, with bounds that occlude window a and window b. Expect it
  329. // to be non-occluded, and window a and b to be occluded.
  330. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  331. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  332. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  333. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  334. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 20, 20));
  335. EXPECT_FALSE(delegate_a->is_expecting_call());
  336. EXPECT_FALSE(delegate_b->is_expecting_call());
  337. EXPECT_FALSE(delegate_c->is_expecting_call());
  338. }
  339. // Verify that a window with one half covered by a child and the other half
  340. // covered by a sibling is non-occluded.
  341. TEST_F(WindowOcclusionTrackerTest, ChildAndSiblingOccludeOneWindow) {
  342. // Create window a. Expect it to be non-occluded.
  343. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  344. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  345. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  346. EXPECT_FALSE(delegate_a->is_expecting_call());
  347. // Create window b, with bounds that occlude half of its parent window a.
  348. // Expect it to be non-occluded.
  349. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  350. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  351. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 20), window_a);
  352. EXPECT_FALSE(delegate_b->is_expecting_call());
  353. // Create window c, with bounds that occlude the other half of window a.
  354. // Expect it to be non-occluded and expect window a to remain non-occluded.
  355. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  356. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  357. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 20)));
  358. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  359. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 20)));
  360. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  361. CreateTrackedWindow(delegate_c, gfx::Rect(10, 0, 10, 20));
  362. EXPECT_FALSE(delegate_a->is_expecting_call());
  363. EXPECT_FALSE(delegate_b->is_expecting_call());
  364. EXPECT_FALSE(delegate_c->is_expecting_call());
  365. }
  366. // Verify that a window covered by 2 non-occluded children is non-occluded.
  367. TEST_F(WindowOcclusionTrackerTest, ChildrenOccludeOneWindow) {
  368. // Create window a. Expect it to be non-occluded.
  369. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  370. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  371. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  372. EXPECT_FALSE(delegate_a->is_expecting_call());
  373. // Create window b, with bounds that cover half of its parent window a. Expect
  374. // it to be non-occluded.
  375. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  376. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  377. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 20), window_a);
  378. EXPECT_FALSE(delegate_a->is_expecting_call());
  379. // Create window c, with bounds that cover the other half of its parent window
  380. // a. Expect it to be non-occluded. Expect window a to remain non-occluded.
  381. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  382. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  383. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 20)));
  384. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  385. CreateTrackedWindow(delegate_c, gfx::Rect(10, 0, 10, 20), window_a);
  386. EXPECT_FALSE(delegate_a->is_expecting_call());
  387. EXPECT_FALSE(delegate_b->is_expecting_call());
  388. EXPECT_FALSE(delegate_c->is_expecting_call());
  389. }
  390. // Verify that when the bounds of a child window covers the bounds of a parent
  391. // window but is itself visible, the parent window is visible.
  392. TEST_F(WindowOcclusionTrackerTest, ChildDoesNotOccludeParent) {
  393. // Create window a. Expect it to be non-occluded.
  394. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  395. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  396. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  397. EXPECT_FALSE(delegate_a->is_expecting_call());
  398. // Create window b with window a as parent. The bounds of window b fully cover
  399. // window a. Expect both windows to be non-occluded.
  400. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  401. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  402. Window* window_b =
  403. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10), window_a);
  404. EXPECT_FALSE(delegate_b->is_expecting_call());
  405. // Create window c whose bounds don't overlap existing windows.
  406. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  407. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  408. SkRegion(SkIRect::MakeXYWH(15, 0, 10, 10)));
  409. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  410. SkRegion(SkIRect::MakeXYWH(15, 0, 10, 10)));
  411. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  412. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(15, 0, 10, 10));
  413. EXPECT_FALSE(delegate_a->is_expecting_call());
  414. EXPECT_FALSE(delegate_b->is_expecting_call());
  415. EXPECT_FALSE(delegate_c->is_expecting_call());
  416. // Change the parent of window b from window a to window c. Expect all windows
  417. // to remain non-occluded.
  418. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  419. window_c->AddChild(window_b);
  420. EXPECT_FALSE(delegate_b->is_expecting_call());
  421. }
  422. // Verify that when the stacking order of windows change, occlusion states are
  423. // updated.
  424. TEST_F(WindowOcclusionTrackerTest, StackingChanged) {
  425. // Create three windows that have the same bounds. Expect window on top of the
  426. // stack to be non-occluded and other windows to be occluded.
  427. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  428. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  429. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  430. EXPECT_FALSE(delegate_a->is_expecting_call());
  431. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  432. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  433. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  434. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
  435. EXPECT_FALSE(delegate_a->is_expecting_call());
  436. EXPECT_FALSE(delegate_b->is_expecting_call());
  437. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  438. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  439. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  440. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 10, 10));
  441. EXPECT_FALSE(delegate_b->is_expecting_call());
  442. EXPECT_FALSE(delegate_c->is_expecting_call());
  443. // Move window a on top of the stack. Expect it to be non-occluded and expect
  444. // window c to be occluded.
  445. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  446. delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  447. root_window()->StackChildAtTop(window_a);
  448. EXPECT_FALSE(delegate_a->is_expecting_call());
  449. EXPECT_FALSE(delegate_c->is_expecting_call());
  450. }
  451. // Verify that when the stacking order of two transparent window changes, the
  452. // occlusion states of their children is updated. The goal of this test is to
  453. // ensure that the fact that the windows whose stacking order change are
  454. // transparent doesn't prevent occlusion states from being recomputed.
  455. TEST_F(WindowOcclusionTrackerTest, TransparentParentStackingChanged) {
  456. // Create window a which is transparent. Expect it to be non-occluded.
  457. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  458. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  459. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10),
  460. root_window(), true);
  461. EXPECT_FALSE(delegate_a->is_expecting_call());
  462. // Create window b which has the same bounds as its parent window a. Expect it
  463. // to be non-occluded.
  464. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  465. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  466. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10), window_a);
  467. EXPECT_FALSE(delegate_b->is_expecting_call());
  468. // Create window c which is transparent and has the same bounds as window a
  469. // and window b. Expect it to be non-occluded.
  470. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  471. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  472. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 10, 10),
  473. root_window(), true);
  474. EXPECT_FALSE(delegate_c->is_expecting_call());
  475. // Create window d which has the same bounds as its parent window c. Expect
  476. // window d to be non-occluded and window a and b to be occluded.
  477. MockWindowDelegate* delegate_d = new MockWindowDelegate();
  478. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  479. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  480. delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  481. CreateTrackedWindow(delegate_d, gfx::Rect(0, 0, 10, 10), window_c);
  482. EXPECT_FALSE(delegate_a->is_expecting_call());
  483. EXPECT_FALSE(delegate_b->is_expecting_call());
  484. EXPECT_FALSE(delegate_d->is_expecting_call());
  485. // Move window a on top of the stack. Expect window a and b to be non-occluded
  486. // and window c and d to be occluded.
  487. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  488. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  489. delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  490. delegate_d->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  491. root_window()->StackChildAtTop(window_a);
  492. EXPECT_FALSE(delegate_a->is_expecting_call());
  493. EXPECT_FALSE(delegate_b->is_expecting_call());
  494. EXPECT_FALSE(delegate_c->is_expecting_call());
  495. EXPECT_FALSE(delegate_d->is_expecting_call());
  496. }
  497. // Verify that when StackChildAtTop() is called on a window whose occlusion
  498. // state is not tracked, the occlusion state of tracked siblings is updated.
  499. TEST_F(WindowOcclusionTrackerTest, UntrackedWindowStackingChanged) {
  500. Window* window_a = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
  501. // Create window b. Expect it to be non-occluded.
  502. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  503. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  504. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 5));
  505. EXPECT_FALSE(delegate_b->is_expecting_call());
  506. // Stack window a on top of window b. Expect window b to be occluded.
  507. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  508. root_window()->StackChildAtTop(window_a);
  509. EXPECT_FALSE(delegate_b->is_expecting_call());
  510. }
  511. // Verify that occlusion states are updated when the bounds of a window change.
  512. TEST_F(WindowOcclusionTrackerTest, BoundsChanged) {
  513. // Create two non-overlapping windows. Expect them to be non-occluded.
  514. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  515. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  516. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  517. EXPECT_FALSE(delegate_a->is_expecting_call());
  518. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  519. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  520. SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
  521. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  522. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
  523. EXPECT_FALSE(delegate_a->is_expecting_call());
  524. EXPECT_FALSE(delegate_b->is_expecting_call());
  525. // Move window b on top of window a. Expect window a to be occluded.
  526. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  527. window_b->SetBounds(window_a->bounds());
  528. EXPECT_FALSE(delegate_a->is_expecting_call());
  529. }
  530. // Verify that when the bounds of a window are animated, occlusion states are
  531. // updated at the beginning and at the end of the animation, but not during the
  532. // animation. At the beginning of the animation, the window animated window
  533. // should be considered non-occluded and should not occlude other windows. The
  534. // animated window starts occluded.
  535. TEST_F(WindowOcclusionTrackerTest, OccludedWindowBoundsAnimated) {
  536. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  537. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  538. ui::LayerAnimatorTestController test_controller(
  539. ui::LayerAnimator::CreateImplicitAnimator());
  540. ui::ScopedLayerAnimationSettings layer_animation_settings(
  541. test_controller.animator());
  542. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  543. // Create 3 windows. Window a is unoccluded. Window c occludes window b.
  544. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  545. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  546. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  547. EXPECT_FALSE(delegate_a->is_expecting_call());
  548. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  549. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  550. SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
  551. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  552. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
  553. EXPECT_FALSE(delegate_a->is_expecting_call());
  554. EXPECT_FALSE(delegate_b->is_expecting_call());
  555. window_b->layer()->SetAnimator(test_controller.animator());
  556. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  557. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  558. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  559. CreateTrackedWindow(delegate_c, gfx::Rect(0, 10, 10, 10));
  560. EXPECT_FALSE(delegate_b->is_expecting_call());
  561. EXPECT_FALSE(delegate_c->is_expecting_call());
  562. // Start animating the bounds of window b so that it moves on top of window a.
  563. // Window b should be non-occluded when the animation starts.
  564. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  565. window_b->SetBounds(window_a->bounds());
  566. test_controller.Step(kTransitionDuration / 3);
  567. EXPECT_FALSE(delegate_b->is_expecting_call());
  568. // Window a should remain non-occluded during the animation.
  569. test_controller.Step(kTransitionDuration / 3);
  570. // Window b should occlude window a at the end of the animation.
  571. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  572. // Window b should have window c in its potential occlusion region.
  573. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  574. SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
  575. test_controller.Step(kTransitionDuration / 3);
  576. EXPECT_FALSE(delegate_a->is_expecting_call());
  577. EXPECT_FALSE(delegate_b->is_expecting_call());
  578. window_b->layer()->SetAnimator(nullptr);
  579. }
  580. // Same as the previous test, but the animated window starts non-occluded.
  581. TEST_F(WindowOcclusionTrackerTest, NonOccludedWindowBoundsAnimated) {
  582. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  583. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  584. ui::LayerAnimatorTestController test_controller(
  585. ui::LayerAnimator::CreateImplicitAnimator());
  586. ui::ScopedLayerAnimationSettings layer_animation_settings(
  587. test_controller.animator());
  588. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  589. // Create 3 windows. Window a is unoccluded. Window c occludes window b.
  590. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  591. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  592. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  593. EXPECT_FALSE(delegate_a->is_expecting_call());
  594. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  595. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  596. SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
  597. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  598. CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
  599. EXPECT_FALSE(delegate_a->is_expecting_call());
  600. EXPECT_FALSE(delegate_b->is_expecting_call());
  601. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  602. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  603. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  604. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 10, 10, 10));
  605. EXPECT_FALSE(delegate_b->is_expecting_call());
  606. EXPECT_FALSE(delegate_c->is_expecting_call());
  607. window_c->layer()->SetAnimator(test_controller.animator());
  608. // Start animating the bounds of window c so that it moves on top of window a.
  609. // Window b should be non-occluded when the animation starts.
  610. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  611. window_c->SetBounds(window_a->bounds());
  612. test_controller.Step(kTransitionDuration / 3);
  613. EXPECT_FALSE(delegate_b->is_expecting_call());
  614. // Window a should remain non-occluded during the animation.
  615. test_controller.Step(kTransitionDuration / 3);
  616. // Window c should occlude window a at the end of the animation.
  617. // Window b should have a potentially occluded region including window c.
  618. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  619. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  620. SkRegion(SkIRect::MakeXYWH(0, 0, 10, 10)));
  621. test_controller.Step(kTransitionDuration / 3);
  622. EXPECT_FALSE(delegate_a->is_expecting_call());
  623. EXPECT_FALSE(delegate_b->is_expecting_call());
  624. window_c->layer()->SetAnimator(nullptr);
  625. }
  626. // Verify that occlusion states are updated when the bounds of a transparent
  627. // window with opaque children change.
  628. TEST_F(WindowOcclusionTrackerTest, TransparentParentBoundsChanged) {
  629. // Create window a. Expect it to be non-occluded.
  630. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  631. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  632. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
  633. EXPECT_FALSE(delegate_a->is_expecting_call());
  634. // Create window b which doesn't overlap window a and is transparent. Expect
  635. // it to be non-occluded.
  636. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  637. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  638. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10),
  639. root_window(), true);
  640. EXPECT_FALSE(delegate_b->is_expecting_call());
  641. // Create window c which has window b as parent and doesn't occlude any
  642. // window.
  643. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  644. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  645. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  646. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  647. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5), window_b);
  648. EXPECT_FALSE(delegate_a->is_expecting_call());
  649. EXPECT_FALSE(delegate_c->is_expecting_call());
  650. // Move window b so that window c occludes window a. Expect window a to be
  651. // occluded and other windows to be non-occluded.
  652. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  653. window_b->SetBounds(gfx::Rect(0, 0, 10, 10));
  654. EXPECT_FALSE(delegate_a->is_expecting_call());
  655. }
  656. // Verify that occlusion states are updated when the bounds of a window whose
  657. // occlusion state is not tracked change.
  658. TEST_F(WindowOcclusionTrackerTest, UntrackedWindowBoundsChanged) {
  659. // Create window a. Expect it to be non-occluded.
  660. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  661. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  662. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
  663. EXPECT_FALSE(delegate_a->is_expecting_call());
  664. // Create window b. It should not occlude window a.
  665. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  666. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  667. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 10, 5, 5));
  668. EXPECT_FALSE(delegate_a->is_expecting_call());
  669. // Move window b on top of window a. Expect window a to be occluded.
  670. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  671. window_b->SetBounds(window_a->bounds());
  672. EXPECT_FALSE(delegate_a->is_expecting_call());
  673. }
  674. // Verify that occlusion states are updated when the transform of a window
  675. // changes.
  676. TEST_F(WindowOcclusionTrackerTest, TransformChanged) {
  677. // Create two non-overlapping windows. Expect them to be non-occluded.
  678. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  679. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  680. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  681. EXPECT_FALSE(delegate_a->is_expecting_call());
  682. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  683. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  684. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  685. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  686. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
  687. EXPECT_FALSE(delegate_a->is_expecting_call());
  688. EXPECT_FALSE(delegate_b->is_expecting_call());
  689. // Scale and translate window b so that it covers window a. Expect window a to
  690. // be occluded.
  691. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  692. gfx::Transform transform;
  693. transform.Translate(0.0f, -10.0f);
  694. transform.Scale(2.0f, 2.0f);
  695. window_b->SetTransform(transform);
  696. EXPECT_FALSE(delegate_a->is_expecting_call());
  697. }
  698. // Verify that when the transform of a window is animated, occlusion states are
  699. // updated at the beginning and at the end of the animation, but not during the
  700. // animation. At the beginning of the animation, the window animated window
  701. // should be considered non-occluded and should not occlude other windows. The
  702. // animated window starts occluded.
  703. TEST_F(WindowOcclusionTrackerTest, OccludedWindowTransformAnimated) {
  704. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  705. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  706. ui::LayerAnimatorTestController test_controller(
  707. ui::LayerAnimator::CreateImplicitAnimator());
  708. ui::ScopedLayerAnimationSettings layer_animation_settings(
  709. test_controller.animator());
  710. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  711. // Create 3 windows. Window a is unoccluded. Window c occludes window b.
  712. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  713. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  714. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  715. EXPECT_FALSE(delegate_a->is_expecting_call());
  716. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  717. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  718. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  719. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  720. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
  721. EXPECT_FALSE(delegate_a->is_expecting_call());
  722. EXPECT_FALSE(delegate_b->is_expecting_call());
  723. window_b->layer()->SetAnimator(test_controller.animator());
  724. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  725. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  726. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  727. CreateTrackedWindow(delegate_c, gfx::Rect(0, 10, 5, 5));
  728. EXPECT_FALSE(delegate_b->is_expecting_call());
  729. EXPECT_FALSE(delegate_c->is_expecting_call());
  730. // Start animating the transform of window b so that it moves on top of window
  731. // a. Window b should be non-occluded when the animation starts.
  732. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  733. auto transform = std::make_unique<ui::InterpolatedScale>(
  734. gfx::Point3F(1, 1, 1), gfx::Point3F(2.0f, 2.0f, 1));
  735. transform->SetChild(std::make_unique<ui::InterpolatedTranslation>(
  736. gfx::PointF(), gfx::PointF(-0.0f, -10.0f)));
  737. test_controller.animator()->StartAnimation(new ui::LayerAnimationSequence(
  738. ui::LayerAnimationElement::CreateInterpolatedTransformElement(
  739. std::move(transform), kTransitionDuration)));
  740. test_controller.Step(kTransitionDuration / 3);
  741. EXPECT_FALSE(delegate_b->is_expecting_call());
  742. // Window a should remain non-occluded during the animation.
  743. test_controller.Step(kTransitionDuration / 3);
  744. // Window b should occlude window a at the end of the animation.
  745. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  746. // Window b should see window c as part of the potential occlusion region.
  747. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  748. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  749. test_controller.Step(kTransitionDuration / 3);
  750. EXPECT_FALSE(delegate_a->is_expecting_call());
  751. EXPECT_FALSE(delegate_b->is_expecting_call());
  752. window_b->layer()->SetAnimator(nullptr);
  753. }
  754. // Same as the previous test, but the animated window starts non-occluded.
  755. TEST_F(WindowOcclusionTrackerTest, NonOccludedWindowTransformAnimated) {
  756. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  757. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  758. ui::LayerAnimatorTestController test_controller(
  759. ui::LayerAnimator::CreateImplicitAnimator());
  760. ui::ScopedLayerAnimationSettings layer_animation_settings(
  761. test_controller.animator());
  762. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  763. // Create 3 windows. Window a is unoccluded. Window c occludes window b.
  764. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  765. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  766. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  767. EXPECT_FALSE(delegate_a->is_expecting_call());
  768. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  769. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  770. SkRegion(SkIRect::MakeXYWH(0, 20, 10, 10)));
  771. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  772. CreateTrackedWindow(delegate_b, gfx::Rect(0, 20, 10, 10));
  773. EXPECT_FALSE(delegate_a->is_expecting_call());
  774. EXPECT_FALSE(delegate_b->is_expecting_call());
  775. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  776. delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  777. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  778. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 20, 10, 10));
  779. EXPECT_FALSE(delegate_b->is_expecting_call());
  780. EXPECT_FALSE(delegate_c->is_expecting_call());
  781. window_c->layer()->SetAnimator(test_controller.animator());
  782. // Start animating the bounds of window c so that it moves on top of window a.
  783. // Window b should be non-occluded when the animation starts.
  784. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  785. auto transform = std::make_unique<ui::InterpolatedScale>(
  786. gfx::Point3F(1, 1, 1), gfx::Point3F(2.0f, 2.0f, 1));
  787. transform->SetChild(std::make_unique<ui::InterpolatedTranslation>(
  788. gfx::PointF(), gfx::PointF(-0.0f, -20.0f)));
  789. test_controller.animator()->StartAnimation(new ui::LayerAnimationSequence(
  790. ui::LayerAnimationElement::CreateInterpolatedTransformElement(
  791. std::move(transform), kTransitionDuration)));
  792. test_controller.Step(kTransitionDuration / 3);
  793. EXPECT_FALSE(delegate_b->is_expecting_call());
  794. // Window a should remain non-occluded during the animation.
  795. test_controller.Step(kTransitionDuration / 3);
  796. // Window c should occlude window a at the end of the animation.
  797. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  798. // Window b should now see window c in the potential occlusion region.
  799. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  800. SkRegion(SkIRect::MakeWH(20, 20)));
  801. test_controller.Step(kTransitionDuration / 3);
  802. EXPECT_FALSE(delegate_a->is_expecting_call());
  803. EXPECT_FALSE(delegate_b->is_expecting_call());
  804. window_c->layer()->SetAnimator(nullptr);
  805. }
  806. // Verify that occlusion states are updated when the transform of a transparent
  807. // window with opaque children change.
  808. TEST_F(WindowOcclusionTrackerTest, TransparentParentTransformChanged) {
  809. // Create window a. Expect it to be non-occluded.
  810. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  811. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  812. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  813. EXPECT_FALSE(delegate_a->is_expecting_call());
  814. // Create window b which doesn't overlap window a and is transparent. Expect
  815. // it to be non-occluded.
  816. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  817. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  818. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10),
  819. root_window(), true);
  820. EXPECT_FALSE(delegate_b->is_expecting_call());
  821. // Create window c which has window b as parent and doesn't occlude any
  822. // window.
  823. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  824. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  825. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  826. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  827. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5), window_b);
  828. EXPECT_FALSE(delegate_a->is_expecting_call());
  829. EXPECT_FALSE(delegate_c->is_expecting_call());
  830. // Scale and translate window b so that window c occludes window a. Expect
  831. // window a to be occluded and other windows to be non-occluded.
  832. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  833. gfx::Transform transform;
  834. transform.Translate(0.0f, -10.0f);
  835. transform.Scale(2.0f, 2.0f);
  836. window_b->SetTransform(transform);
  837. EXPECT_FALSE(delegate_a->is_expecting_call());
  838. }
  839. // Verify that occlusion states are updated when the transform of a window whose
  840. // occlusion state is not tracked changes.
  841. TEST_F(WindowOcclusionTrackerTest, UntrackedWindowTransformChanged) {
  842. // Create window a. Expect it to be non-occluded.
  843. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  844. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  845. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  846. EXPECT_FALSE(delegate_a->is_expecting_call());
  847. // Create window b. It should not occlude window a.
  848. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  849. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  850. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 10, 5, 5));
  851. EXPECT_FALSE(delegate_a->is_expecting_call());
  852. // Scale and translate window b so that it occludes window a. Expect window a
  853. // to be occluded.
  854. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  855. gfx::Transform transform;
  856. transform.Translate(0.0f, -10.0f);
  857. transform.Scale(2.0f, 2.0f);
  858. window_b->SetTransform(transform);
  859. EXPECT_FALSE(delegate_a->is_expecting_call());
  860. }
  861. // Verify that deleting an untracked window which covers a tracked window causes
  862. // the tracked window to be non-occluded.
  863. TEST_F(WindowOcclusionTrackerTest, DeleteUntrackedWindow) {
  864. // Create window a. Expect it to be non-occluded.
  865. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  866. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  867. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
  868. EXPECT_FALSE(delegate_a->is_expecting_call());
  869. // Create window b which occludes window a.
  870. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  871. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
  872. EXPECT_FALSE(delegate_a->is_expecting_call());
  873. // Delete window b. Expect a to be non-occluded.
  874. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  875. delete window_b;
  876. EXPECT_FALSE(delegate_a->is_expecting_call());
  877. }
  878. // Verify that removing an untracked window which covers a tracked window causes
  879. // the tracked window to be non-occluded.
  880. TEST_F(WindowOcclusionTrackerTest, RemoveUntrackedWindow) {
  881. // Create window a. Expect it to be non-occluded.
  882. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  883. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  884. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
  885. EXPECT_FALSE(delegate_a->is_expecting_call());
  886. // Create window b which occludes window a.
  887. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  888. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
  889. EXPECT_FALSE(delegate_a->is_expecting_call());
  890. // Delete window b. Expect a to be non-occluded.
  891. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  892. root_window()->RemoveChild(window_b);
  893. EXPECT_FALSE(delegate_a->is_expecting_call());
  894. delete window_b;
  895. }
  896. // Verify that occlusion tracking with customized WindowHasContent callback.
  897. TEST_F(WindowOcclusionTrackerTest, CustomizedWindowHasContent) {
  898. // Create window a. Expect it to be non-occluded.
  899. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  900. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  901. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  902. EXPECT_FALSE(delegate_a->is_expecting_call());
  903. // Create window b with layer type LAYER_NOT_DRAWN. Occlusion state of a is
  904. // not changed.
  905. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  906. Window* window_b = new Window(delegate_b);
  907. delegate_b->set_window(window_b);
  908. window_b->Init(ui::LAYER_NOT_DRAWN);
  909. window_b->SetBounds(gfx::Rect(0, 0, 10, 10));
  910. root_window()->AddChild(window_b);
  911. delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  912. window_b->TrackOcclusionState();
  913. EXPECT_FALSE(delegate_b->is_expecting_call());
  914. // Use customized WindowHasContent callback to mark b as opaque.
  915. Env* env = Env::GetInstance();
  916. env->GetWindowOcclusionTracker()->set_window_has_content_callback(
  917. base::BindLambdaForTesting([window_b](const Window* window) -> bool {
  918. return window == window_b;
  919. }));
  920. // Show window b to trigger a occlusion compute and window a is occluded.
  921. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  922. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  923. window_b->Show();
  924. EXPECT_FALSE(delegate_a->is_expecting_call());
  925. EXPECT_FALSE(delegate_b->is_expecting_call());
  926. env->GetWindowOcclusionTracker()->set_window_has_content_callback(
  927. base::NullCallback());
  928. }
  929. // Verify that when a tracked window is removed and re-added to a root,
  930. // occlusion states are still tracked.
  931. TEST_F(WindowOcclusionTrackerTest, RemoveAndAddTrackedToRoot) {
  932. Window* window_a = CreateUntrackedWindow(gfx::Rect(0, 0, 1, 1));
  933. CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10), window_a);
  934. // Create window b. Expect it to be non-occluded.
  935. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  936. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  937. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5));
  938. EXPECT_FALSE(delegate_c->is_expecting_call());
  939. // Remove window c from its root.
  940. root_window()->RemoveChild(window_c);
  941. // Add window c back under its root.
  942. root_window()->AddChild(window_c);
  943. // Create untracked window d which covers window a. Expect window a to be
  944. // occluded.
  945. delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  946. Window* window_d = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
  947. EXPECT_FALSE(delegate_c->is_expecting_call());
  948. // Move window d so that it doesn't cover window c.
  949. delegate_c->set_expectation(Window::OcclusionState::VISIBLE,
  950. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  951. window_d->SetBounds(gfx::Rect(0, 10, 5, 5));
  952. EXPECT_FALSE(delegate_c->is_expecting_call());
  953. // Stack window a on top of window c. Expect window c to be non-occluded. This
  954. // won't work if WindowOcclusionTracked didn't register as an observer of
  955. // window a when window c was made a child of root_window().
  956. delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  957. root_window()->StackChildAtTop(window_a);
  958. EXPECT_FALSE(delegate_c->is_expecting_call());
  959. }
  960. namespace {
  961. class ResizeWindowObserver : public WindowObserver {
  962. public:
  963. ResizeWindowObserver(Window* window_to_resize)
  964. : window_to_resize_(window_to_resize) {}
  965. ResizeWindowObserver(const ResizeWindowObserver&) = delete;
  966. ResizeWindowObserver& operator=(const ResizeWindowObserver&) = delete;
  967. void OnWindowBoundsChanged(Window* window,
  968. const gfx::Rect& old_bounds,
  969. const gfx::Rect& new_bounds,
  970. ui::PropertyChangeReason reason) override {
  971. window_to_resize_->SetBounds(old_bounds);
  972. }
  973. private:
  974. const raw_ptr<Window> window_to_resize_;
  975. };
  976. } // namespace
  977. // Verify that when the bounds of a child window are updated in response to the
  978. // bounds of a parent window being updated, occlusion states are updated once.
  979. TEST_F(WindowOcclusionTrackerTest, ResizeChildFromObserver) {
  980. // Create window a. Expect it to be non-occluded.
  981. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  982. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  983. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  984. EXPECT_FALSE(delegate_a->is_expecting_call());
  985. // Create window b. Expect it to be non-occluded and to occlude window a.
  986. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  987. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  988. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  989. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
  990. EXPECT_FALSE(delegate_a->is_expecting_call());
  991. EXPECT_FALSE(delegate_b->is_expecting_call());
  992. // Create window c, which is a child of window b. Expect it to be non-
  993. // occluded.
  994. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  995. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  996. Window* window_c =
  997. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5), window_b);
  998. EXPECT_FALSE(delegate_b->is_expecting_call());
  999. // Create an observer that will resize window c when window b is resized.
  1000. ResizeWindowObserver resize_window_observer(window_c);
  1001. window_b->AddObserver(&resize_window_observer);
  1002. // Resize window b. Expect window c to be resized so that window a stays
  1003. // occluded. Window a should not temporarily be non-occluded.
  1004. window_b->SetBounds(gfx::Rect(0, 0, 5, 5));
  1005. window_b->RemoveObserver(&resize_window_observer);
  1006. }
  1007. // Verify that the bounds of windows are changed multiple times within the scope
  1008. // of a ScopedPause, occlusion states are updated once at the end of the scope.
  1009. TEST_F(WindowOcclusionTrackerTest, ScopedPause) {
  1010. // Create window a. Expect it to be non-occluded.
  1011. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1012. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1013. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
  1014. EXPECT_FALSE(delegate_a->is_expecting_call());
  1015. // Create window b which doesn't overlap window a. Expect it to be non
  1016. // occluded.
  1017. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1018. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1019. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  1020. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1021. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
  1022. EXPECT_FALSE(delegate_a->is_expecting_call());
  1023. EXPECT_FALSE(delegate_b->is_expecting_call());
  1024. // Change bounds multiple times. At the end of the scope, expect window a to
  1025. // be occluded.
  1026. {
  1027. WindowOcclusionTracker::ScopedPause pause_occlusion_tracking;
  1028. window_b->SetBounds(window_a->bounds());
  1029. window_a->SetBounds(gfx::Rect(0, 10, 5, 5));
  1030. window_b->SetBounds(window_a->bounds());
  1031. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1032. }
  1033. EXPECT_FALSE(delegate_a->is_expecting_call());
  1034. }
  1035. // Same as the previous test, but with nested ScopedPause.
  1036. TEST_F(WindowOcclusionTrackerTest, NestedScopedPause) {
  1037. // Create window a. Expect it to be non-occluded.
  1038. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1039. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1040. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
  1041. EXPECT_FALSE(delegate_a->is_expecting_call());
  1042. // Create window b which doesn't overlap window a. Expect it to be non-
  1043. // occluded.
  1044. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1045. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1046. SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
  1047. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1048. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
  1049. EXPECT_FALSE(delegate_a->is_expecting_call());
  1050. EXPECT_FALSE(delegate_b->is_expecting_call());
  1051. // Change bounds multiple times. At the end of the scope, expect window a to
  1052. // be occluded.
  1053. {
  1054. WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_a;
  1055. {
  1056. WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_b;
  1057. window_b->SetBounds(window_a->bounds());
  1058. }
  1059. {
  1060. WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_c;
  1061. window_a->SetBounds(gfx::Rect(0, 10, 5, 5));
  1062. }
  1063. {
  1064. WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_d;
  1065. window_b->SetBounds(window_a->bounds());
  1066. }
  1067. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1068. }
  1069. EXPECT_FALSE(delegate_a->is_expecting_call());
  1070. }
  1071. // Verify that bounds are computed correctly when a hierarchy of windows have
  1072. // transforms.
  1073. TEST_F(WindowOcclusionTrackerTest, HierarchyOfTransforms) {
  1074. gfx::Transform scale_2x_transform;
  1075. scale_2x_transform.Scale(2.0f, 2.0f);
  1076. // Effective bounds: x = 2, y = 2, height = 10, width = 10
  1077. Window* window_a = CreateUntrackedWindow(gfx::Rect(2, 2, 5, 5));
  1078. window_a->SetTransform(scale_2x_transform);
  1079. // Effective bounds: x = 4, y = 4, height = 4, width = 4
  1080. Window* window_b = CreateUntrackedWindow(gfx::Rect(1, 1, 2, 2), window_a);
  1081. // Effective bounds: x = 34, y = 36, height = 8, width = 10
  1082. CreateUntrackedWindow(gfx::Rect(15, 16, 4, 5), window_b);
  1083. MockWindowDelegate* delegate_d = new MockWindowDelegate();
  1084. delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1085. Window* window_d = CreateTrackedWindow(delegate_d, gfx::Rect(34, 36, 8, 10));
  1086. EXPECT_FALSE(delegate_d->is_expecting_call());
  1087. delegate_d->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1088. root_window()->StackChildAtBottom(window_d);
  1089. EXPECT_FALSE(delegate_d->is_expecting_call());
  1090. SkRegion occluded_area = SkRegionFromSkIRects(
  1091. {SkIRect::MakeXYWH(2, 2, 10, 10), SkIRect::MakeXYWH(4, 4, 4, 4),
  1092. SkIRect::MakeXYWH(34, 36, 8, 10)});
  1093. delegate_d->set_expectation(Window::OcclusionState::VISIBLE, occluded_area);
  1094. window_d->SetBounds(gfx::Rect(35, 36, 8, 10));
  1095. EXPECT_FALSE(delegate_d->is_expecting_call());
  1096. // |window_d| should remain non-occluded with the following bounds changes.
  1097. window_d->SetBounds(gfx::Rect(33, 36, 8, 10));
  1098. window_d->SetBounds(gfx::Rect(34, 37, 8, 10));
  1099. window_d->SetBounds(gfx::Rect(34, 35, 8, 10));
  1100. }
  1101. // Verify that clipping is taken into account when computing occlusion.
  1102. TEST_F(WindowOcclusionTrackerTest, Clipping) {
  1103. // Create window a. Expect it to be non-occluded.
  1104. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1105. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1106. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1107. EXPECT_FALSE(delegate_a->is_expecting_call());
  1108. // Create window b. Expect it to be non-occluded.
  1109. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1110. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1111. SkRegion(SkIRect::MakeWH(5, 5)));
  1112. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1113. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 5));
  1114. EXPECT_FALSE(delegate_a->is_expecting_call());
  1115. EXPECT_FALSE(delegate_b->is_expecting_call());
  1116. window_b->layer()->SetMasksToBounds(true);
  1117. // Create window c which has window b as parent. Don't expect it to occlude
  1118. // window a since its bounds are clipped by window b.
  1119. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  1120. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1121. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 10, 10), window_b);
  1122. EXPECT_FALSE(delegate_c->is_expecting_call());
  1123. }
  1124. // Verify that the DCHECK(!WindowIsAnimated(window)) in
  1125. // WindowOcclusionTracker::OnWindowDestroyed() doesn't fire if a window is
  1126. // destroyed with an incomplete animation (~Window should complete the animation
  1127. // and the window should be removed from |animated_windows_| before
  1128. // OnWindowDestroyed() is called).
  1129. TEST_F(WindowOcclusionTrackerTest, DestroyWindowWithPendingAnimation) {
  1130. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  1131. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  1132. ui::LayerAnimatorTestController test_controller(
  1133. ui::LayerAnimator::CreateImplicitAnimator());
  1134. ui::ScopedLayerAnimationSettings layer_animation_settings(
  1135. test_controller.animator());
  1136. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  1137. MockWindowDelegate* delegate = new MockWindowDelegate();
  1138. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1139. Window* window = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
  1140. EXPECT_FALSE(delegate->is_expecting_call());
  1141. window->layer()->SetAnimator(test_controller.animator());
  1142. // Start animating the bounds of window.
  1143. window->SetBounds(gfx::Rect(10, 10, 5, 5));
  1144. test_controller.Step(kTransitionDuration / 3);
  1145. EXPECT_TRUE(test_controller.animator()->IsAnimatingProperty(
  1146. ui::LayerAnimationElement::BOUNDS));
  1147. // Destroy the window. Expect no DCHECK failure.
  1148. delete window;
  1149. }
  1150. // Verify that an animated window stops being considered as animated when its
  1151. // layer is recreated.
  1152. TEST_F(WindowOcclusionTrackerTest, RecreateLayerOfAnimatedWindow) {
  1153. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  1154. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  1155. ui::LayerAnimatorTestController test_controller(
  1156. ui::LayerAnimator::CreateImplicitAnimator());
  1157. ui::ScopedLayerAnimationSettings layer_animation_settings(
  1158. test_controller.animator());
  1159. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  1160. // Create 2 windows. Window b occludes window a.
  1161. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1162. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1163. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(2, 2, 1, 1));
  1164. EXPECT_FALSE(delegate_a->is_expecting_call());
  1165. window_a->layer()->SetAnimator(test_controller.animator());
  1166. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1167. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1168. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1169. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
  1170. EXPECT_FALSE(delegate_a->is_expecting_call());
  1171. EXPECT_FALSE(delegate_b->is_expecting_call());
  1172. // Start animating the bounds of window a. Window a should be non-occluded
  1173. // when the animation starts.
  1174. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1175. window_a->SetBounds(gfx::Rect(6, 6, 1, 1));
  1176. test_controller.Step(kTransitionDuration / 2);
  1177. EXPECT_FALSE(delegate_a->is_expecting_call());
  1178. // Recreate the layer of window b. Expect this to behave the same as if the
  1179. // animation was abandoned. Occlusion region should be half way between the
  1180. // animation bounds.
  1181. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1182. std::unique_ptr<ui::Layer> old_layer = window_a->RecreateLayer();
  1183. EXPECT_FALSE(delegate_a->is_expecting_call());
  1184. window_a->layer()->SetAnimator(nullptr);
  1185. }
  1186. namespace {
  1187. class ObserverChangingWindowBounds : public WindowObserver {
  1188. public:
  1189. ObserverChangingWindowBounds() = default;
  1190. ObserverChangingWindowBounds(const ObserverChangingWindowBounds&) = delete;
  1191. ObserverChangingWindowBounds& operator=(const ObserverChangingWindowBounds&) =
  1192. delete;
  1193. // WindowObserver:
  1194. void OnWindowParentChanged(Window* window, Window* parent) override {
  1195. window->SetBounds(gfx::Rect(1, 2, 3, 4));
  1196. }
  1197. };
  1198. } // namespace
  1199. // Verify that no crash occurs if a tracked window is modified by an observer
  1200. // after it has been added to a new root but before WindowOcclusionTracker has
  1201. // been notified.
  1202. TEST_F(WindowOcclusionTrackerTest, ChangeTrackedWindowBeforeObserveAddToRoot) {
  1203. // Create a window. Expect it to be non-occluded.
  1204. MockWindowDelegate* delegate = new MockWindowDelegate();
  1205. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1206. Window* window = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
  1207. EXPECT_FALSE(delegate->is_expecting_call());
  1208. // Remove the window from its root.
  1209. root_window()->RemoveChild(window);
  1210. // Add an observer that changes the bounds of |window| when it gets a new
  1211. // parent.
  1212. ObserverChangingWindowBounds observer;
  1213. window->AddObserver(&observer);
  1214. // Re-add the window to its root. Expect no crash when |observer| changes the
  1215. // bounds.
  1216. root_window()->AddChild(window);
  1217. window->RemoveObserver(&observer);
  1218. }
  1219. namespace {
  1220. class ObserverDestroyingWindowOnAnimationEnded
  1221. : public ui::LayerAnimationObserver {
  1222. public:
  1223. ObserverDestroyingWindowOnAnimationEnded(Window* window) : window_(window) {}
  1224. ObserverDestroyingWindowOnAnimationEnded(
  1225. const ObserverDestroyingWindowOnAnimationEnded&) = delete;
  1226. ObserverDestroyingWindowOnAnimationEnded& operator=(
  1227. const ObserverDestroyingWindowOnAnimationEnded&) = delete;
  1228. ~ObserverDestroyingWindowOnAnimationEnded() override {
  1229. EXPECT_FALSE(window_);
  1230. }
  1231. void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
  1232. EXPECT_TRUE(window_);
  1233. delete window_;
  1234. window_ = nullptr;
  1235. }
  1236. void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
  1237. void OnLayerAnimationScheduled(
  1238. ui::LayerAnimationSequence* sequence) override {}
  1239. private:
  1240. raw_ptr<Window> window_;
  1241. };
  1242. } // namespace
  1243. // Verify that no crash occurs if a LayerAnimationObserver destroys a tracked
  1244. // window before WindowOcclusionTracker is notified that the animation ended.
  1245. TEST_P(WindowOcclusionTrackerOpacityTest,
  1246. DestroyTrackedWindowFromLayerAnimationObserver) {
  1247. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  1248. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  1249. ui::LayerAnimatorTestController test_controller(
  1250. ui::LayerAnimator::CreateImplicitAnimator());
  1251. ui::ScopedLayerAnimationSettings layer_animation_settings(
  1252. test_controller.animator());
  1253. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  1254. // Create a window. Expect it to be non-occluded.
  1255. MockWindowDelegate* delegate = new MockWindowDelegate();
  1256. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1257. Window* window = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10),
  1258. nullptr, false, layer_type());
  1259. EXPECT_FALSE(delegate->is_expecting_call());
  1260. window->layer()->SetAnimator(test_controller.animator());
  1261. // Add a LayerAnimationObserver that destroys the window when an animation
  1262. // ends.
  1263. ObserverDestroyingWindowOnAnimationEnded observer(window);
  1264. window->layer()->GetAnimator()->AddObserver(&observer);
  1265. // Start animating the opacity of the window.
  1266. SetOpacity(window, 0.5f);
  1267. // Complete the animation. Expect no crash.
  1268. window->layer()->GetAnimator()->StopAnimating();
  1269. }
  1270. // Verify that no crash occurs if an animation completes on a non-tracked
  1271. // window's layer after the window has been removed from a root with a tracked
  1272. // window and deleted.
  1273. TEST_P(WindowOcclusionTrackerOpacityTest,
  1274. DeleteNonTrackedAnimatedWindowRemovedFromTrackedRoot) {
  1275. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  1276. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  1277. ui::LayerAnimatorTestController test_controller(
  1278. ui::LayerAnimator::CreateImplicitAnimator());
  1279. ui::ScopedLayerAnimationSettings layer_animation_settings(
  1280. test_controller.animator());
  1281. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  1282. // Create a tracked window. Expect it to be non-occluded.
  1283. MockWindowDelegate* delegate = new MockWindowDelegate();
  1284. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1285. CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
  1286. EXPECT_FALSE(delegate->is_expecting_call());
  1287. // Create a non-tracked window and add an observer that deletes it when its
  1288. // stops being animated.
  1289. delegate->set_expectation(Window::OcclusionState::VISIBLE,
  1290. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
  1291. Window* window =
  1292. CreateUntrackedWindow(gfx::Rect(10, 0, 10, 10), nullptr, layer_type());
  1293. EXPECT_FALSE(delegate->is_expecting_call());
  1294. window->layer()->SetAnimator(test_controller.animator());
  1295. ObserverDestroyingWindowOnAnimationEnded observer(window);
  1296. window->layer()->GetAnimator()->AddObserver(&observer);
  1297. // Animate the window. WindowOcclusionTracker should add itself as an observer
  1298. // of its LayerAnimator (after |observer|). Upon beginning animation, the
  1299. // window should no longer affect the occluded region.
  1300. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1301. SetOpacity(window, 0.1);
  1302. // Drive the animation so that the color's alpha value changes.
  1303. test_controller.Step(kTransitionDuration / 2);
  1304. EXPECT_FALSE(delegate->is_expecting_call());
  1305. // Remove the non-tracked window from its root. WindowOcclusionTracker should
  1306. // remove the window from its list of animated windows and stop observing it
  1307. // and its LayerAnimator.
  1308. root_window()->RemoveChild(window);
  1309. // Complete animations on the window. |observer| will delete the window when
  1310. // it is notified that animations are complete. Expect that
  1311. // WindowOcclusionTracker will not try to access |window| after that (if it
  1312. // does, the test will crash).
  1313. window->layer()->GetAnimator()->StopAnimating();
  1314. }
  1315. TEST_P(WindowOcclusionTrackerOpacityTest,
  1316. OpacityAnimationShouldNotOccludeWindow) {
  1317. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  1318. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  1319. ui::LayerAnimatorTestController test_controller(
  1320. ui::LayerAnimator::CreateImplicitAnimator());
  1321. ui::ScopedLayerAnimationSettings layer_animation_settings(
  1322. test_controller.animator());
  1323. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  1324. // Create a tracked window. Expect it to be non-occluded.
  1325. MockWindowDelegate* delegate = new MockWindowDelegate();
  1326. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1327. auto* foo = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
  1328. foo->SetName("A");
  1329. EXPECT_FALSE(delegate->is_expecting_call());
  1330. // Create a non-tracked window which occludes the tracked window.
  1331. delegate->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1332. Window* window =
  1333. CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10), nullptr, layer_type());
  1334. window->SetName("B");
  1335. EXPECT_FALSE(delegate->is_expecting_call());
  1336. // Set the opacity to make the tracked window VISIBLE.
  1337. delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1338. SetOpacity(window, 0.1);
  1339. EXPECT_FALSE(delegate->is_expecting_call());
  1340. // Animate the window. WindowOcclusionTracker should add itself as an observer
  1341. // of its LayerAnimator (after |observer|). Upon beginning animation, the
  1342. // window should no longer affect the occluded region.
  1343. window->layer()->SetAnimator(test_controller.animator());
  1344. delegate->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1345. SetOpacity(window, 1.0);
  1346. EXPECT_TRUE(delegate->is_expecting_call());
  1347. // Drive the animation so that the color's alpha value changes.
  1348. test_controller.Step(kTransitionDuration / 2);
  1349. EXPECT_TRUE(delegate->is_expecting_call());
  1350. window->layer()->GetAnimator()->StopAnimating();
  1351. EXPECT_FALSE(delegate->is_expecting_call());
  1352. window->layer()->SetAnimator(nullptr);
  1353. }
  1354. namespace {
  1355. class WindowDelegateHidingWindowIfOccluded : public MockWindowDelegate {
  1356. public:
  1357. explicit WindowDelegateHidingWindowIfOccluded(Window* other_window)
  1358. : other_window_(other_window) {}
  1359. WindowDelegateHidingWindowIfOccluded(
  1360. const WindowDelegateHidingWindowIfOccluded&) = delete;
  1361. WindowDelegateHidingWindowIfOccluded& operator=(
  1362. const WindowDelegateHidingWindowIfOccluded&) = delete;
  1363. // MockWindowDelegate:
  1364. void OnWindowOcclusionChanged(
  1365. Window::OcclusionState occlusion_state) override {
  1366. MockWindowDelegate::OnWindowOcclusionChanged(occlusion_state);
  1367. if (occlusion_state == Window::OcclusionState::HIDDEN)
  1368. other_window_->Hide();
  1369. }
  1370. private:
  1371. raw_ptr<Window> other_window_;
  1372. };
  1373. class WindowDelegateWithQueuedExpectation : public MockWindowDelegate {
  1374. public:
  1375. WindowDelegateWithQueuedExpectation() = default;
  1376. WindowDelegateWithQueuedExpectation(
  1377. const WindowDelegateWithQueuedExpectation&) = delete;
  1378. WindowDelegateWithQueuedExpectation& operator=(
  1379. const WindowDelegateWithQueuedExpectation&) = delete;
  1380. void set_queued_expectation(Window::OcclusionState occlusion_state,
  1381. const SkRegion& occluded_region) {
  1382. queued_expected_occlusion_state_ = occlusion_state;
  1383. queued_expected_occluded_region_ = occluded_region;
  1384. }
  1385. // MockWindowDelegate:
  1386. void OnWindowOcclusionChanged(
  1387. Window::OcclusionState occlusion_state) override {
  1388. MockWindowDelegate::OnWindowOcclusionChanged(occlusion_state);
  1389. if (queued_expected_occlusion_state_ != Window::OcclusionState::UNKNOWN) {
  1390. set_expectation(queued_expected_occlusion_state_,
  1391. queued_expected_occluded_region_);
  1392. queued_expected_occlusion_state_ = Window::OcclusionState::UNKNOWN;
  1393. queued_expected_occluded_region_ = SkRegion();
  1394. }
  1395. }
  1396. private:
  1397. Window::OcclusionState queued_expected_occlusion_state_ =
  1398. Window::OcclusionState::UNKNOWN;
  1399. SkRegion queued_expected_occluded_region_ = SkRegion();
  1400. };
  1401. } // namespace
  1402. // Verify that a window delegate can change the visibility of another window
  1403. // when it is notified that its occlusion changed.
  1404. TEST_F(WindowOcclusionTrackerTest, HideFromOnWindowOcclusionChanged) {
  1405. // Create a tracked window. Expect it to be visible.
  1406. WindowDelegateWithQueuedExpectation* delegate_a =
  1407. new WindowDelegateWithQueuedExpectation();
  1408. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1409. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1410. EXPECT_FALSE(delegate_a->is_expecting_call());
  1411. // Create a tracked window. Expect it to be visible.
  1412. MockWindowDelegate* delegate_b =
  1413. new WindowDelegateHidingWindowIfOccluded(window_a);
  1414. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1415. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
  1416. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1417. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(10, 0, 10, 10));
  1418. EXPECT_FALSE(delegate_a->is_expecting_call());
  1419. EXPECT_FALSE(delegate_b->is_expecting_call());
  1420. // Hide the tracked window. It should be able to hide |window_a|. Before
  1421. // |window_a| is hidden, it will notice that the occlusion region has changed
  1422. // now that |window_b| is hidden. Then, it will be hidden by |window_b|.
  1423. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1424. delegate_a->set_queued_expectation(Window::OcclusionState::HIDDEN,
  1425. SkRegion());
  1426. delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1427. window_b->Hide();
  1428. EXPECT_FALSE(delegate_a->is_expecting_call());
  1429. EXPECT_FALSE(delegate_b->is_expecting_call());
  1430. EXPECT_FALSE(window_a->IsVisible());
  1431. EXPECT_FALSE(window_b->IsVisible());
  1432. }
  1433. namespace {
  1434. class WindowDelegateDeletingWindow : public MockWindowDelegate {
  1435. public:
  1436. WindowDelegateDeletingWindow() = default;
  1437. WindowDelegateDeletingWindow(const WindowDelegateDeletingWindow&) = delete;
  1438. WindowDelegateDeletingWindow& operator=(const WindowDelegateDeletingWindow&) =
  1439. delete;
  1440. void set_other_window(Window* other_window) { other_window_ = other_window; }
  1441. // MockWindowDelegate:
  1442. void OnWindowOcclusionChanged(
  1443. Window::OcclusionState occlusion_state) override {
  1444. MockWindowDelegate::OnWindowOcclusionChanged(occlusion_state);
  1445. if (occlusion_state == Window::OcclusionState::OCCLUDED) {
  1446. delete other_window_;
  1447. other_window_ = nullptr;
  1448. }
  1449. }
  1450. private:
  1451. raw_ptr<Window> other_window_ = nullptr;
  1452. };
  1453. } // namespace
  1454. // Verify that a window can delete a window that is on top of it when it is
  1455. // notified that its occlusion changed (a crash would occur if
  1456. // WindowOcclusionTracker accessed that window after it was deleted).
  1457. TEST_F(WindowOcclusionTrackerTest, DeleteFromOnWindowOcclusionChanged) {
  1458. // Create a tracked window. Expect it to be visible.
  1459. WindowDelegateDeletingWindow* delegate_a = new WindowDelegateDeletingWindow();
  1460. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1461. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1462. EXPECT_FALSE(delegate_a->is_expecting_call());
  1463. // Create a tracked window. Expect it to be visible.
  1464. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1465. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1466. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
  1467. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1468. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(10, 0, 10, 10));
  1469. EXPECT_FALSE(delegate_a->is_expecting_call());
  1470. EXPECT_FALSE(delegate_b->is_expecting_call());
  1471. // Create a tracked window. Expect it to be visible.
  1472. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  1473. delegate_a->set_expectation(
  1474. Window::OcclusionState::VISIBLE,
  1475. SkRegionFromSkIRects({SkIRect::MakeXYWH(10, 0, 10, 10),
  1476. SkIRect::MakeXYWH(20, 0, 10, 10)}));
  1477. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  1478. SkRegion(SkIRect::MakeXYWH(20, 0, 10, 10)));
  1479. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1480. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(20, 0, 10, 10));
  1481. EXPECT_FALSE(delegate_a->is_expecting_call());
  1482. EXPECT_FALSE(delegate_b->is_expecting_call());
  1483. EXPECT_FALSE(delegate_c->is_expecting_call());
  1484. // |window_c| will be deleted when |window_a| is occluded.
  1485. delegate_a->set_other_window(window_c);
  1486. // Move |window_b| on top of |window_a|.
  1487. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1488. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1489. window_b->SetBounds(window_a->bounds());
  1490. EXPECT_FALSE(delegate_a->is_expecting_call());
  1491. EXPECT_FALSE(delegate_b->is_expecting_call());
  1492. }
  1493. namespace {
  1494. class WindowDelegateChangingWindowVisibility : public MockWindowDelegate {
  1495. public:
  1496. WindowDelegateChangingWindowVisibility() = default;
  1497. WindowDelegateChangingWindowVisibility(
  1498. const WindowDelegateChangingWindowVisibility&) = delete;
  1499. WindowDelegateChangingWindowVisibility& operator=(
  1500. const WindowDelegateChangingWindowVisibility&) = delete;
  1501. void set_window_to_update(Window* window) { window_to_update_ = window; }
  1502. // MockWindowDelegate:
  1503. void OnWindowOcclusionChanged(
  1504. Window::OcclusionState occlusion_state) override {
  1505. MockWindowDelegate::OnWindowOcclusionChanged(occlusion_state);
  1506. if (!window_to_update_)
  1507. return;
  1508. ++num_occlusion_change_;
  1509. if (window_to_update_->IsVisible()) {
  1510. window_to_update_->Hide();
  1511. if (num_occlusion_change_ <= 3)
  1512. set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1513. } else {
  1514. window_to_update_->Show();
  1515. set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1516. }
  1517. }
  1518. private:
  1519. raw_ptr<Window> window_to_update_ = nullptr;
  1520. int num_occlusion_change_ = 0;
  1521. };
  1522. } // namespace
  1523. // Verify that if a window changes its visibility every time it is notified that
  1524. // its occlusion state changed, a DCHECK occurs.
  1525. TEST_F(WindowOcclusionTrackerTest, OcclusionStatesDontBecomeStable) {
  1526. test::WindowOcclusionTrackerTestApi test_api(
  1527. Env::GetInstance()->GetWindowOcclusionTracker());
  1528. // Create 2 superposed tracked windows.
  1529. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1530. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1531. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1532. EXPECT_FALSE(delegate_a->is_expecting_call());
  1533. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1534. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1535. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1536. CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
  1537. EXPECT_FALSE(delegate_a->is_expecting_call());
  1538. EXPECT_FALSE(delegate_b->is_expecting_call());
  1539. // Create a hidden tracked window.
  1540. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  1541. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  1542. SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
  1543. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1544. Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(10, 0, 10, 10));
  1545. EXPECT_FALSE(delegate_c->is_expecting_call());
  1546. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1547. delegate_c->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1548. window_c->Hide();
  1549. EXPECT_FALSE(delegate_c->is_expecting_call());
  1550. // Create a tracked window. Expect it to be non-occluded.
  1551. auto* delegate_d = new WindowDelegateChangingWindowVisibility();
  1552. delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
  1553. SkRegion(SkIRect::MakeXYWH(20, 0, 10, 10)));
  1554. delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1555. Window* window_d = CreateTrackedWindow(delegate_d, gfx::Rect(20, 0, 10, 10));
  1556. EXPECT_FALSE(delegate_b->is_expecting_call());
  1557. EXPECT_FALSE(delegate_d->is_expecting_call());
  1558. // Store a pointer to |window_d| in |delegate_d|. This will cause a call to
  1559. // Show()/Hide() every time |delegate_d| is notified of an occlusion change.
  1560. delegate_d->set_window_to_update(window_d);
  1561. // Hide |window_d|. This will cause occlusion to be recomputed multiple times.
  1562. // Once the maximum number of times that occlusion can be recomputed is
  1563. // reached, the occlusion state of all IsVisible() windows should be set to
  1564. // VISIBLE.
  1565. EXPECT_DCHECK_DEATH({
  1566. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1567. SkRegion(SkIRect::MakeXYWH(20, 0, 10, 10)));
  1568. delegate_d->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1569. window_d->Hide();
  1570. });
  1571. }
  1572. // Verify that the occlusion states are correctly updated when a branch of the
  1573. // tree is hidden.
  1574. TEST_F(WindowOcclusionTrackerTest, HideTreeBranch) {
  1575. // Create a branch of 3 tracked windows. Expect them to be visible.
  1576. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1577. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1578. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1579. EXPECT_FALSE(delegate_a->is_expecting_call());
  1580. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1581. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1582. Window* window_b =
  1583. CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10), window_a);
  1584. EXPECT_FALSE(delegate_b->is_expecting_call());
  1585. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  1586. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1587. CreateTrackedWindow(delegate_c, gfx::Rect(0, 20, 10, 10), window_b);
  1588. EXPECT_FALSE(delegate_c->is_expecting_call());
  1589. // Hide |window_b| (and hence |window_c|). Expect |window_b| and |window_c| to
  1590. // be hidden.
  1591. delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1592. delegate_c->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1593. window_b->Hide();
  1594. EXPECT_FALSE(delegate_b->is_expecting_call());
  1595. EXPECT_FALSE(delegate_c->is_expecting_call());
  1596. }
  1597. // Verify that a window covered by a shaped window isn't considered occluded.
  1598. TEST_F(WindowOcclusionTrackerTest, WindowWithAlphaShape) {
  1599. // Create 2 superposed tracked windows.
  1600. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1601. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1602. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1603. EXPECT_FALSE(delegate_a->is_expecting_call());
  1604. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1605. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1606. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1607. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
  1608. EXPECT_FALSE(delegate_a->is_expecting_call());
  1609. EXPECT_FALSE(delegate_b->is_expecting_call());
  1610. // Set a shape for the top window. The window underneath should no longer be
  1611. // occluded.
  1612. auto shape = std::make_unique<ui::Layer::ShapeRects>();
  1613. shape->emplace_back(0, 0, 5, 5);
  1614. // Shaped windows are not considered opaque, so the occluded region is empty.
  1615. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1616. window_b->layer()->SetAlphaShape(std::move(shape));
  1617. EXPECT_FALSE(delegate_a->is_expecting_call());
  1618. // Clear the shape for the top window. The window underneath should be
  1619. // occluded.
  1620. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1621. window_b->layer()->SetAlphaShape(nullptr);
  1622. EXPECT_FALSE(delegate_a->is_expecting_call());
  1623. }
  1624. // Verify that a window covered by a window whose parent has an alpha shape
  1625. // isn't considered occluded.
  1626. TEST_F(WindowOcclusionTrackerTest, WindowWithParentAlphaShape) {
  1627. // Create a child and parent that cover another window.
  1628. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1629. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1630. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  1631. EXPECT_FALSE(delegate_a->is_expecting_call());
  1632. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1633. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1634. SkRegion(SkIRect::MakeWH(10, 10)));
  1635. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1636. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
  1637. EXPECT_FALSE(delegate_a->is_expecting_call());
  1638. EXPECT_FALSE(delegate_b->is_expecting_call());
  1639. MockWindowDelegate* delegate_c = new MockWindowDelegate();
  1640. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1641. delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1642. CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 20, 20), window_b);
  1643. EXPECT_FALSE(delegate_a->is_expecting_call());
  1644. EXPECT_FALSE(delegate_c->is_expecting_call());
  1645. // Set a shape for |window_b|. |window_a| and |window_b| should no longer be
  1646. // occluded.
  1647. auto shape = std::make_unique<ui::Layer::ShapeRects>();
  1648. shape->emplace_back(0, 0, 5, 5);
  1649. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1650. window_b->layer()->SetAlphaShape(std::move(shape));
  1651. EXPECT_FALSE(delegate_a->is_expecting_call());
  1652. // Clear the shape for |window_b|. |window_a| and |window_b| should be
  1653. // occluded.
  1654. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1655. window_b->layer()->SetAlphaShape(nullptr);
  1656. EXPECT_FALSE(delegate_a->is_expecting_call());
  1657. }
  1658. namespace {
  1659. class WindowDelegateHidingWindow : public MockWindowDelegate {
  1660. public:
  1661. WindowDelegateHidingWindow() = default;
  1662. WindowDelegateHidingWindow(const WindowDelegateHidingWindow&) = delete;
  1663. WindowDelegateHidingWindow& operator=(const WindowDelegateHidingWindow&) =
  1664. delete;
  1665. void set_window_to_update(Window* window) { window_to_update_ = window; }
  1666. // MockWindowDelegate:
  1667. void OnWindowOcclusionChanged(
  1668. Window::OcclusionState occlusion_state) override {
  1669. MockWindowDelegate::OnWindowOcclusionChanged(occlusion_state);
  1670. if (!window_to_update_)
  1671. return;
  1672. window_to_update_->Hide();
  1673. }
  1674. private:
  1675. raw_ptr<Window> window_to_update_ = nullptr;
  1676. };
  1677. class WindowDelegateAddingAndHidingChild : public MockWindowDelegate {
  1678. public:
  1679. explicit WindowDelegateAddingAndHidingChild(WindowOcclusionTrackerTest* test)
  1680. : test_(test) {}
  1681. WindowDelegateAddingAndHidingChild(
  1682. const WindowDelegateAddingAndHidingChild&) = delete;
  1683. WindowDelegateAddingAndHidingChild& operator=(
  1684. const WindowDelegateAddingAndHidingChild&) = delete;
  1685. void set_queued_expectation(Window::OcclusionState occlusion_state,
  1686. const SkRegion& occluded_region) {
  1687. queued_expected_occlusion_state_ = occlusion_state;
  1688. queued_expected_occluded_region_ = occluded_region;
  1689. }
  1690. void set_window_to_update(Window* window) { window_to_update_ = window; }
  1691. // MockWindowDelegate:
  1692. void OnWindowOcclusionChanged(
  1693. Window::OcclusionState occlusion_state) override {
  1694. MockWindowDelegate::OnWindowOcclusionChanged(occlusion_state);
  1695. if (queued_expected_occlusion_state_ != Window::OcclusionState::UNKNOWN) {
  1696. set_expectation(queued_expected_occlusion_state_,
  1697. queued_expected_occluded_region_);
  1698. queued_expected_occlusion_state_ = Window::OcclusionState::UNKNOWN;
  1699. queued_expected_occluded_region_ = SkRegion();
  1700. }
  1701. if (!window_to_update_)
  1702. return;
  1703. // Create a child window and hide it. Since this code runs when occlusion
  1704. // has already been recomputed twice, if one of the operations below causes
  1705. // occlusion to be recomputed, the test will fail with a DCHECK.
  1706. Window* window =
  1707. test_->CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5), window_to_update_);
  1708. window->Hide();
  1709. }
  1710. private:
  1711. raw_ptr<WindowOcclusionTrackerTest> test_;
  1712. raw_ptr<Window> window_to_update_ = nullptr;
  1713. Window::OcclusionState queued_expected_occlusion_state_ =
  1714. Window::OcclusionState::UNKNOWN;
  1715. SkRegion queued_expected_occluded_region_ = SkRegion();
  1716. };
  1717. } // namespace
  1718. // Verify that hiding a window that has a hidden parent doesn't cause occlusion
  1719. // to be recomputed.
  1720. TEST_F(WindowOcclusionTrackerTest,
  1721. HideWindowWithHiddenParentOnOcclusionChange) {
  1722. test::WindowOcclusionTrackerTestApi test_api(
  1723. Env::GetInstance()->GetWindowOcclusionTracker());
  1724. auto* delegate_a = new WindowDelegateAddingAndHidingChild(this);
  1725. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1726. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1727. EXPECT_FALSE(delegate_a->is_expecting_call());
  1728. auto* delegate_b = new WindowDelegateHidingWindow();
  1729. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1730. SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
  1731. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1732. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
  1733. EXPECT_FALSE(delegate_a->is_expecting_call());
  1734. EXPECT_FALSE(delegate_b->is_expecting_call());
  1735. // When |window_b| is hidden, it will hide |window_a|. |window_a| will in turn
  1736. // add a child to itself and hide it.
  1737. delegate_a->set_window_to_update(window_a);
  1738. delegate_b->set_window_to_update(window_a);
  1739. // Initially A is marked as visible with no potential occlusion.
  1740. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1741. delegate_a->set_queued_expectation(Window::OcclusionState::HIDDEN,
  1742. SkRegion());
  1743. delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1744. // Hiding a child to |window_a| and hiding it shouldn't cause occlusion to be
  1745. // recomputed too many times (i.e. the call below shouldn't DCHECK).
  1746. window_b->Hide();
  1747. EXPECT_FALSE(delegate_a->is_expecting_call());
  1748. EXPECT_FALSE(delegate_b->is_expecting_call());
  1749. }
  1750. // Verify that hiding a window changes the occlusion region to show that the
  1751. // window is fully occluded.
  1752. TEST_F(WindowOcclusionTrackerTest,
  1753. HideWindowChangesOcclusionRegionToBeFullyOccluded) {
  1754. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1755. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1756. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1757. EXPECT_FALSE(delegate_a->is_expecting_call());
  1758. delegate_a->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1759. window_a->Hide();
  1760. EXPECT_FALSE(delegate_a->is_expecting_call());
  1761. }
  1762. // Test partial occlusion, test partial occlusion changing hidden, alpha shape
  1763. // occlusion from multiple windows
  1764. // Verify that a window can occlude another one partially.
  1765. TEST_F(WindowOcclusionTrackerTest, WindowOccludesWindowPartially) {
  1766. // Create window a. Expect it to be non-occluded.
  1767. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1768. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1769. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  1770. EXPECT_FALSE(delegate_a->is_expecting_call());
  1771. // Create window b, occluding window a partially.
  1772. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1773. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1774. SkRegion(SkIRect::MakeXYWH(6, 7, 8, 9)));
  1775. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1776. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(6, 7, 8, 9));
  1777. EXPECT_FALSE(delegate_a->is_expecting_call());
  1778. EXPECT_FALSE(delegate_b->is_expecting_call());
  1779. // Hiding window b should stop occluding window a partially.
  1780. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1781. delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1782. window_b->Hide();
  1783. EXPECT_FALSE(delegate_a->is_expecting_call());
  1784. EXPECT_FALSE(delegate_b->is_expecting_call());
  1785. }
  1786. // Verify that windows with alpha shape do not affect occlusion regions.
  1787. TEST_F(WindowOcclusionTrackerTest,
  1788. WindowWithAlphaShapeDoesNotPartiallyOccludeOtherWindows) {
  1789. // Create window a. Expect it to be non-occluded.
  1790. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1791. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1792. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
  1793. EXPECT_FALSE(delegate_a->is_expecting_call());
  1794. // Create window b, occluding window a partially.
  1795. MockWindowDelegate* delegate_b = new MockWindowDelegate();
  1796. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1797. SkRegion(SkIRect::MakeXYWH(6, 7, 8, 9)));
  1798. delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1799. Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(6, 7, 8, 9));
  1800. EXPECT_FALSE(delegate_a->is_expecting_call());
  1801. EXPECT_FALSE(delegate_b->is_expecting_call());
  1802. // Set a shape for window b. The window underneath should no longer be
  1803. // partially occluded.
  1804. auto shape = std::make_unique<ui::Layer::ShapeRects>();
  1805. shape->emplace_back(0, 0, 5, 5);
  1806. // Shaped windows are not considered opaque, so the occluded region is empty.
  1807. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1808. window_b->layer()->SetAlphaShape(std::move(shape));
  1809. EXPECT_FALSE(delegate_a->is_expecting_call());
  1810. // Clear the shape for the top window. The window underneath should be
  1811. // occluded.
  1812. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1813. SkRegion(SkIRect::MakeXYWH(6, 7, 8, 9)));
  1814. window_b->layer()->SetAlphaShape(nullptr);
  1815. EXPECT_FALSE(delegate_a->is_expecting_call());
  1816. }
  1817. // Verify that a window can be occluded by multiple other windows.
  1818. TEST_F(WindowOcclusionTrackerTest, WindowCanBeOccludedByMultipleWindows) {
  1819. // Create window a. Expect it to be non-occluded.
  1820. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1821. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1822. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1823. EXPECT_FALSE(delegate_a->is_expecting_call());
  1824. SkRegion window_a_occlusion = SkRegion(SkIRect::MakeXYWH(9, 9, 5, 5));
  1825. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1826. window_a_occlusion);
  1827. CreateUntrackedWindow(gfx::Rect(9, 9, 5, 5));
  1828. EXPECT_FALSE(delegate_a->is_expecting_call());
  1829. window_a_occlusion.op(SkIRect::MakeXYWH(-4, -4, 5, 5),
  1830. SkRegion::Op::kUnion_Op);
  1831. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1832. window_a_occlusion);
  1833. CreateUntrackedWindow(gfx::Rect(-4, -4, 5, 5));
  1834. EXPECT_FALSE(delegate_a->is_expecting_call());
  1835. window_a_occlusion.op(SkIRect::MakeXYWH(9, -4, 5, 5),
  1836. SkRegion::Op::kUnion_Op);
  1837. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1838. window_a_occlusion);
  1839. CreateUntrackedWindow(gfx::Rect(9, -4, 5, 5));
  1840. EXPECT_FALSE(delegate_a->is_expecting_call());
  1841. window_a_occlusion.op(SkIRect::MakeXYWH(5, 5, 2, 3), SkRegion::Op::kUnion_Op);
  1842. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1843. window_a_occlusion);
  1844. CreateUntrackedWindow(gfx::Rect(5, 5, 2, 3));
  1845. EXPECT_FALSE(delegate_a->is_expecting_call());
  1846. }
  1847. // Verify that the excluded window is indeed ignored by occlusion tracking.
  1848. TEST_P(WindowOcclusionTrackerOpacityTest, ExcludeWindow) {
  1849. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1850. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1851. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1852. EXPECT_FALSE(delegate_a->is_expecting_call());
  1853. delegate_a->SetName("WindowA");
  1854. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1855. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 100, 100), nullptr);
  1856. EXPECT_FALSE(delegate_a->is_expecting_call());
  1857. MockWindowDelegate* delegate_bb = new MockWindowDelegate();
  1858. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1859. CreateTrackedWindow(delegate_bb, gfx::Rect(0, 0, 10, 10), window_b);
  1860. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1861. delegate_bb->SetName("WindowBB");
  1862. delegate_bb->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1863. Window* window_c = CreateUntrackedWindow(gfx::Rect(0, 0, 100, 100), nullptr);
  1864. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1865. {
  1866. // |window_b| is excluded, so its child's occlusion state becomes VISIBlE.
  1867. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1868. EXPECT_TRUE(delegate_bb->is_expecting_call());
  1869. WindowOcclusionTracker::ScopedExclude scoped(window_b);
  1870. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1871. // Moving |window_c| out from |window_a| will make |window_a| visible
  1872. // because |window_b| is ignored.
  1873. SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
  1874. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1875. window_a_occlusion);
  1876. SkRegion window_bb_occlusion;
  1877. window_bb_occlusion.op(SkIRect::MakeXYWH(100, 100, 100, 100),
  1878. SkRegion::kUnion_Op);
  1879. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE,
  1880. window_bb_occlusion);
  1881. window_c->SetBounds(gfx::Rect(100, 100, 100, 100));
  1882. // Un-excluding wil make |window_bb| OCCLUDED.
  1883. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1884. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE,
  1885. window_bb_occlusion);
  1886. }
  1887. EXPECT_FALSE(delegate_a->is_expecting_call());
  1888. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1889. {
  1890. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1891. SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
  1892. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1893. window_a_occlusion);
  1894. EXPECT_TRUE(delegate_bb->is_expecting_call());
  1895. WindowOcclusionTracker::ScopedExclude scoped(window_b);
  1896. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1897. EXPECT_FALSE(delegate_a->is_expecting_call());
  1898. // Moving |window_b| will not affect the occlusion status.
  1899. window_b->SetBounds(gfx::Rect(5, 5, 100, 100));
  1900. // Un-excluding will update the occlustion status.
  1901. // A's occlustion status includes all windows above a.
  1902. window_a_occlusion.setEmpty();
  1903. window_a_occlusion.op(SkIRect::MakeXYWH(5, 5, 100, 100),
  1904. SkRegion::kUnion_Op);
  1905. window_a_occlusion.op(SkIRect::MakeXYWH(100, 100, 100, 100),
  1906. SkRegion::kUnion_Op);
  1907. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1908. window_a_occlusion);
  1909. SkRegion window_bb_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
  1910. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE,
  1911. window_bb_occlusion);
  1912. }
  1913. EXPECT_FALSE(delegate_a->is_expecting_call());
  1914. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1915. {
  1916. delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1917. SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
  1918. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1919. window_a_occlusion);
  1920. EXPECT_TRUE(delegate_bb->is_expecting_call());
  1921. WindowOcclusionTracker::ScopedExclude scoped(window_b);
  1922. EXPECT_FALSE(delegate_bb->is_expecting_call());
  1923. EXPECT_FALSE(delegate_a->is_expecting_call());
  1924. // Deleting the excluded window will un-exclude itself and recomputes the
  1925. // occlustion state, but should not affect the state on existing windows
  1926. // because it's already excluded.
  1927. delete window_b;
  1928. EXPECT_FALSE(scoped.window());
  1929. }
  1930. MockWindowDelegate* delegate_d = new MockWindowDelegate();
  1931. delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1932. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1933. auto* window_d = CreateTrackedWindow(delegate_d, gfx::Rect(0, 0, 10, 10),
  1934. nullptr, false, layer_type());
  1935. window_d->SetName("WindowD");
  1936. EXPECT_FALSE(delegate_a->is_expecting_call());
  1937. EXPECT_FALSE(delegate_d->is_expecting_call());
  1938. {
  1939. // Make sure excluding the tracked window also works.
  1940. SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
  1941. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  1942. window_a_occlusion);
  1943. WindowOcclusionTracker::ScopedExclude scoped(window_d);
  1944. EXPECT_FALSE(delegate_a->is_expecting_call());
  1945. // Changing opacity/bounds shouldn't change the occlusion state.
  1946. SetOpacity(window_d, 0.5f);
  1947. window_d->SetBounds(gfx::Rect(0, 0, 20, 20));
  1948. // A is now visible even if |window_d| is un-excluded because
  1949. // window_d is not fully opaque.
  1950. }
  1951. EXPECT_FALSE(delegate_a->is_expecting_call());
  1952. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1953. SetOpacity(window_d, 1.f);
  1954. EXPECT_FALSE(delegate_a->is_expecting_call());
  1955. }
  1956. // Test that calling OnOcclusionStateChanged on a root window causes children
  1957. // of the root window to have their delegate notified that it is occluded or
  1958. // visible, depending on whether the root window is occluded or not.
  1959. TEST_F(WindowOcclusionTrackerTest, NativeWindowOcclusion) {
  1960. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1961. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1962. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1963. EXPECT_FALSE(delegate_a->is_expecting_call());
  1964. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1965. // Make the host call OnOcclusionStateChanged on the root window.
  1966. host()->SetNativeWindowOcclusionState(Window::OcclusionState::OCCLUDED, {});
  1967. EXPECT_FALSE(delegate_a->is_expecting_call());
  1968. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1969. host()->SetNativeWindowOcclusionState(Window::OcclusionState::VISIBLE, {});
  1970. EXPECT_FALSE(delegate_a->is_expecting_call());
  1971. }
  1972. TEST_F(WindowOcclusionTrackerTest, ScopedForceVisible) {
  1973. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1974. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1975. Window* window = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1976. EXPECT_FALSE(delegate_a->is_expecting_call());
  1977. delegate_a->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1978. window->Hide();
  1979. EXPECT_FALSE(delegate_a->is_expecting_call());
  1980. // Using ScopedForceVisible when the window is hidden should force it visible.
  1981. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1982. {
  1983. WindowOcclusionTracker::ScopedForceVisible force_visible(window);
  1984. EXPECT_FALSE(delegate_a->is_expecting_call());
  1985. // Destroying the ScopedForceVisible should return the window to hidden.
  1986. delegate_a->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
  1987. }
  1988. EXPECT_FALSE(delegate_a->is_expecting_call());
  1989. }
  1990. TEST_F(WindowOcclusionTrackerTest, ScopedForceVisibleSiblingsIgnored) {
  1991. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  1992. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  1993. Window* window = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  1994. EXPECT_FALSE(delegate_a->is_expecting_call());
  1995. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  1996. CreateUntrackedWindow(gfx::Rect(0, 0, 100, 100), nullptr);
  1997. EXPECT_FALSE(delegate_a->is_expecting_call());
  1998. // Using ScopedForceVisible when the window is occluded should force it
  1999. // visible.
  2000. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2001. {
  2002. WindowOcclusionTracker::ScopedForceVisible force_visible(window);
  2003. EXPECT_FALSE(delegate_a->is_expecting_call());
  2004. // Destroying the ScopedForceVisible should return the window to hidden.
  2005. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2006. }
  2007. EXPECT_FALSE(delegate_a->is_expecting_call());
  2008. }
  2009. TEST_F(WindowOcclusionTrackerTest, ScopedForceVisibleWithOccludedSibling) {
  2010. // Creates three windows, a parent with two children. Both children have
  2011. // the same bounds.
  2012. std::unique_ptr<WindowOcclusionTracker::ScopedPause>
  2013. pause_occlusion_tracking =
  2014. std::make_unique<WindowOcclusionTracker::ScopedPause>();
  2015. MockWindowDelegate* parent_delegate = new MockWindowDelegate();
  2016. Window* parent_window =
  2017. CreateTrackedWindow(parent_delegate, gfx::Rect(0, 0, 10, 10));
  2018. EXPECT_FALSE(parent_delegate->is_expecting_call());
  2019. MockWindowDelegate* occluded_child_delegate = new MockWindowDelegate();
  2020. CreateTrackedWindow(occluded_child_delegate, gfx::Rect(0, 0, 10, 10),
  2021. parent_window);
  2022. EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
  2023. MockWindowDelegate* visible_child_delegate = new MockWindowDelegate();
  2024. CreateTrackedWindow(visible_child_delegate, gfx::Rect(0, 0, 10, 10),
  2025. parent_window);
  2026. EXPECT_FALSE(visible_child_delegate->is_expecting_call());
  2027. // Initial state after creation.
  2028. parent_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2029. occluded_child_delegate->set_expectation(Window::OcclusionState::OCCLUDED);
  2030. visible_child_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2031. pause_occlusion_tracking.reset();
  2032. EXPECT_FALSE(parent_delegate->is_expecting_call());
  2033. EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
  2034. EXPECT_FALSE(visible_child_delegate->is_expecting_call());
  2035. // Hiding the parent should result in all windows being hidden.
  2036. parent_delegate->set_expectation(Window::OcclusionState::HIDDEN);
  2037. occluded_child_delegate->set_expectation(Window::OcclusionState::HIDDEN);
  2038. visible_child_delegate->set_expectation(Window::OcclusionState::HIDDEN);
  2039. parent_window->Hide();
  2040. EXPECT_FALSE(parent_delegate->is_expecting_call());
  2041. EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
  2042. EXPECT_FALSE(visible_child_delegate->is_expecting_call());
  2043. // Creating a ScopedForceVisible for the parent should return to the initial
  2044. // state.
  2045. parent_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2046. occluded_child_delegate->set_expectation(Window::OcclusionState::OCCLUDED);
  2047. visible_child_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2048. WindowOcclusionTracker::ScopedForceVisible force_visible(parent_window);
  2049. EXPECT_FALSE(parent_delegate->is_expecting_call());
  2050. EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
  2051. EXPECT_FALSE(visible_child_delegate->is_expecting_call());
  2052. // Do another show, so that once the |force_visible| is destroyed the
  2053. // assertions in MockWindowDelegate aren't tripped.
  2054. parent_window->Show();
  2055. }
  2056. // Simulates a scenario in which a browser window is forced visible (e.g. while
  2057. // projecting) and its parent container (e.g. a virtual desks container) was
  2058. // hidden. Verifies that the browser window and its descendants remain visible
  2059. // from an occlusion stand point.
  2060. TEST_F(WindowOcclusionTrackerTest, ScopedForceVisibleHiddenContainer) {
  2061. std::unique_ptr<WindowOcclusionTracker::ScopedPause>
  2062. pause_occlusion_tracking =
  2063. std::make_unique<WindowOcclusionTracker::ScopedPause>();
  2064. MockWindowDelegate* root_delegate = new MockWindowDelegate();
  2065. Window* root = CreateTrackedWindow(root_delegate, gfx::Rect(0, 0, 100, 100));
  2066. MockWindowDelegate* container_delegate = new MockWindowDelegate();
  2067. Window* container =
  2068. CreateTrackedWindow(container_delegate, gfx::Rect(0, 0, 100, 100), root);
  2069. MockWindowDelegate* browser_delegate = new MockWindowDelegate();
  2070. Window* browser =
  2071. CreateTrackedWindow(browser_delegate, gfx::Rect(0, 0, 10, 10), container);
  2072. MockWindowDelegate* webcontents_delegate = new MockWindowDelegate();
  2073. Window* webcontents = CreateTrackedWindow(webcontents_delegate,
  2074. gfx::Rect(0, 0, 10, 10), browser);
  2075. root_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2076. container_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2077. browser_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2078. webcontents_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2079. pause_occlusion_tracking.reset();
  2080. EXPECT_FALSE(root_delegate->is_expecting_call());
  2081. EXPECT_FALSE(container_delegate->is_expecting_call());
  2082. EXPECT_FALSE(browser_delegate->is_expecting_call());
  2083. EXPECT_FALSE(webcontents_delegate->is_expecting_call());
  2084. WindowOcclusionTracker::ScopedForceVisible force_visible(browser);
  2085. container_delegate->set_expectation(Window::OcclusionState::HIDDEN);
  2086. container->Hide();
  2087. EXPECT_FALSE(root_delegate->is_expecting_call());
  2088. EXPECT_FALSE(container_delegate->is_expecting_call());
  2089. EXPECT_FALSE(browser_delegate->is_expecting_call());
  2090. EXPECT_FALSE(webcontents_delegate->is_expecting_call());
  2091. EXPECT_EQ(Window::OcclusionState::VISIBLE, webcontents->GetOcclusionState());
  2092. EXPECT_TRUE(webcontents->TargetVisibility());
  2093. container_delegate->set_expectation(Window::OcclusionState::VISIBLE);
  2094. container->Show();
  2095. }
  2096. TEST_F(WindowOcclusionTrackerTest, ComputeTargetOcclusionForWindow) {
  2097. auto* window_a = CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10));
  2098. CreateUntrackedWindow(gfx::Rect(9, 9, 5, 5));
  2099. CreateUntrackedWindow(gfx::Rect(-4, -4, 5, 5));
  2100. CreateUntrackedWindow(gfx::Rect(9, -4, 5, 5));
  2101. CreateUntrackedWindow(gfx::Rect(5, 5, 2, 3));
  2102. SkRegion window_a_occlusion = SkRegionFromSkIRects(
  2103. {SkIRect::MakeXYWH(9, 9, 5, 5), SkIRect::MakeXYWH(-4, -4, 5, 5),
  2104. SkIRect::MakeXYWH(9, -4, 5, 5), SkIRect::MakeXYWH(5, 5, 2, 3)});
  2105. auto& occlusion_tracker = GetOcclusionTracker();
  2106. window_a->TrackOcclusionState();
  2107. auto occlusion_data =
  2108. occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2109. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2110. EXPECT_EQ(window_a_occlusion, occlusion_data.occluded_region);
  2111. }
  2112. TEST_F(WindowOcclusionTrackerTest,
  2113. ComputeTargetOcclusionForWindowUsesTargetBounds) {
  2114. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2115. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2116. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  2117. EXPECT_FALSE(delegate_a->is_expecting_call());
  2118. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  2119. SkRegion(SkIRect::MakeXYWH(10, 10, 10, 10)));
  2120. Window* window_b = CreateUntrackedWindow(gfx::Rect(10, 10, 10, 10));
  2121. EXPECT_FALSE(delegate_a->is_expecting_call());
  2122. // Should be visible for target occlusion.
  2123. auto& occlusion_tracker = GetOcclusionTracker();
  2124. auto occlusion_data =
  2125. occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2126. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2127. EXPECT_EQ(SkRegion(SkIRect::MakeXYWH(10, 10, 10, 10)),
  2128. occlusion_data.occluded_region);
  2129. // Start animating |window_b| to fully occlude |window_a|.
  2130. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  2131. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  2132. ui::LayerAnimatorTestController test_controller(
  2133. ui::LayerAnimator::CreateImplicitAnimator());
  2134. ui::ScopedLayerAnimationSettings layer_animation_settings(
  2135. test_controller.animator());
  2136. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  2137. window_b->layer()->SetAnimator(test_controller.animator());
  2138. window_b->SetBounds(gfx::Rect(0, 0, 10, 10));
  2139. // Animated windows are ignored by the occlusion tracker.
  2140. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2141. test_controller.Step(kTransitionDuration / 3);
  2142. EXPECT_FALSE(delegate_a->is_expecting_call());
  2143. // Target occlusion should include them, however:
  2144. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2145. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2146. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2147. // Don't expect the target occlusion to be affected by progress through
  2148. // the animation.
  2149. test_controller.Step(kTransitionDuration / 3);
  2150. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2151. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2152. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2153. // Finish the animation, and expect the occlusion state to update.
  2154. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2155. test_controller.Step(kTransitionDuration / 3);
  2156. window_b->layer()->SetAnimator(nullptr);
  2157. EXPECT_FALSE(delegate_a->is_expecting_call());
  2158. }
  2159. TEST_P(WindowOcclusionTrackerOpacityTest,
  2160. ComputeTargetOcclusionForWindowUsesTargetOpacity) {
  2161. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2162. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2163. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  2164. EXPECT_FALSE(delegate_a->is_expecting_call());
  2165. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2166. Window* window_b =
  2167. CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10), nullptr, layer_type());
  2168. EXPECT_FALSE(delegate_a->is_expecting_call());
  2169. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2170. SetOpacity(window_b, 0.0f);
  2171. EXPECT_FALSE(delegate_a->is_expecting_call());
  2172. // Should be visible for target occlusion.
  2173. auto& occlusion_tracker = GetOcclusionTracker();
  2174. auto occlusion_data =
  2175. occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2176. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2177. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2178. // Start animating |window_b| to fully occlude |window_a|.
  2179. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  2180. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  2181. ui::LayerAnimatorTestController test_controller(
  2182. ui::LayerAnimator::CreateImplicitAnimator());
  2183. ui::ScopedLayerAnimationSettings layer_animation_settings(
  2184. test_controller.animator());
  2185. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  2186. window_b->layer()->SetAnimator(test_controller.animator());
  2187. SetOpacity(window_b, 1.0f);
  2188. // Opacity animation uses threaded animation.
  2189. test_controller.StartThreadedAnimationsIfNeeded();
  2190. // Animated windows are ignored by the occlusion tracker.
  2191. test_controller.Step(kTransitionDuration / 3);
  2192. // Target occlusion should include them, however:
  2193. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2194. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2195. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2196. // Don't expect the target occlusion to be affected by progress through
  2197. // the animation.
  2198. test_controller.Step(kTransitionDuration / 3);
  2199. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2200. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2201. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2202. // Finish the animation, and expect the occlusion state to update.
  2203. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2204. test_controller.Step(kTransitionDuration / 3);
  2205. // Explicitly stop animation because threaded animation may have started
  2206. // a bit later. |kTransitionDuration| may not be quite enough to reach the
  2207. // end.
  2208. window_b->layer()->GetAnimator()->StopAnimating();
  2209. window_b->layer()->SetAnimator(nullptr);
  2210. EXPECT_FALSE(delegate_a->is_expecting_call());
  2211. }
  2212. TEST_F(WindowOcclusionTrackerTest,
  2213. ComputeTargetOcclusionForWindowUsesTargetVisibility) {
  2214. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2215. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2216. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  2217. EXPECT_FALSE(delegate_a->is_expecting_call());
  2218. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2219. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10));
  2220. EXPECT_FALSE(delegate_a->is_expecting_call());
  2221. // Hide the window.
  2222. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2223. window_b->Hide();
  2224. EXPECT_FALSE(delegate_a->is_expecting_call());
  2225. // Should be visible for target occlusion.
  2226. auto& occlusion_tracker = GetOcclusionTracker();
  2227. auto occlusion_data =
  2228. occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2229. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2230. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2231. // Start animating |window_b| to fully occlude |window_a|.
  2232. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  2233. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  2234. ui::LayerAnimatorTestController test_controller(
  2235. ui::LayerAnimator::CreateImplicitAnimator());
  2236. ui::ScopedLayerAnimationSettings layer_animation_settings(
  2237. test_controller.animator());
  2238. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  2239. window_b->layer()->SetAnimator(test_controller.animator());
  2240. window_b->Show();
  2241. // Animated windows are ignored by the occlusion tracker.
  2242. test_controller.Step(kTransitionDuration / 3);
  2243. // Target occlusion should include them, however:
  2244. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2245. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2246. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2247. // Don't expect the target occlusion to be affected by progress through
  2248. // the animation.
  2249. test_controller.Step(kTransitionDuration / 3);
  2250. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2251. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2252. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2253. // Finish the animation, and expect the occlusion state to update.
  2254. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2255. test_controller.Step(kTransitionDuration / 3);
  2256. window_b->layer()->SetAnimator(nullptr);
  2257. EXPECT_FALSE(delegate_a->is_expecting_call());
  2258. }
  2259. TEST_F(WindowOcclusionTrackerTest,
  2260. ComputeTargetOcclusionForWindowTransformHierarchy) {
  2261. gfx::Transform scale_2x_transform;
  2262. scale_2x_transform.Scale(2.0f, 2.0f);
  2263. // Effective bounds: x = 2, y = 2, height = 10, width = 10
  2264. Window* window_a = CreateUntrackedWindow(gfx::Rect(2, 2, 5, 5));
  2265. window_a->SetTransform(scale_2x_transform);
  2266. // Effective bounds: x = 4, y = 4, height = 4, width = 4
  2267. Window* window_b = CreateUntrackedWindow(gfx::Rect(1, 1, 2, 2), window_a);
  2268. // Effective bounds: x = 34, y = 36, height = 8, width = 10
  2269. CreateUntrackedWindow(gfx::Rect(15, 16, 4, 5), window_b);
  2270. Window* window_d = CreateUntrackedWindow(gfx::Rect(34, 36, 8, 10));
  2271. auto& occlusion_tracker = GetOcclusionTracker();
  2272. window_d->TrackOcclusionState();
  2273. auto occlusion_data =
  2274. occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
  2275. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2276. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2277. root_window()->StackChildAtBottom(window_d);
  2278. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
  2279. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2280. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2281. window_d->SetBounds(gfx::Rect(63, 72, 8, 10));
  2282. SkRegion occluded_area = SkRegionFromSkIRects(
  2283. {SkIRect::MakeXYWH(2, 2, 10, 10), SkIRect::MakeXYWH(4, 4, 4, 4),
  2284. SkIRect::MakeXYWH(34, 36, 8, 10)});
  2285. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
  2286. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2287. EXPECT_EQ(occluded_area, occlusion_data.occluded_region);
  2288. // Set a target transform on |window_b| which should increase the size of
  2289. // its child window, occluding |window_d|.
  2290. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  2291. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  2292. ui::LayerAnimatorTestController test_controller(
  2293. ui::LayerAnimator::CreateImplicitAnimator());
  2294. ui::ScopedLayerAnimationSettings layer_animation_settings(
  2295. test_controller.animator());
  2296. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  2297. // Scale |window_b|.
  2298. // |window_b| effective bounds: 4,4 8x8
  2299. // |window_b|'s child's effective bounds: 64,68 16x20
  2300. window_b->layer()->SetAnimator(test_controller.animator());
  2301. window_b->layer()->SetTransform(scale_2x_transform);
  2302. // Transform animation uses threaded animation.
  2303. test_controller.StartThreadedAnimationsIfNeeded();
  2304. // Animated windows are ignored by the occlusion tracker.
  2305. test_controller.Step(kTransitionDuration / 3);
  2306. // Target occlusion should include them, however:
  2307. SkRegion occluded_area_transformed = SkRegionFromSkIRects(
  2308. {SkIRect::MakeXYWH(2, 2, 10, 10), SkIRect::MakeXYWH(4, 4, 8, 8),
  2309. SkIRect::MakeXYWH(64, 68, 16, 20)});
  2310. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
  2311. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2312. EXPECT_EQ(occluded_area_transformed, occlusion_data.occluded_region);
  2313. // Don't expect the target occlusion to be affected by progress through
  2314. // the animation.
  2315. test_controller.Step(kTransitionDuration / 3);
  2316. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
  2317. EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
  2318. EXPECT_EQ(occluded_area_transformed, occlusion_data.occluded_region);
  2319. window_b->layer()->GetAnimator()->StopAnimating();
  2320. window_b->layer()->SetAnimator(nullptr);
  2321. }
  2322. TEST_F(WindowOcclusionTrackerTest, ComputeTargetOcclusionForAnimatedWindow) {
  2323. // Computing the target occlusion for an animated window should use the
  2324. // target values, and therefore compute the target occlusion for the animated
  2325. // window at its final location in this test.
  2326. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2327. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2328. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  2329. EXPECT_FALSE(delegate_a->is_expecting_call());
  2330. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  2331. SkRegion(SkIRect::MakeXYWH(10, 10, 10, 10)));
  2332. Window* window_b = CreateUntrackedWindow(gfx::Rect(10, 10, 10, 10));
  2333. EXPECT_FALSE(delegate_a->is_expecting_call());
  2334. // Start animating |window_a| to be fully occluded by |window_b|.
  2335. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  2336. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  2337. ui::LayerAnimatorTestController test_controller(
  2338. ui::LayerAnimator::CreateImplicitAnimator());
  2339. ui::ScopedLayerAnimationSettings layer_animation_settings(
  2340. test_controller.animator());
  2341. layer_animation_settings.SetTransitionDuration(kTransitionDuration);
  2342. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2343. window_a->layer()->SetAnimator(test_controller.animator());
  2344. window_a->SetBounds(gfx::Rect(10, 10, 10, 10));
  2345. test_controller.Step(kTransitionDuration / 3);
  2346. EXPECT_FALSE(delegate_a->is_expecting_call());
  2347. // Target occlusion should take into account the final bounds.
  2348. auto& occlusion_tracker = GetOcclusionTracker();
  2349. auto occlusion_data =
  2350. occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2351. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2352. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2353. // Don't expect the target occlusion to be affected by progress through
  2354. // the animation.
  2355. test_controller.Step(kTransitionDuration / 3);
  2356. occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
  2357. EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
  2358. EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
  2359. // Finish the animation, and expect the occlusion state to update.
  2360. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2361. test_controller.Step(kTransitionDuration / 3);
  2362. window_b->layer()->SetAnimator(nullptr);
  2363. EXPECT_FALSE(delegate_a->is_expecting_call());
  2364. }
  2365. TEST_F(WindowOcclusionTrackerTest,
  2366. SetOpaqueRegionsForOcclusionAffectsOcclusionOfOtherWindows) {
  2367. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2368. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2369. CreateTrackedWindow(delegate_a, gfx::Rect(10, 10, 10, 10));
  2370. EXPECT_FALSE(delegate_a->is_expecting_call());
  2371. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2372. Window* window_b = CreateUntrackedWindow(gfx::Rect(10, 10, 10, 10));
  2373. EXPECT_FALSE(delegate_a->is_expecting_call());
  2374. // Make |window_b| transparent, which should make it no longer affect
  2375. // |window_a|'s occlusion.
  2376. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2377. window_b->SetTransparent(true);
  2378. EXPECT_FALSE(delegate_a->is_expecting_call());
  2379. // Set the opaque regions for occlusion to fully cover |window_a|. Opaque
  2380. // regions for occlusion are relative to the window.
  2381. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2382. window_b->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 10, 10)});
  2383. EXPECT_FALSE(delegate_a->is_expecting_call());
  2384. // Setting the opaque regions for occlusion to an empty list should restore to
  2385. // normal behavior:
  2386. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2387. window_b->SetOpaqueRegionsForOcclusion({});
  2388. EXPECT_FALSE(delegate_a->is_expecting_call());
  2389. }
  2390. TEST_F(
  2391. WindowOcclusionTrackerTest,
  2392. SetOpaqueRegionsForOcclusionOfAWindowDoesNotAffectOcclusionOfThatWindowItself) {
  2393. // The opaque regions for occlusion of a window affect how that window
  2394. // occludes other windows, but should not affect occlusion for that window
  2395. // itself. This is because occluding only the opaque regions of occlusion for
  2396. // a window may still leave translucent parts of that window visible.
  2397. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2398. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2399. Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  2400. EXPECT_FALSE(delegate_a->is_expecting_call());
  2401. delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
  2402. SkRegion(SkIRect::MakeXYWH(5, 5, 5, 5)));
  2403. CreateUntrackedWindow(gfx::Rect(5, 5, 5, 5));
  2404. EXPECT_FALSE(delegate_a->is_expecting_call());
  2405. window_a->SetTransparent(true);
  2406. // Changing the opaque regions for occlusion should not affect how much
  2407. // |window_a| is occluded by |window_b|.
  2408. window_a->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 0, 0)});
  2409. window_a->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 1, 1)});
  2410. window_a->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 5, 5)});
  2411. window_a->SetOpaqueRegionsForOcclusion({});
  2412. }
  2413. TEST_F(WindowOcclusionTrackerTest,
  2414. SemiOpaqueSolidColorLayerDoesNotAffectChildOpacity) {
  2415. // Create window a. Expect it to be non-occluded.
  2416. MockWindowDelegate* delegate_a = new MockWindowDelegate();
  2417. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2418. CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
  2419. EXPECT_FALSE(delegate_a->is_expecting_call());
  2420. // Create window b. Expect it to be non-occluded and expect window a to be
  2421. // occluded.
  2422. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2423. Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 15, 15), nullptr,
  2424. ui::LAYER_SOLID_COLOR);
  2425. EXPECT_FALSE(delegate_a->is_expecting_call());
  2426. // Semi-opaque color on the window_b should make window a visible.
  2427. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2428. window_b->layer()->SetColor(SkColorSetARGB(127, 255, 255, 255));
  2429. EXPECT_FALSE(delegate_a->is_expecting_call());
  2430. // Creating opaque layer on top of a half-opaque solid_color layer
  2431. // can occlude the window.
  2432. delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
  2433. Window* window_c = CreateUntrackedWindow(gfx::Rect(0, 0, 15, 15), window_b);
  2434. EXPECT_FALSE(delegate_a->is_expecting_call());
  2435. DCHECK(window_c);
  2436. // Removing the opaque layer should make the window_a visible.
  2437. delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
  2438. delete window_c;
  2439. EXPECT_FALSE(delegate_a->is_expecting_call());
  2440. }
  2441. // Run tests with LAYER_TEXTURE_LAYER type or LAYER_SOLID_COLOR type.
  2442. INSTANTIATE_TEST_SUITE_P(All,
  2443. WindowOcclusionTrackerOpacityTest,
  2444. testing::Bool());
  2445. } // namespace aura