120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu/*
2907c5763bb700c705108948328c8892500e59185Andrei Popescu * Copyright (C) 2009 The Android Open Source Project
320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu *
420634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * Licensed under the Apache License, Version 2.0 (the "License");
520634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * you may not use this file except in compliance with the License.
620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * You may obtain a copy of the License at
720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu *
820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu *      http://www.apache.org/licenses/LICENSE-2.0
920634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu *
1020634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * Unless required by applicable law or agreed to in writing, software
1120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * distributed under the License is distributed on an "AS IS" BASIS,
1220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * See the License for the specific language governing permissions and
1420634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu * limitations under the License.
1520634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu */
1620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
1720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescupackage com.android.browser;
1820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
1920634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescuimport android.test.AndroidTestCase;
2020634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescuimport android.test.suitebuilder.annotation.MediumTest;
21907c5763bb700c705108948328c8892500e59185Andrei Popescuimport android.webkit.WebStorage;
2220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
2320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu/**
2479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu * This is a series of unit tests for the WebStorageSizeManager class.
2520634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu *
2620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu */
2720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu@MediumTest
2879e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescupublic class WebStorageSizeManagerUnitTests extends AndroidTestCase {
29907c5763bb700c705108948328c8892500e59185Andrei Popescu    // Used for testing the out-of-space callbacks.
30907c5763bb700c705108948328c8892500e59185Andrei Popescu    private long mNewQuota;
31907c5763bb700c705108948328c8892500e59185Andrei Popescu    // Callback functor that sets a new quota in case of out-of-space scenarios.
32907c5763bb700c705108948328c8892500e59185Andrei Popescu    private class MockQuotaUpdater implements WebStorage.QuotaUpdater {
33907c5763bb700c705108948328c8892500e59185Andrei Popescu        public void updateQuota(long newQuota) {
34907c5763bb700c705108948328c8892500e59185Andrei Popescu            mNewQuota = newQuota;
35907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
36907c5763bb700c705108948328c8892500e59185Andrei Popescu    }
3720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
38907c5763bb700c705108948328c8892500e59185Andrei Popescu    // Mock the DiskInfo.
39907c5763bb700c705108948328c8892500e59185Andrei Popescu    private class MockDiskInfo implements WebStorageSizeManager.DiskInfo {
40907c5763bb700c705108948328c8892500e59185Andrei Popescu        private long mFreeSize;
41907c5763bb700c705108948328c8892500e59185Andrei Popescu        private long mTotalSize;
42907c5763bb700c705108948328c8892500e59185Andrei Popescu
43907c5763bb700c705108948328c8892500e59185Andrei Popescu        public long getFreeSpaceSizeBytes() {
44907c5763bb700c705108948328c8892500e59185Andrei Popescu            return mFreeSize;
45907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
46907c5763bb700c705108948328c8892500e59185Andrei Popescu
47907c5763bb700c705108948328c8892500e59185Andrei Popescu        public long getTotalSizeBytes() {
48907c5763bb700c705108948328c8892500e59185Andrei Popescu            return mTotalSize;
49907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
50907c5763bb700c705108948328c8892500e59185Andrei Popescu
51907c5763bb700c705108948328c8892500e59185Andrei Popescu        public void setFreeSpaceSizeBytes(long freeSize) {
52907c5763bb700c705108948328c8892500e59185Andrei Popescu            mFreeSize = freeSize;
53907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
54907c5763bb700c705108948328c8892500e59185Andrei Popescu
55907c5763bb700c705108948328c8892500e59185Andrei Popescu        public void setTotalSizeBytes(long totalSize) {
56907c5763bb700c705108948328c8892500e59185Andrei Popescu            mTotalSize = totalSize;
57907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
58907c5763bb700c705108948328c8892500e59185Andrei Popescu    }
59907c5763bb700c705108948328c8892500e59185Andrei Popescu
60907c5763bb700c705108948328c8892500e59185Andrei Popescu    // Mock the AppCacheInfo
61907c5763bb700c705108948328c8892500e59185Andrei Popescu    public class MockAppCacheInfo implements WebStorageSizeManager.AppCacheInfo {
62907c5763bb700c705108948328c8892500e59185Andrei Popescu        private long mAppCacheSize;
63907c5763bb700c705108948328c8892500e59185Andrei Popescu
64907c5763bb700c705108948328c8892500e59185Andrei Popescu        public long getAppCacheSizeBytes() {
65907c5763bb700c705108948328c8892500e59185Andrei Popescu            return mAppCacheSize;
66907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
67907c5763bb700c705108948328c8892500e59185Andrei Popescu
68907c5763bb700c705108948328c8892500e59185Andrei Popescu        public void setAppCacheSizeBytes(long appCacheSize) {
69907c5763bb700c705108948328c8892500e59185Andrei Popescu            mAppCacheSize = appCacheSize;
70907c5763bb700c705108948328c8892500e59185Andrei Popescu        }
71907c5763bb700c705108948328c8892500e59185Andrei Popescu    }
72907c5763bb700c705108948328c8892500e59185Andrei Popescu
73907c5763bb700c705108948328c8892500e59185Andrei Popescu    private MockQuotaUpdater mQuotaUpdater = new MockQuotaUpdater();
74907c5763bb700c705108948328c8892500e59185Andrei Popescu    private final MockDiskInfo mDiskInfo = new MockDiskInfo();
75907c5763bb700c705108948328c8892500e59185Andrei Popescu    private final MockAppCacheInfo mAppCacheInfo = new MockAppCacheInfo();
76907c5763bb700c705108948328c8892500e59185Andrei Popescu    // Utility for making size computations easier to read.
77907c5763bb700c705108948328c8892500e59185Andrei Popescu    private long bytes(double megabytes) {
78907c5763bb700c705108948328c8892500e59185Andrei Popescu        return (new Double(megabytes * 1024 * 1024)).longValue();
79907c5763bb700c705108948328c8892500e59185Andrei Popescu    }
80907c5763bb700c705108948328c8892500e59185Andrei Popescu    /**
81907c5763bb700c705108948328c8892500e59185Andrei Popescu     * Test the onExceededDatabaseQuota and onReachedMaxAppCacheSize callbacks
82907c5763bb700c705108948328c8892500e59185Andrei Popescu     */
83907c5763bb700c705108948328c8892500e59185Andrei Popescu    public void testCallbacks() {
84907c5763bb700c705108948328c8892500e59185Andrei Popescu        long totalUsedQuota = 0;
85907c5763bb700c705108948328c8892500e59185Andrei Popescu        final long quotaIncrease = WebStorageSizeManager.QUOTA_INCREASE_STEP;  // 1MB
8625a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch
87907c5763bb700c705108948328c8892500e59185Andrei Popescu        // We have 75 MB total, 24MB free so the global limit will be 12 MB.
88907c5763bb700c705108948328c8892500e59185Andrei Popescu        mDiskInfo.setTotalSizeBytes(bytes(75));
89907c5763bb700c705108948328c8892500e59185Andrei Popescu        mDiskInfo.setFreeSpaceSizeBytes(bytes(24));
90907c5763bb700c705108948328c8892500e59185Andrei Popescu        // We have an appcache file size of 0 MB.
91907c5763bb700c705108948328c8892500e59185Andrei Popescu        mAppCacheInfo.setAppCacheSizeBytes(0);
92907c5763bb700c705108948328c8892500e59185Andrei Popescu        // Create the manager.
93327e3ac66da7b67dadf16e93dc3a8bb5ecf0efd0Ben Murdoch        WebStorageSizeManager manager = new WebStorageSizeManager(getContext(), mDiskInfo,
94327e3ac66da7b67dadf16e93dc3a8bb5ecf0efd0Ben Murdoch                mAppCacheInfo);
95907c5763bb700c705108948328c8892500e59185Andrei Popescu        // We add origin 1.
96907c5763bb700c705108948328c8892500e59185Andrei Popescu        long origin1Quota = 0;
9725a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        long origin1EstimatedSize = bytes(3.5);
9825a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        manager.onExceededDatabaseQuota("1", "1", origin1Quota, origin1EstimatedSize, totalUsedQuota, mQuotaUpdater);
9925a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(origin1EstimatedSize, mNewQuota);
100907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin1Quota = mNewQuota;
101907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota += origin1Quota;
102907c5763bb700c705108948328c8892500e59185Andrei Popescu
103907c5763bb700c705108948328c8892500e59185Andrei Popescu        // We add origin 2.
104907c5763bb700c705108948328c8892500e59185Andrei Popescu        long origin2Quota = 0;
10525a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        long origin2EstimatedSize = bytes(2.5);
10625a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        manager.onExceededDatabaseQuota("2", "2", origin2Quota, origin2EstimatedSize, totalUsedQuota, mQuotaUpdater);
10725a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(origin2EstimatedSize, mNewQuota);
108907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin2Quota = mNewQuota;
109907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota += origin2Quota;
110907c5763bb700c705108948328c8892500e59185Andrei Popescu
111907c5763bb700c705108948328c8892500e59185Andrei Popescu        // Origin 1 runs out of space.
11234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "1", origin1Quota, 0, totalUsedQuota, mQuotaUpdater);
11325a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(origin1EstimatedSize + quotaIncrease, mNewQuota);
114907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota -= origin1Quota;
115907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin1Quota = mNewQuota;
116907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota += origin1Quota;
117907c5763bb700c705108948328c8892500e59185Andrei Popescu
118907c5763bb700c705108948328c8892500e59185Andrei Popescu        // Origin 2 runs out of space.
11934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("2", "2", origin2Quota, 0, totalUsedQuota, mQuotaUpdater);
12025a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(origin2EstimatedSize + quotaIncrease, mNewQuota);
121907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota -= origin2Quota;
122907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin2Quota = mNewQuota;
123907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota += origin2Quota;
124907c5763bb700c705108948328c8892500e59185Andrei Popescu
12525a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        // We add origin 3. TotalUsedQuota is 8 (3.5 + 2.5 + 1 + 1). AppCacheMaxSize is 3 (12 / 4).
126907c5763bb700c705108948328c8892500e59185Andrei Popescu        // So we have 1 MB free.
127907c5763bb700c705108948328c8892500e59185Andrei Popescu        long origin3Quota = 0;
12825a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        long origin3EstimatedSize = bytes(5);
12925a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        manager.onExceededDatabaseQuota("3", "3", origin3Quota, origin3EstimatedSize, totalUsedQuota, mQuotaUpdater);
13025a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(0, mNewQuota);  // We cannot satisfy the estimatedSize
131907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin3Quota = mNewQuota;
132907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota += origin3Quota;
133907c5763bb700c705108948328c8892500e59185Andrei Popescu
13425a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        // Origin 1 runs out of space again. It should increase it's quota to take the last 1MB.
13534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "1", origin1Quota, 0, totalUsedQuota, mQuotaUpdater);
13625a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(origin1Quota + quotaIncrease, mNewQuota);
13725a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        totalUsedQuota -= origin1Quota;
13825a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        origin1Quota = mNewQuota;
13925a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        totalUsedQuota += origin1Quota;
14025a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch
14125a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        // Origin 1 runs out of space again. It should inow fail to increase in size.
14234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "1", origin1Quota, 0, totalUsedQuota, mQuotaUpdater);
143907c5763bb700c705108948328c8892500e59185Andrei Popescu        assertEquals(origin1Quota, mNewQuota);
144907c5763bb700c705108948328c8892500e59185Andrei Popescu
145907c5763bb700c705108948328c8892500e59185Andrei Popescu        // We try adding a new origin. Which will fail.
14625a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        manager.onExceededDatabaseQuota("4", "4", 0, bytes(1), totalUsedQuota, mQuotaUpdater);
147907c5763bb700c705108948328c8892500e59185Andrei Popescu        assertEquals(0, mNewQuota);
148907c5763bb700c705108948328c8892500e59185Andrei Popescu
149907c5763bb700c705108948328c8892500e59185Andrei Popescu        // AppCache size increases to 2MB...
150907c5763bb700c705108948328c8892500e59185Andrei Popescu        mAppCacheInfo.setAppCacheSizeBytes(bytes(2));
151907c5763bb700c705108948328c8892500e59185Andrei Popescu        // ... and wants 2MB more. Fail.
152907c5763bb700c705108948328c8892500e59185Andrei Popescu        manager.onReachedMaxAppCacheSize(bytes(2), totalUsedQuota, mQuotaUpdater);
153907c5763bb700c705108948328c8892500e59185Andrei Popescu        assertEquals(0, mNewQuota);
154907c5763bb700c705108948328c8892500e59185Andrei Popescu
155907c5763bb700c705108948328c8892500e59185Andrei Popescu        // The user nukes origin 2
156907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota -= origin2Quota;
157907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin2Quota = 0;
15825a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        // TotalUsedQuota is 5.5 (9 - 3.5). AppCacheMaxSize is 3. AppCacheSize is 2.
159907c5763bb700c705108948328c8892500e59185Andrei Popescu        // AppCache wants 1.5MB more
160907c5763bb700c705108948328c8892500e59185Andrei Popescu        manager.onReachedMaxAppCacheSize(bytes(1.5), totalUsedQuota, mQuotaUpdater);
161907c5763bb700c705108948328c8892500e59185Andrei Popescu        mAppCacheInfo.setAppCacheSizeBytes(mAppCacheInfo.getAppCacheSizeBytes() + bytes(2.5));
16225a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(mAppCacheInfo.getAppCacheSizeBytes(), mNewQuota - WebStorageSizeManager.APPCACHE_MAXSIZE_PADDING);
163907c5763bb700c705108948328c8892500e59185Andrei Popescu
164907c5763bb700c705108948328c8892500e59185Andrei Popescu        // We try adding a new origin. This time we succeed.
16525a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        // TotalUsedQuota is 5.5. AppCacheMaxSize is 5.0. So we have 12 - 10.5 = 1.5 available.
166907c5763bb700c705108948328c8892500e59185Andrei Popescu        long origin4Quota = 0;
16725a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        long origin4EstimatedSize = bytes(1.5);
16825a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        manager.onExceededDatabaseQuota("4", "4", origin4Quota, origin4EstimatedSize, totalUsedQuota, mQuotaUpdater);
16925a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch        assertEquals(bytes(1.5), mNewQuota);
170907c5763bb700c705108948328c8892500e59185Andrei Popescu        origin4Quota = mNewQuota;
171907c5763bb700c705108948328c8892500e59185Andrei Popescu        totalUsedQuota += origin4Quota;
172907c5763bb700c705108948328c8892500e59185Andrei Popescu    }
17320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu    /**
17420634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu     * Test the application caches max size calculator.
17520634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu     */
17679e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu    public void testCalculateGlobalLimit() {
17720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        long fileSystemSize = 78643200;  // 75 MB
17820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        long freeSpaceSize = 25165824;  // 24 MB
17979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        long maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
18079e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(12582912, maxSize);  // 12MB
18120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
18220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 78643200;  // 75 MB
18320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 60 * 1024 * 1024;  // 60MB
18479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
18579e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(19922944, maxSize);  // 19MB
18620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
18720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 8589934592L;  // 8 GB
18820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 4294967296L;  // 4 GB
18979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
19079e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(536870912L, maxSize);  // 512 MB
19120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
19220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = -14;
19320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 21;
19479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
19520634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        assertEquals(0, maxSize);
19620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
19720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 100;
19820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 101;
19979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
20020634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        assertEquals(0, maxSize);
20120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
20220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 3774873; // ~4.2 MB
20320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 2560000;  // ~2.4 MB
20479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
20579e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(2097152, maxSize);  // 2 MB
20620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
20720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 4404019; // ~4.2 MB
20820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 3774873;  // ~3.6 MB
20979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
21079e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(2097152, maxSize);  // 2 MB
21120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
21220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 4404019; // ~4.2 MB
21320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 4404019;  // ~4.2 MB
21479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
21579e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(3145728, maxSize);  // 3 MB
21620634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
21720634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 1048576; // 1 MB
21820634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 1048575;  // 1 MB - 1 byte
21979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
22020634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        assertEquals(0, maxSize);
22120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu
22220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        fileSystemSize = 3774873; // ~3.6 MB
22320634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu        freeSpaceSize = 2097151;  // 2 MB - 1 byte
22479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
22579e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(0, maxSize);
22679e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu
22779e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        fileSystemSize = 3774873; // ~3.6 MB
22879e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        freeSpaceSize = 2097151;  // 2 MB
22979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        maxSize = WebStorageSizeManager.calculateGlobalLimit(fileSystemSize, freeSpaceSize);
23079e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        assertEquals(0, maxSize);
23120634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu    }
23234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
23334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch    public void testManyDatabasesOnOneOrigin() {
23434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // This test ensures that if an origin creates more than one database, the quota that is
23534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // assigned to the origin after the second creation is enough to satisfy all databases
23634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // under that origin.
23734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // See b/2417477.
23834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
23934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long totalUsedQuota = 0;
24034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        mDiskInfo.setTotalSizeBytes(bytes(100));
24134b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        mDiskInfo.setFreeSpaceSizeBytes(bytes(100));
24234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // This should give us a storage area of 13MB, with 3.25MB for appcache and 9.75MB for
24334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // databases.
24434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(bytes(13), WebStorageSizeManager.calculateGlobalLimit(
24534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                mDiskInfo.getTotalSizeBytes(), mDiskInfo.getFreeSpaceSizeBytes()));
24634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
24734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // We have an appcache file size of 0 MB.
24834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        mAppCacheInfo.setAppCacheSizeBytes(0);
24934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
25034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // Create the manager.
251327e3ac66da7b67dadf16e93dc3a8bb5ecf0efd0Ben Murdoch        WebStorageSizeManager manager = new WebStorageSizeManager(getContext(), mDiskInfo,
252327e3ac66da7b67dadf16e93dc3a8bb5ecf0efd0Ben Murdoch                mAppCacheInfo);
25334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
25434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // We add an origin.
25534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long originQuota = 0;
25634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long database1EstimatedSize = bytes(2);
25734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "1", originQuota, database1EstimatedSize,
25834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                totalUsedQuota, mQuotaUpdater);
25934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(database1EstimatedSize, mNewQuota);
26034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        originQuota = mNewQuota;
26134b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        totalUsedQuota = originQuota;
26234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
26334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // Now try to create a new database under the origin, by invoking onExceededDatabaseQuota
26434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // again. This time, request more space than the old quota + the quota increase step.
26534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long database2EstimatedSize = bytes(3.5);
26634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "2", originQuota, database2EstimatedSize,
26734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                totalUsedQuota, mQuotaUpdater);
26834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(database1EstimatedSize + database2EstimatedSize, mNewQuota);
26934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        originQuota = mNewQuota;
27034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        totalUsedQuota = originQuota;
27134b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
27234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // Create another database, but this time use a size that will overflow the space on the
27334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // device. It should be denied.
27434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long database3EstimatedSize = bytes(50);
27534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "3", originQuota, database3EstimatedSize,
27634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                totalUsedQuota, mQuotaUpdater);
27734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(originQuota, mNewQuota);
27834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
27934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // Create another database. This time, request less than the old quota.
28034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long database4EstimatedSize = bytes(2);
28134b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "4", originQuota, database4EstimatedSize,
28234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                totalUsedQuota, mQuotaUpdater);
28334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(database1EstimatedSize + database2EstimatedSize + database4EstimatedSize,
28434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                mNewQuota);
28534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        originQuota = mNewQuota;
28634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        totalUsedQuota = originQuota;
28734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
28834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // Now have the first database overflow it's quota. We should get 1 more MB.
28934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "1", originQuota, 0, totalUsedQuota, mQuotaUpdater);
29034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(database1EstimatedSize + database2EstimatedSize + database4EstimatedSize +
29134b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                bytes(1), mNewQuota);
29234b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        originQuota = mNewQuota;
29334b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        totalUsedQuota = originQuota;
29434b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch
29534b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        // Create a db under the origin that uses a quota less than the usual quota increase step.
29634b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        long database5EstimatedSize = bytes(0.5);
29734b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        manager.onExceededDatabaseQuota("1", "5", originQuota, database5EstimatedSize,
29834b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                totalUsedQuota, mQuotaUpdater);
29934b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch        assertEquals(database1EstimatedSize + database2EstimatedSize + database4EstimatedSize +
30034b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch                bytes(1) + database5EstimatedSize, mNewQuota);
30134b89ef113c7e5e365f75ec692a98a5484834ca0Ben Murdoch    }
30220634ce1fa1af93df5d12b5485b87ecd294281bbAndrei Popescu}
303