Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 17 | #include "dex_cache.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include "class_linker.h" |
| 22 | #include "common_runtime_test.h" |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 23 | #include "linear_alloc.h" |
| 24 | #include "mirror/class_loader-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 25 | #include "handle_scope-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 27 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 28 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | namespace mirror { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 31 | class DexCacheTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 32 | |
| 33 | TEST_F(DexCacheTest, Open) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 34 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 36 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 | Handle<DexCache> dex_cache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 38 | hs.NewHandle(class_linker_->AllocDexCache(soa.Self(), |
| 39 | *java_lang_dex_file_, |
| 40 | Runtime::Current()->GetLinearAlloc()))); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 41 | ASSERT_TRUE(dex_cache.Get() != nullptr); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 42 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 43 | EXPECT_EQ(java_lang_dex_file_->NumStringIds(), dex_cache->NumStrings()); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 44 | EXPECT_EQ(java_lang_dex_file_->NumTypeIds(), dex_cache->NumResolvedTypes()); |
| 45 | EXPECT_EQ(java_lang_dex_file_->NumMethodIds(), dex_cache->NumResolvedMethods()); |
| 46 | EXPECT_EQ(java_lang_dex_file_->NumFieldIds(), dex_cache->NumResolvedFields()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 49 | TEST_F(DexCacheTest, LinearAlloc) { |
| 50 | ScopedObjectAccess soa(Thread::Current()); |
| 51 | jobject jclass_loader(LoadDex("Main")); |
| 52 | ASSERT_TRUE(jclass_loader != nullptr); |
| 53 | Runtime* const runtime = Runtime::Current(); |
| 54 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
| 55 | StackHandleScope<1> hs(soa.Self()); |
| 56 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 57 | soa.Decode<mirror::ClassLoader*>(jclass_loader))); |
| 58 | mirror::Class* klass = class_linker->FindClass(soa.Self(), "LMain;", class_loader); |
| 59 | ASSERT_TRUE(klass != nullptr); |
| 60 | LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator(); |
| 61 | EXPECT_NE(linear_alloc, runtime->GetLinearAlloc()); |
| 62 | EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods())); |
| 63 | } |
| 64 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | } // namespace mirror |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 66 | } // namespace art |