javamanager/javainstaller/installer/tsrc/javasrc/com/nokia/mj/impl/installer/utils/SysUtilTest.java
branchRCL_3
changeset 83 26b2b12093af
parent 60 6c158198356e
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
     1 /*
     1 /*
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    31 /**
    31 /**
    32  * SysUtil unit tests.
    32  * SysUtil unit tests.
    33  */
    33  */
    34 public class SysUtilTest extends TestCase implements InstallerMain
    34 public class SysUtilTest extends TestCase implements InstallerMain
    35 {
    35 {
    36     private int iPropertyValue = 0; // Used in testPropertyListener test case.
       
    37 
       
    38     // Begin j2meunit test framework setup
    36     // Begin j2meunit test framework setup
    39 
    37 
    40     public void installerMain(String[] args)
    38     public void installerMain(String[] args)
    41     {
    39     {
    42         TestSuite suite = new TestSuite(this.getClass().getName());
    40         TestSuite suite = new TestSuite(this.getClass().getName());
   167             {
   165             {
   168                 ((SysUtilTest)tc).testIsDiskSpaceBelowCriticalLevel();
   166                 ((SysUtilTest)tc).testIsDiskSpaceBelowCriticalLevel();
   169             }
   167             }
   170         }));
   168         }));
   171 
   169 
   172         if (Platform.isS60())
       
   173         {
       
   174         suite.addTest(new SysUtilTest("testPropertyListener", new TestMethod()
       
   175         {
       
   176             public void run(TestCase tc)
       
   177             {
       
   178                 ((SysUtilTest)tc).testPropertyListener();
       
   179             }
       
   180         }));
       
   181 
       
   182         suite.addTest(new SysUtilTest("testGetProcessState", new TestMethod()
       
   183         {
       
   184             public void run(TestCase tc)
       
   185             {
       
   186                 ((SysUtilTest)tc).testGetProcessState();
       
   187             }
       
   188         }));
       
   189 
       
   190         suite.addTest(new SysUtilTest("testDriveGetters", new TestMethod()
       
   191         {
       
   192             public void run(TestCase tc)
       
   193             {
       
   194                 ((SysUtilTest)tc).testDriveGetters();
       
   195             }
       
   196         }));
       
   197 
       
   198         suite.addTest(new SysUtilTest("testGetScreenSize", new TestMethod()
       
   199         {
       
   200             public void run(TestCase tc)
       
   201             {
       
   202                 ((SysUtilTest)tc).testGetScreenSize();
       
   203             }
       
   204         }));
       
   205 
       
   206         suite.addTest(new SysUtilTest("testIsoToLang", new TestMethod()
       
   207         {
       
   208             public void run(TestCase tc)
       
   209             {
       
   210                 ((SysUtilTest)tc).testIsoToLang();
       
   211             }
       
   212         }));
       
   213         }
       
   214 
       
   215         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
   170         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
   216     }
   171     }
   217 
   172 
   218     public SysUtilTest()
   173     public SysUtilTest()
   219     {
   174     {
   241 
   196 
   242     public void testGetPropertyValue()
   197     public void testGetPropertyValue()
   243     {
   198     {
   244         try
   199         try
   245         {
   200         {
   246             // Test PS keys defined in hwrmpowerstatesdkpskey.h
   201             // Test PS keys defined in ScreensaverInternalPSKeys.h
   247             Uid uid = PlatformUid.createUid("0x10205041");  // KPSUidHWRMPowerState
   202             Uid uid = PlatformUid.createUid("0x101F8771");  // KPSUidScreenSaver
   248             int key = 0x00000003; // KHWRMChargingStatus
   203             int key = 0x00000001; // KScreenSaverPreviewMode
   249             int value = SysUtil.getPropertyValue(uid, key);
   204             int value = SysUtil.getPropertyValue(uid, key);
   250             assertTrue("KHWRMChargingStatus(!=0): " + value, value == 0);
   205             assertTrue("KScreenSaverPreviewMode(!=0): " + value, value == 0);
   251         }
   206         }
   252         catch (InstallerException ie)
   207         catch (InstallerException ie)
   253         {
   208         {
   254             ie.printStackTrace();
   209             ie.printStackTrace();
   255             assertTrue("Unexpected exception: " + ie, false);
   210             assertTrue("Unexpected exception: " + ie, false);
   258 
   213 
   259     public void testGetUndefinedPropertyValue()
   214     public void testGetUndefinedPropertyValue()
   260     {
   215     {
   261         try
   216         try
   262         {
   217         {
   263             // Test PS keys defined in hwrmpowerstatesdkpskey.h
   218             // Test PS keys defined in ScreensaverInternalPSKeys.h
   264             Uid uid = PlatformUid.createUid("0x10205041");  // KPSUidHWRMPowerState
   219             Uid uid = PlatformUid.createUid("0x101F8771");  // KPSUidScreenSaver
   265             int key = 0x00000010; // Undefined key
   220             int key = 0x00000010; // Undefined key
   266             int value = SysUtil.getPropertyValue(uid, key);
   221             int value = SysUtil.getPropertyValue(uid, key);
   267             assertTrue("Getting undefined property value did not fail", false);
   222             assertTrue("Getting undefined property value did not fail", false);
   268         }
   223         }
   269         catch (InstallerException ie)
   224         catch (InstallerException ie)
   274 
   229 
   275     public void testSetPropertyValue()
   230     public void testSetPropertyValue()
   276     {
   231     {
   277         try
   232         try
   278         {
   233         {
   279             Uid uid = SysUtil.PROP_CATEGORY_SYSTEM;
   234             // Test PS keys defined in ScreensaverInternalPSKeys.h
   280             long key = SysUtil.PROP_KEY_JAVA_LATEST_INSTALLATION_PROGRESS;
   235             Uid uid = PlatformUid.createUid("0x101F8771");  // KPSUidScreenSaver
       
   236             int key = 0x00000001; // KScreenSaverPreviewMode
       
   237             // Set screensaver preview mode on (stays on for 10 secs).
   281             SysUtil.setPropertyValue(uid, key, 1);
   238             SysUtil.setPropertyValue(uid, key, 1);
   282             int value = SysUtil.getPropertyValue(uid, key);
   239             int value = SysUtil.getPropertyValue(uid, key);
   283             assertTrue("PROP_KEY_JAVA_LATEST_INSTALLATION_PROGRESS(!=1): " +
   240             assertTrue("KScreenSaverPreviewMode(!=1): " + value, value == 1);
   284                        value, value == 1);
       
   285         }
   241         }
   286         catch (InstallerException ie)
   242         catch (InstallerException ie)
   287         {
   243         {
   288             ie.printStackTrace();
   244             ie.printStackTrace();
   289             assertTrue("Unexpected exception: " + ie, false);
   245             assertTrue("Unexpected exception: " + ie, false);
   292 
   248 
   293     public void testSetUndefinedPropertyValue()
   249     public void testSetUndefinedPropertyValue()
   294     {
   250     {
   295         try
   251         try
   296         {
   252         {
   297             Uid uid = SysUtil.PROP_CATEGORY_SYSTEM;
   253             // Test PS keys defined in ScreensaverInternalPSKeys.h
   298             int key = 0x00000100; // Undefined key
   254             Uid uid = PlatformUid.createUid("0x101F8771");  // KPSUidScreenSaver
       
   255             int key = 0x00000010; // Undefined key
   299             SysUtil.setPropertyValue(uid, key, 1);
   256             SysUtil.setPropertyValue(uid, key, 1);
   300             assertTrue("Setting undefined property value did not fail", false);
   257             assertTrue("Setting undefined property value did not fail", false);
   301         }
   258         }
   302         catch (InstallerException ie)
   259         catch (InstallerException ie)
   303         {
   260         {
   380 
   337 
   381     public void testGetRepositoryValue()
   338     public void testGetRepositoryValue()
   382     {
   339     {
   383         try
   340         try
   384         {
   341         {
   385             // Test CenRep keys defined in Java security.
   342             // Test CenRep keys defined in ScreensaverInternalCRKeys.h
   386             Uid uid = PlatformUid.createUid("0x2001B289"); // KJavaSecurity
   343             Uid uid = PlatformUid.createUid("0x101F8770"); // KCRUidScreenSaver
   387             long key = 0x00000004; // KJavaSecurity/KWarningsMode
   344             long key = 0x00000004; // KScreenSaverInvertedColors
   388             int value = SysUtil.getRepositoryValue(uid, key);
   345             int value = SysUtil.getRepositoryValue(uid, key);
   389             assertTrue("KJavaSecurity/KWarningsMode(!=2): " + value,
   346             assertTrue("KScreenSaverInvertedColors(!=0): " + value, value == 0);
   390                        value == 2);
       
   391         }
   347         }
   392         catch (InstallerException ie)
   348         catch (InstallerException ie)
   393         {
   349         {
   394             ie.printStackTrace();
   350             ie.printStackTrace();
   395             assertTrue("Unexpected exception: " + ie, false);
   351             assertTrue("Unexpected exception: " + ie, false);
   417         try
   373         try
   418         {
   374         {
   419             Uid uid = SysUtil.REPO_ID_JAVA_INST_VARIATION;
   375             Uid uid = SysUtil.REPO_ID_JAVA_INST_VARIATION;
   420             long key = SysUtil.REPO_KEY_JAVA_INST_SHELL_FOLDER_VALUE;
   376             long key = SysUtil.REPO_KEY_JAVA_INST_SHELL_FOLDER_VALUE;
   421             String value = SysUtil.getRepositoryStringValue(uid, key);
   377             String value = SysUtil.getRepositoryStringValue(uid, key);
   422             assertTrue("Unexpected value: " + value, "Games,Applications".equals(value));
   378             //assertTrue("Unexpected value: " + value, "Games,Applications".equals(value));
   423             //assertTrue("Unexpected value: " + value, "".equals(value));
   379             assertTrue("Unexpected value: " + value, "".equals(value));
   424         }
   380         }
   425         catch (InstallerException ie)
   381         catch (InstallerException ie)
   426         {
   382         {
   427             ie.printStackTrace();
   383             ie.printStackTrace();
   428             assertTrue("Unexpected exception: " + ie, false);
   384             assertTrue("Unexpected exception: " + ie, false);
   615         catch (InstallerException ie)
   571         catch (InstallerException ie)
   616         {
   572         {
   617             // OK, expected exception.
   573             // OK, expected exception.
   618         }
   574         }
   619     }
   575     }
   620 
       
   621     public void testPropertyListener()
       
   622     {
       
   623         final int category = 0x101f75b6;
       
   624         final int key = 0x20019546;
       
   625         final int value = 15;
       
   626         final Object synchObject = this;
       
   627         PropertyListener listener = new PropertyListener()
       
   628         {
       
   629             public void valueChanged(int aCategory, int aKey, int aValue)
       
   630             {
       
   631                 try
       
   632                 {
       
   633                     if (aValue == 0)
       
   634                     {
       
   635                         // Ignore the first event which is sent when
       
   636                         // subscription is made.
       
   637                         return;
       
   638                     }
       
   639                     synchronized (synchObject)
       
   640                     {
       
   641                         iPropertyValue = aValue;
       
   642                         Log.log("PropertyListener.valueChanged: " + aValue +
       
   643                                 " (" + aCategory + ", " + aKey + ")");
       
   644                         if (aCategory != category)
       
   645                         {
       
   646                             Log.logError("PropertyListener.valueChanged: " +
       
   647                                          "invalid category " + aCategory);
       
   648                         }
       
   649                         if (aKey != key)
       
   650                         {
       
   651                             Log.logError("PropertyListener.valueChanged: " +
       
   652                                          "invalid key " + aKey);
       
   653                         }
       
   654                         synchObject.notify();
       
   655                     }
       
   656                 }
       
   657                 catch (Throwable t)
       
   658                 {
       
   659                     Log.logError("PropertyListener.valueChanged exception", t);
       
   660                 }
       
   661             }
       
   662         };
       
   663         try
       
   664         {
       
   665             iPropertyValue = 0;
       
   666             SysUtil.setPropertyValue(
       
   667                 PlatformUid.createUid(category), key, iPropertyValue);
       
   668             PropertyProvider provider = new PropertyProvider();
       
   669             provider.subscribe(category, key, listener);
       
   670             SysUtil.setPropertyValue(
       
   671                 PlatformUid.createUid(category), key, value);
       
   672             try
       
   673             {
       
   674                 synchronized (synchObject)
       
   675                 {
       
   676                     if (iPropertyValue == 0)
       
   677                     {
       
   678                         synchObject.wait(2000);
       
   679                     }
       
   680                 }
       
   681             }
       
   682             catch (InterruptedException ie)
       
   683             {
       
   684             }
       
   685             provider.unsubscribe();
       
   686             assertTrue("Unexpected property value after test: " +
       
   687                        iPropertyValue, iPropertyValue == value);
       
   688         }
       
   689         catch (Throwable t)
       
   690         {
       
   691             Log.logError("testPropertyListener exception", t);
       
   692             assertTrue("Unexpected exception " + t, false);
       
   693         }
       
   694     }
       
   695 
       
   696     public void testGetProcessState()
       
   697     {
       
   698         int state = SysUtil.getProcessState(PlatformUid.createUid("[102033e6]"));
       
   699         assertTrue("installer process state is " + state +
       
   700                    ", not " + SysUtil.PROC_STATE_ALIVE,
       
   701                    SysUtil.PROC_STATE_ALIVE == state);
       
   702         state = SysUtil.getProcessState(PlatformUid.createUid("[e0001001]"));
       
   703         assertTrue("nonexisting process state is " + state + ", not 0",
       
   704                    0 == state);
       
   705     }
       
   706 
       
   707     public void testDriveGetters()
       
   708     {
       
   709         int defMem = SysUtil.getDefaultPhoneMemory();
       
   710         int defMassStorage = SysUtil.getDefaultMassStorage();
       
   711         Log.log("testDriveGetter: defaultPhoneMemory = " + defMem);
       
   712         Log.log("testDriveGetter: defaultMassStorage = " + defMassStorage);
       
   713     }
       
   714 
       
   715     public void testGetScreenSize()
       
   716     {
       
   717         int screenWidth = SysUtil.getScreenWidth();
       
   718         int screenHeight = SysUtil.getScreenHeight();
       
   719         Log.log("testGetScreenSize: screenWidth = " + screenWidth);
       
   720         Log.log("testGetScreenSize: screenHeight = " + screenHeight);
       
   721         assertTrue("screenWidth <= 0", screenWidth > 0);
       
   722         assertTrue("screenHeight <= 0", screenHeight > 0);
       
   723     }
       
   724 
       
   725     public void testIsoToLang()
       
   726     {
       
   727         testIsoToLang("unknown", -1); // unknown locale is indicated with -1
       
   728         testIsoToLang("fi", 9); // ELangFinnish, Finnish
       
   729         testIsoToLang("fi_FI", 9); // ELangFinnish, Finnish
       
   730         testIsoToLang("sv", 6); // ELangSwedish, Swedish
       
   731         testIsoToLang("sv_FI", 85); // ELangFinlandSwedish, Finland Swedish
       
   732         testIsoToLang("en", 1); // ELangEnglish, UK English
       
   733         testIsoToLang("en_US", 10); // ELangAmerican, American
       
   734         testIsoToLang("en_AU", 20); // ELangAustralian, Australian English
       
   735     }
       
   736 
       
   737     private void testIsoToLang(String aLocale, int aLang)
       
   738     {
       
   739         assertTrue("SysUtil.isoToLang " + aLocale + " != " + aLang,
       
   740                    SysUtil.isoToLang(aLocale) == aLang);
       
   741     }
       
   742 }
   576 }