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