OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / core / tests / coretests / src / android / webkit / UrlInterceptRegistryTest.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package android.webkit;
18
19 import android.test.AndroidTestCase;
20 import android.util.Log;
21 import android.webkit.CacheManager.CacheResult;
22 import android.webkit.PluginData;
23 import android.webkit.UrlInterceptHandler;
24
25 import java.util.LinkedList;
26 import java.util.Map;
27
28 public class UrlInterceptRegistryTest extends AndroidTestCase {
29
30     /**
31      * To run these tests: $ mmm
32      * frameworks/base/tests/CoreTests/android && adb remount && adb
33      * sync $ adb shell am instrument -w  -e class \
34      * android.webkit.UrlInterceptRegistryTest \
35      * android.core/android.test.InstrumentationTestRunner
36      */
37
38     private static class MockUrlInterceptHandler implements UrlInterceptHandler {
39         private PluginData mData;
40         private String mUrl;
41
42         public MockUrlInterceptHandler(PluginData data, String url) {
43             mData = data;
44             mUrl = url;
45         }
46
47         public CacheResult service(String url, Map<String, String> headers) {
48             return null;
49         }
50
51         public PluginData getPluginData(String url,
52                                         Map<String,
53                                         String> headers) {
54             if (mUrl.equals(url)) {
55                 return mData;
56             }
57
58             return null;
59         }
60     }
61
62     public void testGetPluginData() {
63         PluginData data = new PluginData(null, 0 , null, 200);
64         String url = new String("url1");
65         MockUrlInterceptHandler handler1 =
66                 new MockUrlInterceptHandler(data, url);
67
68         data = new PluginData(null, 0 , null, 404);
69         url = new String("url2");
70         MockUrlInterceptHandler handler2 =
71                 new MockUrlInterceptHandler(data, url);
72
73         assertTrue(UrlInterceptRegistry.registerHandler(handler1));
74         assertTrue(UrlInterceptRegistry.registerHandler(handler2));
75
76         data = UrlInterceptRegistry.getPluginData("url1", null);
77         assertTrue(data != null);
78         assertTrue(data.getStatusCode() == 200);
79
80         data = UrlInterceptRegistry.getPluginData("url2", null);
81         assertTrue(data != null);
82         assertTrue(data.getStatusCode() == 404);
83
84         assertTrue(UrlInterceptRegistry.unregisterHandler(handler1));
85         assertTrue(UrlInterceptRegistry.unregisterHandler(handler2));
86
87     }
88 }