OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.chrome.browser.customtabs; | 5 package org.chromium.chrome.browser.customtabs; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
| 9 import android.support.test.annotation.UiThreadTest; |
8 import android.support.test.filters.SmallTest; | 10 import android.support.test.filters.SmallTest; |
9 import android.test.InstrumentationTestCase; | 11 import android.support.test.rule.UiThreadTestRule; |
10 import android.test.UiThreadTest; | 12 |
| 13 import org.junit.After; |
| 14 import org.junit.Assert; |
| 15 import org.junit.Before; |
| 16 import org.junit.Rule; |
| 17 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; |
| 19 |
| 20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
11 | 21 |
12 /** Tests for RequestThrottler. | 22 /** Tests for RequestThrottler. |
13 * | 23 * |
14 * Note: tests are @UiThreadTest because RequestThrottler is not thread-safe. | 24 * Note: tests are @UiThreadTest because RequestThrottler is not thread-safe. |
15 */ | 25 */ |
16 public class RequestThrottlerTest extends InstrumentationTestCase { | 26 @RunWith(ChromeJUnit4ClassRunner.class) |
| 27 public class RequestThrottlerTest { |
17 private static final int UID = 1234; | 28 private static final int UID = 1234; |
18 private static final int UID2 = 12345; | 29 private static final int UID2 = 12345; |
19 private static final String URL = "https://www.google.com"; | 30 private static final String URL = "https://www.google.com"; |
20 private static final String URL2 = "https://www.chromium.org"; | 31 private static final String URL2 = "https://www.chromium.org"; |
21 | 32 |
22 private Context mContext; | 33 private Context mContext; |
23 | 34 |
24 @Override | 35 @Rule |
25 protected void setUp() throws Exception { | 36 public UiThreadTestRule mRule = new UiThreadTestRule(); |
26 super.setUp(); | 37 |
27 mContext = getInstrumentation().getTargetContext(); | 38 @Before |
| 39 public void setUp() throws Exception { |
| 40 mContext = InstrumentationRegistry.getInstrumentation().getTargetContext
(); |
28 RequestThrottler.purgeAllEntriesForTesting(mContext); | 41 RequestThrottler.purgeAllEntriesForTesting(mContext); |
29 } | 42 } |
30 | 43 |
31 @Override | 44 @After |
32 protected void tearDown() throws Exception { | 45 public void tearDown() throws Exception { |
33 super.tearDown(); | |
34 RequestThrottler.purgeAllEntriesForTesting(mContext); | 46 RequestThrottler.purgeAllEntriesForTesting(mContext); |
35 } | 47 } |
36 | 48 |
37 /** Tests that a client starts not banned. */ | 49 /** Tests that a client starts not banned. */ |
| 50 @Test |
38 @SmallTest | 51 @SmallTest |
39 @UiThreadTest | 52 @UiThreadTest |
40 public void testIsInitiallyNotBanned() { | 53 public void testIsInitiallyNotBanned() { |
41 assertTrue(RequestThrottler.getForUid(mContext, UID).isPrerenderingAllow
ed()); | 54 Assert.assertTrue(RequestThrottler.getForUid(mContext, UID).isPrerenderi
ngAllowed()); |
42 } | 55 } |
43 | 56 |
44 /** Tests that a misbehaving client gets banned. */ | 57 /** Tests that a misbehaving client gets banned. */ |
| 58 @Test |
45 @SmallTest | 59 @SmallTest |
46 @UiThreadTest | 60 @UiThreadTest |
47 public void testBansUid() { | 61 public void testBansUid() { |
48 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); | 62 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); |
49 assertTrue(throttler.isPrerenderingAllowed()); | 63 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
50 for (int i = 0; i < 100; i++) throttler.registerPrerenderRequest(URL); | 64 for (int i = 0; i < 100; i++) throttler.registerPrerenderRequest(URL); |
51 assertFalse(throttler.isPrerenderingAllowed()); | 65 Assert.assertFalse(throttler.isPrerenderingAllowed()); |
52 } | 66 } |
53 | 67 |
54 /** Tests that the URL needs to match to avoid getting banned. */ | 68 /** Tests that the URL needs to match to avoid getting banned. */ |
| 69 @Test |
55 @SmallTest | 70 @SmallTest |
56 @UiThreadTest | 71 @UiThreadTest |
57 public void testBanningMatchesUrls() { | 72 public void testBanningMatchesUrls() { |
58 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); | 73 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); |
59 assertTrue(throttler.isPrerenderingAllowed()); | 74 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
60 for (int i = 0; i < 100; i++) { | 75 for (int i = 0; i < 100; i++) { |
61 throttler.registerPrerenderRequest(URL); | 76 throttler.registerPrerenderRequest(URL); |
62 throttler.registerPrerenderRequest(URL); | 77 throttler.registerPrerenderRequest(URL); |
63 throttler.registerSuccess(URL2); | 78 throttler.registerSuccess(URL2); |
64 } | 79 } |
65 assertFalse(throttler.isPrerenderingAllowed()); | 80 Assert.assertFalse(throttler.isPrerenderingAllowed()); |
66 } | 81 } |
67 | 82 |
68 /** Tests that a client can send a lot of requests, as long as they are matc
hed by successes. */ | 83 /** Tests that a client can send a lot of requests, as long as they are matc
hed by successes. */ |
| 84 @Test |
69 @SmallTest | 85 @SmallTest |
70 @UiThreadTest | 86 @UiThreadTest |
71 public void testDontBanAccurateClients() { | 87 public void testDontBanAccurateClients() { |
72 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); | 88 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); |
73 assertTrue(throttler.isPrerenderingAllowed()); | 89 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
74 for (int i = 0; i < 100; i++) { | 90 for (int i = 0; i < 100; i++) { |
75 throttler.registerPrerenderRequest(URL); | 91 throttler.registerPrerenderRequest(URL); |
76 throttler.registerSuccess(URL); | 92 throttler.registerSuccess(URL); |
77 } | 93 } |
78 assertTrue(throttler.isPrerenderingAllowed()); | 94 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
79 } | 95 } |
80 | 96 |
81 /** Tests that partially accurate clients are not banned. */ | 97 /** Tests that partially accurate clients are not banned. */ |
| 98 @Test |
82 @SmallTest | 99 @SmallTest |
83 @UiThreadTest | 100 @UiThreadTest |
84 public void testDontBanPartiallyAccurateClients() { | 101 public void testDontBanPartiallyAccurateClients() { |
85 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); | 102 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); |
86 assertTrue(throttler.isPrerenderingAllowed()); | 103 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
87 for (int j = 0; j < 10; j++) { | 104 for (int j = 0; j < 10; j++) { |
88 throttler.registerPrerenderRequest(URL); | 105 throttler.registerPrerenderRequest(URL); |
89 throttler.registerPrerenderRequest(URL); | 106 throttler.registerPrerenderRequest(URL); |
90 throttler.registerSuccess(URL2); | 107 throttler.registerSuccess(URL2); |
91 throttler.registerSuccess(URL); | 108 throttler.registerSuccess(URL); |
92 assertTrue(throttler.isPrerenderingAllowed()); | 109 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
93 } | 110 } |
94 } | 111 } |
95 | 112 |
96 /** Tests that banning a UID doesn't ban another one. */ | 113 /** Tests that banning a UID doesn't ban another one. */ |
| 114 @Test |
97 @SmallTest | 115 @SmallTest |
98 @UiThreadTest | 116 @UiThreadTest |
99 public void testThrottlingBanIsByUid() { | 117 public void testThrottlingBanIsByUid() { |
100 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); | 118 RequestThrottler throttler = RequestThrottler.getForUid(mContext, UID); |
101 assertTrue(throttler.isPrerenderingAllowed()); | 119 Assert.assertTrue(throttler.isPrerenderingAllowed()); |
102 for (int i = 0; i < 100; i++) throttler.registerPrerenderRequest(URL); | 120 for (int i = 0; i < 100; i++) throttler.registerPrerenderRequest(URL); |
103 assertFalse(throttler.isPrerenderingAllowed()); | 121 Assert.assertFalse(throttler.isPrerenderingAllowed()); |
104 assertTrue(RequestThrottler.getForUid(mContext, UID2).isPrerenderingAllo
wed()); | 122 Assert.assertTrue(RequestThrottler.getForUid(mContext, UID2).isPrerender
ingAllowed()); |
105 } | 123 } |
106 } | 124 } |
OLD | NEW |