OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "base/run_loop.h" |
| 6 #include "components/memory_coordinator/browser/memory_state_notifier.h" |
| 7 #include "content/browser/browser_main_loop.h" |
| 8 #include "content/public/common/content_switches.h" |
| 9 #include "content/public/test/content_browser_test.h" |
| 10 #include "content/public/test/content_browser_test_utils.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 class MemoryStateNotifierTest : public ContentBrowserTest { |
| 15 public: |
| 16 MemoryStateNotifierTest() {} |
| 17 |
| 18 void SetUp() override { |
| 19 memory_coordinator::EnableForTesting(); |
| 20 ContentBrowserTest::SetUp(); |
| 21 } |
| 22 |
| 23 protected: |
| 24 memory_coordinator::MemoryStateNotifier* memory_state_notifier() { |
| 25 return BrowserMainLoop::GetInstance()->memory_state_notifier(); |
| 26 } |
| 27 |
| 28 DISALLOW_COPY_AND_ASSIGN(MemoryStateNotifierTest); |
| 29 }; |
| 30 |
| 31 class LastStateChecker { |
| 32 public: |
| 33 explicit LastStateChecker(memory_coordinator::MemoryStateNotifier* notifier) |
| 34 : notifier_(notifier) {} |
| 35 ~LastStateChecker() {} |
| 36 |
| 37 void Check(memory_coordinator::mojom::MemoryState expected) { |
| 38 DCHECK(notifier_); |
| 39 notifier_->IterateChildren(base::Bind(&LastStateChecker::GetLastState, |
| 40 base::Unretained(this), expected)); |
| 41 run_loop_.Run(); |
| 42 } |
| 43 |
| 44 private: |
| 45 void GetLastState( |
| 46 memory_coordinator::mojom::MemoryState expected, |
| 47 memory_coordinator::mojom::ChildMemoryCoordinatorPtr& child) { |
| 48 ++num_children_; |
| 49 child->GetCurrentState(base::Bind(&LastStateChecker::AppendLastState, |
| 50 base::Unretained(this), expected)); |
| 51 } |
| 52 |
| 53 void AppendLastState(memory_coordinator::mojom::MemoryState expected, |
| 54 memory_coordinator::mojom::MemoryState actual) { |
| 55 EXPECT_EQ(expected, actual); |
| 56 if (--num_children_ == 0) { |
| 57 run_loop_.QuitWhenIdle(); |
| 58 } |
| 59 } |
| 60 |
| 61 memory_coordinator::MemoryStateNotifier* notifier_ = nullptr; |
| 62 base::RunLoop run_loop_; |
| 63 size_t num_children_ = 0; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(LastStateChecker); |
| 66 }; |
| 67 |
| 68 |
| 69 IN_PROC_BROWSER_TEST_F(MemoryStateNotifierTest, Throttled) { |
| 70 GURL url = GetTestUrl("", "simple_page.html"); |
| 71 NavigateToURL(shell(), url); |
| 72 memory_state_notifier()->Notify( |
| 73 memory_coordinator::mojom::MemoryState::THROTTLED); |
| 74 |
| 75 LastStateChecker checker(memory_state_notifier()); |
| 76 checker.Check(memory_coordinator::mojom::MemoryState::THROTTLED); |
| 77 } |
| 78 |
| 79 } // namespace content |
OLD | NEW |