OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.base.process_launcher; |
6 | 6 |
7 import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; | 7 import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
8 import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; | 8 import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
9 import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; | 9 import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
10 | 10 |
11 import android.app.Activity; | 11 import android.app.Activity; |
12 import android.app.Application; | 12 import android.app.Application; |
| 13 import android.os.Handler; |
13 import android.util.Pair; | 14 import android.util.Pair; |
14 | 15 |
15 import org.junit.After; | |
16 import org.junit.Assert; | 16 import org.junit.Assert; |
17 import org.junit.Before; | 17 import org.junit.Before; |
18 import org.junit.Test; | 18 import org.junit.Test; |
19 import org.junit.runner.RunWith; | 19 import org.junit.runner.RunWith; |
20 import org.robolectric.Robolectric; | 20 import org.robolectric.Robolectric; |
21 import org.robolectric.annotation.Config; | 21 import org.robolectric.annotation.Config; |
22 import org.robolectric.shadows.ShadowLooper; | 22 import org.robolectric.shadows.ShadowLooper; |
23 | 23 |
24 import org.chromium.base.process_launcher.ChildProcessCreationParams; | |
25 import org.chromium.base.test.util.Feature; | 24 import org.chromium.base.test.util.Feature; |
26 import org.chromium.testing.local.LocalRobolectricTestRunner; | 25 import org.chromium.testing.local.LocalRobolectricTestRunner; |
27 | 26 |
28 import java.util.ArrayList; | 27 import java.util.ArrayList; |
29 | 28 |
30 /** | 29 /** |
31 * Unit tests for BindingManagerImpl. The tests run agains mock ChildProcessConn
ection | 30 * Unit tests for BindingManagerImpl. The tests run agains mock ChildProcessConn
ection |
32 * implementation, thus testing only the BindingManagerImpl itself. | 31 * implementation, thus testing only the BindingManagerImpl itself. |
33 * | 32 * |
34 * Default property of being low-end device is overriden, so that both low-end a
nd high-end policies | 33 * Default property of being low-end device is overriden, so that both low-end a
nd high-end policies |
(...skipping 25 matching lines...) Expand all Loading... |
60 | 59 |
61 private static class TestChildProcessConnection extends ManagedChildProcessC
onnection { | 60 private static class TestChildProcessConnection extends ManagedChildProcessC
onnection { |
62 private final int mPid; | 61 private final int mPid; |
63 private boolean mConnected; | 62 private boolean mConnected; |
64 | 63 |
65 /** | 64 /** |
66 * Creates a mock binding corresponding to real ManagedChildProcessConne
ction after the | 65 * Creates a mock binding corresponding to real ManagedChildProcessConne
ction after the |
67 * connection is established: with initial binding bound and no strong b
inding. | 66 * connection is established: with initial binding bound and no strong b
inding. |
68 */ | 67 */ |
69 private TestChildProcessConnection(int pid) { | 68 private TestChildProcessConnection(int pid) { |
70 super(null /* context */, pid /* number */, true /* sandboxed */, | 69 super(new Handler(), null /* context */, pid /* number */, true /* s
andboxed */, |
71 null /* deathCallback */, null /* serviceClassName */, | 70 null /* deathCallback */, null /* serviceClassName */, |
72 null /* childProcessCommonParameters */, | 71 null /* childProcessCommonParameters */, |
73 new ChildProcessCreationParams("org.chromium.test", | 72 new ChildProcessCreationParams("org.chromium.test", |
74 false /* isExternalService */, 0 /* libraryProcessTy
pe */, | 73 false /* isExternalService */, 0 /* libraryProcessTy
pe */, |
75 false /* bindToCallerCheck */)); | 74 false /* bindToCallerCheck */)); |
76 mPid = pid; | 75 mPid = pid; |
77 } | 76 } |
78 | 77 |
79 @Override | 78 @Override |
80 public int getPid() { | 79 public int getPid() { |
(...skipping 24 matching lines...) Expand all Loading... |
105 } | 104 } |
106 } | 105 } |
107 | 106 |
108 /** | 107 /** |
109 * Helper class that stores a manager along with its text label. This is use
d for tests that | 108 * Helper class that stores a manager along with its text label. This is use
d for tests that |
110 * iterate over all managers to indicate which manager was being tested when
an assertion | 109 * iterate over all managers to indicate which manager was being tested when
an assertion |
111 * failed. | 110 * failed. |
112 */ | 111 */ |
113 private static class ManagerEntry { | 112 private static class ManagerEntry { |
114 BindingManagerImpl mManager; | 113 BindingManagerImpl mManager; |
115 String mLabel; // Name of the manager. | 114 String mLabel; // Name of the manager. |
116 | 115 |
117 ManagerEntry(BindingManagerImpl manager, String label) { | 116 ManagerEntry(BindingManagerImpl manager, String label) { |
118 mManager = manager; | 117 mManager = manager; |
119 mLabel = label; | 118 mLabel = label; |
120 } | 119 } |
121 | 120 |
122 String getErrorMessage() { | 121 String getErrorMessage() { |
123 return "Failed for the " + mLabel + " manager."; | 122 return "Failed for the " + mLabel + " manager."; |
124 } | 123 } |
125 } | 124 } |
126 | 125 |
127 Activity mActivity; | 126 Activity mActivity; |
128 | 127 |
129 // The managers are created in setUp() for convenience. | 128 // The managers are created in setUp() for convenience. |
130 BindingManagerImpl mLowEndManager; | 129 BindingManagerImpl mLowEndManager; |
131 BindingManagerImpl mHighEndManager; | 130 BindingManagerImpl mHighEndManager; |
132 BindingManagerImpl mModerateBindingManager; | 131 BindingManagerImpl mModerateBindingManager; |
133 ManagerEntry[] mAllManagers; | 132 ManagerEntry[] mAllManagers; |
134 | 133 |
135 @Before | 134 @Before |
136 public void setUp() { | 135 public void setUp() { |
137 // The tests run on only one thread. Pretend that is the launcher thread
so LauncherThread | |
138 // asserts are not triggered. | |
139 LauncherThread.setCurrentThreadAsLauncherThread(); | |
140 | |
141 mActivity = Robolectric.buildActivity(Activity.class).setup().get(); | 136 mActivity = Robolectric.buildActivity(Activity.class).setup().get(); |
142 | 137 Handler handler = new Handler(); |
143 mLowEndManager = | 138 mLowEndManager = BindingManagerImpl.createBindingManagerForTesting( |
144 BindingManagerImpl.createBindingManagerForTesting(true /* isLowE
ndDevice */); | 139 handler, true /* isLowEndDevice */); |
145 mHighEndManager = | 140 mHighEndManager = BindingManagerImpl.createBindingManagerForTesting( |
146 BindingManagerImpl.createBindingManagerForTesting(false /* isLow
EndDevice */); | 141 handler, false /* isLowEndDevice */); |
147 mModerateBindingManager = | 142 mModerateBindingManager = BindingManagerImpl.createBindingManagerForTest
ing( |
148 BindingManagerImpl.createBindingManagerForTesting(false /* isLow
EndDevice */); | 143 handler, false /* isLowEndDevice */); |
149 mModerateBindingManager.startModerateBindingManagement(mActivity, 4 /* m
axSize */); | 144 mModerateBindingManager.startModerateBindingManagement(mActivity, 4 /* m
axSize */); |
150 mAllManagers = new ManagerEntry[] { | 145 mAllManagers = new ManagerEntry[] {new ManagerEntry(mLowEndManager, "low
-end"), |
151 new ManagerEntry(mLowEndManager, "low-end"), | |
152 new ManagerEntry(mHighEndManager, "high-end"), | 146 new ManagerEntry(mHighEndManager, "high-end"), |
153 new ManagerEntry(mModerateBindingManager, "moderate-binding")}; | 147 new ManagerEntry(mModerateBindingManager, "moderate-binding")}; |
154 } | 148 } |
155 | 149 |
156 @After | |
157 public void tearDown() { | |
158 LauncherThread.setLauncherThreadAsLauncherThread(); | |
159 } | |
160 | |
161 /** | 150 /** |
162 * Verifies that when running on low-end, the binding manager drops the oom
bindings for the | 151 * Verifies that when running on low-end, the binding manager drops the oom
bindings for the |
163 * previously bound connection when a new connection is used in foreground. | 152 * previously bound connection when a new connection is used in foreground. |
164 */ | 153 */ |
165 @Test | 154 @Test |
166 @Feature({"ProcessManagement"}) | 155 @Feature({"ProcessManagement"}) |
167 public void testNewConnectionDropsPreviousOnLowEnd() { | 156 public void testNewConnectionDropsPreviousOnLowEnd() { |
168 // This test applies only to the low-end manager. | 157 // This test applies only to the low-end manager. |
169 BindingManagerImpl manager = mLowEndManager; | 158 BindingManagerImpl manager = mLowEndManager; |
170 | 159 |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 } | 596 } |
608 } | 597 } |
609 | 598 |
610 /* | 599 /* |
611 * Test that a moderate binding is added to background renderer processes wh
en the initial | 600 * Test that a moderate binding is added to background renderer processes wh
en the initial |
612 * binding is removed. | 601 * binding is removed. |
613 */ | 602 */ |
614 @Test | 603 @Test |
615 @Feature({"ProcessManagement"}) | 604 @Feature({"ProcessManagement"}) |
616 public void testModerateBindingTillBackgroundedBackgroundProcess() { | 605 public void testModerateBindingTillBackgroundedBackgroundProcess() { |
617 BindingManagerImpl manager = BindingManagerImpl.createBindingManagerForT
esting(false); | 606 BindingManagerImpl manager = |
| 607 BindingManagerImpl.createBindingManagerForTesting(new Handler(),
false); |
618 manager.startModerateBindingManagement(mActivity, 4); | 608 manager.startModerateBindingManagement(mActivity, 4); |
619 | 609 |
620 TestChildProcessConnection connection = new TestChildProcessConnection(0
); | 610 TestChildProcessConnection connection = new TestChildProcessConnection(0
); |
621 connection.start(null /* startCallback */); | 611 connection.start(null /* startCallback */); |
622 manager.addNewConnection(connection.getPid(), connection); | 612 manager.addNewConnection(connection.getPid(), connection); |
623 Assert.assertTrue(connection.isInitialBindingBound()); | 613 Assert.assertTrue(connection.isInitialBindingBound()); |
624 Assert.assertFalse(connection.isModerateBindingBound()); | 614 Assert.assertFalse(connection.isModerateBindingBound()); |
625 | 615 |
626 manager.setInForeground(connection.getPid(), false); | 616 manager.setInForeground(connection.getPid(), false); |
627 manager.onDeterminedVisibility(connection.getPid()); | 617 manager.onDeterminedVisibility(connection.getPid()); |
628 Assert.assertFalse(connection.isInitialBindingBound()); | 618 Assert.assertFalse(connection.isInitialBindingBound()); |
629 Assert.assertTrue(connection.isModerateBindingBound()); | 619 Assert.assertTrue(connection.isModerateBindingBound()); |
630 } | 620 } |
631 | 621 |
632 /* | 622 /* |
633 * Test that a moderate binding is not added to foreground renderer processe
s when the initial | 623 * Test that a moderate binding is not added to foreground renderer processe
s when the initial |
634 * binding is removed. | 624 * binding is removed. |
635 */ | 625 */ |
636 @Test | 626 @Test |
637 @Feature({"ProcessManagement"}) | 627 @Feature({"ProcessManagement"}) |
638 public void testModerateBindingTillBackgroundedForegroundProcess() { | 628 public void testModerateBindingTillBackgroundedForegroundProcess() { |
639 BindingManagerImpl manager = BindingManagerImpl.createBindingManagerForT
esting(false); | 629 BindingManagerImpl manager = |
| 630 BindingManagerImpl.createBindingManagerForTesting(new Handler(),
false); |
640 manager.startModerateBindingManagement(mActivity, 4); | 631 manager.startModerateBindingManagement(mActivity, 4); |
641 | 632 |
642 TestChildProcessConnection connection = new TestChildProcessConnection(0
); | 633 TestChildProcessConnection connection = new TestChildProcessConnection(0
); |
643 connection.start(null /* startCallback */); | 634 connection.start(null /* startCallback */); |
644 manager.addNewConnection(connection.getPid(), connection); | 635 manager.addNewConnection(connection.getPid(), connection); |
645 Assert.assertTrue(connection.isInitialBindingBound()); | 636 Assert.assertTrue(connection.isInitialBindingBound()); |
646 Assert.assertFalse(connection.isStrongBindingBound()); | 637 Assert.assertFalse(connection.isStrongBindingBound()); |
647 Assert.assertFalse(connection.isModerateBindingBound()); | 638 Assert.assertFalse(connection.isModerateBindingBound()); |
648 | 639 |
649 manager.setInForeground(connection.getPid(), true); | 640 manager.setInForeground(connection.getPid(), true); |
650 manager.onDeterminedVisibility(connection.getPid()); | 641 manager.onDeterminedVisibility(connection.getPid()); |
651 Assert.assertFalse(connection.isInitialBindingBound()); | 642 Assert.assertFalse(connection.isInitialBindingBound()); |
652 Assert.assertTrue(connection.isStrongBindingBound()); | 643 Assert.assertTrue(connection.isStrongBindingBound()); |
653 Assert.assertFalse(connection.isModerateBindingBound()); | 644 Assert.assertFalse(connection.isModerateBindingBound()); |
654 } | 645 } |
655 | 646 |
656 /* | 647 /* |
657 * Test that Chrome is sent to the background, that the initially added mode
rate bindings are | 648 * Test that Chrome is sent to the background, that the initially added mode
rate bindings are |
658 * removed and are not re-added when Chrome is brought back to the foregroun
d. | 649 * removed and are not re-added when Chrome is brought back to the foregroun
d. |
659 */ | 650 */ |
660 @Test | 651 @Test |
661 @Feature({"ProcessManagement"}) | 652 @Feature({"ProcessManagement"}) |
662 public void testModerateBindingTillBackgroundedSentToBackground() { | 653 public void testModerateBindingTillBackgroundedSentToBackground() { |
663 BindingManagerImpl manager = BindingManagerImpl.createBindingManagerForT
esting(false); | 654 BindingManagerImpl manager = |
| 655 BindingManagerImpl.createBindingManagerForTesting(new Handler(),
false); |
664 manager.startModerateBindingManagement(mActivity, 4); | 656 manager.startModerateBindingManagement(mActivity, 4); |
665 | 657 |
666 TestChildProcessConnection connection = new TestChildProcessConnection(0
); | 658 TestChildProcessConnection connection = new TestChildProcessConnection(0
); |
667 connection.start(null /* startCallback */); | 659 connection.start(null /* startCallback */); |
668 manager.addNewConnection(connection.getPid(), connection); | 660 manager.addNewConnection(connection.getPid(), connection); |
669 manager.setInForeground(connection.getPid(), false); | 661 manager.setInForeground(connection.getPid(), false); |
670 manager.onDeterminedVisibility(connection.getPid()); | 662 manager.onDeterminedVisibility(connection.getPid()); |
671 Assert.assertTrue(connection.isModerateBindingBound()); | 663 Assert.assertTrue(connection.isModerateBindingBound()); |
672 | 664 |
673 manager.onSentToBackground(); | 665 manager.onSentToBackground(); |
674 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); | 666 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
675 Assert.assertFalse(connection.isModerateBindingBound()); | 667 Assert.assertFalse(connection.isModerateBindingBound()); |
676 | 668 |
677 // Bringing Chrome to the foreground should not re-add the moderate bind
ings. | 669 // Bringing Chrome to the foreground should not re-add the moderate bind
ings. |
678 manager.onBroughtToForeground(); | 670 manager.onBroughtToForeground(); |
679 Assert.assertFalse(connection.isModerateBindingBound()); | 671 Assert.assertFalse(connection.isModerateBindingBound()); |
680 } | 672 } |
681 } | 673 } |
OLD | NEW |